def daemonize(reactor, os): """ Daemonizes the application on Unix. This is done by the usual double forking approach. @see: U{http://code.activestate.com/recipes/278731/} @see: W. Richard Stevens, "Advanced Programming in the Unix Environment", 1992, Addison-Wesley, ISBN 0-201-56317-7 @param reactor: The reactor in use. If it provides L{IReactorDaemonize}, its daemonization-related callbacks will be invoked. @param os: An object like the os module to use to perform the daemonization. """ ## If the reactor requires hooks to be called for daemonization, call them. ## Currently the only reactor which provides/needs that is KQueueReactor. if IReactorDaemonize.providedBy(reactor): reactor.beforeDaemonize() if os.fork(): # launch child and... os._exit(0) # kill off parent os.setsid() if os.fork(): # launch child and... os._exit(0) # kill off parent again. null = os.open('/dev/null', os.O_RDWR) for i in range(3): try: os.dup2(null, i) except OSError, e: if e.errno != errno.EBADF: raise
def daemonize(self, reactor): """ Daemonizes the application on Unix. This is done by the usual double forking approach. @see: U{http://code.activestate.com/recipes/278731/} @see: W. Richard Stevens, "Advanced Programming in the Unix Environment", 1992, Addison-Wesley, ISBN 0-201-56317-7 @param reactor: The reactor in use. If it provides L{IReactorDaemonize}, its daemonization-related callbacks will be invoked. @return: A writable pipe to be used to report errors. @rtype: C{int} """ # If the reactor requires hooks to be called for daemonization, call # them. Currently the only reactor which provides/needs that is # KQueueReactor. if IReactorDaemonize.providedBy(reactor): reactor.beforeDaemonize() r, w = os.pipe() if os.fork(): # launch child and... code = self._waitForStart(r) os.close(r) os._exit(code) # kill off parent os.setsid() if os.fork(): # launch child and... os._exit(0) # kill off parent again. null = os.open('/dev/null', os.O_RDWR) for i in range(3): try: os.dup2(null, i) except OSError as e: if e.errno != errno.EBADF: raise os.close(null) if IReactorDaemonize.providedBy(reactor): reactor.afterDaemonize() return w
+ from twisted.internet import kqreactor + kqreactor.install() +else: + from twisted.internet import epollreactor + epollreactor.install() + from twisted.internet import reactor from OPSI.Application import Application @@ -551,12 +561,25 @@ class OpsiconfdInit(Application): signal(SIGHUP, self.signalHandler) if self.config['daemon']: + if platform.system().lower().endswith('bsd'): + if IReactorDaemonize.providedBy(reactor): + reactor.beforeDaemonize() + daemonize() time.sleep(2) + if platform.system().lower().endswith('bsd'): + if IReactorDaemonize.providedBy(reactor): + reactor.afterDaemonize() + self.createPidFile() - libc = CDLL("libc.so.6") - libc.prctl(15, 'opsiconfd', 0, 0, 0) + + if platform.system().lower().endswith('bsd'): + libc = CDLL(ctypes.util.find_library("c")) + libc.setproctitle('opsiconfd')