Esempio n. 1
0
 def _monitor(self):
     while not self.stop_signal:
         try:
             proc = os.waitpid(-1, 0)
         except OSError,err:
             if err.errno == 4:
                 continue
         
         if self.stop_signal:
             break
                 
         if os.path.exists(self.pidsdir+'/gfchild_'+str(proc[0])+'.pid'):
             os.remove(self.pidsdir+'/gfchild_'+str(proc[0])+'.pid')
             
         logging.debug("Child # %s died" % proc[0])
         
         index = self.children.index(proc[0])
         forked = gevent.fork()
         
         if forked == 0:
             setupLogging(index,debug=self.debug,directory=self.logs_dir,
                             count=self.logs_count,max_size=self.logs_size)
             self.runChild(index,sockets=self.sockets)
             return
         else:
             file(self.pidsdir+'/gfchild_'+str(forked)+'.pid','w+').write("%s\n" % forked)
             self.children[index] = forked
             logging.debug("Created child # %s" % forked)    
Esempio n. 2
0
 def start_worker(self):
     w = Worker(self.sock, self.script)
     pid = fork()
     if pid:
         self.worker_pids.append(pid)
         return
     w.start()
Esempio n. 3
0
def drop_privileges(uid_name='nobody', gid_name='nogroup'):
    print("Init: Running as {0}/{1}.".format(
        pwd.getpwuid(os.getuid())[0],
        grp.getgrgid(os.getgid())[0]))
    wanted_uid = pwd.getpwnam(uid_name)[2]
    wanted_gid = grp.getgrnam(gid_name)[2]

    pid = gevent.fork()
    if pid == 0:
        # child
        print 'starting child process'
        child_process = gevent.spawn(root_process)
        child_process.join()
        print 'Child done:', child_process.successful()
        oschameleon.osfuscation.flush_tables()
        print 'Child exit'
    else:
        # parent
        os.setgid(wanted_gid)
        os.setuid(wanted_uid)
        new_uid_name = pwd.getpwuid(os.getuid())[0]
        new_gid_name = grp.getgrgid(os.getgid())[0]
        print("Parent: Privileges dropped, running as {0}/{1}.".format(
            new_uid_name, new_gid_name))
        while True:
            try:
                gevent.sleep(1)
                print 'Parent: ping'
            except KeyboardInterrupt:
                break
Esempio n. 4
0
def run_server(host=None, port=None, workers=None, debug=None,
               logfile=None, stdout=None, loglevel=None):
    if not host:
        host = settings.server_host
    if not port:
        port = settings.server_port

    if workers is not None:
        settings.workers = workers

    if debug:
        settings.debug = True

    try:
        proctitle = settings.proctitle
    except AttributeError:
        proctitle = 'geweb'
    setproctitle(proctitle)

    log.info('Starting HTTP server at %s:%d' % (host, port))

    pool = Pool(10000)
    server = WSGIServer("%s:%s" % (host, port), handle, spawn=pool)
    server.init_socket()

    for i in xrange(settings.workers - 1):
        pid = gevent.fork()
        if pid == 0:
            break

    server.serve_forever()
Esempio n. 5
0
    def spawn_child(self):
        """Spawn a child."""
        try:
            child = InstanceRunner(self.app, self.log_queue,
               ppid=self.pid, timeout=self.timeout, name=self.name,
               uid=self.uid, gid=self.gid)
        except:
            log.info("Unhandled exception while creating '%s':\n%s",
                            self.name, traceback.format_exc())
            return

        pid = gevent.fork()
        if pid != 0:
            self._CHILDREN[pid] = (child, 1)
            return

        # Process Child
        worker_pid = os.getpid()
        try:
            child.run()
            sys.exit(0)
        except SystemExit:
            raise
        except:
            log.exception("Exception in worker process:")
            if not child.booted:
                sys.exit(self._WORKER_BOOT_ERROR)
            sys.exit(-1)
        finally:
            log.info("Instance exiting (pid: %s)", worker_pid)
            try:
                child.tmp.close()
            except:
                pass
Esempio n. 6
0
 def start_worker(self):
     w = Worker(self.sock, self.script)
     pid = fork()
     if pid:
         self.worker_pids.append(pid)
         return
     w.start()
