Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/smc_sagews/smc_sagews/daemon.py
Views: 286
1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2009-2010 Sauce Labs Inc
3
#
4
# Portions taken from twistd:
5
#
6
# Copyright (c) 2001-2009
7
# Allen Short
8
# Andrew Bennetts
9
# Apple Computer, Inc.
10
# Benjamin Bruheim
11
# Bob Ippolito
12
# Canonical Limited
13
# Christopher Armstrong
14
# David Reid
15
# Donovan Preston
16
# Eric Mangold
17
# Itamar Shtull-Trauring
18
# James Knight
19
# Jason A. Mobarak
20
# Jean-Paul Calderone
21
# Jonathan Lange
22
# Jonathan D. Simms
23
# Jürgen Hermann
24
# Kevin Turner
25
# Mary Gardiner
26
# Matthew Lefkowitz
27
# Massachusetts Institute of Technology
28
# Moshe Zadka
29
# Paul Swartz
30
# Pavel Pergamenshchik
31
# Ralph Meijer
32
# Sean Riley
33
# Software Freedom Conservancy
34
# Travis B. Hartwell
35
# Thomas Herve
36
# Eyal Lotem
37
# Antoine Pitrou
38
# Andy Gayton
39
#
40
# Permission is hereby granted, free of charge, to any person obtaining
41
# a copy of this software and associated documentation files (the
42
# "Software"), to deal in the Software without restriction, including
43
# without limitation the rights to use, copy, modify, merge, publish,
44
# distribute, sublicense, and/or sell copies of the Software, and to
45
# permit persons to whom the Software is furnished to do so, subject to
46
# the following conditions:
47
#
48
# The above copyright notice and this permission notice shall be
49
# included in all copies or substantial portions of the Software.
50
#
51
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
54
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
55
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
56
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
57
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58
59
from __future__ import absolute_import
60
61
import os
62
import sys
63
import errno
64
65
66
def basic_daemonize(silence=True):
67
# See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
68
if os.fork(): # launch child and...
69
os._exit(0) # kill off parent
70
os.setsid()
71
if os.fork(): # launch child and...
72
os._exit(0) # kill off parent again.
73
os.umask(0o022) # Don't allow others to write
74
75
if not silence:
76
print("daemonize.basic_daemonize: process NOT silenced, for debugging")
77
else:
78
null = os.open('/dev/null', os.O_RDWR)
79
for i in range(3):
80
try:
81
os.dup2(null, i)
82
except OSError as e:
83
if e.errno != errno.EBADF:
84
raise
85
os.close(null)
86
87
88
def writePID(pidfile):
89
open(pidfile, 'w').write(str(os.getpid()))
90
if not os.path.exists(pidfile):
91
raise Exception("pidfile %s does not exist" % pidfile)
92
93
94
def checkPID(pidfile):
95
if not pidfile:
96
return
97
if os.path.exists(pidfile):
98
try:
99
pid = int(open(pidfile).read())
100
except ValueError:
101
sys.exit('Pidfile %s contains non-numeric value' % pidfile)
102
try:
103
os.kill(pid, 0)
104
except OSError as why:
105
if why.args[0] == errno.ESRCH:
106
# The pid doesnt exists.
107
print(('Removing stale pidfile %s' % pidfile))
108
os.remove(pidfile)
109
else:
110
sys.exit("Can't check status of PID %s from pidfile %s: %s" %
111
(pid, pidfile, why.args[1]))
112
else:
113
sys.exit("Another server is running, PID %s\n" % pid)
114
115
116
def daemonize(pidfile):
117
checkPID(pidfile)
118
# CRITICAL: DO NOT set silence=False in production. It hangs starting the sage server
119
# properly, which breaks things badly for users (e.g., their first worksheet
120
# never works).
121
basic_daemonize(silence=True)
122
writePID(pidfile)
123
124