/usr/share/pyshared/sclapp/services.py is in python-sclapp 0.5.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | # Copyright (c) 2005-2007 Forest Bond.
# This file is part of the sclapp software package.
#
# sclapp is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# A copy of the license has been included in the COPYING file.
import sys, os, signal
from sclapp.daemonize import daemonize
from sclapp.exceptions import *
from sclapp.error_output import *
class InvalidPidFileError(Error):
pass
def readPidFile(pid_file_name):
try:
f = open(pid_file_name, 'r')
except IOError:
return None
try:
contents = f.read().strip()
assert(len(contents.split(u'\n')) < 2)
f.close()
pid = int(contents)
except (AssertionError, ValueError):
raise InvalidPidFileError
return pid
def writePidFile(pid_file_name, pid = None):
if pid is None:
pid = os.getpid()
pid_file = open(pid_file_name, 'w')
pid_file.write(unicode(os.getpid()))
pid_file.close()
def removePidFile(pid_file_name):
try:
os.unlink(pid_file_name)
except OSError, e:
if e.errno != 2:
raise
def startService(pid_file_name, fn, args = None, kwargs = None):
if args is None:
args = [ ]
if kwargs is None:
kwargs = { }
# TODO: verify that this pid actually belongs to an instance of the service.
pid = readPidFile(pid_file_name)
if pid is not None:
raise CriticalError, (1, u'already running? process ID %u' % pid)
pid = os.fork()
if not pid:
# child process
daemonize()
try:
writePidFile(pid_file_name, pid = pid)
fn(*args, **kwargs)
finally:
try:
removePidFile(pid_file_name)
except Exception, e:
printCritical(unicode(e))
os._exit(0)
def stopService(pid_file_name):
try:
pid_file = open(pid_file_name, 'r')
except IOError:
return
pid = int(pid_file.read().strip())
pid_file.close()
# FIXME: wait for process to die, try to kill again if necessary
try:
os.kill(pid, signal.SIGINT)
except OSError:
pass
|