Esempio n. 7
0
def drop_privileges(uid_name='nobody', gid_name='nogroup'):
    print("Init: Running as {0}/{1}.".format(pwd.getpwuid(os.getuid())[0], grp.getgrgid(os.getgid())[0]))
    wanted_uid = pwd.getpwnam(uid_name)[2]
    wanted_gid = grp.getgrnam(gid_name)[2]

    pid = gevent.fork()
    if pid == 0:
        # child
        print 'starting child process'
        child_process = gevent.spawn(root_process)
        child_process.join()
        print 'Child done:', child_process.successful()
        oschameleon.osfuscation.flush_tables()
        print 'Child exit'
    else:
        # parent
        os.setgid(wanted_gid)
        os.setuid(wanted_uid)
        new_uid_name = pwd.getpwuid(os.getuid())[0]
        new_gid_name = grp.getgrgid(os.getgid())[0]
        print("Parent: Privileges dropped, running as {0}/{1}.".format(new_uid_name, new_gid_name))
        while True:
            try:
                gevent.sleep(1)
                print 'Parent: ping'
            except KeyboardInterrupt:
                break
Esempio n. 8
0
def daemonize():
	import gevent
	pid = gevent.fork()
	if pid == 0:
		run()
		sys.exit(0)
	else:
		writepid(pid)
Esempio n. 9
0
def fork_workers(num):
    pids = []
    for i in range(num):
        pid = gevent.fork()
        if pid == 0:
            run_child(os.getpid())
            sys.exit(0)
        else:
            pids.append(pid)
    return pids
Esempio n. 10
0
def daemonize(umask=0027):
    if gevent.fork():
        os._exit(0)
    os.setsid()
    if gevent.fork():
        os._exit(0)
    os.umask(umask)
    fd_limit = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
    if fd_limit == resource.RLIM_INFINITY:
        fd_limit = 1024
    for fd in xrange(0, fd_limit):
        try:
            os.close(fd)
        except:
            pass
    os.open(os.devnull, os.O_RDWR)
    os.dup2(0, 1)
    os.dup2(0, 2)
    gevent.reinit()
Esempio n. 11
0
    def _daemonize(self):
        """
        Standard daemonization of a process.
        http://www.svbug.com/documentation/comp.unix.programmer-FAQ/faq_2.html#SEC16
        """
        if gevent.fork():
            os._exit(0)

        os.setsid()
        if gevent.fork():
            os._exit(0)

        os.umask(0)
        
        if self.chdir:        
            os.chdir(self.chdir)

        """sys.stdout.flush()
        sys.stderr.flush()
        si = file('/dev/null', 'r')
        so = file('logs/console.log', 'a+')
        se = file('logs/console.log', 'a+', 0)
        os.dup2(si.fileno(), sys.stdin.fileno())
        os.dup2(so.fileno(), sys.stdout.fileno())
        os.dup2(se.fileno(), sys.stderr.fileno())
        """
        for i in xrange(3):
            # Closing parent standart in,out,error fd's
            os.close(i)
            
        os.open(REDIRECT_TO, os.O_RDWR)
        os.dup2(0, 1)
        os.dup2(0, 2)
        
        if self.uid:
            os.setuid(self.uid)
        if self.gid:
            os.setgid(self.gid)
            
        self.pid = os.getpid()
        file(self.pidsdir+'/gfdaemon.pid','w+').write(str(self.pid))
Esempio n. 12
0
File: util.py Progetto: adieu/tproxy
def daemonize(close=False):
    """\
    Standard daemonization of a process.
    http://www.svbug.com/documentation/comp.unix.programmer-FAQ/faq_2.html#SEC16
    """
    if not 'TPROXY_FD' in os.environ:
        try:
            if fork():
                os._exit(0)
        except OSError, e:
            sys.stderr.write("fork #1 failed: %s\n" % str(e))
            sys.exit(1)

        os.setsid()
        
        try:
            if fork():
                os._exit(0)
        except OSError, e:
            sys.stderr.write("fork #2 failed: %s\n" % str(e))
            sys.exit(1)
