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