jwmud 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #! /usr/bin/env python3
  2. #jwmud - a program to create dynamic alarms based on configurable wake up
  3. # times
  4. #
  5. # written by Helmut Pozimski in 2014
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; version 2
  10. # of the License.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. # Boston, MA 02110-1301, USA.
  21. import logging
  22. import logging.handlers
  23. import argparse
  24. import sys
  25. import os
  26. from jwmudlib import database
  27. from jwmudlib import jwmu_exceptions
  28. from jwmudlib import daemon
  29. from jwmudlib import configuration
  30. parser = argparse.ArgumentParser(prog="jwmud", add_help=True)
  31. parser.add_argument("-d", "--daemon", action="store_true", dest="daemon",
  32. help="run jwmud as a daemon")
  33. parser.add_argument("-u", "--user", dest="user", help="define an unprivileged "
  34. "user to run the daemon")
  35. parser.add_argument("-g", "--group", dest="group", help="define an "
  36. "unprivileged group "
  37. "to run the daemon")
  38. parser.add_argument("-c", "--config", dest="config", help="define an alternate"
  39. " path to the "
  40. "configuration file")
  41. arguments = parser.parse_args()
  42. if arguments.config is not None:
  43. config = configuration.conf(arguments.config)
  44. else:
  45. config = configuration.conf()
  46. try:
  47. config.Read()
  48. except jwmu_exceptions.ConfigurationFileAccessDenied as e:
  49. print(e, file=sys.stderr)
  50. sys.exit(1)
  51. except jwmu_exceptions.ConfigurationFileMissing as e:
  52. print(e, file=sys.stderr)
  53. sys.exit(1)
  54. logger = logging.getLogger("jwmud")
  55. if config.values["syslog_level"] == "debug":
  56. logger.setLevel(logging.DEBUG)
  57. elif config.values["syslog_level"] == "error":
  58. logger.setLevel(logging.ERROR)
  59. else:
  60. logger.setLevel(logging.INFO)
  61. formatter = logging.Formatter("%(name)s[" + str(os.getpid()) + "]: %(message)s")
  62. if arguments.daemon is True:
  63. if config.values["syslog_facility"] == "daemon":
  64. syslog_handler = logging.handlers.SysLogHandler(
  65. "/dev/log", facility=logging.handlers.SysLogHandler.LOG_DAEMON)
  66. else:
  67. syslog_handler = logging.handlers.SysLogHandler("/dev/log")
  68. syslog_handler.setFormatter(formatter)
  69. logger.addHandler(syslog_handler)
  70. else:
  71. console_handler = logging.StreamHandler()
  72. console_handler.setFormatter(formatter)
  73. logger.addHandler(console_handler)
  74. if arguments.daemon is True:
  75. if os.access("/run", os.F_OK & os.W_OK):
  76. daemon_obj = daemon.Daemon("/run/jwmud", "jwmud.pid")
  77. else:
  78. daemon_obj = daemon.Daemon("/var/run/jwmud", "jwmud.pid")
  79. daemon_obj.Daemonize()
  80. try:
  81. daemon_obj.Start()
  82. except jwmu_exceptions.DaemonAlreadyRunning as e:
  83. logger.error(e)
  84. except jwmu_exceptions.WritingPIDFileFailed as e:
  85. logger.error(e)
  86. else:
  87. logger.info("Daemon started.")
  88. if arguments.user is not None and arguments.group is not None:
  89. try:
  90. daemon_obj.DropPrivileges(arguments.user, arguments.group)
  91. except jwmu_exceptions.PasswdOrGroupAccessFailed as e:
  92. logger.error(e)
  93. sys.exit(1)
  94. else:
  95. logger.debug("Dropped privileges, now running as user %s and "
  96. "group %s." % (arguments.user, arguments.group))