Hi,
this patch adds posix_setrlimit function (in posix extension), which can be 
useful in standalone apps to lower your resources limits (for example cpu 
limit). 

Example usage is:
<?php
$a = array('cpu' => array('1', '3'));
posix_setrlimit($a);
?>

Could you review it and consider to apply if it's ok?

-- 
mg
diff -u -r1.14 php_posix.h
--- php_posix.h	8 Jan 2004 17:32:41 -0000	1.14
+++ php_posix.h	19 Aug 2004 15:37:30 -0000
@@ -98,6 +98,7 @@
 
 #ifdef HAVE_GETRLIMIT
 PHP_FUNCTION(posix_getrlimit);
+PHP_FUNCTION(posix_setrlimit);
 #endif
 
 PHP_FUNCTION(posix_get_last_error);
Index: posix.c
===================================================================
RCS file: /repository/php-src/ext/posix/posix.c,v
retrieving revision 1.60
diff -u -r1.60 posix.c
--- posix.c	18 Apr 2004 21:49:10 -0000	1.60
+++ posix.c	19 Aug 2004 15:37:31 -0000
@@ -50,7 +50,7 @@
 /* {{{ posix_functions[]
  */
 function_entry posix_functions[] = {
-    /* POSIX.1, 3.3 */
+	/* POSIX.1, 3.3 */
 	PHP_FE(posix_kill,		NULL)
 
 	/* POSIX.1, 4.1 */
@@ -100,7 +100,7 @@
 	PHP_FE(posix_ttyname,	NULL)
 	PHP_FE(posix_isatty,	NULL)
 
-    /* POSIX.1, 5.2 */
+	/* POSIX.1, 5.2 */
 	PHP_FE(posix_getcwd,	NULL)
 
 	/* POSIX.1, 5.4 */
@@ -116,6 +116,7 @@
 
 #ifdef HAVE_GETRLIMIT
 	PHP_FE(posix_getrlimit,	NULL)
+	PHP_FE(posix_setrlimit,	NULL)
 #endif
 
 	PHP_FE(posix_get_last_error,					NULL)
@@ -163,7 +164,7 @@
 	NULL,
 	NULL, 
 	PHP_MINFO(posix),
-    NO_VERSION_YET,
+	NO_VERSION_YET,
 	STANDARD_MODULE_PROPERTIES
 };
 /* }}} */
@@ -200,8 +201,8 @@
 	if (kill(pid, sig) < 0) {
 		POSIX_G(last_error) = errno;
 		RETURN_FALSE;
-  	}
-  	
+	}
+	
 	RETURN_TRUE;
 }
 /* }}} */
@@ -877,6 +878,80 @@
 }
 /* }}} */
 
+/* {{{ posix_setlimit
+ */
+static int posix_setlimit(int limit, zval **value TSRMLS_DC)
+{
+	zval **soft, **hard;
+	struct rlimit rl;
+	int result;
+	
+	result = getrlimit(limit, &rl);
+	if (result < 0) {
+		POSIX_G(last_error) = errno;
+		return FAILURE;
+	}
+	
+	if (zend_hash_index_find(Z_ARRVAL_PP(value), 0, (void **) &soft) == FAILURE ||
+		zend_hash_index_find(Z_ARRVAL_PP(value), 1, (void **) &hard) == FAILURE) 
+	{
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "unexpected error");
+		return FAILURE;
+	}
+	
+	if (Z_TYPE_PP(soft) == IS_STRING && !strcmp(Z_STRVAL_PP(value), UNLIMITED_STRING)) {
+		rl.rlim_cur = RLIM_INFINITY;	
+	} else if (Z_TYPE_PP(soft) != IS_NULL){
+		convert_to_long_ex(soft);
+		rl.rlim_cur = Z_LVAL_PP(soft);
+	}
+	
+	if (Z_TYPE_PP(hard) == IS_STRING && !strcmp(Z_STRVAL_PP(hard), UNLIMITED_STRING)) {
+		rl.rlim_max = RLIM_INFINITY;	
+	} else if (Z_TYPE_PP(hard) != IS_NULL){
+		convert_to_long_ex(hard);
+		rl.rlim_max = Z_LVAL_PP(hard);
+	}
+	
+	result = setrlimit(limit, &rl);
+	if (result < 0) {
+		POSIX_G(last_error) = errno;
+		return FAILURE;
+	}
+	
+	return SUCCESS;
+}
+/* }}} */
+
+/* {{{ proto bool posix_setrlimit(array)
+   Set system resource consumption limits (This is not a POSIX function, but a BSDism and a SVR4ism. We compile conditionally) */
+PHP_FUNCTION(posix_setrlimit)
+{
+	zval *array, **value;
+	struct limitlist *l = NULL;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) {
+		return;
+	}
+	
+	for (l = limits; l->name; l++) {
+		if (zend_hash_find(Z_ARRVAL_P(array), l->name, strlen(l->name) + 1, (void **) &value) != FAILURE) {
+			if (Z_TYPE_PP(value) != IS_ARRAY) {
+				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array");
+				continue;
+			} else if (zend_hash_num_elements(Z_ARRVAL_PP(value)) != 2) {
+				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array with values should have exacly 2 fields");
+				continue;					
+			}
+
+			if (posix_setlimit(l->limit, value TSRMLS_CC) != SUCCESS)
+				RETURN_FALSE;
+		}
+	}
+	
+	RETURN_TRUE;
+}
+
 #endif /* HAVE_GETRLIMIT */
 
 /* {{{ proto int posix_get_last_error(void)

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to