本文整理汇总了PHP中posix_get_last_error函数的典型用法代码示例。如果您正苦于以下问题:PHP posix_get_last_error函数的具体用法?PHP posix_get_last_error怎么用?PHP posix_get_last_error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_get_last_error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: error
/**
* Returns text of last system error
*
* @return string
*/
public function error()
{
if (!function_exists('posix_get_last_error')) {
return 'n/a';
}
return posix_strerror(posix_get_last_error());
}
示例2: __construct
/**
* Initializes exception.
*
* @param string $message Error message.
* @param int $code Error code.
* @param \Exception $previous Previous exception.
* @version 0.0.1
* @since 0.0.1
*/
public function __construct($message = null, $code = null, \Exception $previous = null)
{
// detect POSIX error info
$code = isset($code) ? $code : \posix_get_last_error();
$message = isset($message) ? $message : \posix_strerr($code);
parent::__construct($message, $code, $previous);
}
示例3: handleStop
protected function handleStop()
{
$pidFile = APPLICATION_PATH . '/runtime/jobserver.pid';
if (file_exists($pidFile)) {
$pids = explode("|", file_get_contents($pidFile));
$del = 1;
foreach ($pids as $pid) {
$rs = posix_kill($pid, 15);
if (!$rs) {
$del = 0;
echo "del_fail\r\n";
print_r(posix_get_last_error());
echo "\r\n";
}
}
if ($del) {
echo "del_ok\r\n";
print_r(posix_get_last_error());
echo "\r\n";
do {
unlink($pidFile);
usleep(100000);
} while (file_exists($pidFile));
return 0;
}
}
return 1;
}
示例4: __construct
/**
* Constructor.
*
* @param Master $master The master object
* @param integer $pid The child process id or null if this is the child
*
* @throws \Exception
* @throws \RuntimeException
*/
public function __construct(Master $master, $pid = null)
{
$this->master = $master;
$directions = array('up', 'down');
if (null === $pid) {
// child
$pid = posix_getpid();
$pPid = posix_getppid();
$modes = array('write', 'read');
} else {
// parent
$pPid = null;
$modes = array('read', 'write');
}
$this->pid = $pid;
$this->ppid = $pPid;
foreach (array_combine($directions, $modes) as $direction => $mode) {
$fifo = $this->getPath($direction);
if (!file_exists($fifo) && !posix_mkfifo($fifo, 0600) && 17 !== ($error = posix_get_last_error())) {
throw new \Exception(sprintf('Error while creating FIFO: %s (%d)', posix_strerror($error), $error));
}
$this->{$mode} = fopen($fifo, $mode[0]);
if (false === ($this->{$mode} = fopen($fifo, $mode[0]))) {
throw new \RuntimeException(sprintf('Unable to open %s FIFO.', $mode));
}
}
}
示例5: tryStart
/**
* 尝试开始任务
*
* @return boolean
*/
protected function tryStart()
{
// 检查是否达到预定时间
try {
if (!$this->testTimer()) {
return false;
}
} catch (\Exception $ex) {
$this->log('error', 'Job testTimer() error', ['error' => $ex->getMessage()]);
return false;
}
// 上下文中是否保存了前一个任务pid
if (!($recent_proc_id = $this->getContext(self::KEY_PROC_ID))) {
return true;
}
// 检查进程ID是否真正存在
if (!posix_kill($recent_proc_id, 0)) {
$errno = posix_get_last_error();
if ($errno === 3) {
return true;
}
$this->log('warning', 'Job kill error', ['error' => posix_strerror($errno)]);
return false;
}
// 如果上一个任务还没有超时就放弃当前任务
$recent_proc_time = $this->getContext(self::KEY_PROC_TIME);
if (time() - $recent_proc_time < $this->timeout) {
$this->log('notice', 'Job cancel, previous job still run', ['previous_proc_id' => $recent_proc_id]);
return false;
}
// 中止超时任务
posix_kill($recent_proc_id, SIGKILL);
$this->log('warning', 'Job killed by timeout', ['previous_proc_id' => $recent_proc_id]);
return true;
}
示例6: open
public function open($fifoFile)
{
$this->fifoFile = $fifoFile;
$dir = pathinfo($this->fifoFile, PATHINFO_DIRNAME);
umask(0);
$this->selfCreated = false;
if (!is_dir($dir)) {
if (!mkdir($dir, 0777, true)) {
throw new \Exception("Could not create directory {$dir}");
}
}
// If pipe was not create on another side
if (!file_exists($this->fifoFile)) {
if (!posix_mkfifo($this->fifoFile, 0777)) {
throw new \Exception("Could not create {$this->fifoFile}: " . posix_strerror(posix_get_last_error()));
} else {
$this->selfCreated = true;
}
}
log::debug("Creating stream for {$this->fifoFile}");
$stream = fopen($this->fifoFile, "c+");
log::debug("Stream {$stream} = {$this->fifoFile}");
$this->valid = (bool) $stream;
parent::open($stream);
$this->setBlocking(false);
}
示例7: daemonize
/**
* Daemonize the current process so it can run in the background.
*
* If $pidfile is supplied, the process ID is written there.
* Provide absolute path names for the parameters to avoid file not found errors or logs inside the source code folder.
*
* If an error occurred, a RuntimeException is thrown.
*
* @param string $pidfile File to write the process ID of the daemon to
* @param string $stderr File to redirect STDERR to
* @param string $stdout File to redirect STDOUT to
* @param string $stdin File to read STDIN from
* @throws \RuntimeException
* @return true
*/
public static function daemonize($pidfile = null, $stderr = '/dev/null', $stdout = '/dev/null', $stdin = '/dev/null')
{
// Allow only cli scripts to daemonize, otherwise you may confuse your webserver
if (\php_sapi_name() !== 'cli') {
throw new \RuntimeException('Can only daemonize a CLI process!');
}
self::checkPID($pidfile);
self::reopenFDs($stdin, $stdout, $stderr);
if (($pid1 = @\pcntl_fork()) < 0) {
throw new \RuntimeException('Failed to fork, reason: "' . \pcntl_strerror(\pcntl_get_last_error()) . '"');
} elseif ($pid1 > 0) {
exit;
}
if (@posix_setsid() === -1) {
throw new \RuntimeException('Failed to become session leader, reason: "' . \posix_strerror(\posix_get_last_error()) . '"');
}
if (($pid2 = @\pcntl_fork()) < 0) {
throw new \RuntimeException('Failed to fork, reason: "' . \pcntl_strerror(\pcntl_get_last_error()) . '"');
} elseif ($pid2 > 0) {
exit;
}
chdir('/');
umask(022);
self::writePID($pidfile);
return true;
}
示例8: setUid
public static function setUid($uid, $gid)
{
if (!posix_setgid($gid)) {
// 必须先设置GID, 再设置UID
throw new Exception("Unable to set GID: " . posix_strerror(posix_get_last_error()));
}
if (!posix_setuid($uid)) {
throw new Exception("Unable to set UID: " . posix_strerror(posix_get_last_error()));
}
}
示例9: kill_concentrator
function kill_concentrator()
{
$result = posix_kill($this->concentrator_pid, SIGTERM);
if ($result === FALSE) {
$errno = posix_get_last_error();
throw new Exception("Error killing off mysql concentrator: {$errno}: " . posix_strerror($errno) . "\n");
}
$status = null;
$result = pcntl_waitpid($this->concentrator_pid, $status);
if ($result == -1) {
$errno = posix_get_last_error();
throw new Exception("Error waiting for concentrator ({$this->concentrator_pid}): {$errno}: " . posix_strerror($errno) . "\n");
}
}
示例10: wait
/**
* Wait for all child processes to complete
*/
public function wait()
{
// Wait for all children to return
foreach ($this->child_pid_list as $child_pid) {
if (pcntl_waitpid($child_pid, $status) < 0) {
error_log(posix_strerror(posix_get_last_error()));
}
// Check to see if the child died a graceful death
$status = 0;
if (pcntl_wifsignaled($status)) {
$return_code = pcntl_wexitstatus($status);
$term_sig = pcntl_wtermsig($status);
error_log("Child terminated with return code {$return_code} and signal {$term_sig}");
}
}
}
示例11: __getpid
function __getpid()
{
$pid_file = '/tmp/swoole.pid';
if (defined('SWOOLE_PID')) {
$pid_file = SWOOLE_PID;
}
$pid = file_exists($pid_file) ? file_get_contents($pid_file) : 0;
// 检查进程是否真正存在
if ($pid && !posix_kill($pid, 0)) {
$errno = posix_get_last_error();
if ($errno === 3) {
$pid = 0;
}
}
return $pid;
}
示例12: __construct
/**
* @param int $pool_size
* The number of worker processes to create
*
* @param array $task_data_iterator
* An array of task data items to be divided up among the
* workers
*
* @param \Closure $startup_closure
* A closure to execute upon starting a child
*
* @param \Closure $task_closure
* A method to execute on each task data
*
* @param \Closure $shutdown_closure
* A closure to execute upon shutting down a child
*/
public function __construct(int $pool_size, array $task_data_iterator, \Closure $startup_closure, \Closure $task_closure, \Closure $shutdown_closure)
{
assert($pool_size > 1, 'The pool size must be >= 2 to use the fork pool.');
assert(extension_loaded('pcntl'), 'The pcntl extension must be loaded in order for Phan to be able to fork.');
// We'll keep track of if this is the parent process
// so that we can tell who will be doing the waiting
$is_parent = false;
// Fork as many times as requested to get the given
// pool size
for ($proc_id = 0; $proc_id < $pool_size; $proc_id++) {
// Fork
$pid = 0;
if (($pid = pcntl_fork()) < 0) {
error_log(posix_strerror(posix_get_last_error()));
exit(EXIT_FAILURE);
}
// Parent
if ($pid > 0) {
$is_parent = true;
$this->child_pid_list[] = $pid;
continue;
}
// Child
if ($pid === 0) {
$is_parent = false;
break;
}
}
// If we're the parent, return
if ($is_parent) {
return;
}
// Execute anything the children wanted to execute upon
// starting up
$startup_closure();
// Otherwise, take on a slice of the task list
foreach ($task_data_iterator as $i => $task_data) {
if ($i % $pool_size === $proc_id) {
$task_closure($i, $task_data);
}
}
// Execute each child's shutdown closure before
// exiting the process
$shutdown_closure();
// Children exit after completing their work
exit(EXIT_SUCCESS);
}
示例13: isProcessRunning
public static function isProcessRunning($pid)
{
if (!$pid) {
return false;
}
// This may fail if we can't signal the process because we are running as
// a different user (for example, we are 'apache' and the process is some
// other user's, or we are a normal user and the process is root's), but
// we can check the error code to figure out if the process exists.
$is_running = posix_kill($pid, 0);
if (posix_get_last_error() == 1) {
// "Operation Not Permitted", indicates that the PID exists. If it
// doesn't, we'll get an error 3 ("No such process") instead.
$is_running = true;
}
return $is_running;
}
示例14: stop
public function stop()
{
if (!$this->getPid()) {
throw new \Exception("Process not running");
}
foreach (range(1, 10) as $i) {
switch ($i) {
case 1:
$this->logger->debug('Sending SIGTERM');
posix_kill($this->getPid(), SIGTERM);
if (posix_get_last_error() == SOCKET_EPERM) {
throw new \Exception("You do not have permission to stop this process");
}
if ($this->status() != self::RUNNING_OK) {
$this->logger->debug('First attempt results in full stop!');
break 2;
}
break;
case 10:
$this->logger->debug('Sending SIGKILL!!');
posix_kill($this->getPid(), SIGKILL);
if ($this->status() != self::RUNNING_OK) {
$this->logger->debug('Results in full stop!');
break 2;
}
break;
default:
$this->logger->debug('Sending another SIGTERM');
posix_kill($this->getPid(), SIGTERM);
if ($this->status() != self::RUNNING_OK) {
$this->logger->debug('Results in full stop!');
break 2;
}
break;
}
sleep(3);
}
if ($this->status() == self::RUNNING_OK) {
throw new \Exception("There was an error attempting to end the process");
}
$this->clearPid();
}
示例15: action_exit
public function action_exit()
{
if (file_exists($this->_config['pid_path'])) {
$pid = file_get_contents($this->_config['pid_path']);
if ($pid !== 0) {
Kohana::$log->add('debug', 'Sending SIGTERM to pid ' . $pid);
echo 'Sending SIGTERM to pid ' . $pid . PHP_EOL;
posix_kill($pid, SIGTERM);
if (posix_get_last_error() === 0) {
echo "Signal send SIGTERM to pid " . $pid . PHP_EOL;
} else {
echo "An error occured while sending SIGTERM" . PHP_EOL;
unlink($this->_config['pid_path']);
}
} else {
Kohana::$log->add("debug", "Could not find MangoQueue pid in file :" . $this->_config['pid_path']);
echo "Could not find task_queue pid in file :" . $this->_config['pid_path'] . PHP_EOL;
}
} else {
Kohana::$log->add("error", "MangoQueue pid file " . $this->_config['pid_path'] . " does not exist");
echo "MangoQueue pid file " . $this->_config['pid_path'] . " does not exist" . PHP_EOL;
}
}