# -*- coding: utf-8 -*-1# Copyright (c) 2009-2010 Sauce Labs Inc2#3# Portions taken from twistd:4#5# Copyright (c) 2001-20096# Allen Short7# Andrew Bennetts8# Apple Computer, Inc.9# Benjamin Bruheim10# Bob Ippolito11# Canonical Limited12# Christopher Armstrong13# David Reid14# Donovan Preston15# Eric Mangold16# Itamar Shtull-Trauring17# James Knight18# Jason A. Mobarak19# Jean-Paul Calderone20# Jonathan Lange21# Jonathan D. Simms22# Jürgen Hermann23# Kevin Turner24# Mary Gardiner25# Matthew Lefkowitz26# Massachusetts Institute of Technology27# Moshe Zadka28# Paul Swartz29# Pavel Pergamenshchik30# Ralph Meijer31# Sean Riley32# Software Freedom Conservancy33# Travis B. Hartwell34# Thomas Herve35# Eyal Lotem36# Antoine Pitrou37# Andy Gayton38#39# Permission is hereby granted, free of charge, to any person obtaining40# a copy of this software and associated documentation files (the41# "Software"), to deal in the Software without restriction, including42# without limitation the rights to use, copy, modify, merge, publish,43# distribute, sublicense, and/or sell copies of the Software, and to44# permit persons to whom the Software is furnished to do so, subject to45# the following conditions:46#47# The above copyright notice and this permission notice shall be48# included in all copies or substantial portions of the Software.49#50# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,51# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF52# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND53# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE54# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION55# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION56# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.5758import os59import sys60import errno6162def basic_daemonize():63# See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC1664if os.fork(): # launch child and...65os._exit(0) # kill off parent66os.setsid()67if os.fork(): # launch child and...68os._exit(0) # kill off parent again.69os.umask(022) # Don't allow others to write70null=os.open('/dev/null', os.O_RDWR)71for i in range(3):72try:73os.dup2(null, i)74except OSError, e:75if e.errno != errno.EBADF:76raise77os.close(null)787980def writePID(pidfile):81open(pidfile,'wb').write(str(os.getpid()))82if not os.path.exists(pidfile):83raise Exception( "pidfile %s does not exist" % pidfile )848586def checkPID(pidfile):87if not pidfile:88return89if os.path.exists(pidfile):90try:91pid = int(open(pidfile).read())92except ValueError:93sys.exit('Pidfile %s contains non-numeric value' % pidfile)94try:95os.kill(pid, 0)96except OSError, why:97if why[0] == errno.ESRCH:98# The pid doesnt exists.99print('Removing stale pidfile %s' % pidfile)100os.remove(pidfile)101else:102sys.exit("Can't check status of PID %s from pidfile %s: %s" %103(pid, pidfile, why[1]))104else:105sys.exit("Another server is running, PID %s\n" % pid)106107def daemonize(pidfile):108checkPID(pidfile)109basic_daemonize()110writePID(pidfile)111112113114