From 8aa7eaf22559c42cbabc685e436bc8962087d3a1 Mon Sep 17 00:00:00 2001 From: lars Date: Mon, 14 Jan 2008 23:54:02 +0000 Subject: [PATCH] more verbose error reporting for non-writeable pid file --- bin/CryptoBoxWebserver | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/bin/CryptoBoxWebserver b/bin/CryptoBoxWebserver index aecd495..afdc40f 100755 --- a/bin/CryptoBoxWebserver +++ b/bin/CryptoBoxWebserver @@ -234,13 +234,25 @@ def close_open_files(): os.dup2(0, 1) # standard output (1) -def write_pid_file(pid_file): +def write_pid_file(pid_file, pid=None): + """write the process ID of the cryptonas daemon to a file + + call this function with the second parameter (e.g. pid=0) to check, if the + given location is writeable + @param pid_file: the path of the pid file to be written + @type pid_file: string + @param pid: use a specific PID instead of the PID of the current process + @type pid: int + """ + if pid is None: + ## use the PID of the current process + pid = os.getpid() if os.path.exists(pid_file): sys.stderr.write( "Warning: pid file (%s) already exists - overwriting ...\n" % pid_file) try: pidf = open(pid_file,"w") - pidf.write(str(os.getpid())) + pidf.write(str(pid)) pidf.close() except (IOError, OSError), err_msg: sys.stderr.write( @@ -371,6 +383,10 @@ if __name__ == "__main__": sys.stderr.write("Check the log file for details.\n") cherrypy.server.stop() sys.exit(1) + ## test if we can write to the PID file + ## this _must_ be done before forking, since a potential error would be + ## silent (due to the closed files - e.g. STDERR) + write_pid_file(options.pidfile, 0) ## redirect stderr to the webserver's logfile if options.background: ## replace stdin and stdout by /dev/null