Esempio n. 13
0
def daemonize(close=False):
    """\
    Standard daemonization of a process.
    http://www.svbug.com/documentation/comp.unix.programmer-FAQ/faq_2.html#SEC16
    """
    if not 'TPROXY_FD' in os.environ:
        try:
            if fork():
                os._exit(0)
        except OSError, e:
            sys.stderr.write("fork #1 failed: %s\n" % str(e))
            sys.exit(1)

        os.setsid()

        try:
            if fork():
                os._exit(0)
        except OSError, e:
            sys.stderr.write("fork #2 failed: %s\n" % str(e))
            sys.exit(1)
Esempio n. 14
0
 def start_daemon_process(self, pid_file_name):
     """ Run server in daemon mode. """
     # First fork: allow shell to return, detach from controlling
     # terminal using setsid()
     try:
         pid = gevent.fork()
         if pid > 0:
             sys.exit(0)
     except OSError, e:
         print >>sys.stderr, 'First fork failed with error %d (%s)' \
             % (e.errno, e.strerror)
         sys.exit(1)
Esempio n. 15
0
def daemon(user, group, path='/', pidfile='/tmp/%s.pid' % __default_servicename__, other_groups=()):
    '''
    Daemonizes current application.
    '''
    # Get uid and gid from user and group names
    uid = int(pwd.getpwnam(user)[2])
    gid = int(grp.getgrnam(group)[2])
    # Get ID of other groups
    other_groups_id = []
    for name in other_groups:
        try:
            other_groups_id.append(int(grp.getgrnam(name)[2]) )
        except:
            pass
    # First fork
    pid = gevent.fork()
    if not pid == 0:
        os._exit(0)
    # Creates a session and sets the process group ID
    os.setsid()
    # Second fork
    pid = gevent.fork()
    if not pid == 0:
        os._exit(0)
    # Change directoty
    os.chdir(path)
    # Set umask
    os.umask(0)
    # Write pidfile
    open(pidfile, 'w').write(str(os.getpid()))
    # Set group and groups
    os.setgid(gid)
    if other_groups_id:
        os.setgroups(other_groups_id)
    # Set user
    os.setuid(uid)
    # Redirect stdout/stderr to /dev/null
    sys.stdout = sys.stderr = open(os.devnull, 'a+')
    gevent.reinit()
Esempio n. 16
0
def fork():
    """ A workaround for gevent issue 154[0], until that is fixed.

        [0]: http://code.google.com/p/gevent/issues/detail?id=154 """
    tp = gevent.get_hub().threadpool
    if len(tp):
        sys.stderr.write("WARNING: calling fork() while threads are active; "
                         "they will be killed.\n")
    tp.kill()
    tp.join()
    result = gevent.fork()
    gevent.sleep(0)
    return result
Esempio n. 17
0
def fork():
    """ A workaround for gevent issue 154[0], until that is fixed.

        [0]: http://code.google.com/p/gevent/issues/detail?id=154 """
    tp = get_hub().threadpool
    if len(tp):
        sys.stderr.write("WARNING: calling fork() while threads are active; "
                         "they will be killed.\n")
    tp.kill()
    tp.join()
    result = gevent.fork()
    gevent.sleep(0)
    return result
Esempio n. 18
0
	def daemonize(self):
		"""
		Do the UNIX double-fork magic, see Stevens' "Advanced 
		Programming in the UNIX Environment" for details (ISBN 0201563177)
		http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
		"""
		try: 
			pid = gevent.fork() 
			if pid > 0:
				# Exit first parent
				sys.exit(0) 
		except OSError, e: 
			sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
			sys.exit(1)
Esempio n. 19
0
def daemonize(pidfile, stdin='/dev/null', stdout='/dev/null',
              stderr='/dev/null'):
    """Creates a child process, shuts down the parent process, and
    then redirects the io streams to a log file or /dev/null.
    """
    # Do first fork.
    try:
        pid = gevent.fork()
        if pid != 0:
            os._exit(0)
    except OSError, exc:
        # Python raises OSError rather than returning negative numbers.
        sys.exit("%s: fork #1 failed: (%d) %s\n"
                 % (sys.argv[0], exc.errno, exc.strerror))
Esempio n. 20
0
def daemonize(pidfile,
              stdin='/dev/null',
              stdout='/dev/null',
              stderr='/dev/null'):
    """Creates a child process, shuts down the parent process, and
    then redirects the io streams to a log file or /dev/null.
    """
    # Do first fork.
    try:
        pid = gevent.fork()
        if pid != 0:
            os._exit(0)
    except OSError, exc:
        # Python raises OSError rather than returning negative numbers.
        sys.exit("%s: fork #1 failed: (%d) %s\n" %
                 (sys.argv[0], exc.errno, exc.strerror))
