Debian Codegraph @

test/integration/targets/service/files/ansible_test_service.py

raw ◀ ▶
1 Debian patch touch this file
•1 #!/usr/bin/env python3
2
3 # this is mostly based off of the code found here:
4 # http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
5
6 from __future__ import annotations
7
8 import os
9 import resource
10 import signal
11 import time
12
13 UMASK = 0
14 WORKDIR = "/"
15 MAXFD = 1024
16
17 if (hasattr(os, "devnull")):
18 REDIRECT_TO = os.devnull
19 else:
20 REDIRECT_TO = "/dev/null"
21
22
23 def createDaemon():
24 try:
25 pid = os.fork()
26 except OSError as e:
27 raise Exception("%s [%d]" % (e.strerror, e.errno))
28
29 if (pid == 0):
30 os.setsid()
31
32 try:
33 pid = os.fork()
34 except OSError as e:
35 raise Exception("%s [%d]" % (e.strerror, e.errno))
36
37 if (pid == 0):
38 os.chdir(WORKDIR)
39 os.umask(UMASK)
40 else:
41 with open('/var/run/ansible_test_service.pid', 'w') as f:
42 f.write("%d\n" % pid)
43 os._exit(0)
44 else:
45 os._exit(0)
46
47 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
48 if (maxfd == resource.RLIM_INFINITY):
49 maxfd = MAXFD
50
51 for fd in range(0, maxfd):
52 try:
53 os.close(fd)
54 except OSError: # ERROR, fd wasn't open to begin with (ignored)
55 pass
56
57 os.open(REDIRECT_TO, os.O_RDWR)
58 os.dup2(0, 1)
59 os.dup2(0, 2)
60
61 return (0)
62
63
64 if __name__ == "__main__":
65
66 signal.signal(signal.SIGHUP, signal.SIG_IGN)
67
68 retCode = createDaemon()
69
70 while True:
71 time.sleep(1000)