summaryrefslogtreecommitdiff
path: root/program/lib/Crypt/GPG/ProcessControl.php
blob: d6dae03252eead050a9e5173e614398cd6d195b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * A class for monitoring and terminating processes
 *
 * PHP version 5
 *
 * LICENSE:
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * @category  Encryption
 * @package   Crypt_GPG
 * @author    Michael Gauthier <mike@silverorange.com>
 * @copyright 2013 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @version   CVS: $Id$
 * @link      http://pear.php.net/package/Crypt_GPG
 */

// {{{ class Crypt_GPG_ProcessControl

/**
 * A class for monitoring and terminating processes by PID
 *
 * This is used to safely terminate the gpg-agent for GnuPG 2.x. This class
 * is limited in its abilities and can only check if a PID is running and
 * send a PID SIGTERM.
 *
 * @category  Encryption
 * @package   Crypt_GPG
 * @author    Michael Gauthier <mike@silverorange.com>
 * @copyright 2013 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @link      http://pear.php.net/package/Crypt_GPG
 */
class Crypt_GPG_ProcessControl
{
    // {{{ protected properties

    /**
     * The PID (process identifier) being monitored
     *
     * @var integer
     */
    protected $pid;

    // }}}
    // {{{ __construct()

    /**
     * Creates a new process controller from the given PID (process identifier)
     *
     * @param integer $pid the PID (process identifier).
     */
    public function __construct($pid)
    {
        $this->pid = $pid;
    }

    // }}}
    // {{{ public function getPid()

    /**
     * Gets the PID (process identifier) being controlled
     *
     * @return integer the PID being controlled.
     */
    public function getPid()
    {
        return $this->pid;
    }

    // }}}
    // {{{ isRunning()

    /**
     * Checks if the process is running
     *
     * Uses <kbd>ps</kbd> on UNIX-like systems and <kbd>tasklist</kbd> on
     * Windows.
     *
     * @return boolean true if the process is running, false if not.
     */
    public function isRunning()
    {
        $running = false;

        if (PHP_OS === 'WINNT') {
            $command = 'tasklist /fo csv /nh /fi '
                . escapeshellarg('PID eq ' . $this->pid);

            $result  = exec($command);
            $parts   = explode(',', $result);
            $running = (count($parts) > 1 && trim($parts[1], '"') == $this->pid);
        } else {
            $result  = exec('ps -p ' . escapeshellarg($this->pid) . ' -o pid=');
            $running = (trim($result) == $this->pid);
        }

        return $running;
    }

    // }}}
    // {{{ terminate()

    /**
     * Ends the process gracefully
     *
     * The signal SIGTERM is sent to the process. The gpg-agent process will
     * end gracefully upon receiving the SIGTERM signal. Upon 3 consecutive
     * SIGTERM signals the gpg-agent will forcefully shut down.
     *
     * If the <kbd>posix</kbd> extension is available, <kbd>posix_kill()</kbd>
     * is used. Otherwise <kbd>kill</kbd> is used on UNIX-like systems and
     * <kbd>taskkill</kbd> is used in Windows.
     *
     * @return void
     */
    public function terminate()
    {
        if (function_exists('posix_kill')) {
            posix_kill($this->pid, 15);
        } elseif (PHP_OS === 'WINNT') {
            exec('taskkill /PID ' . escapeshellarg($this->pid));
        } else {
            exec('kill -15 ' . escapeshellarg($this->pid));
        }
    }

    // }}}
}

// }}}

?>