Пример #1
0
    def daemonize(self, args):
        log.setup(self.log_file)
        log.info("Daemonizing.")

        self.before_daemonize(args)

        if unix.still_running(self.name, pid_file_path=self.pid_path):
            log.error("%s still running. Aborting." % self.name)
            sys.exit(1)
        else:
            unix.daemonize(self.name, pid_file_path=self.pid_path)

        def shutdown_handler(signal, frame):
            self.shutdown(signal)
            sys.exit(0)

        unix.register_shutdown(shutdown_handler)

        self.before_jail(args)

        log.info("Setting up the chroot jail to: %s" % self.run_dir)
        unix.chroot_jail(self.run_dir)

        self.before_drop_privs(args)

        unix.drop_privileges(uid_name=self.uid, gid_name=self.gid)

        log.info("Server %s running." % self.name)
        self.start(args)
Пример #2
0
def test_daemonize_dont_exit():
    if os.path.exists('/tmp/test_daemonize_no_exit.log'): os.unlink('/tmp/test_daemonize_no_exit.log')
    if os.path.exists('/tmp/test_daemonize_no_exit.pid'): os.unlink('/tmp/test_daemonize_no_exit.pid')

    def main():
        """This will exit the daemon after 4 seconds."""
        log.setup('/tmp/test_daemonize_no_exit.log', force=True)
        for i in range(0, 4):
            log.info("I ran!")
            time.sleep(1)

    unix.daemonize('test_daemonize_no_exit', pid_file_path="/tmp",
                         dont_exit=True, main=main)

    while not unix.still_running('test_daemonize_no_exit', pid_file_path='/tmp'):
        time.sleep(1)

    assert_true(os.path.exists('/tmp/test_daemonize_no_exit.pid'))
Пример #3
0
def test_simple_server():
    if not os.path.exists("tests/simpledaemon"):
        os.mkdir("tests/simpledaemon")

    os.system('sudo python tests/simple_test_server.py start')
    sleep(1)

    EXPECT_PATHS = [
        'tests/simpledaemon.pid', 'tests/simpledaemon.log',
        'tests/simpledaemon/before_drop_priv.txt', '/tmp/before_jail.txt',
        '/tmp/before_daemonize.txt'
    ]

    for path in EXPECT_PATHS:
        assert_true(os.path.exists(path), "File %s not there." % path)

    os.system('sudo python tests/simple_test_server.py stop')
    sleep(1)

    assert_false(unix.still_running("simpledaemon", pid_file_path="tests"))
Пример #4
0
def test_simple_server():
    if not os.path.exists("tests/simpledaemon"):
        os.mkdir("tests/simpledaemon")

    os.system('sudo python tests/simple_test_server.py start')
    sleep(1)

    EXPECT_PATHS=['tests/simpledaemon.pid',
                  'tests/simpledaemon.log',
                  'tests/simpledaemon/before_drop_priv.txt',
                  '/tmp/before_jail.txt',
                  '/tmp/before_daemonize.txt']

    for path in EXPECT_PATHS:
        assert_true(os.path.exists(path), "File %s not there." % path)

    os.system('sudo python tests/simple_test_server.py stop')
    sleep(1)

    assert_false(unix.still_running("simpledaemon", pid_file_path="tests"))
Пример #5
0
def test_daemonize_dont_exit():
    if os.path.exists('/tmp/test_daemonize_no_exit.log'):
        os.unlink('/tmp/test_daemonize_no_exit.log')
    if os.path.exists('/tmp/test_daemonize_no_exit.pid'):
        os.unlink('/tmp/test_daemonize_no_exit.pid')

    def main():
        """This will exit the daemon after 4 seconds."""
        log.setup('/tmp/test_daemonize_no_exit.log', force=True)
        for i in range(0, 4):
            log.info("I ran!")
            time.sleep(1)

    unix.daemonize('test_daemonize_no_exit',
                   pid_file_path="/tmp",
                   dont_exit=True,
                   main=main)

    while not unix.still_running('test_daemonize_no_exit',
                                 pid_file_path='/tmp'):
        time.sleep(1)

    assert_true(os.path.exists('/tmp/test_daemonize_no_exit.pid'))
Пример #6
0
def test_still_running_stale_process():
    unix.pid_store("test_still_running", pid_file_path="/tmp")
    assert_false(unix.still_running("test_still_running",
                                    pid_file_path="/tmp"))
    os.unlink("/tmp/test_still_running.pid")
Пример #7
0
def test_still_running():
    unix.pid_store("test_still_running", pid_file_path="/tmp")
    assert_true(unix.still_running("test_still_running", pid_file_path="/tmp"))
    os.unlink("/tmp/test_still_running.pid")
Пример #8
0
def setup():
    if unix.still_running("simpledaemon", pid_file_path="tests"):
        os.system('sudo python tests/simple_test_server.py stop')
Пример #9
0
def test_still_running_stale_process():
    unix.pid_store("test_still_running", pid_file_path="/tmp")
    assert_false(unix.still_running("test_still_running", pid_file_path="/tmp"))
    os.unlink("/tmp/test_still_running.pid")
Пример #10
0
def test_still_running():
    unix.pid_store("test_still_running", pid_file_path="/tmp")
    assert_true(unix.still_running("test_still_running", pid_file_path="/tmp"))
    os.unlink("/tmp/test_still_running.pid")
Пример #11
0
def setup():
    if unix.still_running("simpledaemon", pid_file_path="tests"):
        os.system('sudo python tests/simple_test_server.py stop')