MCPcopy
hub / github.com/shadowsocks/shadowsocks / daemon_start

Function daemon_start

shadowsocks/daemon.py:93–131  ·  view source on GitHub ↗
(pid_file, log_file)

Source from the content-addressed store, hash-verified

91
92
93def daemon_start(pid_file, log_file):
94
95 def handle_exit(signum, _):
96 if signum == signal.SIGTERM:
97 sys.exit(0)
98 sys.exit(1)
99
100 signal.signal(signal.SIGINT, handle_exit)
101 signal.signal(signal.SIGTERM, handle_exit)
102
103 # fork only once because we are sure parent will exit
104 pid = os.fork()
105 assert pid != -1
106
107 if pid > 0:
108 # parent waits for its child
109 time.sleep(5)
110 sys.exit(0)
111
112 # child signals its parent to exit
113 ppid = os.getppid()
114 pid = os.getpid()
115 if write_pid_file(pid_file, pid) != 0:
116 os.kill(ppid, signal.SIGINT)
117 sys.exit(1)
118
119 os.setsid()
120 signal.signal(signal.SIGHUP, signal.SIG_IGN)
121
122 print('started')
123 os.kill(ppid, signal.SIGTERM)
124
125 sys.stdin.close()
126 try:
127 freopen(log_file, 'a', sys.stdout)
128 freopen(log_file, 'a', sys.stderr)
129 except IOError as e:
130 shell.print_exception(e)
131 sys.exit(1)
132
133
134def daemon_stop(pid_file):

Callers 1

daemon_execFunction · 0.85

Calls 3

write_pid_fileFunction · 0.85
freopenFunction · 0.85
closeMethod · 0.45

Tested by

no test coverage detected