3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
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 Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
26 var $daemonize = true;
29 function __construct($daemonize = true)
31 $this->daemonize = $daemonize;
52 if ($pid < 0) { // error
53 common_log(LOG_ERR, "Could not fork.");
55 } else if ($pid > 0) { // parent
56 common_log(LOG_INFO, "Successfully forked.");
63 function alreadyRunning()
65 $pidfilename = $this->pidFilename();
71 if (!file_exists($pidfilename)) {
74 $contents = file_get_contents($pidfilename);
75 if (posix_kill(trim($contents), 0)) {
82 function writePidFile()
84 $pidfilename = $this->pidFilename();
90 return file_put_contents($pidfilename, posix_getpid() . "\n");
93 function clearPidFile()
95 $pidfilename = $this->pidFilename();
99 return unlink($pidfilename);
102 function pidFilename()
104 $piddir = common_config('daemon', 'piddir');
108 $name = $this->name();
112 return $piddir . '/' . $name . '.pid';
115 function changeUser()
117 $username = common_config('daemon', 'user');
120 $user_info = posix_getpwnam($username);
122 common_log(LOG_WARNING,
123 'Ignoring unknown user for daemon: ' . $username);
125 common_log(LOG_INFO, "Setting user to " . $username);
126 posix_setuid($user_info['uid']);
130 $groupname = common_config('daemon', 'group');
133 $group_info = posix_getgrnam($groupname);
135 common_log(LOG_WARNING,
136 'Ignoring unknown group for daemon: ' . $groupname);
138 common_log(LOG_INFO, "Setting group to " . $groupname);
139 posix_setgid($group_info['gid']);
146 if ($this->alreadyRunning()) {
147 common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
151 if ($this->daemonize) {
152 common_log(LOG_INFO, 'Backgrounding daemon "'.$this->name().'"');
156 $this->writePidFile();
159 $this->clearPidFile();