OLD | NEW |
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This file is part of logilab-common. | 4 # This file is part of logilab-common. |
5 # | 5 # |
6 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 6 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
7 # the terms of the GNU Lesser General Public License as published by the Free | 7 # the terms of the GNU Lesser General Public License as published by the Free |
8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y | 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
9 # later version. | 9 # later version. |
10 # | 10 # |
11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT | 11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | 13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 # details. | 14 # details. |
15 # | 15 # |
16 # You should have received a copy of the GNU Lesser General Public License along | 16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. | 17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. |
18 """A daemonize function (for Unices)""" | 18 """A daemonize function (for Unices)""" |
19 | 19 |
20 __docformat__ = "restructuredtext en" | 20 __docformat__ = "restructuredtext en" |
21 | 21 |
22 import os | 22 import os |
23 import errno | 23 import errno |
24 import signal | 24 import signal |
25 import sys | 25 import sys |
26 import time | 26 import time |
27 import warnings | 27 import warnings |
28 | 28 |
| 29 from six.moves import range |
| 30 |
29 def setugid(user): | 31 def setugid(user): |
30 """Change process user and group ID | 32 """Change process user and group ID |
31 | 33 |
32 Argument is a numeric user id or a user name""" | 34 Argument is a numeric user id or a user name""" |
33 try: | 35 try: |
34 from pwd import getpwuid | 36 from pwd import getpwuid |
35 passwd = getpwuid(int(user)) | 37 passwd = getpwuid(int(user)) |
36 except ValueError: | 38 except ValueError: |
37 from pwd import getpwnam | 39 from pwd import getpwnam |
38 passwd = getpwnam(user) | 40 passwd = getpwnam(user) |
39 | 41 |
40 if hasattr(os, 'initgroups'): # python >= 2.7 | 42 if hasattr(os, 'initgroups'): # python >= 2.7 |
41 os.initgroups(passwd.pw_name, passwd.pw_gid) | 43 os.initgroups(passwd.pw_name, passwd.pw_gid) |
42 else: | 44 else: |
43 import ctypes | 45 import ctypes |
44 if ctypes.CDLL(None).initgroups(passwd.pw_name, passwd.pw_gid) < 0: | 46 if ctypes.CDLL(None).initgroups(passwd.pw_name, passwd.pw_gid) < 0: |
45 err = ctypes.c_int.in_dll(ctypes.pythonapi,"errno").value | 47 err = ctypes.c_int.in_dll(ctypes.pythonapi,"errno").value |
46 raise OSError(err, os.strerror(err), 'initgroups') | 48 raise OSError(err, os.strerror(err), 'initgroups') |
47 os.setgid(passwd.pw_gid) | 49 os.setgid(passwd.pw_gid) |
48 os.setuid(passwd.pw_uid) | 50 os.setuid(passwd.pw_uid) |
49 os.putenv('HOME', passwd.pw_dir) | 51 os.environ['HOME'] = passwd.pw_dir |
50 | 52 |
51 | 53 |
52 def daemonize(pidfile=None, uid=None, umask=077): | 54 def daemonize(pidfile=None, uid=None, umask=077): |
53 """daemonize a Unix process. Set paranoid umask by default. | 55 """daemonize a Unix process. Set paranoid umask by default. |
54 | 56 |
55 Return 1 in the original process, 2 in the first fork, and None for the | 57 Return 1 in the original process, 2 in the first fork, and None for the |
56 second fork (eg daemon process). | 58 second fork (eg daemon process). |
57 """ | 59 """ |
58 # http://www.faqs.org/faqs/unix-faq/programmer/faq/ | 60 # http://www.faqs.org/faqs/unix-faq/programmer/faq/ |
59 # | 61 # |
(...skipping 10 matching lines...) Expand all Loading... |
70 # move to the root to avoit mount pb | 72 # move to the root to avoit mount pb |
71 os.chdir('/') | 73 os.chdir('/') |
72 # set umask if specified | 74 # set umask if specified |
73 if umask is not None: | 75 if umask is not None: |
74 os.umask(umask) | 76 os.umask(umask) |
75 # redirect standard descriptors | 77 # redirect standard descriptors |
76 null = os.open('/dev/null', os.O_RDWR) | 78 null = os.open('/dev/null', os.O_RDWR) |
77 for i in range(3): | 79 for i in range(3): |
78 try: | 80 try: |
79 os.dup2(null, i) | 81 os.dup2(null, i) |
80 except OSError, e: | 82 except OSError as e: |
81 if e.errno != errno.EBADF: | 83 if e.errno != errno.EBADF: |
82 raise | 84 raise |
83 os.close(null) | 85 os.close(null) |
84 # filter warnings | 86 # filter warnings |
85 warnings.filterwarnings('ignore') | 87 warnings.filterwarnings('ignore') |
86 # write pid in a file | 88 # write pid in a file |
87 if pidfile: | 89 if pidfile: |
88 # ensure the directory where the pid-file should be set exists (for | 90 # ensure the directory where the pid-file should be set exists (for |
89 # instance /var/run/cubicweb may be deleted on computer restart) | 91 # instance /var/run/cubicweb may be deleted on computer restart) |
90 piddir = os.path.dirname(pidfile) | 92 piddir = os.path.dirname(pidfile) |
91 if not os.path.exists(piddir): | 93 if not os.path.exists(piddir): |
92 os.makedirs(piddir) | 94 os.makedirs(piddir) |
93 f = file(pidfile, 'w') | 95 f = file(pidfile, 'w') |
94 f.write(str(os.getpid())) | 96 f.write(str(os.getpid())) |
95 f.close() | 97 f.close() |
96 os.chmod(pidfile, 0644) | 98 os.chmod(pidfile, 0644) |
97 # change process uid | 99 # change process uid |
98 if uid: | 100 if uid: |
99 setugid(uid) | 101 setugid(uid) |
100 return None | 102 return None |
OLD | NEW |