Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:12333 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27401 invoked by uid 1010); 24 Aug 2004 19:03:41 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 98507 invoked from network); 24 Aug 2004 18:56:47 -0000 Received: from unknown (HELO ?193.29.205.125?) (193.29.205.125) by pb1.pair.com with SMTP; 24 Aug 2004 18:56:47 -0000 Received: from srv1bgp.conecto1.starybrowar.corp by [193.29.205.125] via smtpd (for pb1.pair.com [216.92.131.4]) with ESMTP; Tue, 24 Aug 2004 20:49:41 +0200 Received: from auh59.neoplus.adsl.tpnet.pl ([83.27.15.59]) by s1.conecto.pl with asmtp (Exim 4.34) id 1BzgTO-0004PT-HS for internals@lists.php.net; Tue, 24 Aug 2004 20:56:46 +0200 Received: from auh59.neoplus.adsl.tpnet.pl ([83.27.15.59]) by auh59.neoplus.adsl.tpnet.pl via smtpd (for srv1bgp.conecto1.starybrowar.corp [172.16.16.60]) with ESMTP; Tue, 24 Aug 2004 20:49:40 +0200 To: internals@lists.php.net Date: Tue, 24 Aug 2004 20:56:45 +0200 User-Agent: KMail/1.6.82 MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_t94KB3c5ZbBvmgN" Message-ID: <200408242056.45151.mg@iceni.pl> Subject: posix_setrlimit (patch attached) From: mg@iceni.pl (Marcin =?iso-8859-2?q?Gibu=B3a?=) --Boundary-00=_t94KB3c5ZbBvmgN Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline 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: array('1', '3')); posix_setrlimit($a); ?> Could you review it and consider to apply if it's ok? -- mg --Boundary-00=_t94KB3c5ZbBvmgN Content-Type: text/x-diff; charset="us-ascii"; name="posix_setrlimit.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="posix_setrlimit.diff" 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) --Boundary-00=_t94KB3c5ZbBvmgN--