1
|
|
|
<?php namespace Comodojo\Daemon\Utils; |
2
|
|
|
|
3
|
|
|
use \Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package Comodojo Daemon |
7
|
|
|
* @author Marco Giovinazzi <[email protected]> |
8
|
|
|
* @license MIT |
9
|
|
|
* |
10
|
|
|
* LICENSE: |
11
|
|
|
* |
12
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
13
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
14
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
15
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
16
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
17
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18
|
|
|
* THE SOFTWARE. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class ProcessTools { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Terminate a process, asking PID to terminate or killing it directly. |
25
|
|
|
* |
26
|
|
|
* @param int $pid |
27
|
|
|
* @param int $lagger_timeout Timeout to wait before killing process if it refuses to terminate |
28
|
|
|
* @param int $signal Signal to send (default to SIGTERM) |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public static function term($pid, $lagger_timeout = 0, $signal = SIGTERM) { |
33
|
|
|
|
34
|
|
|
$kill_time = time() + $lagger_timeout; |
35
|
|
|
|
36
|
|
|
// $term = posix_kill($pid, $signal); |
|
|
|
|
37
|
|
|
$term = self::signal($pid, $signal); |
38
|
|
|
|
39
|
|
|
while ( time() < $kill_time ) { |
40
|
|
|
|
41
|
|
|
if ( !self::isRunning($pid) ) return $term; |
42
|
|
|
usleep(20000); |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return self::kill($pid); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Kill a process |
52
|
|
|
* |
53
|
|
|
* @param int $pid |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public static function kill($pid) { |
57
|
|
|
|
58
|
|
|
// return posix_kill($pid, SIGKILL); |
|
|
|
|
59
|
|
|
return self::signal($pid, SIGKILL); |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function signal($pid, $signal = SIGUSR1) { |
64
|
|
|
|
65
|
|
|
return posix_kill($pid, $signal); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return true if process is still running, false otherwise |
71
|
|
|
* |
72
|
|
|
* @param int $pid |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public static function isRunning($pid) { |
76
|
|
|
|
77
|
|
|
return (pcntl_waitpid($pid, $status, WNOHANG) === 0); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get niceness of a running process |
83
|
|
|
* |
84
|
|
|
* @param int|null $pid The pid to query, or current process if null |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
|
|
public static function getNiceness($pid = null) { |
88
|
|
|
|
89
|
|
|
return @pcntl_getpriority($pid); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set niceness of a running process |
95
|
|
|
* |
96
|
|
|
* @param int|null $pid The pid to query, or current process if null |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
1 |
|
public static function setNiceness($niceness, $pid = null) { |
100
|
|
|
|
101
|
1 |
|
return is_null($pid) ? @proc_nice($niceness) : @pcntl_setpriority($pid, $niceness); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get current process PID |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
1 |
|
public static function getPid() { |
111
|
|
|
|
112
|
1 |
|
return posix_getpid(); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.