Only su if we're root.
[uccvend-vendserver.git] / sql-edition / servers / VendServer.py
index 2133f37..cad1c39 100755 (executable)
@@ -3,6 +3,7 @@
 
 USE_DB = 0
 
+import ConfigParser
 import sys, os, string, re, pwd
 if USE_DB: import pg
 from time import time, sleep
@@ -14,6 +15,7 @@ from random import random, seed
 from Idler import TrainIdler,GrayIdler
 import socket
 from traceback import print_tb
+from posix import geteuid
 
 GREETING = 'UCC SNACKS'
 PIN_LENGTH = 4
@@ -339,7 +341,10 @@ def run_forever(rfh, wfh, options, cf):
                                        # XXX this should move somewhere else:
                                        if cur_selection == '55':
                                                mk.set_message('OPENSESAME')
-                                               ret = os.system('su - "%s" -c "dispense door"'%username)
+                                               if geteuid() == 0:
+                                                       ret = os.system('su - "%s" -c "dispense door"'%username)
+                                               else:
+                                                       ret = os.system('dispense door')
                                                if ret == 0:
                                                        mk.set_message(center('DOOR OPEN'))
                                                else:
@@ -422,10 +427,7 @@ class VendConfigFile:
                        print "Error reading config file "+config_file+": " + str(e)
                        sys.exit(1)
 
-
-if __name__ == '__main__':
-       import ConfigParser
-
+def do_vend_server():
        options = parse_args()
        config_opts = VendConfigFile(options.config_file, config_options)
 
@@ -434,10 +436,9 @@ if __name__ == '__main__':
                        rfh, wfh = connect_to_vend(options, config_opts)
                except (LATClientException, socket.error), e:
                        (exc_type, exc_value, exc_traceback) = sys.exc_info()
+                       del exc_traceback
                        print
                        print "Connection error: "+str(exc_type)+" "+str(e)
-                       print_tb(exc_traceback)
-                       del exc_traceback
                        print "Trying again in 5 seconds."
                        sleep(5)
                        continue
@@ -447,3 +448,25 @@ if __name__ == '__main__':
                        print
                        print "Connection died, trying again..."
 
+
+if __name__ == '__main__':
+       while True:
+               try:
+                       do_vend_server()
+               except KeyboardInterrupt:
+                       print "Killed by SIGINT."
+                       break
+               except:
+                       (exc_type, exc_value, exc_traceback) = sys.exc_info()
+                       print
+                       print "Uh-oh, unhandled " + str(exc_type) + " exception"
+                       print "Message: ", str(exc_value)
+                       print
+                       print_tb(exc_traceback)
+                       del exc_traceback
+                       print
+                       print "This message should be considered a bug in the Vend Server."
+                       print "Please report this to someone who can fix it."
+                       print
+                       print "Trying again anyway (might not help, but hey...)"
+

UCC git Repository :: git.ucc.asn.au