| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\PcntlException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Executes the program with the given arguments. |
| 9 |
* |
| 10 |
* @param string $path path must be the path to a binary executable or a |
| 11 |
* script with a valid path pointing to an executable in the shebang ( |
| 12 |
* #!/usr/local/bin/perl for example) as the first line. See your system's |
| 13 |
* man execve(2) page for additional information. |
| 14 |
* @param array $args args is an array of argument strings passed to the |
| 15 |
* program. |
| 16 |
* @param array $envs envs is an array of strings which are passed as |
| 17 |
* environment to the program. The array is in the format of name => value, |
| 18 |
* the key being the name of the environmental variable and the value being |
| 19 |
* the value of that variable. |
| 20 |
* @throws PcntlException |
| 21 |
* |
| 22 |
*/ |
| 23 |
function pcntl_exec(string $path, array $args = null, array $envs = null): void |
| 24 |
{ |
| 25 |
error_clear_last(); |
| 26 |
if ($envs !== null) { |
| 27 |
$result = \pcntl_exec($path, $args, $envs); |
| 28 |
} elseif ($args !== null) { |
| 29 |
$result = \pcntl_exec($path, $args); |
| 30 |
} else { |
| 31 |
$result = \pcntl_exec($path); |
| 32 |
} |
| 33 |
if ($result === false) { |
| 34 |
throw PcntlException::createFromPhpError(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* pcntl_getpriority gets the priority of |
| 41 |
* pid. Because priority levels can differ between |
| 42 |
* system types and kernel versions, please see your system's getpriority(2) |
| 43 |
* man page for specific details. |
| 44 |
* |
| 45 |
* @param int $pid If not specified, the pid of the current process is used. |
| 46 |
* @param int $process_identifier One of PRIO_PGRP, PRIO_USER |
| 47 |
* or PRIO_PROCESS. |
| 48 |
* @return int pcntl_getpriority returns the priority of the process. A lower numerical value causes more favorable |
| 49 |
* scheduling. |
| 50 |
* @throws PcntlException |
| 51 |
* |
| 52 |
*/ |
| 53 |
function pcntl_getpriority(int $pid = null, int $process_identifier = PRIO_PROCESS): int |
| 54 |
{ |
| 55 |
error_clear_last(); |
| 56 |
if ($process_identifier !== PRIO_PROCESS) { |
| 57 |
$result = \pcntl_getpriority($pid, $process_identifier); |
| 58 |
} elseif ($pid !== null) { |
| 59 |
$result = \pcntl_getpriority($pid); |
| 60 |
} else { |
| 61 |
$result = \pcntl_getpriority(); |
| 62 |
} |
| 63 |
if ($result === false) { |
| 64 |
throw PcntlException::createFromPhpError(); |
| 65 |
} |
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* pcntl_setpriority sets the priority of |
| 72 |
* pid. |
| 73 |
* |
| 74 |
* @param int $priority priority is generally a value in the range |
| 75 |
* -20 to 20. The default priority |
| 76 |
* is 0 while a lower numerical value causes more |
| 77 |
* favorable scheduling. Because priority levels can differ between |
| 78 |
* system types and kernel versions, please see your system's setpriority(2) |
| 79 |
* man page for specific details. |
| 80 |
* @param int $pid If not specified, the pid of the current process is used. |
| 81 |
* @param int $process_identifier One of PRIO_PGRP, PRIO_USER |
| 82 |
* or PRIO_PROCESS. |
| 83 |
* @throws PcntlException |
| 84 |
* |
| 85 |
*/ |
| 86 |
function pcntl_setpriority(int $priority, int $pid = null, int $process_identifier = PRIO_PROCESS): void |
| 87 |
{ |
| 88 |
error_clear_last(); |
| 89 |
if ($process_identifier !== PRIO_PROCESS) { |
| 90 |
$result = \pcntl_setpriority($priority, $pid, $process_identifier); |
| 91 |
} elseif ($pid !== null) { |
| 92 |
$result = \pcntl_setpriority($priority, $pid); |
| 93 |
} else { |
| 94 |
$result = \pcntl_setpriority($priority); |
| 95 |
} |
| 96 |
if ($result === false) { |
| 97 |
throw PcntlException::createFromPhpError(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* The pcntl_signal_dispatch function calls the signal |
| 104 |
* handlers installed by pcntl_signal for each pending |
| 105 |
* signal. |
| 106 |
* |
| 107 |
* @throws PcntlException |
| 108 |
* |
| 109 |
*/ |
| 110 |
function pcntl_signal_dispatch(): void |
| 111 |
{ |
| 112 |
error_clear_last(); |
| 113 |
$result = \pcntl_signal_dispatch(); |
| 114 |
if ($result === false) { |
| 115 |
throw PcntlException::createFromPhpError(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* The pcntl_sigprocmask function adds, removes or sets blocked |
| 122 |
* signals, depending on the how parameter. |
| 123 |
* |
| 124 |
* @param int $how Sets the behavior of pcntl_sigprocmask. Possible |
| 125 |
* values: |
| 126 |
* |
| 127 |
* SIG_BLOCK: Add the signals to the |
| 128 |
* currently blocked signals. |
| 129 |
* SIG_UNBLOCK: Remove the signals from the |
| 130 |
* currently blocked signals. |
| 131 |
* SIG_SETMASK: Replace the currently |
| 132 |
* blocked signals by the given list of signals. |
| 133 |
* |
| 134 |
* @param array $set List of signals. |
| 135 |
* @param array|null $oldset The oldset parameter is set to an array |
| 136 |
* containing the list of the previously blocked signals. |
| 137 |
* @throws PcntlException |
| 138 |
* |
| 139 |
*/ |
| 140 |
function pcntl_sigprocmask(int $how, array $set, ?array &$oldset = null): void |
| 141 |
{ |
| 142 |
error_clear_last(); |
| 143 |
$result = \pcntl_sigprocmask($how, $set, $oldset); |
| 144 |
if ($result === false) { |
| 145 |
throw PcntlException::createFromPhpError(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* |
| 152 |
* |
| 153 |
* @param int $errno |
| 154 |
* @return string Returns error description on success. |
| 155 |
* @throws PcntlException |
| 156 |
* |
| 157 |
*/ |
| 158 |
function pcntl_strerror(int $errno): string |
| 159 |
{ |
| 160 |
error_clear_last(); |
| 161 |
$result = \pcntl_strerror($errno); |
| 162 |
if ($result === false) { |
| 163 |
throw PcntlException::createFromPhpError(); |
| 164 |
} |
| 165 |
return $result; |
| 166 |
} |
| 167 |
|