Esempio n. 1
0
def test_message_levels(time_ctime):
    log.warn("test warning")
    assert_true(time_ctime.called)
    time_ctime.reset

    log.error("test error")
    assert_true(time_ctime.called)
    time_ctime.reset

    log.info("test info")
    assert_true(time_ctime.called)
    time_ctime.reset
    def before_jail(self, args):
        from gevent.server import StreamServer

        self.host = self.config.get("geventserver.host", "0.0.0.0")
        self.ports = (int(x) for x in self.config["geventserver.ports"].split())
        log.info("Listening ports %r" % self.ports)

        self.server = None  # this gets the last one to do a forever on

        for port in self.ports:
            self.server = StreamServer((self.host, port), handle)
            self.server.start()
Esempio n. 3
0
def test_message_levels(time_ctime):
    log.warn("test warning")
    assert_true(time_ctime.called)
    time_ctime.reset

    log.error("test error")
    assert_true(time_ctime.called)
    time_ctime.reset

    log.info("test info")
    assert_true(time_ctime.called)
    time_ctime.reset
Esempio n. 4
0
    def before_jail(self, args):
        from gevent.server import StreamServer

        self.host = self.config.get('geventserver.host', '0.0.0.0')
        self.ports = (int(x)
                      for x in self.config['geventserver.ports'].split())
        log.info("Listening ports %r" % self.ports)

        self.server = None  # this gets the last one to do a forever on

        for port in self.ports:
            self.server = StreamServer((self.host, port), handle)
            self.server.start()
Esempio n. 5
0
def drop_privileges(running_uid, running_gid):
    if os.getuid() != 0:
        return

    log.info("Dropping pivs to UID %r GID %r" % (running_uid, running_gid))

    # Remove group privileges
    os.setgroups([])

    # Try setting the new uid/gid
    os.setgid(running_gid)
    os.setuid(running_uid)

    # Ensure a very conservative umask
    os.umask(077)
Esempio n. 6
0
def drop_privileges(running_uid, running_gid):
    if os.getuid() != 0:
        return

    log.info("Dropping pivs to UID %r GID %r" % (running_uid, running_gid))

    # Remove group privileges
    os.setgroups([])

    # Try setting the new uid/gid
    os.setgid(running_gid)
    os.setuid(running_uid)

    # Ensure a very conservative umask
    os.umask(077)
Esempio n. 7
0
def tail_lines(file_name):
    _, orig_stat = file_rotated(file_name, None)
    log_file = open(file_name)
    log_file.seek(0, os.SEEK_END)

    while True:
        line = log_file.readline()
        if line:
            yield line
        else:
            time.sleep(0.1)

            # check for rotation and reopen if it did
            rotated, orig_stat = file_rotated(file_name, orig_stat)
            if rotated:
                log.info("Log file %s rotated." % file_name)
                log_file.close()
                log_file = open(file_name)
Esempio n. 8
0
def tail_lines(file_name):
    _, orig_stat = file_rotated(file_name, None)
    log_file = open(file_name)
    log_file.seek(0, os.SEEK_END)

    while True:
        line = log_file.readline()
        if line:
            yield line
        else:
            time.sleep(0.1)

            # check for rotation and reopen if it did
            rotated, orig_stat = file_rotated(file_name, orig_stat)
            if rotated:
                log.info("Log file %s rotated." % file_name)
                log_file.close()
                log_file = open(file_name)
Esempio n. 9
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)
Esempio n. 10
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)

        if not os.path.exists(self.run_dir):
            log.warn("Directory %s does not exist, attempting to create it." %
                     self.run_dir)
            os.mkdir(self.run_dir)

            log.info(
                "Giving default permissions to %s, change them later if you need."
                % self.run_dir)
            os.chown(self.run_dir, self.unum, self.gnum)
            os.chmod(self.run_dir, self.run_dir_mode)

        if self.should_jail:
            self.before_jail(args)
            log.info("Setting up the chroot jail to: %s" % self.run_dir)
            unix.chroot_jail(self.run_dir)
        else:
            log.warn("This daemon does not jail itself, chdir to %s instead" %
                     self.run_dir)
            os.chdir(self.run_dir)

        if self.should_drop_priv:
            self.before_drop_privs(args)
            unix.drop_privileges(self.unum, self.gnum)
        else:
            log.warn("This daemon does not drop privileges.")

        log.info("Server %s running." % self.name)
        self.start(args)
Esempio n. 11
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)

        if not os.path.exists(self.run_dir):
            log.warn("Directory %s does not exist, attempting to create it." %
                     self.run_dir)
            os.mkdir(self.run_dir)

            log.info("Giving default permissions to %s, change them later if you need."
                     % self.run_dir)
            os.chown(self.run_dir, self.unum, self.gnum)
            os.chmod(self.run_dir, self.run_dir_mode)

        if self.should_jail:
            self.before_jail(args)
            log.info("Setting up the chroot jail to: %s" % self.run_dir)
            unix.chroot_jail(self.run_dir)
        else:
            log.warn("This daemon does not jail itself, chdir to %s instead" % self.run_dir)
            os.chdir(self.run_dir)

        if self.should_drop_priv:
            self.before_drop_privs(args)
            unix.drop_privileges(self.unum, self.gnum)
        else:
            log.warn("This daemon does not drop privileges.")

        log.info("Server %s running." % self.name)
        self.start(args)
Esempio n. 12
0
 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)
Esempio n. 13
0
 def stop(self, args):
     log.info("Stopping server.")
     unix.kill_server(self.name, pid_file_path=self.pid_path)
Esempio n. 14
0
 def callme():
     log.setup('/tmp/test_daemonize_no_exit.log')
     for i in range(0, 3):
         log.info("I ran!")
         time.sleep(1)
Esempio n. 15
0
def handle(socket, address):
    log.info("Blocking %r:%r" % address)
    socket.close()
Esempio n. 16
0
 def shutdown(self, signal):
     log.info("Shutting down now signal: %d" % signal)
Esempio n. 17
0
 def start(self, args):
     log.info('start')
     IOLoop.instance.start()
Esempio n. 18
0
 def stop(self, args):
     log.info("Stopping server.")
     unix.kill_server(self.name, pid_file_path=self.pid_path)
Esempio n. 19
0
 def handle(self):
     log.info("Connection %r:%r" % self.client_address)
     self.request.sendall("HI!")
     self.request.close()
Esempio n. 20
0
 def handle(self):
     log.info("Connection %r:%r" % self.client_address)
     self.request.sendall("HI!")
     self.request.close()
Esempio n. 21
0
 def start(self, args):
     log.info('start')
     IOLoop.instance.start()
Esempio n. 22
0
def handle(socket, address):
    log.info("Blocking %r:%r" % address)
    socket.close()
Esempio n. 23
0
 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)
Esempio n. 24
0
 def shutdown(self, signal):
     log.info("Shutting down now signal: %d" % signal)
Esempio n. 25
0
 def stop(self, args):
     log.info("Stopping server.")
     unix.kill_server(self.name)