/usr/bin/al-session-daemon is in python-albatross-common 1.36-5.5.
This file is owned by root:root, with mode 0o755.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | #! /usr/bin/python
#
# Copyright 2001 by Object Craft P/L, Melbourne, Australia.
#
# LICENCE - see LICENCE file distributed with this software for details.
#
#
# Run the session server as a daemon
#
# This won't work under Windows as the daemonisation process is specific
# to unix. Hints on how to make it work gratefully received.
#
# $Id: al-session-daemon 8003 2006-11-20 01:05:51Z andrewm $
#
# standard libraries
import getopt
import os
import sys
import signal
from errno import *
import time
import traceback
# our modules
from albatross import simpleserver
from albatross import pidfile
ourname = "al-session-daemon"
EXIT_OK, EXIT_ERROR, EXIT_USAGE = range(3)
class Options:
def __init__(self):
# Defaults
self.debug = 0
self.logfile_name = "/var/log/al-session-daemon.log"
self.ourname = ourname
self.pidfile_name = "/var/run/%s.pid" % ourname
self.port = 34343
class Pipe:
"""
A simple wrapper around a unix pipe, intended to be used to
communicate between a parent and a child process.
One process holds the read end, the other process holds the write end.
The first use decides which direction the pipe runs in, with the
unused file descriptors being closed (so the other end can detect
EOF).
On destruction of the object, any open file descriptors are closed.
"""
def __init__(self):
self.read_fd, self.write_fd = os.pipe()
def write(self, buf):
if self.read_fd >= 0:
try:
os.close(self.read_fd)
except:
pass
self.read_fd = -1
return os.write(self.write_fd, buf)
def read(self, count):
if self.write_fd >= 0:
try:
os.close(self.write_fd)
except:
pass
self.write_fd = -1
return os.read(self.read_fd, count)
def __del__(self):
if self.read_fd >= 0:
try:
os.close(self.read_fd)
except:
pass
if self.write_fd >= 0:
try:
os.close(self.write_fd)
except:
pass
class Daemon:
def __init__(self, port, logfile_name, pidfile_name, debug = 0):
self.port = port
if logfile_name:
self.logfile = open(logfile_name, "a", 1)
else:
self.logfile = None
self.pidfile = pidfile.PidFile(pidfile_name)
if debug:
self.debugfile = self.logfile
else:
self.debugfile = None
def start(self):
# We want the parent to be able to return useful exit codes to
# whoever called it, so we use a pipe to pass status from the
# child back to the parent.
pipe = Pipe()
pid = os.fork()
if pid:
# Parent
status = pipe.read(3)
if status == str(EXIT_OK):
print "session-server pid %d" % pid
return EXIT_OK
else:
return EXIT_ERROR
# Child
del pid
try:
self.pidfile.start()
except pidfile.PidFileError, msg:
sys.stderr.write("pidfile: %s\n" % msg)
sys.exit(EXIT_ERROR)
try:
self.server = simpleserver.Server(self.port, self.debugfile)
# Divorce us from the parents controlling TTY (so we don't
# receive their signals, etc). It's somewhat rude to change
# these file descriptors from under the python libraries, but
# it should be safe enough.
devnull = os.open("/dev/null", os.O_RDWR)
if not self.logfile:
opfile = devnull
else:
opfile = self.logfile.fileno()
os.dup2(devnull, 0)
os.dup2(opfile, 1)
os.dup2(opfile, 2)
os.close(devnull)
del opfile, devnull
try:
os.setsid()
except:
pass
signal.signal(signal.SIGTERM, self.sig_stop)
if self.logfile:
self.logfile.write("\nServer starting at %s\n" % self.asctime())
self.logfile.flush()
pipe.write(str(EXIT_OK)) # Signal parent that all is ok
del pipe
self.server.select_loop()
except:
traceback.print_exc()
if self.logfile:
self.logfile.write("Server stopped at %s\n" % self.asctime())
try:
self.pidfile.stop()
except pidfile.PidFileError, msg:
sys.stderr.write("pidfile: %s\n" % msg)
sys.exit(EXIT_OK)
def sig_stop(self, sig, stack):
self.server.stop()
def stop(self):
tries = 5
try:
pid = self.pidfile.getpid()
except pidfile.PidFileError, msg:
sys.stderr.write("pidfile: %s\n" % msg)
return EXIT_ERROR
if pid:
print "stopping %d" % pid,
while self.pidfile.is_running() and tries:
try:
self.pidfile.kill()
except pidfile.PidFileError, msg:
sys.stderr.write("pidfile: %s\n" % msg)
return EXIT_ERROR
print ".",
sys.stdout.flush()
tries = tries - 1
time.sleep(1)
if tries:
print "done"
return EXIT_OK
else:
print "failed to kill %s" % pid
else:
print "daemon not running"
return EXIT_ERROR
def status(self):
pid = self.pidfile.getpid()
if pid:
print "Daemon pid is %d" % pid
return EXIT_OK
else:
print "Daemon is not running"
return EXIT_ERROR
def command(self, cmd_name):
cmds = {
"start": self.start,
"stop": self.stop,
"status": self.status,
}
try:
cmd = cmds[cmd_name]
except KeyError:
return EXIT_USAGE
return cmd()
def asctime(self):
# Python 1.5.2's time.asctime() wasn't as enlightened as 2.0, so we
# roll our own
return time.asctime(time.localtime(time.time()))
def usage():
sys.stderr.write("Usage: %s [OPTIONS]... <command>\n" % ourname)
sys.stderr.write("Try '%s --help' for more information\n" % ourname)
sys.exit(EXIT_USAGE)
def help():
print """\
Usage: %(ourname)s [options]... <command>
Where [options] are:
-D, --debug Write debugging to log
-h, --help Display this help and exit
-k <pid-file>, Record server pid in <pid-file>, default is
--pidfile=<pid-file> %(pidfile_name)s
-p <port>, --port=<port> Listen on <port>, default is %(port)s
-l <log-file>, Write log to <log-file>, default is
--log=<log-file> %(logfile_name)s
<command> is one of:
start start a new daemon
stop kill the current daemon
status request the daemon's status
""" % Options().__dict__
sys.exit(EXIT_OK)
def main(argv):
options = Options()
try:
opts, args = getopt.getopt(argv,
"Dhk:p:l:",
["debug", "help", "pidfile=",
"port=", "log="])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt in ("-D", "--debug"):
options.debug = not options.debug
elif opt in ("-h", "--help"):
help()
elif opt in ("-k", "--pidfile"):
options.pidfile_name = arg
elif opt in ("-p", "--port"):
try:
options.port = int(arg)
except ValueError:
usage()
elif opt in ("-l", "--log"):
options.logfile_name = arg
if not args:
usage()
daemon = Daemon(port = options.port,
logfile_name = options.logfile_name,
pidfile_name = options.pidfile_name,
debug = options.debug)
exit_code = daemon.command(args[0])
if exit_code == EXIT_USAGE:
usage()
sys.exit(exit_code)
if __name__ == '__main__':
main(sys.argv[1:])
|