daemon.py 3.2 KB

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