You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProcessControl.php 4.2KB

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