-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
57 lines (45 loc) · 1.35 KB
/
utils.py
File metadata and controls
57 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import errno
import os
import os.path
import signal
import sys
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
_PIDFile = os.environ['HOME'] + '/.nm_synergy.pid'
def checkPID (path):
if os.path.isfile(path):
pid = 0
with open(path, 'r') as file:
pid = int(file.read())
alive = True
try:
os.kill(pid, signal.SIGCONT)
except OSError as (error, stderr):
if error == errno.ESRCH:
alive = False
if alive: sys.exit()
else: os.remove(path)
def createPIDFile (path, pid):
with open(path, 'w') as file:
file.write(str(pid))
def deletePIDFile (path = _PIDFile):
os.remove(path)
def exitHandler (*args):
deletePIDFile()
sys.exit()
# NOTE: signal handlers need to be registered prior to main loop forking/running.
# NOTE: signal handlers only work for "clean" exit. checkPID is used to clean up
# after a "dirty" exit.
def fork (path = _PIDFile):
pid = os.fork()
if pid == 0:
signal.signal(signal.SIGTERM, exitHandler)
signal.signal(signal.SIGINT, exitHandler)
gobject.MainLoop().run()
else:
createPIDFile(path, pid)
def init (path = _PIDFile):
# FIXME: need to investigate atomic file creation in Python...
checkPID(path)
DBusGMainLoop(set_as_default = True)