55# SPDX-License-Identifier: GPL-3.0-or-later
66#
77
8- import sys
98import agent
109import os
1110
12- # retrieve the first argument as the php value
13- if len (sys .argv ) <= 1 :
14- print ("Error: Missing argument for PHP value" , file = sys .stderr )
15- sys .exit (1 )
16- php_value = sys .argv [1 ]
17-
1811# Connect the local Redis replica. This is necessary to consistently start
1912# the service if the leader node is not reachable:
2013rdb = agent .redis_connect (use_replica = True )
2114smtp_settings = agent .get_smarthost_settings (rdb )
2215
2316# create the smarthost folder
24- envfile = f 'smarthosts/php { php_value } _smarthost .env'
17+ envfile = 'smarthosts/smarthost .env'
2518
2619# Ensure the target directory exists:
2720os .makedirs (os .path .dirname (envfile ), exist_ok = True )
2821
22+ # Create a unique temporary file using process PID to avoid concurrent writes:
23+ tmp_path = f"{ envfile } .tmp.{ os .getpid ()} "
2924
30- # Using .tmp suffix: do not overwrite the target file until the new one is
31- # saved to disk:
32- with open (envfile + ".tmp" , "w" ) as efp :
25+ with open (tmp_path , "w" ) as efp :
3326 # HINT for lamp: adjust variable names as needed
3427 print (f"SMTP_ENABLED={ '1' if smtp_settings ['enabled' ] else '' } " , file = efp )
3528 print (f"SMTP_HOST={ smtp_settings ['host' ]} " , file = efp )
@@ -39,5 +32,5 @@ with open(envfile + ".tmp", "w") as efp:
3932 print (f"SMTP_ENCRYPTION={ smtp_settings ['encrypt_smtp' ]} " , file = efp )
4033 print (f"SMTP_TLSVERIFY={ '1' if smtp_settings ['tls_verify' ] else '' } " , file = efp )
4134
42- # Commit changes by replacing the existing envfile:
43- os .replace (envfile + ".tmp" , envfile )
35+ # Commit changes by atomically replacing the existing envfile:
36+ os .replace (tmp_path , envfile )
0 commit comments