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()
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()
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)
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)
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)
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)
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)
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)
def stop(self, args): log.info("Stopping server.") unix.kill_server(self.name, pid_file_path=self.pid_path)
def callme(): log.setup('/tmp/test_daemonize_no_exit.log') for i in range(0, 3): log.info("I ran!") time.sleep(1)
def handle(socket, address): log.info("Blocking %r:%r" % address) socket.close()
def shutdown(self, signal): log.info("Shutting down now signal: %d" % signal)
def start(self, args): log.info('start') IOLoop.instance.start()
def handle(self): log.info("Connection %r:%r" % self.client_address) self.request.sendall("HI!") self.request.close()
def stop(self, args): log.info("Stopping server.") unix.kill_server(self.name)