3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) {
34 if ($pid < 0) { // error
35 common_log(LOG_ERR, "Could not fork.");
37 } else if ($pid > 0) { // parent
38 common_log(LOG_INFO, "Successfully forked.");
45 function alreadyRunning()
47 $pidfilename = $this->pidFilename();
53 if (!file_exists($pidfilename)) {
56 $contents = file_get_contents($pidfilename);
57 if (posix_kill(trim($contents), 0)) {
64 function writePidFile()
66 $pidfilename = $this->pidFilename();
72 return file_put_contents($pidfilename, posix_getpid() . "\n");
75 function clearPidFile()
77 $pidfilename = $this->pidFilename();
81 return unlink($pidfilename);
84 function pidFilename()
86 $piddir = common_config('daemon', 'piddir');
90 $name = $this->name();
94 return $piddir . '/' . $name . '.pid';
99 $username = common_config('daemon', 'user');
102 $user_info = posix_getpwnam($username);
104 common_log(LOG_WARNING,
105 'Ignoring unknown user for daemon: ' . $username);
107 common_log(LOG_INFO, "Setting user to " . $username);
108 posix_setuid($user_info['uid']);
112 $groupname = common_config('daemon', 'group');
115 $group_info = posix_getgrnam($groupname);
117 common_log(LOG_WARNING,
118 'Ignoring unknown group for daemon: ' . $groupname);
120 common_log(LOG_INFO, "Setting group to " . $groupname);
121 posix_setgid($group_info['gid']);
128 if ($this->alreadyRunning()) {
129 common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
132 if ($this->background()) {
133 $this->writePidFile();
136 $this->clearPidFile();