php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #52173
Patch pcntl_get_last_error-bug52173 revision 2010-06-24 17:06 UTC by nick dot telford at gmail dot com

Patch pcntl_get_last_error-bug52173 for PCNTL related Bug #52173

Patch version 2010-06-24 17:06 UTC

Return to Bug #52173 | Download this patch
Patch Revisions:

Developer: nick.telford@gmail.com

Index: ext/pcntl/pcntl.c
===================================================================
--- ext/pcntl/pcntl.c	(revision 300731)
+++ ext/pcntl/pcntl.c	(working copy)
@@ -44,6 +44,8 @@
 #include <sys/resource.h>
 #endif
 
+#include <errno.h>
+
 ZEND_DECLARE_MODULE_GLOBALS(pcntl)
 static PHP_GINIT_FUNCTION(pcntl);
 
@@ -134,6 +136,10 @@
 	ZEND_ARG_INFO(0, process_identifier)
 ZEND_END_ARG_INFO()
 #endif
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_pcntl_strerror, 0, 0, 1)
+        ZEND_ARG_INFO(0, errno)
+ZEND_END_ARG_INFO()
 /* }}} */
 
 const zend_function_entry pcntl_functions[] = {
@@ -150,6 +156,9 @@
 	PHP_FE(pcntl_wstopsig,		arginfo_pcntl_wstopsig)
 	PHP_FE(pcntl_exec,			arginfo_pcntl_exec)
 	PHP_FE(pcntl_alarm,			arginfo_pcntl_alarm)
+	PHP_FE(pcntl_get_last_error,	arginfo_pcntl_void)
+	PHP_FALIAS(pcntl_errno, pcntl_get_last_error,	NULL)
+	PHP_FE(pcntl_strerror,		arginfo_pcntl_strerror)
 #ifdef HAVE_GETPRIORITY
 	PHP_FE(pcntl_getpriority,	arginfo_pcntl_getpriority)
 #endif
@@ -410,6 +419,7 @@
 static PHP_GINIT_FUNCTION(pcntl)
 { 
 	memset(pcntl_globals, 0, sizeof(*pcntl_globals));
+	pcntl_globals->last_error = 0;
 }
 
 PHP_RINIT_FUNCTION(pcntl)
@@ -467,6 +477,7 @@
 
 	id = fork();
 	if (id == -1) {
+		PCNTL_G(last_error) = errno;
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d", errno);
 	}
 	
@@ -505,6 +516,10 @@
 
 	child_id = waitpid((pid_t) pid, &status, options);
 
+	if (child_id < 0) {
+		PCNTL_G(last_error) = errno;
+	}
+
 	Z_LVAL_P(z_status) = status;
 
 	RETURN_LONG((long) child_id);
@@ -536,6 +551,10 @@
 #else
 	child_id = wait(&status);
 #endif
+	if (child_id < 0) {
+		PCNTL_G(last_error) = errno;
+	}
+
 	Z_LVAL_P(z_status) = status;
 
 	RETURN_LONG((long) child_id);
@@ -1073,6 +1092,28 @@
 /* }}} */
 #endif
 
+/* {{{ proto int pcntl_get_last_error(void)
+   Retrieve the error number set by the last pcntl function which failed. */
+PHP_FUNCTION(pcntl_get_last_error)
+{
+        RETURN_LONG(PCNTL_G(last_error));
+}
+/* }}} */
+
+/* {{{ proto string pcntl_strerror(int errno)
+   Retrieve the system error message associated with the given errno. */
+PHP_FUNCTION(pcntl_strerror)
+{
+        long error;
+
+        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &error) == FAILURE) {
+                RETURN_FALSE;
+        }
+
+        RETURN_STRING(strerror(error), 1);
+}
+/* }}} */
+
 /* Our custom signal handler that calls the appropriate php_function */
 static void pcntl_signal_handler(int signo)
 {
Index: ext/pcntl/php_pcntl.h
===================================================================
--- ext/pcntl/php_pcntl.h	(revision 300731)
+++ ext/pcntl/php_pcntl.h	(working copy)
@@ -42,6 +42,8 @@
 PHP_FUNCTION(pcntl_wstopsig);
 PHP_FUNCTION(pcntl_signal);
 PHP_FUNCTION(pcntl_signal_dispatch);
+PHP_FUNCTION(pcntl_get_last_error);
+PHP_FUNCTION(pcntl_strerror);
 #ifdef HAVE_SIGPROCMASK
 PHP_FUNCTION(pcntl_sigprocmask);
 #endif
@@ -66,6 +68,7 @@
 	HashTable php_signal_table;
 	int processing_signal_queue;
 	struct php_pcntl_pending_signal *head, *tail, *spares;
+	int last_error;
 ZEND_END_MODULE_GLOBALS(pcntl)
 
 #ifdef ZTS
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC