| 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 # |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 else: | 44 else: |
| 45 import ctypes | 45 import ctypes |
| 46 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: |
| 47 err = ctypes.c_int.in_dll(ctypes.pythonapi,"errno").value | 47 err = ctypes.c_int.in_dll(ctypes.pythonapi,"errno").value |
| 48 raise OSError(err, os.strerror(err), 'initgroups') | 48 raise OSError(err, os.strerror(err), 'initgroups') |
| 49 os.setgid(passwd.pw_gid) | 49 os.setgid(passwd.pw_gid) |
| 50 os.setuid(passwd.pw_uid) | 50 os.setuid(passwd.pw_uid) |
| 51 os.environ['HOME'] = passwd.pw_dir | 51 os.environ['HOME'] = passwd.pw_dir |
| 52 | 52 |
| 53 | 53 |
| 54 def daemonize(pidfile=None, uid=None, umask=077): | 54 def daemonize(pidfile=None, uid=None, umask=0o77): |
| 55 """daemonize a Unix process. Set paranoid umask by default. | 55 """daemonize a Unix process. Set paranoid umask by default. |
| 56 | 56 |
| 57 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 |
| 58 second fork (eg daemon process). | 58 second fork (eg daemon process). |
| 59 """ | 59 """ |
| 60 # http://www.faqs.org/faqs/unix-faq/programmer/faq/ | 60 # http://www.faqs.org/faqs/unix-faq/programmer/faq/ |
| 61 # | 61 # |
| 62 # fork so the parent can exit | 62 # fork so the parent can exit |
| 63 if os.fork(): # launch child and... | 63 if os.fork(): # launch child and... |
| 64 return 1 | 64 return 1 |
| 65 # disconnect from tty and create a new session | 65 # disconnect from tty and create a new session |
| 66 os.setsid() | 66 os.setsid() |
| 67 # fork again so the parent, (the session group leader), can exit. | 67 # fork again so the parent, (the session group leader), can exit. |
| 68 # as a non-session group leader, we can never regain a controlling | 68 # as a non-session group leader, we can never regain a controlling |
| 69 # terminal. | 69 # terminal. |
| 70 if os.fork(): # launch child again. | 70 if os.fork(): # launch child again. |
| 71 return 2 | 71 return 2 |
| 72 # move to the root to avoit mount pb | 72 # move to the root to avoit mount pb |
| 73 os.chdir('/') | 73 os.chdir('/') |
| 74 # set umask if specified | |
| 75 if umask is not None: | |
| 76 os.umask(umask) | |
| 77 # redirect standard descriptors | 74 # redirect standard descriptors |
| 78 null = os.open('/dev/null', os.O_RDWR) | 75 null = os.open('/dev/null', os.O_RDWR) |
| 79 for i in range(3): | 76 for i in range(3): |
| 80 try: | 77 try: |
| 81 os.dup2(null, i) | 78 os.dup2(null, i) |
| 82 except OSError as e: | 79 except OSError as e: |
| 83 if e.errno != errno.EBADF: | 80 if e.errno != errno.EBADF: |
| 84 raise | 81 raise |
| 85 os.close(null) | 82 os.close(null) |
| 86 # filter warnings | 83 # filter warnings |
| 87 warnings.filterwarnings('ignore') | 84 warnings.filterwarnings('ignore') |
| 88 # write pid in a file | 85 # write pid in a file |
| 89 if pidfile: | 86 if pidfile: |
| 90 # ensure the directory where the pid-file should be set exists (for | 87 # ensure the directory where the pid-file should be set exists (for |
| 91 # instance /var/run/cubicweb may be deleted on computer restart) | 88 # instance /var/run/cubicweb may be deleted on computer restart) |
| 92 piddir = os.path.dirname(pidfile) | 89 piddir = os.path.dirname(pidfile) |
| 93 if not os.path.exists(piddir): | 90 if not os.path.exists(piddir): |
| 94 os.makedirs(piddir) | 91 os.makedirs(piddir) |
| 95 f = file(pidfile, 'w') | 92 f = file(pidfile, 'w') |
| 96 f.write(str(os.getpid())) | 93 f.write(str(os.getpid())) |
| 97 f.close() | 94 f.close() |
| 98 os.chmod(pidfile, 0644) | 95 # set umask if specified |
| 96 if umask is not None: |
| 97 os.umask(umask) |
| 99 # change process uid | 98 # change process uid |
| 100 if uid: | 99 if uid: |
| 101 setugid(uid) | 100 setugid(uid) |
| 102 return None | 101 return None |
| OLD | NEW |