> I browser through it and it seems you put it in the HAVE_GETRLIMIT > define, you might want to add a new config/m4 check for it and use > HAVE_SETRLIMIT around it. Conceptually I see no reason why we should not > add it.
Here is updated patch. -- mg
Index: config.m4 =================================================================== RCS file: /repository/php-src/ext/posix/config.m4,v retrieving revision 1.7 diff -u -r1.7 config.m4 --- config.m4 12 Mar 2002 16:32:13 -0000 1.7 +++ config.m4 24 Aug 2004 20:45:15 -0000 @@ -9,5 +9,5 @@ AC_DEFINE(HAVE_POSIX, 1, [whether to include POSIX-like functions]) PHP_NEW_EXTENSION(posix, posix.c, $ext_shared) - AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo getrlimit) + AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo getrlimit setrlimit) fi Index: php_posix.h =================================================================== RCS file: /repository/php-src/ext/posix/php_posix.h,v retrieving revision 1.14 diff -u -r1.14 php_posix.h --- php_posix.h 8 Jan 2004 17:32:41 -0000 1.14 +++ php_posix.h 24 Aug 2004 20:45:15 -0000 @@ -1,4 +1,4 @@ -/* +/* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ @@ -100,6 +100,10 @@ PHP_FUNCTION(posix_getrlimit); #endif +#ifdef HAVE_SETRLIMIT +PHP_FUNCTION(posix_setrlimit); +#endif + PHP_FUNCTION(posix_get_last_error); PHP_FUNCTION(posix_strerror); 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 24 Aug 2004 20:45:16 -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) @@ -156,14 +157,14 @@ */ zend_module_entry posix_module_entry = { STANDARD_MODULE_HEADER, - "posix", - posix_functions, + "posix", + posix_functions, PHP_MINIT(posix), NULL, NULL, - NULL, + NULL, PHP_MINFO(posix), - NO_VERSION_YET, + NO_VERSION_YET, STANDARD_MODULE_PROPERTIES }; /* }}} */ @@ -196,12 +197,12 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &pid, &sig) == FAILURE) return; - + if (kill(pid, sig) < 0) { POSIX_G(last_error) = errno; RETURN_FALSE; - } - + } + RETURN_TRUE; } /* }}} */ @@ -299,7 +300,7 @@ int i; PHP_POSIX_NO_ARGS; - + if ((result = getgroups(NGROUPS_MAX, gidlist)) < 0) { POSIX_G(last_error) = errno; RETURN_FALSE; @@ -313,19 +314,19 @@ } /* }}} */ -/* {{{ proto string posix_getlogin(void) +/* {{{ proto string posix_getlogin(void) Get user name (POSIX.1, 4.2.4) */ PHP_FUNCTION(posix_getlogin) { char *p; - + PHP_POSIX_NO_ARGS; - + if (NULL == (p = getlogin())) { POSIX_G(last_error) = errno; RETURN_FALSE; } - + RETURN_STRING(p, 1); } /* }}} */ @@ -356,12 +357,12 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &pid, &pgid) == FAILURE) return; - + if (setpgid(pid, pgid) < 0) { POSIX_G(last_error) = errno; RETURN_FALSE; } - + RETURN_TRUE; } /* }}} */ @@ -450,7 +451,7 @@ PHP_FUNCTION(posix_ctermid) { char buffer[L_ctermid]; - + PHP_POSIX_NO_ARGS; if (NULL == ctermid(buffer)) { @@ -460,7 +461,7 @@ POSIX_G(last_error) = errno; RETURN_FALSE; } - + RETURN_STRING(buffer, 1); } #endif @@ -480,7 +481,7 @@ if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) { php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)fd, 0); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not use stream of type '%s'", + php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not use stream of type '%s'", stream->ops->label); return 0; } @@ -513,7 +514,7 @@ POSIX_G(last_error) = errno; RETURN_FALSE; } - + RETURN_STRING(p, 1); } /* }}} */ @@ -524,7 +525,7 @@ { zval *z_fd; int fd; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z_fd) == FAILURE) return; @@ -590,7 +591,7 @@ int path_len; long mode; int result; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &path, &path_len, &mode) == FAILURE) return; @@ -623,7 +624,7 @@ MAKE_STD_ZVAL(array_members); array_init(array_members); - + add_assoc_string(array_group, "name", g->gr_name, 1); add_assoc_string(array_group, "passwd", g->gr_passwd, 1); for (count=0; g->gr_mem[count] != NULL; count++) { @@ -645,7 +646,7 @@ POSIX.1, 6.x most I/O functions already supported by PHP. POSIX.1, 7.x tty functions, TODO POSIX.1, 8.x interactions with other C language functions - POSIX.1, 9.x system database access + POSIX.1, 9.x system database access */ /* {{{ proto array posix_getgrnam(string groupname) @@ -655,7 +656,7 @@ char *name; struct group *g; int name_len; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) return; @@ -663,7 +664,7 @@ POSIX_G(last_error) = errno; RETURN_FALSE; } - + array_init(return_value); if (!php_posix_group_to_array(g, return_value)) { @@ -673,13 +674,13 @@ } /* }}} */ -/* {{{ proto array posix_getgrgid(long gid) +/* {{{ proto array posix_getgrgid(long gid) Group database access (POSIX.1, 9.2.1) */ PHP_FUNCTION(posix_getgrgid) { long gid; struct group *g; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &gid) == FAILURE) return; @@ -713,14 +714,14 @@ return 1; } -/* {{{ proto array posix_getpwnam(string groupname) +/* {{{ proto array posix_getpwnam(string groupname) User database access (POSIX.1, 9.2.2) */ PHP_FUNCTION(posix_getpwnam) { struct passwd *pw; char *name; int name_len; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) return; @@ -728,7 +729,7 @@ POSIX_G(last_error) = errno; RETURN_FALSE; } - + array_init(return_value); if (!php_posix_passwd_to_array(pw, return_value)) { @@ -739,13 +740,13 @@ } /* }}} */ -/* {{{ proto array posix_getpwuid(long uid) +/* {{{ proto array posix_getpwuid(long uid) User database access (POSIX.1, 9.2.2) */ PHP_FUNCTION(posix_getpwuid) { long uid; struct passwd *pw; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &uid) == FAILURE) return; @@ -763,46 +764,12 @@ } /* }}} */ - -#ifdef HAVE_GETRLIMIT - -#define UNLIMITED_STRING "unlimited" - -/* {{{ posix_addlimit +#if HAVE_GETRLIMIT || HAVE_SETRLIMIT +/* {{{ limits[] */ -static int posix_addlimit(int limit, char *name, zval *return_value TSRMLS_DC) { - int result; - struct rlimit rl; - char hard[80]; - char soft[80]; - snprintf(hard, 80, "hard %s", name); - snprintf(soft, 80, "soft %s", name); - - result = getrlimit(limit, &rl); - if (result < 0) { - POSIX_G(last_error) = errno; - return FAILURE; - } - - if (rl.rlim_cur == RLIM_INFINITY) { - add_assoc_stringl(return_value, soft, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1, 1); - } else { - add_assoc_long(return_value, soft, rl.rlim_cur); - } - - if (rl.rlim_max == RLIM_INFINITY) { - add_assoc_stringl(return_value, hard, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1, 1); - } else { - add_assoc_long(return_value, hard, rl.rlim_max); - } - - return SUCCESS; -} -/* }}} */ +#define UNLIMITED_STRING "unlimited" -/* {{{ limits[] - */ struct limitlist { int limit; char *name; @@ -858,7 +825,42 @@ { 0, NULL } }; /* }}} */ +#endif + +#ifdef HAVE_GETRLIMIT + +/* {{{ posix_addlimit + */ +static int posix_addlimit(int limit, char *name, zval *return_value TSRMLS_DC) { + int result; + struct rlimit rl; + char hard[80]; + char soft[80]; + + snprintf(hard, 80, "hard %s", name); + snprintf(soft, 80, "soft %s", name); + + result = getrlimit(limit, &rl); + if (result < 0) { + POSIX_G(last_error) = errno; + return FAILURE; + } + if (rl.rlim_cur == RLIM_INFINITY) { + add_assoc_stringl(return_value, soft, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1, 1); + } else { + add_assoc_long(return_value, soft, rl.rlim_cur); + } + + if (rl.rlim_max == RLIM_INFINITY) { + add_assoc_stringl(return_value, hard, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1, 1); + } else { + add_assoc_long(return_value, hard, rl.rlim_max); + } + + return SUCCESS; +} +/* }}} */ /* {{{ proto array posix_getrlimit(void) Get system resource consumption limits (This is not a POSIX function, but a BSDism and a SVR4ism. We compile conditionally) */ @@ -879,11 +881,87 @@ #endif /* HAVE_GETRLIMIT */ +#ifdef HAVE_SETRLIMIT +/* {{{ 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_SETRLIMIT */ + /* {{{ proto int posix_get_last_error(void) Retrieve the error number set by the last posix function which failed. */ PHP_FUNCTION(posix_get_last_error) { - PHP_POSIX_NO_ARGS; + PHP_POSIX_NO_ARGS; RETURN_LONG(POSIX_G(last_error)); }
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php