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')) { exit(1); }
28 function background() {
30 if ($pid < 0) { # error
31 common_log(LOG_ERR, "Could not fork.");
33 } else if ($pid > 0) { # parent
34 common_log(LOG_INFO, "Successfully forked.");
41 function alreadyRunning() {
43 $pidfilename = $this->pidFilename();
49 if (!file_exists($pidfilename)) {
52 $contents = file_get_contents($pidfilename);
53 if (posix_kill(trim($contents),0)) {
60 function writePidFile() {
61 $pidfilename = $this->pidFilename();
67 return file_put_contents($pidfilename, posix_getpid() . "\n");
70 function clearPidFile() {
71 $pidfilename = $this->pidFilename();
75 return unlink($pidfilename);
78 function pidFilename() {
79 $piddir = common_config('daemon', 'piddir');
83 $name = $this->name();
87 return $piddir . '/' . $name . '.pid';
90 function changeUser() {
92 $username = common_config('daemon', 'user');
95 $user_info = posix_getpwnam($username);
97 common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username);
99 common_log(LOG_INFO, "Setting user to " . $username);
100 posix_setuid($user_info['uid']);
104 $groupname = common_config('daemon', 'group');
107 $group_info = posix_getgrnam($groupname);
109 common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname);
111 common_log(LOG_INFO, "Setting group to " . $groupname);
112 posix_setgid($group_info['gid']);
118 if ($this->alreadyRunning()) {
119 common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
122 if ($this->background()) {
123 $this->writePidFile();
126 $this->clearPidFile();