comparison vendor/pear/crypt_gpg/Crypt/GPG/ProcessControl.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * A class for monitoring and terminating processes
7 *
8 * PHP version 5
9 *
10 * LICENSE:
11 *
12 * This library is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation; either version 2.1 of the
15 * License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, see
24 * <http://www.gnu.org/licenses/>
25 *
26 * @category Encryption
27 * @package Crypt_GPG
28 * @author Michael Gauthier <mike@silverorange.com>
29 * @copyright 2013 silverorange
30 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
31 * @link http://pear.php.net/package/Crypt_GPG
32 */
33
34 // {{{ class Crypt_GPG_ProcessControl
35
36 /**
37 * A class for monitoring and terminating processes by PID
38 *
39 * This is used to safely terminate the gpg-agent for GnuPG 2.x. This class
40 * is limited in its abilities and can only check if a PID is running and
41 * send a PID SIGTERM.
42 *
43 * @category Encryption
44 * @package Crypt_GPG
45 * @author Michael Gauthier <mike@silverorange.com>
46 * @copyright 2013 silverorange
47 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
48 * @link http://pear.php.net/package/Crypt_GPG
49 */
50 class Crypt_GPG_ProcessControl
51 {
52 // {{{ protected properties
53
54 /**
55 * The PID (process identifier) being monitored
56 *
57 * @var integer
58 */
59 protected $pid;
60
61 // }}}
62 // {{{ __construct()
63
64 /**
65 * Creates a new process controller from the given PID (process identifier)
66 *
67 * @param integer $pid the PID (process identifier).
68 */
69 public function __construct($pid)
70 {
71 $this->pid = $pid;
72 }
73
74 // }}}
75 // {{{ public function getPid()
76
77 /**
78 * Gets the PID (process identifier) being controlled
79 *
80 * @return integer the PID being controlled.
81 */
82 public function getPid()
83 {
84 return $this->pid;
85 }
86
87 // }}}
88 // {{{ isRunning()
89
90 /**
91 * Checks if the process is running
92 *
93 * If the <kbd>posix</kbd> extension is available, <kbd>posix_getpgid()</kbd>
94 * is used. Otherwise <kbd>ps</kbd> is used on UNIX-like systems and
95 * <kbd>tasklist</kbd> on Windows.
96 *
97 * @return boolean true if the process is running, false if not.
98 */
99 public function isRunning()
100 {
101 $running = false;
102
103 if (function_exists('posix_getpgid')) {
104 $running = false !== posix_getpgid($this->pid);
105 } elseif (PHP_OS === 'WINNT') {
106 $command = 'tasklist /fo csv /nh /fi '
107 . escapeshellarg('PID eq ' . $this->pid);
108
109 $result = exec($command);
110 $parts = explode(',', $result);
111 $running = (count($parts) > 1 && trim($parts[1], '"') == $this->pid);
112 } else {
113 $result = exec('ps -p ' . escapeshellarg($this->pid) . ' -o pid=');
114 $running = (trim($result) == $this->pid);
115 }
116
117 return $running;
118 }
119
120 // }}}
121 // {{{ terminate()
122
123 /**
124 * Ends the process gracefully
125 *
126 * The signal SIGTERM is sent to the process. The gpg-agent process will
127 * end gracefully upon receiving the SIGTERM signal. Upon 3 consecutive
128 * SIGTERM signals the gpg-agent will forcefully shut down.
129 *
130 * If the <kbd>posix</kbd> extension is available, <kbd>posix_kill()</kbd>
131 * is used. Otherwise <kbd>kill</kbd> is used on UNIX-like systems and
132 * <kbd>taskkill</kbd> is used in Windows.
133 *
134 * @return void
135 */
136 public function terminate()
137 {
138 if (function_exists('posix_kill')) {
139 posix_kill($this->pid, 15);
140 } elseif (PHP_OS === 'WINNT') {
141 exec('taskkill /PID ' . escapeshellarg($this->pid));
142 } else {
143 exec('kill -15 ' . escapeshellarg($this->pid));
144 }
145 }
146
147 // }}}
148 }
149
150 // }}}
151
152 ?>