diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2008-07-25 09:18:56 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2008-07-25 09:18:56 +0200 |
commit | 22c6d6d182b5480dc6833e455bb3e2188d66af2a (patch) | |
tree | b7852e11b17d300814f4b2b8c6e4ce845b426dd1 /portato.py | |
parent | 8b71eed2f8ef0714aba080ded2fcb96630b77d69 (diff) | |
download | portato-22c6d6d182b5480dc6833e455bb3e2188d66af2a.tar.gz portato-22c6d6d182b5480dc6833e455bb3e2188d66af2a.tar.bz2 portato-22c6d6d182b5480dc6833e455bb3e2188d66af2a.zip |
Fixing subprocess/listener handling
Diffstat (limited to '')
-rwxr-xr-x | portato.py | 41 |
1 files changed, 27 insertions, 14 deletions
@@ -14,8 +14,9 @@ from __future__ import with_statement, absolute_import -import signal -import sys, os, subprocess +import sys, os +import subprocess, threading +import atexit import gettext, locale from optparse import OptionParser, SUPPRESS_HELP @@ -23,11 +24,6 @@ from portato import get_listener from portato.helper import debug, info from portato.constants import VERSION, LOCALE_DIR, APP, SU_COMMAND -def sigchld_aborter (signal, frame): - """Aborts the current process when the child dies.""" - debug("Child process died. Trying to abort.") - raise KeyboardInterrupt - def main (): # set gettext stuff locale.setlocale(locale.LC_ALL, '') @@ -49,6 +45,9 @@ def main (): # run parser (options, args) = parser.parse_args() + # close listener at exit + atexit.register(get_listener().close) + if options.nofork or os.getuid() == 0: # start GUI from portato.gui import run info("%s v. %s", _("Starting Portato"), VERSION) @@ -58,7 +57,10 @@ def main (): else: get_listener().set_send() - run() + try: + run() + except KeyboardInterrupt: + debug("Got KeyboardInterrupt.") else: # start us again in root modus and launch listener @@ -68,17 +70,28 @@ def main (): sig = shm.create_semaphore(InitialValue = 0, permissions = 0600) rw = shm.create_semaphore(InitialValue = 1, permissions = 0600) + # start listener + lt = threading.Thread(target=get_listener().set_recv, args = (mem, sig, rw)) + lt.setDaemon(False) + lt.start() + # set DBUS_SESSION_BUS_ADDRESS to "" to make dbus work as root ;) env = os.environ.copy() env.update(DBUS_SESSION_BUS_ADDRESS="") cmd = SU_COMMAND.split() - subprocess.Popen(cmd+["%s --no-fork --shm %ld %ld %ld" % (sys.argv[0], mem.key, sig.key, rw.key)], env = env) - - # set handler to abort the listener if needed - signal.signal(signal.SIGCHLD, sigchld_aborter) - # stast listener - get_listener().set_recv(mem, sig, rw) + sp = subprocess.Popen(cmd+["%s --no-fork --shm %ld %ld %ld" % (sys.argv[0], mem.key, sig.key, rw.key)], env = env) + + # wait for process to finish + try: + sp.wait() + debug("Subprocess finished") + except KeyboardInterrupt: + debug("Got KeyboardInterrupt.") + + if lt.isAlive(): + debug("Listener is still running. Close it.") + get_listener().close() if __name__ == "__main__": main() |