X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fnepi%2Futil%2Fserver.py;h=0a95275155b4b508c4f9e31364ff8194e39f5c54;hb=c6b18a46730b58f14d337e0985f88f1809c44a81;hp=704984c2f979d99bc267b78ceb3b8a2707ce217b;hpb=d654ef403a0ec18b5d4296b19c026a4234ca7c15;p=nepi.git diff --git a/src/nepi/util/server.py b/src/nepi/util/server.py index 704984c2..0a952751 100644 --- a/src/nepi/util/server.py +++ b/src/nepi/util/server.py @@ -24,6 +24,7 @@ import functools import collections CTRL_SOCK = "ctrl.sock" +CTRL_PID = "ctrl.pid" STD_ERR = "stderr.log" MAX_FD = 1024 @@ -198,8 +199,35 @@ class Server(object): # create control socket self._ctrl_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - self._ctrl_sock.bind(CTRL_SOCK) + try: + self._ctrl_sock.bind(CTRL_SOCK) + except socket.error: + # Address in use, check pidfile + pid = None + try: + pidfile = open(CTRL_PID, "r") + pid = pidfile.read() + pidfile.close() + pid = int(pid) + except: + # no pidfile + pass + + if pid is not None: + # Check process liveliness + if not os.path.exists("/proc/%d" % (pid,)): + # Ok, it's dead, clean the socket + os.remove(CTRL_SOCK) + + # try again + self._ctrl_sock.bind(CTRL_SOCK) + self._ctrl_sock.listen(0) + + # Save pidfile + pidfile = open(CTRL_PID, "w") + pidfile.write(str(os.getpid())) + pidfile.close() # let the parent process know that the daemonization is finished os.write(w, "\n")