X-Git-Url: https://git.ralfj.de/saartuer.git/blobdiff_plain/79f438378491fe0059574437edfaa8389836ec4f..afd14c9b67b6fb41219abf0dcd5e7fda37a1b8a9:/tuerd diff --git a/tuerd b/tuerd index 00b2420..f679fa8 100755 --- a/tuerd +++ b/tuerd @@ -1,7 +1,8 @@ #!/usr/bin/python3 -import time, socket, os, stat, atexit +import time, socket, os, stat, atexit, errno, struct from datetime import datetime import RPi.GPIO as GPIO +SO_PEERCRED = 17 # DO - NOT - TOUCH GPIO.setmode(GPIO.BOARD) # ******** definitions ********* @@ -25,8 +26,12 @@ def doNothing (conn): # delete a file, don't care if it did not exist in the first place def forcerm(name): - if os.path.exists(name): + try: os.unlink (name) + except OSError as e: + # only ignore error if it was "file didn't exist" + if e.errno != errno.ENOENT: + raise # commands: on a pin do a series of timed on/off switches class Pinoutput: @@ -85,7 +90,8 @@ sock.listen(1) while True: # accept connections conn, addr = sock.accept() - # TODO: use addr to determine the client for logging + # get peer information (TODO use it for logging) + (pid, uid, gid) = (struct.unpack('3i', conn.getsockopt(socket.SOL_SOCKET, SO_PEERCRED, struct.calcsize('3i')))) # get some data from the client (enough to hold any valid command) data = conn.recv (32) # log the command