Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: infra/libs/service_utils/_daemon_nix.py

Issue 1365583002: Have service_manager start a cloudtail attached to each service it starts. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Just use the default project ID Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2015 The Chromium Authors. All rights reserved. 1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import contextlib 5 import contextlib
6 import errno 6 import errno
7 import fcntl 7 import fcntl
8 import os 8 import os
9 import sys 9 import sys
10 import tempfile 10 import tempfile
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 # If the file was deleted for some other reason, don't sweat it. 96 # If the file was deleted for some other reason, don't sweat it.
97 pass 97 pass
98 98
99 99
100 def _fork_then_exit_parent(): 100 def _fork_then_exit_parent():
101 pid = os.fork() 101 pid = os.fork()
102 if pid > 0: 102 if pid > 0:
103 os._exit(0) 103 os._exit(0)
104 104
105 105
106 def become_daemon(keep_fds=None): 106 def close_all_fds(keep_fds):
107 """Makes this process a daemon process.
108
109 Starts a new process group, closes all open file handles, opens /dev/null on
110 stdin, stdout and stderr, and changes the current working directory to /.
111 """
112
113 if keep_fds is None: 107 if keep_fds is None:
114 keep_fds = set() 108 keep_fds = set()
115 109
116 _fork_then_exit_parent()
117 os.setsid()
118 _fork_then_exit_parent()
119
120 # Close all open files.
121 for fd in reversed(range(2048)): 110 for fd in reversed(range(2048)):
Vadim Sh. 2015/09/23 17:40:21 (I don't remember this code) Why 2048? There's no
dsansome 2015/09/24 04:34:13 I just picked a high number :(
122 if fd in keep_fds: 111 if fd in keep_fds:
123 continue 112 continue
124 113
125 try: 114 try:
126 os.close(fd) 115 os.close(fd)
127 except EnvironmentError as ex: 116 except EnvironmentError as ex:
128 if ex.errno != errno.EBADF: 117 if ex.errno != errno.EBADF:
129 raise 118 raise
130 119
131 # Open /dev/null on stdin, stdout and stderr. 120 # Open /dev/null on stdin, stdout and stderr.
132 null = os.open(os.devnull, os.O_RDWR) 121 null = os.open(os.devnull, os.O_RDWR)
133 for i in range(3): 122 for i in range(3):
134 os.dup2(null, i) 123 if i not in keep_fds:
124 os.dup2(null, i)
125
126
127 def become_daemon(keep_fds=None):
128 """Makes this process a daemon process.
129
130 Starts a new process group, closes all open file handles, opens /dev/null on
131 stdin, stdout and stderr, and changes the current working directory to /.
132
133 Args:
134 keep_fds: One of:
135 None: close all FDs
136 True: keep all FDs (don't close any)
137 a set: close all FDs except the ones in this set.
138 """
139
140 _fork_then_exit_parent()
141 os.setsid()
142 _fork_then_exit_parent()
143
144 if keep_fds != True:
145 # Close all open files.
146 close_all_fds(keep_fds)
135 147
136 os.chdir('/') 148 os.chdir('/')
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698