my senf to tuerd
authorRalf Jung <post@ralfj.de>
Sun, 15 Sep 2013 15:43:01 +0000 (17:43 +0200)
committerRalf Jung <post@ralfj.de>
Sun, 15 Sep 2013 15:43:01 +0000 (17:43 +0200)
tuerd

diff --git a/tuerd b/tuerd
index 1d49f652b5e7b892a7ec0789c96a308f39d9cf6a..00b2420aade8493b21940f9d14ef2e0735e38d7b 100755 (executable)
--- a/tuerd
+++ b/tuerd
@@ -23,6 +23,11 @@ def doNothing (conn):
        waynesend(conn,b"0")
        pass
 
+# delete a file, don't care if it did not exist in the first place
+def forcerm(name):
+       if os.path.exists(name):
+               os.unlink (name)
+
 # commands: on a pin do a series of timed on/off switches
 class Pinoutput:
        # name is for logging and also used for mapping command names to instances of this class
@@ -63,12 +68,12 @@ for pin in pinlist:
 # create socket
 sock = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM)
 # delete old socket file and don't bitch around if it's not there
-try:
-       os.unlink (socketname)
-except OSError:
-       pass
+forcerm(socketname)
 # bind socket to file name
 sock.bind (socketname)
+# ensure we close and delete the socket when we quit (atexit.register is LIFO!)
+atexit.register(forcerm, socketname)
+atexit.register(sock.close)
 # allow only users in the tuergroup to write to the socket
 os.chown (socketname, 0, tuergroupid)
 os.chmod (socketname, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)