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
32 } else if ($pid > 0) { # parent
33 common_log(LOG_INFO, "Successfully forked.");
40 function alreadyRunning() {
42 $pidfilename = $this->pidFilename();
48 if (!file_exists($pidfilename)) {
51 $contents = file_get_contents($pidfilename);
52 if (posix_kill(trim($contents),0)) {
59 function writePidFile() {
60 $pidfilename = $this->pidFilename();
66 file_put_contents($pidfilename, posix_getpid() . "\n");
69 function clearPidFile() {
70 $pidfilename = $this->pidFilename();
74 function pidFilename() {
75 $piddir = common_config('daemon', 'piddir');
79 $name = $this->name();
83 return $piddir . '/' . $name . '.pid';
86 function changeUser() {
88 $username = common_config('daemon', 'user');
91 $user_info = posix_getpwnam($username);
93 common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username);
95 common_log(LOG_INFO, "Setting user to " . $username);
96 posix_setuid($user_info['uid']);
100 $groupname = common_config('daemon', 'group');
103 $group_info = posix_getgrnam($groupname);
105 common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname);
107 common_log(LOG_INFO, "Setting group to " . $groupname);
108 posix_setgid($group_info['gid']);
114 if ($this->alreadyRunning()) {
115 common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
118 if ($this->background()) {
119 $this->writePidFile();
122 $this->clearPidFile();