-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnv-image-daemon
executable file
·41 lines (37 loc) · 1.15 KB
/
nv-image-daemon
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
#!/usr/bin/python
import libHTTPServer, sys, os
workpath = '/usr/share/cloudvirt/'
def main(argv):
hostname = argv[1]
port = int(argv[2])
path = argv[3]
concurrent = int(argv[4])
logpath = argv[5]
libHTTPServer.server_start(hostname, port, path, concurrent, logpath)
if __name__ == "__main__":
# do the UNIX double-fork magic, see Stevens' "Advanced
# Programming in the UNIX Environment" for details (ISBN 0201563177)
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
# decouple from parent envirtonment
os.chdir(workpath)
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0 :
# exit from second parent, print eventual PID before
print "Daemon PID %d" % pid
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
#start the daemon main loop
main(sys.argv)