Esempio n. 21
0
def drop_privileges(uid_name='nobody', gid_name='nogroup'):
    print("Running as {0}/{1}.".format(
        pwd.getpwuid(os.getuid())[0],
        grp.getgrgid(os.getgid())[0]))
    wanted_uid = pwd.getpwnam(uid_name)[2]
    wanted_gid = grp.getgrnam(gid_name)[2]

    pid = gevent.fork()
    if pid == 0:
        os.setgid(wanted_gid)
        os.setuid(wanted_uid)
        new_uid_name = pwd.getpwuid(os.getuid())[0]
        new_gid_name = grp.getgrgid(os.getgid())[0]
        print("Privileges dropped, running as {0}/{1}.".format(
            new_uid_name, new_gid_name))
    else:
        lets = gevent.spawn(root_process)
        gevent.joinall([
            lets,
        ])
Esempio n. 22
0
def test_main():
    # This function spawns a child process to insulate the main
    # test-running process from all the signals. It then
    # communicates with that child process over a pipe and
    # re-raises information about any exceptions the child
    # throws. The real work happens in self.run_test().
    os_done_r, os_done_w = os.pipe()
    with closing(os.fdopen(os_done_r)) as done_r:
        with closing(os.fdopen(os_done_w, 'w')) as done_w:
            child = gevent.fork()
            if not child:
                # In the child process; run the test and report results
                # through the pipe.
                try:
                    done_r.close()
                    # Have to close done_w again here because
                    # exit_subprocess() will skip the enclosing with block.
                    with closing(done_w):
                        try:
                            run_test()
                        except:
                            pickle.dump(traceback.format_exc(), done_w)
                        else:
                            pickle.dump(None, done_w)
                except:
                    print('Uh oh, raised from pickle.')
                    traceback.print_exc()
                finally:
                    os._exit(0)

            done_w.close()
            # Block for up to MAX_DURATION seconds for the test to finish.
            r, w, x = select.select([done_r], [], [], MAX_DURATION)
            if done_r in r:
                tb = pickle.load(done_r)
                assert not tb, tb
            else:
                os.kill(child, 9)
                assert False, 'Test deadlocked after %d seconds.' % MAX_DURATION
Esempio n. 23
0
    def spawn_child(self):
        """Spawn a child."""
        try:
            child = InstanceRunner(self.app,
                                   self.log_queue,
                                   ppid=self.pid,
                                   timeout=self.timeout,
                                   name=self.name,
                                   uid=self.uid,
                                   gid=self.gid)
        except:
            log.info("Unhandled exception while creating '%s':\n%s", self.name,
                     traceback.format_exc())
            return

        pid = gevent.fork()
        if pid != 0:
            self._CHILDREN[pid] = (child, 1)
            return

        # Process Child
        worker_pid = os.getpid()
        try:
            child.run()
            sys.exit(0)
        except SystemExit:
            raise
        except:
            log.exception("Exception in worker process:")
            if not child.booted:
                sys.exit(self._WORKER_BOOT_ERROR)
            sys.exit(-1)
        finally:
            log.info("Instance exiting (pid: %s)", worker_pid)
            try:
                child.tmp.close()
            except:
                pass
Esempio n. 24
0
 def start_worker(self):
     """Start a worker and add to list."""
     if self.stop_event.is_set():
         return
     pipe_r, pipe_w = os.pipe()
     pid = gevent.fork()
     if pid:
         os.close(pipe_r)
         cntr = WorkerController(pid, pipe_w=pipe_w)
         self.workers.add(cntr)
         self.exit_completed.clear()
         cntr.notify("hello")
         return pid
     os.close(pipe_w)
     w_args, w_kwds = self.worker_args
     worker = Worker(self.sock, self.application, *w_args, **w_kwds)
     cntr = WorkerController(os.getpid(), pipe_r=pipe_r)
     cntr.begin_notify_receive(worker.handle_notification)
     try:
         worker.serve_forever()
     except KeyboardInterrupt:
         pass
     os._exit(self.exit_stopped)
