]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/daemon.php
Update copyright dates in files modified in 2009
[quix0rs-gnu-social.git] / lib / daemon.php
1 <?php
2 /**
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) {
21     exit(1);
22 }
23
24 class Daemon
25 {
26     function name()
27     {
28         return null;
29     }
30
31     function background()
32     {
33         $pid = pcntl_fork();
34         if ($pid < 0) { // error
35             common_log(LOG_ERR, "Could not fork.");
36             return false;
37         } else if ($pid > 0) { // parent
38             common_log(LOG_INFO, "Successfully forked.");
39             exit(0);
40         } else { // child
41             return true;
42         }
43     }
44
45     function alreadyRunning()
46     {
47         $pidfilename = $this->pidFilename();
48
49         if (!$pidfilename) {
50             return false;
51         }
52
53         if (!file_exists($pidfilename)) {
54             return false;
55         }
56         $contents = file_get_contents($pidfilename);
57         if (posix_kill(trim($contents), 0)) {
58             return true;
59         } else {
60             return false;
61         }
62     }
63
64     function writePidFile()
65     {
66         $pidfilename = $this->pidFilename();
67
68         if (!$pidfilename) {
69             return false;
70         }
71
72         return file_put_contents($pidfilename, posix_getpid() . "\n");
73     }
74
75     function clearPidFile()
76     {
77         $pidfilename = $this->pidFilename();
78         if (!$pidfilename) {
79             return false;
80         }
81         return unlink($pidfilename);
82     }
83
84     function pidFilename()
85     {
86         $piddir = common_config('daemon', 'piddir');
87         if (!$piddir) {
88             return null;
89         }
90         $name = $this->name();
91         if (!$name) {
92             return null;
93         }
94         return $piddir . '/' . $name . '.pid';
95     }
96
97     function changeUser()
98     {
99         $username = common_config('daemon', 'user');
100
101         if ($username) {
102             $user_info = posix_getpwnam($username);
103             if (!$user_info) {
104                 common_log(LOG_WARNING,
105                            'Ignoring unknown user for daemon: ' . $username);
106             } else {
107                 common_log(LOG_INFO, "Setting user to " . $username);
108                 posix_setuid($user_info['uid']);
109             }
110         }
111
112         $groupname = common_config('daemon', 'group');
113
114         if ($groupname) {
115             $group_info = posix_getgrnam($groupname);
116             if (!$group_info) {
117                 common_log(LOG_WARNING,
118                            'Ignoring unknown group for daemon: ' . $groupname);
119             } else {
120                 common_log(LOG_INFO, "Setting group to " . $groupname);
121                 posix_setgid($group_info['gid']);
122             }
123         }
124     }
125
126     function runOnce()
127     {
128         if ($this->alreadyRunning()) {
129             common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
130             exit(0);
131         }
132         if ($this->background()) {
133             $this->writePidFile();
134             $this->changeUser();
135             $this->run();
136             $this->clearPidFile();
137         }
138     }
139
140     function run()
141     {
142         return true;
143     }
144 }