daemon.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # This file is part of jwmud, written by Helmut Pozimski in 2014.
  2. #
  3. # jwmud is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, version 2 of the License.
  6. #
  7. # jwmud is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with jwmud. If not, see <http://www.gnu.org/licenses/>.
  14. # -*- coding: utf8 -*-
  15. import os
  16. import sys
  17. class Daemon(object):
  18. """ Tries to implement a well behaving unix daemon in a generic way,
  19. so the code could be used in different projects.
  20. """
  21. def __init__(self, pfile_path, pfile_name):
  22. """ Initializes the object. """
  23. self.__pfile_path = pfile_path
  24. self.__pfile_name = pfile_name
  25. self.__daemon = False
  26. self.__pid = 0
  27. self.__pidfile = ""
  28. def Daemonize(self):
  29. """ Turns the calling process into a daemon running on it's own """
  30. try:
  31. # Fork for the first time
  32. self.__pid = os.fork()
  33. except OSError:
  34. sys.exit(os.EX_OSERR)
  35. else:
  36. if self.__pid > 0:
  37. sys.exit(os.EX_OK)
  38. # Become session and group leader
  39. # noinspection PyArgumentList
  40. os.setsid()
  41. try:
  42. #Fork for the second time
  43. self.__pid = os.fork()
  44. except OSError:
  45. sys.exit(os.EX_OSERR)
  46. else:
  47. if self.__pid > 0:
  48. sys.exit(os.EX_OK)
  49. # Change cwd to / to avoid interfering with other mounted file systems
  50. os.chdir("/")
  51. # Reset the umask
  52. os.umask(0)
  53. # Close possibly open file descriptors
  54. os.close(0)
  55. os.close(1)
  56. os.close(2)
  57. # And redirect them to /dev/null
  58. os.open("/dev/null", 0)
  59. os.open("/dev/null", 1)
  60. os.open("/dev/null", 2)
  61. self.__daemon = True
  62. def DropPriv(self, uid, gid):
  63. """ If the daemon is running as root user, drop privileges and continue
  64. running as the defined unprivileged user """
  65. if os.getuid() == 0:
  66. os.setgid(gid)
  67. os.setuid(uid)
  68. def SetName(self, name, cmdline):
  69. """ Sets the name of the process shown visible in ps and top,
  70. this allows to make your daemon look more like a standalone
  71. program instead of a python script.
  72. """
  73. try:
  74. # First check if prctl is available, otherwise this
  75. # function does nothing
  76. # noinspection PyUnresolvedReferences
  77. import prctl
  78. except ImportError:
  79. return False
  80. else:
  81. prctl.set_name(name)
  82. prctl.set_proctitle(cmdline)
  83. return True
  84. def Start(self):
  85. """ Performs the operations needed to "start" the daemon """
  86. if self.__daemon is True:
  87. if os.access(self.__pfile_path, os.F_OK & os.W_OK):
  88. self.__pidfile = open(os.path.join(self.__pfile_path,
  89. self.__pfile_name), "w")
  90. self.__pidfile.write(str(os.getpid()) + "\n")
  91. self.__pidfile.close()
  92. return True
  93. else:
  94. return False
  95. else:
  96. return False
  97. def Stop(self):
  98. """ Performs the operations needed to stop the daemon """
  99. if self.__daemon is True:
  100. try:
  101. os.remove(os.path.join(self.__pfile_path, self.__pfile_name))
  102. except OSError:
  103. return False
  104. else:
  105. return True
  106. else:
  107. return True