Esempio n. 25
0
def run_server(host=None,
               port=None,
               workers=None,
               debug=None,
               logfile=None,
               stdout=None,
               loglevel=None):
    if not host:
        host = settings.server_host
    if not port:
        port = settings.server_port

    if workers is not None:
        settings.workers = workers

    if debug:
        settings.debug = True

    try:
        proctitle = settings.proctitle
    except AttributeError:
        proctitle = 'geweb'
    setproctitle(proctitle)

    log.info('Starting HTTP server at %s:%d' % (host, port))

    pool = Pool(10000)
    server = WSGIServer("%s:%s" % (host, port), handle, spawn=pool)
    server.init_socket()

    for i in xrange(settings.workers - 1):
        pid = gevent.fork()
        if pid == 0:
            break

    server.serve_forever()
Esempio n. 26
0
import gevent
from gevent.server import StreamServer


def eat_cpu():
    for i in xrange(10000):
        pass


def cb(socket, address):
    eat_cpu()
    socket.recv(1024)
    socket.sendall("HTTP/1.1 200 OK\n\nHello World!!")
    socket.close()


server = StreamServer(("", 8000), cb, backlog=100000)
server.pre_start()

gevent.fork()

server.start_accepting()
server._stopped_event.wait()
Esempio n. 27
0
    def _start_server(self):
        """
        Low level start
        """

        # Allocate a server, and provide a connection callback
        logger.info("listen_addr=%s", self._tcp_server_config.listen_addr)
        logger.info("listen_port=%s", self._tcp_server_config.listen_port)
        logger.info("ssl_enable=%s", self._tcp_server_config.ssl_enable)
        logger.info("ssl_key_file=%s", self._tcp_server_config.ssl_key_file)
        logger.info("ssl_certificate_file=%s",
                    self._tcp_server_config.ssl_certificate_file)

        logger.info("child_process_count=%s",
                    self._tcp_server_config.child_process_count)

        logger.info("ssl_handshake_timeout_ms=%s",
                    self._tcp_server_config.ssl_handshake_timeout_ms)

        logger.info("onstop_call_client_stopsynch=%s",
                    self._tcp_server_config.onstop_call_client_stopsynch)

        logger.info("socket_absolute_timeout_ms=%s",
                    self._tcp_server_config.socket_absolute_timeout_ms)
        logger.info("socket_relative_timeout_ms=%s",
                    self._tcp_server_config.socket_relative_timeout_ms)
        logger.info("socket_min_checkinterval_ms=%s",
                    self._tcp_server_config.socket_min_checkinterval_ms)

        logger.info("_effective_control_interval_ms=%s",
                    self._effective_control_interval_ms)

        logger.info("stop_client_timeout_ms=%s",
                    self._tcp_server_config.stop_client_timeout_ms)
        logger.info("stop_server_timeout_ms=%s",
                    self._tcp_server_config.stop_server_timeout_ms)

        logger.info("client_factory=%s",
                    self._tcp_server_config.client_factory)

        if self._tcp_server_config.ssl_enable is False:
            # No SSL
            logger.info("Starting in TCP/CLEAR mode")
            self._server = StreamServerNoClose(
                (self._tcp_server_config.listen_addr,
                 self._tcp_server_config.listen_port), self._on_connection)
        else:
            # SSL ON
            logger.info("Starting in TCP/SSL mode")
            self._server = StreamServerNoClose(
                (self._tcp_server_config.listen_addr,
                 self._tcp_server_config.listen_port),
                self._on_connection,
                # SSL enabling
                keyfile=self._tcp_server_config.ssl_key_file,
                certfile=self._tcp_server_config.ssl_certificate_file,
                # SSL handshake after accept
                do_handshake_on_connect=True,
                # TLS
                ssl_version=PROTOCOL_TLSv1,
                # Cipher
                # ciphers="RC4-MD5",
                # Args
            )

            self._server.min_delay = 0.0
            self._server.max_delay = 0.0

        # Startup
        if self._tcp_server_config.child_process_count <= 0:
            # Normal start-up
            logger.info("Starting in NON-FORKED mode")
            self._server.start()
        else:
            # Child process startup : prestart
            logger.info("Pre-starting in FORKED mode, subprocess=%s",
                        self._tcp_server_config.child_process_count)
            # GEVENT_RC1 fix : pre_start => init_socket
            self._server.init_socket()

            # Let's rock
            logger.info("Forking gevent")
            for idx in range(self._tcp_server_config.child_process_count):
                # Fork gevent hub
                # GEVENT_RC1 fix : hub.fork => os.fork
                _fork_pid = gevent.fork()

                logger.info("Forking gevent hub done, idx=%s, forkPid=%s, %s",
                            idx, _fork_pid,
                            SolBase.get_current_pid_as_string())

                # Check it
                if _fork_pid == 0:
                    # We are in a child => exit this loop
                    SolBase.set_master_process(False)
                    break
                else:
                    # Master on
                    SolBase.set_master_process(True)

                    # Store child pid
                    logger.info("Storing child _fork_pid=%s", _fork_pid)
                    self._fork_pid_list.append(int(_fork_pid))

            # Start accepting now (parent and all sub-processes)
            logger.info("Accepting now, %s",
                        SolBase.get_current_pid_as_string())
            self._server.start_accepting()
