#! /usr/bin/env python3 #jwmud - a program to create dynamic alarms based on configurable wake up # times # # written by Helmut Pozimski in 2014 # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; version 2 # of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. import logging import logging.handlers import argparse import sys import os from jwmudlib import database from jwmudlib import jwmu_exceptions from jwmudlib import daemon from jwmudlib import configuration parser = argparse.ArgumentParser(prog="jwmud", add_help=True) parser.add_argument("-d", "--daemon", action="store_true", dest="daemon", help="run jwmud as a daemon") parser.add_argument("-u", "--user", dest="user", help="define an unprivileged " "user to run the daemon") parser.add_argument("-g", "--group", dest="group", help="define an " "unprivileged group " "to run the daemon") parser.add_argument("-c", "--config", dest="config", help="define an alternate" " path to the " "configuration file") arguments = parser.parse_args() if arguments.config is not None: config = configuration.conf(arguments.config) else: config = configuration.conf() try: config.Read() except jwmu_exceptions.ConfigurationFileAccessDenied as e: print(e, file=sys.stderr) sys.exit(1) except jwmu_exceptions.ConfigurationFileMissing as e: print(e, file=sys.stderr) sys.exit(1) logger = logging.getLogger("jwmud") if config.values["syslog_level"] == "debug": logger.setLevel(logging.DEBUG) elif config.values["syslog_level"] == "error": logger.setLevel(logging.ERROR) else: logger.setLevel(logging.INFO) formatter = logging.Formatter("%(name)s[" + str(os.getpid()) + "]: %(message)s") if arguments.daemon is True: if config.values["syslog_facility"] == "daemon": syslog_handler = logging.handlers.SysLogHandler( "/dev/log", facility=logging.handlers.SysLogHandler.LOG_DAEMON) else: syslog_handler = logging.handlers.SysLogHandler("/dev/log") syslog_handler.setFormatter(formatter) logger.addHandler(syslog_handler) else: console_handler = logging.StreamHandler() console_handler.setFormatter(formatter) logger.addHandler(console_handler) if arguments.daemon is True: if os.access("/run", os.F_OK & os.W_OK): daemon_obj = daemon.Daemon("/run/jwmud", "jwmud.pid") else: daemon_obj = daemon.Daemon("/var/run/jwmud", "jwmud.pid") daemon_obj.Daemonize() try: daemon_obj.Start() except jwmu_exceptions.DaemonAlreadyRunning as e: logger.error(e) except jwmu_exceptions.WritingPIDFileFailed as e: logger.error(e) else: logger.info("Daemon started.") if arguments.user is not None and arguments.group is not None: try: daemon_obj.DropPrivileges(arguments.user, arguments.group) except jwmu_exceptions.PasswdOrGroupAccessFailed as e: logger.error(e) sys.exit(1) else: logger.debug("Dropped privileges, now running as user %s and " "group %s." % (arguments.user, arguments.group))