]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/daemon.php
Introduced common_location_shared() to check if location sharing is always,
[quix0rs-gnu-social.git] / lib / daemon.php
1 <?php
2 /**
3  * StatusNet - the 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('STATUSNET') && !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         /*
52          * This prefers to Starting PHP 5.4 (dotdeb), maybe earlier for some version/distrib
53          * seems MySQL connection using mysqli driver get lost when fork.
54          * Need to unset it so that child process recreate it.
55          *
56          * @todo FIXME cleaner way to do it ?
57          */
58         global $_DB_DATAOBJECT;
59         unset($_DB_DATAOBJECT['CONNECTIONS']);
60
61         $pid = pcntl_fork();
62         if ($pid < 0) { // error
63             common_log(LOG_ERR, "Could not fork.");
64             return false;
65         } else if ($pid > 0) { // parent
66             common_log(LOG_INFO, "Successfully forked.");
67             exit(0);
68         } else { // child
69             return true;
70         }
71     }
72
73     function alreadyRunning()
74     {
75         $pidfilename = $this->pidFilename();
76
77         if (!$pidfilename) {
78             return false;
79         }
80
81         if (!file_exists($pidfilename)) {
82             return false;
83         }
84         $contents = file_get_contents($pidfilename);
85         if (posix_kill(trim($contents), 0)) {
86             return true;
87         } else {
88             return false;
89         }
90     }
91
92     function writePidFile()
93     {
94         $pidfilename = $this->pidFilename();
95
96         if (!$pidfilename) {
97             return false;
98         }
99
100         return file_put_contents($pidfilename, posix_getpid() . "\n");
101     }
102
103     function clearPidFile()
104     {
105         $pidfilename = $this->pidFilename();
106         if (!$pidfilename) {
107             return false;
108         }
109         return unlink($pidfilename);
110     }
111
112     function pidFilename()
113     {
114         $piddir = common_config('daemon', 'piddir');
115         if (!$piddir) {
116             return null;
117         }
118         $name = $this->name();
119         if (!$name) {
120             return null;
121         }
122         return $piddir . '/' . $name . '.pid';
123     }
124
125     function changeUser()
126     {
127         $groupname = common_config('daemon', 'group');
128
129         if ($groupname) {
130             $group_info = posix_getgrnam($groupname);
131             if (!$group_info) {
132                 common_log(LOG_WARNING,
133                            'Ignoring unknown group for daemon: ' . $groupname);
134             } else {
135                 common_log(LOG_INFO, "Setting group to " . $groupname);
136                 posix_setgid($group_info['gid']);
137             }
138         }
139
140         $username = common_config('daemon', 'user');
141
142         if ($username) {
143             $user_info = posix_getpwnam($username);
144             if (!$user_info) {
145                 common_log(LOG_WARNING,
146                            'Ignoring unknown user for daemon: ' . $username);
147             } else {
148                 common_log(LOG_INFO, "Setting user to " . $username);
149                 posix_setuid($user_info['uid']);
150             }
151         }
152     }
153
154     function runOnce()
155     {
156         if ($this->alreadyRunning()) {
157             common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
158             exit(0);
159         }
160
161         if ($this->daemonize) {
162             common_log(LOG_INFO, 'Backgrounding daemon "'.$this->name().'"');
163             $this->background();
164         }
165
166         $this->writePidFile();
167         $this->changeUser();
168         $this->run();
169         $this->clearPidFile();
170     }
171
172     function run()
173     {
174         return true;
175     }
176 }