]> git.mxchange.org Git - friendica.git/blob - src/Util/PidFile.php
bump version 2023.12
[friendica.git] / src / Util / PidFile.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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 nonexistent
35          */
36         private static function pidFromFile(string $file)
37         {
38                 if (!file_exists($file)) {
39                         return false;
40                 }
41
42                 return trim(@file_get_contents($file));
43         }
44
45         /**
46          * Is there a running process with the given pid file
47          *
48          * @param string $file Filename of pid file
49          *
50          * @return boolean Is it running?
51          */
52         public static function isRunningProcess(string $file): bool
53         {
54                 $pid = self::pidFromFile($file);
55
56                 if (!$pid) {
57                         return false;
58                 }
59
60                 // Is the process running?
61                 $running = posix_kill($pid, 0);
62
63                 // If not, then we will kill the stale file
64                 if (!$running) {
65                         self::delete($file);
66                 }
67                 return $running;
68         }
69
70         /**
71          * Kills a process from a given pid file
72          *
73          * @param string $file Filename of pid file
74          *
75          * @return boolean Was it killed successfully?
76          */
77         public static function killProcess(string $file): bool
78         {
79                 $pid = self::pidFromFile($file);
80
81                 // We don't have a process id? then we quit
82                 if (!$pid) {
83                         return false;
84                 }
85
86                 // We now kill the process
87                 $killed = posix_kill($pid, SIGTERM);
88
89                 // If we killed the process successfully, we can remove the pidfile
90                 if ($killed) {
91                         self::delete($file);
92                 }
93                 return $killed;
94         }
95
96         /**
97          * Creates a pid file
98          *
99          * @param string $file Filename of pid file
100          *
101          * @return boolean|string PID or "false" if not created
102          */
103         public static function create(string $file)
104         {
105                 $pid = self::pidFromFile($file);
106
107                 // We have a process id? then we quit
108                 if ($pid) {
109                         return false;
110                 }
111
112                 $pid = getmypid();
113                 file_put_contents($file, $pid);
114
115                 // Now we check if everything is okay
116                 return self::pidFromFile($file);
117         }
118
119         /**
120          * Deletes a given pid file
121          *
122          * @param string $file Filename of pid file
123          *
124          * @return boolean Is it running?
125          */
126         public static function delete(string $file): bool
127         {
128                 return @unlink($file);
129         }
130 }