]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/daemon.php
Twitter-API: Viewing direct msgs in xml and json now work
[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                         common_log(LOG_ERR, "Could not fork.");
32                         return false;
33                 } else if ($pid > 0) { # parent
34                         common_log(LOG_INFO, "Successfully forked.");
35                         exit(0);
36                 } else { # child
37                         return true;
38                 }
39         }
40
41         function alreadyRunning() {
42
43                 $pidfilename = $this->pidFilename();
44
45                 if (!$pidfilename) {
46                         return false;
47                 }
48                 
49                 if (!file_exists($pidfilename)) {
50                         return false;
51                 }
52                 $contents = file_get_contents($pidfilename);
53                 if (posix_kill(trim($contents),0)) {
54                         return true;
55                 } else {
56                         return false;
57                 }
58         }
59         
60         function writePidFile() {
61                 $pidfilename = $this->pidFilename();
62                 
63                 if (!$pidfilename) {
64                         return false;
65                 }
66                 
67             return file_put_contents($pidfilename, posix_getpid() . "\n");
68         }
69
70         function clearPidFile() {
71                 $pidfilename = $this->pidFilename();
72                 if (!$pidfilename) {
73                     return false;
74                 }
75                 return unlink($pidfilename);
76         }
77         
78         function pidFilename() {
79                 $piddir = common_config('daemon', 'piddir');
80                 if (!$piddir) {
81                         return NULL;
82                 }
83                 $name = $this->name();
84                 if (!$name) {
85                         return NULL;
86                 }
87                 return $piddir . '/' . $name . '.pid';
88         }
89
90         function changeUser() {
91
92                 $username = common_config('daemon', 'user');
93                 
94                 if ($username) {
95                         $user_info = posix_getpwnam($username);
96                         if (!$user_info) {
97                                 common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username);
98                         } else {
99                                 common_log(LOG_INFO, "Setting user to " . $username);
100                                 posix_setuid($user_info['uid']);
101                         }
102                 }
103
104                 $groupname = common_config('daemon', 'group');
105                 
106                 if ($groupname) {
107                         $group_info = posix_getgrnam($groupname);
108                         if (!$group_info) {
109                                 common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname);
110                         } else {
111                                 common_log(LOG_INFO, "Setting group to " . $groupname);
112                                 posix_setgid($group_info['gid']);
113                         }
114                 }
115         }
116         
117         function runOnce() {
118                 if ($this->alreadyRunning()) {
119                         common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
120                         exit(0);
121                 }
122                 if ($this->background()) {
123                         $this->writePidFile();
124                         $this->changeUser();
125                         $this->run();
126                         $this->clearPidFile();
127                 }
128         }
129         
130         function run() {
131                 return true;
132         }
133 }