| 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 | |
| 31 def setugid(user): | 29 def setugid(user): |
| 32 """Change process user and group ID | 30 """Change process user and group ID |
| 33 | 31 |
| 34 Argument is a numeric user id or a user name""" | 32 Argument is a numeric user id or a user name""" |
| 35 try: | 33 try: |
| 36 from pwd import getpwuid | 34 from pwd import getpwuid |
| 37 passwd = getpwuid(int(user)) | 35 passwd = getpwuid(int(user)) |
| 38 except ValueError: | 36 except ValueError: |
| 39 from pwd import getpwnam | 37 from pwd import getpwnam |
| 40 passwd = getpwnam(user) | 38 passwd = getpwnam(user) |
| 41 | 39 |
| 42 if hasattr(os, 'initgroups'): # python >= 2.7 | 40 if hasattr(os, 'initgroups'): # python >= 2.7 |
| 43 os.initgroups(passwd.pw_name, passwd.pw_gid) | 41 os.initgroups(passwd.pw_name, passwd.pw_gid) |
| 44 else: | 42 else: |
| 45 import ctypes | 43 import ctypes |
| 46 if ctypes.CDLL(None).initgroups(passwd.pw_name, passwd.pw_gid) < 0: | 44 if ctypes.CDLL(None).initgroups(passwd.pw_name, passwd.pw_gid) < 0: |
| 47 err = ctypes.c_int.in_dll(ctypes.pythonapi,"errno").value | 45 err = ctypes.c_int.in_dll(ctypes.pythonapi,"errno").value |
| 48 raise OSError(err, os.strerror(err), 'initgroups') | 46 raise OSError(err, os.strerror(err), 'initgroups') |
| 49 os.setgid(passwd.pw_gid) | 47 os.setgid(passwd.pw_gid) |
| 50 os.setuid(passwd.pw_uid) | 48 os.setuid(passwd.pw_uid) |
| 51 os.environ['HOME'] = passwd.pw_dir | 49 os.putenv('HOME', passwd.pw_dir) |
| 52 | 50 |
| 53 | 51 |
| 54 def daemonize(pidfile=None, uid=None, umask=077): | 52 def daemonize(pidfile=None, uid=None, umask=077): |
| 55 """daemonize a Unix process. Set paranoid umask by default. | 53 """daemonize a Unix process. Set paranoid umask by default. |
| 56 | 54 |
| 57 Return 1 in the original process, 2 in the first fork, and None for the | 55 Return 1 in the original process, 2 in the first fork, and None for the |
| 58 second fork (eg daemon process). | 56 second fork (eg daemon process). |
| 59 """ | 57 """ |
| 60 # http://www.faqs.org/faqs/unix-faq/programmer/faq/ | 58 # http://www.faqs.org/faqs/unix-faq/programmer/faq/ |
| 61 # | 59 # |
| (...skipping 10 matching lines...) Expand all Loading... |
| 72 # move to the root to avoit mount pb | 70 # move to the root to avoit mount pb |
| 73 os.chdir('/') | 71 os.chdir('/') |
| 74 # set umask if specified | 72 # set umask if specified |
| 75 if umask is not None: | 73 if umask is not None: |
| 76 os.umask(umask) | 74 os.umask(umask) |
| 77 # redirect standard descriptors | 75 # redirect standard descriptors |
| 78 null = os.open('/dev/null', os.O_RDWR) | 76 null = os.open('/dev/null', os.O_RDWR) |
| 79 for i in range(3): | 77 for i in range(3): |
| 80 try: | 78 try: |
| 81 os.dup2(null, i) | 79 os.dup2(null, i) |
| 82 except OSError as e: | 80 except OSError, e: |
| 83 if e.errno != errno.EBADF: | 81 if e.errno != errno.EBADF: |
| 84 raise | 82 raise |
| 85 os.close(null) | 83 os.close(null) |
| 86 # filter warnings | 84 # filter warnings |
| 87 warnings.filterwarnings('ignore') | 85 warnings.filterwarnings('ignore') |
| 88 # write pid in a file | 86 # write pid in a file |
| 89 if pidfile: | 87 if pidfile: |
| 90 # ensure the directory where the pid-file should be set exists (for | 88 # ensure the directory where the pid-file should be set exists (for |
| 91 # instance /var/run/cubicweb may be deleted on computer restart) | 89 # instance /var/run/cubicweb may be deleted on computer restart) |
| 92 piddir = os.path.dirname(pidfile) | 90 piddir = os.path.dirname(pidfile) |
| 93 if not os.path.exists(piddir): | 91 if not os.path.exists(piddir): |
| 94 os.makedirs(piddir) | 92 os.makedirs(piddir) |
| 95 f = file(pidfile, 'w') | 93 f = file(pidfile, 'w') |
| 96 f.write(str(os.getpid())) | 94 f.write(str(os.getpid())) |
| 97 f.close() | 95 f.close() |
| 98 os.chmod(pidfile, 0644) | 96 os.chmod(pidfile, 0644) |
| 99 # change process uid | 97 # change process uid |
| 100 if uid: | 98 if uid: |
| 101 setugid(uid) | 99 setugid(uid) |
| 102 return None | 100 return None |
| OLD | NEW |