Esempio n. 28
0
    def run(self):

        if os.path.isfile(self.args.pidfile):
            self.log.error("%s found: is the server already running?" % self.args.pidfile)
            return 1

        stores = dict()
        for container, values in self.conf.iteritems():
            cli = client.Connection(
                values['authurl'], values['username'], values['password'],
                auth_version=values['auth_version'],
                tenant_name=values['tenant_name'])

            try:
                headers, _ = cli.get_container(container)
            except (socket.error, client.ClientException) as ex:
                if getattr(ex, 'http_status', None) == 404:
                    self.log.warning("%s doesn't exist, skipping" % container)
                    continue
                else:
                    self.log.error(ex)
                    return 1

            self.log.debug(headers)

            meta = getMeta(headers)
            if not meta:
                self.log.warning("%s doesn't appear to be setup, skipping" % container)
                continue

            self.log.debug("Meta: %s" % meta)

            try:
                object_size = int(meta['object-size'])
                objects = int(meta['objects'])
            except ValueError as ex:
                self.log.error("%s doesn't appear to be correct: %s" % (container, ex))
                return 1

            if meta['version'] != disk_version:
                self.log.warning("Version mismatch %s != %s in %s" % (meta['version'], disk_version, container))

            stores[container] = SwiftStorage(values['authurl'],
                                             values['username'],
                                             values['password'],
                                             container,
                                             object_size,
                                             objects,
                                             Cache(int(self.args.cache_limit*1024**2 / object_size)),
                                             values['read-only'].lower() in ('1', 'yes', 'true', 'on'),
                                             auth_version=values['auth_version'],
                                             tenant_name=values['tenant_name']
                                            )

        addr = (self.args.bind_address, self.args.bind_port)
        server = Server(addr, stores)

        gevent.signal(signal.SIGTERM, server.stop)
        gevent.signal(signal.SIGINT, server.stop)

        if not self.args.foreground:
            try:
                if gevent.fork() != 0:
                    os._exit(0)
            except OSError as ex:
                self.log.error("Failed to daemonize: %s" % ex)
                return 1

            os.setsid()
            fd = os.open(os.devnull, os.O_RDWR)
            os.dup2(fd, sys.stdin.fileno())
            os.dup2(fd, sys.stdout.fileno())
            os.dup2(fd, sys.stderr.fileno())

            gevent.reinit()

        self.log.info("Starting to serve on %s:%s" % (addr[0], addr[1]))

        try:
            fd = os.open(self.args.pidfile, (os.O_CREAT|os.O_EXCL|os.O_WRONLY), 0644)
        except OSError as ex:
            self.log.error("Failed to create the pidfile: %s" % ex)
            return 1

        with os.fdopen(fd, "w") as pidfile_handle:
            pidfile_handle.write("%s\n" % os.getpid())
            pidfile_handle.flush()

            server.serve_forever()

        os.remove(self.args.pidfile)

        # unlock the storages before exit
        server.unlock_all()

        self.log.info("Exiting...")
        return 0
Esempio n. 29
0
 def _fork(self):
     """Override Daemon._fork with gevent's fork."""
     return gevent.fork()
