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