Path: blob/master/src/smc_sagews/smc_sagews/daemon.py
Views: 286
# -*- 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.5758from __future__ import absolute_import5960import os61import sys62import errno636465def basic_daemonize(silence=True):66# See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC1667if os.fork(): # launch child and...68os._exit(0) # kill off parent69os.setsid()70if os.fork(): # launch child and...71os._exit(0) # kill off parent again.72os.umask(0o022) # Don't allow others to write7374if not silence:75print("daemonize.basic_daemonize: process NOT silenced, for debugging")76else:77null = os.open('/dev/null', os.O_RDWR)78for i in range(3):79try:80os.dup2(null, i)81except OSError as e:82if e.errno != errno.EBADF:83raise84os.close(null)858687def writePID(pidfile):88open(pidfile, 'w').write(str(os.getpid()))89if not os.path.exists(pidfile):90raise Exception("pidfile %s does not exist" % pidfile)919293def checkPID(pidfile):94if not pidfile:95return96if os.path.exists(pidfile):97try:98pid = int(open(pidfile).read())99except ValueError:100sys.exit('Pidfile %s contains non-numeric value' % pidfile)101try:102os.kill(pid, 0)103except OSError as why:104if why.args[0] == errno.ESRCH:105# The pid doesnt exists.106print(('Removing stale pidfile %s' % pidfile))107os.remove(pidfile)108else:109sys.exit("Can't check status of PID %s from pidfile %s: %s" %110(pid, pidfile, why.args[1]))111else:112sys.exit("Another server is running, PID %s\n" % pid)113114115def daemonize(pidfile):116checkPID(pidfile)117# CRITICAL: DO NOT set silence=False in production. It hangs starting the sage server118# properly, which breaks things badly for users (e.g., their first worksheet119# never works).120basic_daemonize(silence=True)121writePID(pidfile)122123124