daemon.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. class Daemon
  18. {
  19. public $daemonize = true;
  20. public $_id = 'generic';
  21. public function __construct($daemonize = true)
  22. {
  23. $this->daemonize = $daemonize;
  24. }
  25. public function name()
  26. {
  27. return null;
  28. }
  29. public function get_id()
  30. {
  31. return $this->_id;
  32. }
  33. public function set_id($id)
  34. {
  35. $this->_id = $id;
  36. }
  37. public function background()
  38. {
  39. // Database connection will likely get lost after forking
  40. $this->resetDb();
  41. // Double-forking.
  42. foreach (['single', 'double'] as $v) {
  43. switch ($pid = pcntl_fork()) {
  44. case -1: // error
  45. common_log(LOG_ERR, 'Could not fork.');
  46. return false;
  47. case 0: // child
  48. if ($v === 'single') {
  49. posix_setsid();
  50. }
  51. break;
  52. default: // parent
  53. if ($v === 'double') {
  54. common_log(LOG_INFO, 'Successfully forked.');
  55. }
  56. die();
  57. }
  58. }
  59. return true;
  60. }
  61. public function alreadyRunning()
  62. {
  63. $pidfilename = $this->pidFilename();
  64. if (!$pidfilename) {
  65. return false;
  66. }
  67. if (!file_exists($pidfilename)) {
  68. return false;
  69. }
  70. $contents = file_get_contents($pidfilename);
  71. if (posix_kill(trim($contents), 0)) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. public function writePidFile()
  78. {
  79. $pidfilename = $this->pidFilename();
  80. if (!$pidfilename) {
  81. return false;
  82. }
  83. return file_put_contents($pidfilename, posix_getpid() . "\n");
  84. }
  85. public function clearPidFile()
  86. {
  87. $pidfilename = $this->pidFilename();
  88. if (!$pidfilename) {
  89. return false;
  90. }
  91. return unlink($pidfilename);
  92. }
  93. public function pidFilename()
  94. {
  95. $piddir = common_config('daemon', 'piddir');
  96. if (!$piddir) {
  97. return null;
  98. }
  99. $name = $this->name();
  100. if (!$name) {
  101. return null;
  102. }
  103. return $piddir . '/' . $name . '.pid';
  104. }
  105. public function changeUser()
  106. {
  107. $groupname = common_config('daemon', 'group');
  108. if ($groupname) {
  109. $group_info = posix_getgrnam($groupname);
  110. if (!$group_info) {
  111. common_log(
  112. LOG_WARNING,
  113. 'Ignoring unknown group for daemon: ' . $groupname
  114. );
  115. } else {
  116. common_log(LOG_INFO, "Setting group to " . $groupname);
  117. posix_setgid($group_info['gid']);
  118. }
  119. }
  120. $username = common_config('daemon', 'user');
  121. if ($username) {
  122. $user_info = posix_getpwnam($username);
  123. if (!$user_info) {
  124. common_log(
  125. LOG_WARNING,
  126. 'Ignoring unknown user for daemon: ' . $username
  127. );
  128. } else {
  129. common_log(LOG_INFO, "Setting user to " . $username);
  130. posix_setuid($user_info['uid']);
  131. }
  132. }
  133. }
  134. public function runOnce()
  135. {
  136. if ($this->alreadyRunning()) {
  137. common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
  138. exit(0);
  139. }
  140. if ($this->daemonize) {
  141. common_log(LOG_INFO, 'Backgrounding daemon "'.$this->name().'"');
  142. $this->background();
  143. }
  144. $this->writePidFile();
  145. $this->changeUser();
  146. $this->run();
  147. $this->clearPidFile();
  148. }
  149. public function run()
  150. {
  151. return true;
  152. }
  153. }