Esempio n. 30
0
			pid = gevent.fork() 
			if pid > 0:
				# Exit first parent
				sys.exit(0) 
		except OSError, e: 
			sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
			sys.exit(1)
		
		# Decouple from parent environment
		os.chdir(self.home_dir)
		os.setsid() 
		os.umask(self.umask)
	
		# Do second fork
		try: 
			pid = gevent.fork() 
			if pid > 0:
				# Exit from second parent
				sys.exit(0) 
		except OSError, e: 
			sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
			sys.exit(1) 
	
		if sys.platform != 'darwin': # This block breaks on OS X
			# Redirect standard file descriptors
			sys.stdout.flush()
			sys.stderr.flush()
			si = file(self.stdin, 'r')
			so = file(self.stdout, 'a+')
			if self.stderr:
				se = file(self.stderr, 'a+', 0)
Esempio n. 31
0
    """
    # Do first fork.
    try:
        pid = gevent.fork()
        if pid != 0:
            os._exit(0)
    except OSError, exc:
        # Python raises OSError rather than returning negative numbers.
        sys.exit("%s: fork #1 failed: (%d) %s\n"
                 % (sys.argv[0], exc.errno, exc.strerror))
        
    os.setsid()

    # Do second fork
    try:
        pid = gevent.fork()
        if pid > 0:
            # Second parent.
            os._exit(0)
    except OSError, exc:
        sys.exit("%s: fork #2 failed: (%d) %s\n"
                 % (sys.argv[0], exc.errno, exc.strerror))

    #os.chdir("/")

    si = open(stdin, "r")
    so = open(stdout, "a+")
    se = open(stderr, "a+", 0)

    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
Esempio n. 32
0
    def _godaemon(self):
        """
        daemonize us
        """

        logger.debug("Entering, pid=%s", os.getpid())

        # Limit
        self._set_limits()

        # Fork1
        logger.debug("fork1, %s", SolBase.get_current_pid_as_string())
        try:
            pid = gevent.fork()
            if pid > 0:
                # Exit first parent
                logger.debug("exit(0) first parent")
                sys.exit(0)
        except OSError as ex:
            logger.error("fork1 failed, exit(1) now : errno=%s, err=%s, ex=%s",
                         ex.errno, ex.strerror, SolBase.extostr(ex))
            sys.exit(1)
        logger.debug("fork1 done, %s", SolBase.get_current_pid_as_string())

        # Diverge from parent
        if self._changeDir:
            logger.debug("chdir now")
            os.chdir("/")

        # Set stuff
        logger.debug("setsid and umask")
        # noinspection PyArgumentList
        os.setsid()
        os.umask(0)

        # Fork2
        logger.debug("fork2, %s", SolBase.get_current_pid_as_string())
        try:
            pid = gevent.fork()
            if pid > 0:
                # exit from second parent
                logger.debug("exit(0) second parent")
                sys.exit(0)
        except OSError as ex:
            logger.error("fork2 failed, exit(2) now : errno=%s, err=%s, ex=%s",
                         ex.errno, ex.strerror, SolBase.extostr(ex))
            sys.exit(2)
        logger.debug("fork2 done, %s", SolBase.get_current_pid_as_string())

        # Redirect std
        self._redirect_all_std()

        # Go
        logger.debug("initializing _pidfile=%s", self._pidfile)

        # Register the method called at exit
        atexit.register(self._remove_pid_file)

        # Write pidfile
        pid = str(os.getpid())
        try:
            f = open(self._pidfile, "w")
            f.write("%s" % pid)
            f.close()

        except IOError as ex:
            logger.error(
                "pid file initialization failed, going exit(3), ex=%s",
                SolBase.extostr(ex))
            sys.exit(3)

            # Ok
        logger.debug("pid file set")

        # Finish
        logger.debug("registering gevent signal handler : SIGUSR1")
        signal(SIGUSR1, self._on_reload)
        logger.debug("registering gevent signal handler : SIGUSR2")
        signal(SIGUSR2, self._on_status)
        logger.debug("registering gevent signal handler : SIGTERM")
        signal(SIGTERM, self._exit_handler)

        logger.debug("registering gevent signal handler : done")

        # Fatality
        SolBase.voodoo_init()
        logger.debug("process started, pid=%s, pidfile=%s", os.getpid(),
                     self._pidfile)