diff --git a/tuned.py b/tuned.py index 3803a11..d82a0f6 100755 --- a/tuned.py +++ b/tuned.py @@ -36,7 +36,8 @@ if __name__ == "__main__": parser = argparse.ArgumentParser(description = "Daemon for monitoring and adaptive tuning of system devices.") parser.add_argument("--daemon", "-d", action = "store_true", help = "run on background") parser.add_argument("--debug", "-D", action = "store_true", help = "show/log debugging messages") - parser.add_argument("--log", "-l", nargs = "?", const = consts.LOG_FILENAME, help = "log to file, default file: " + consts.LOG_FILENAME) + parser.add_argument("--log", "-l", nargs = "?", const = consts.LOG_FILE, help = "log to file, default file: " + consts.LOG_FILE) + parser.add_argument("--pid", "-P", nargs = "?", const = consts.PID_FILE, help = "write PID file, default file: " + consts.PID_FILE) parser.add_argument("--no-dbus", action = "store_true", help = "do not attach to DBus") parser.add_argument("--profile", "-p", action = "store", type=str, metavar = "name", help = "tuning profile to be activated") parser.add_argument('--version', "-v", action = "version", version = "%%(prog)s %s.%s.%s" % (ver.TUNED_VERSION_MAJOR, ver.TUNED_VERSION_MINOR, ver.TUNED_VERSION_PATCH)) @@ -53,7 +54,7 @@ if __name__ == "__main__": try: if args.daemon: if args.log is None: - args.log = consts.LOG_FILENAME + args.log = consts.LOG_FILE log.switch_to_file(args.log) else: if args.log is not None: @@ -65,7 +66,12 @@ if __name__ == "__main__": app.attach_to_dbus(consts.DBUS_BUS, consts.DBUS_OBJECT, consts.DBUS_INTERFACE) if args.daemon: - app.daemonize() + if args.pid is None: + args.pid = consts.PID_FILE + app.daemonize(args.pid) + else: + if args.pid is not None: + app.write_pid_file(args.pid) app.run() except tuned.exceptions.TunedException as exception: diff --git a/tuned/consts.py b/tuned/consts.py index 06b90c4..b2fd077 100644 --- a/tuned/consts.py +++ b/tuned/consts.py @@ -9,6 +9,6 @@ LOAD_DIRECTORIES = ["/usr/lib/tuned", "/etc/tuned"] # number of backups LOG_FILE_COUNT = 2 LOG_FILE_MAXBYTES = 100*1000 -LOG_FILENAME = "/var/log/tuned/tuned.log" +LOG_FILE = "/var/log/tuned/tuned.log" PID_FILE = "/run/tuned/tuned.pid" SYSTEM_RELEASE_FILE = "/etc/system-release-cpe" diff --git a/tuned/daemon/application.py b/tuned/daemon/application.py index 86a006e..3632871 100644 --- a/tuned/daemon/application.py +++ b/tuned/daemon/application.py @@ -64,7 +64,7 @@ class Application(object): some uninteresting data into the pipe. """ os.close(child_out_fd) - (read_ready, drop, drop) = select.select([parent_in_fd], [], [], DAEMONIZE_PARENT_TIMEOUT) + (read_ready, drop, drop) = select.select([parent_in_fd], [], [], consts.DAEMONIZE_PARENT_TIMEOUT) if len(read_ready) != 1: os.close(parent_in_fd) @@ -79,8 +79,8 @@ class Application(object): if response != ("%c" % True): raise TunedException("Cannot daemonize, child process reports failure.") - def _write_pid_file(self): - self._pid_file = PID_FILE + def write_pid_file(self, pid_file = consts.PID_FILE): + self._pid_file = pid_file self._delete_pid_file() try: dir_name = os.path.dirname(self._pid_file) @@ -100,7 +100,7 @@ class Application(object): except OSError as error: log.warning("cannot remove existing PID file %s, %s" % (self._pid_file, str(error))) - def _daemonize_child(self, parent_in_fd, child_out_fd): + def _daemonize_child(self, pid_file, parent_in_fd, child_out_fd): """ Finishes daemonizing process, writes a PID file and signalizes to the parent that the initialization is complete. @@ -128,13 +128,13 @@ class Application(object): os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) - self._write_pid_file() + self.write_pid_file(pid_file) log.debug("successfully daemonized") os.write(child_out_fd, "%c" % True) os.close(child_out_fd) - def daemonize(self): + def daemonize(self, pid_file = consts.PID_FILE): """ Daemonizes the application. In case of failure, TunedException is raised in the parent process. If the operation is successfull, the main process @@ -153,7 +153,7 @@ class Application(object): self._daemonize_parent(*parent_child_fds) sys.exit(0) else: - self._daemonize_child(*parent_child_fds) + self._daemonize_child(pid_file, *parent_child_fds) except: # pass exceptions only into parent process if child_pid > 0: diff --git a/tuned/logs.py b/tuned/logs.py index af2ce0f..6cc6956 100644 --- a/tuned/logs.py +++ b/tuned/logs.py @@ -51,7 +51,7 @@ class TunedLogger(logging.getLoggerClass()): self.remove_all_handlers() self.addHandler(self._console_handler) - def switch_to_file(self, filename = consts.LOG_FILENAME): + def switch_to_file(self, filename = consts.LOG_FILE): self._setup_file_handler(filename) self.remove_all_handlers() self.addHandler(self._file_handler)