]> git.mxchange.org Git - friendica.git/blob - src/Util/PidFile.php
Rename class for PSR-0
[friendica.git] / src / Util / PidFile.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 /**
25  * Pidfile class
26  */
27 class PidFile
28 {
29         /**
30          * Read the pid from a given pid file
31          *
32          * @param string $file Filename of pid file
33          *
34          * @return boolean|string PID or "false" if not existent
35          */
36         static private function pidFromFile($file) {
37                 if (!file_exists($file)) {
38                         return false;
39                 }
40
41                 return trim(@file_get_contents($file));
42         }
43
44         /**
45          * Is there a running process with the given pid file
46          *
47          * @param string $file Filename of pid file
48          *
49          * @return boolean Is it running?
50          */
51         static public function isRunningProcess($file) {
52                 $pid = self::pidFromFile($file);
53
54                 if (!$pid) {
55                         return false;
56                 }
57
58                 // Is the process running?
59                 $running = posix_kill($pid, 0);
60
61                 // If not, then we will kill the stale file
62                 if (!$running) {
63                         self::delete($file);
64                 }
65                 return $running;
66         }
67
68         /**
69          * Kills a process from a given pid file
70          *
71          * @param string $file Filename of pid file
72          *
73          * @return boolean Was it killed successfully?
74          */
75         static public function killProcess($file) {
76                 $pid = self::pidFromFile($file);
77
78                 // We don't have a process id? then we quit
79                 if (!$pid) {
80                         return false;
81                 }
82
83                 // We now kill the process
84                 $killed = posix_kill($pid, SIGTERM);
85
86                 // If we killed the process successfully, we can remove the pidfile
87                 if ($killed) {
88                         self::delete($file);
89                 }
90                 return $killed;
91         }
92
93         /**
94          * Creates a pid file
95          *
96          * @param string $file Filename of pid file
97          *
98          * @return boolean|string PID or "false" if not created
99          */
100         static public function create($file) {
101                 $pid = self::pidFromFile($file);
102
103                 // We have a process id? then we quit
104                 if ($pid) {
105                         return false;
106                 }
107
108                 $pid = getmypid();
109                 file_put_contents($file, $pid);
110
111                 // Now we check if everything is okay
112                 return self::pidFromFile($file);
113         }
114
115         /**
116          * Deletes a given pid file
117          *
118          * @param string $file Filename of pid file
119          *
120          * @return boolean Is it running?
121          */
122         static public function delete($file) {
123                 return @unlink($file);
124         }
125 }