Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow FCGI worker processes to have CPU and RAM limits imposed

  • Loading branch information...
commit 523fd83aee7e8250b88029d0ef0c4ecfd7750147 1 parent da1b534
@mk270 authored
Showing with 37 additions and 0 deletions.
  1. +37 −0 sapi/cgi/cgi_main.c
View
37 sapi/cgi/cgi_main.c
@@ -116,6 +116,10 @@ static int exit_signal = 0;
/* Is Parent waiting for children to exit */
static int parent_waiting = 0;
+/* CPU seconds and memory limits for worker processes */
+static int rlimit_cpu = 0;
+static int rlimit_as = 0;
@dsp
dsp added a note

I think it's better to use rlim_t here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
/**
* Process group
*/
@@ -1469,6 +1473,22 @@ static zend_module_entry cgi_module_entry = {
STANDARD_MODULE_PROPERTIES
};
+void set_worker_rlimits(void)
+{
+ struct rlimit rlim;
+
+ if (rlimit_cpu > 0) {
+ rlim.rlim_cur = rlimit_cpu;
+ rlim.rlim_max = rlimit_cpu;
+ setrlimit(RLIMIT_CPU, &rlim);
+ }
+ if (rlimit_as > 0) {
+ rlim.rlim_cur = rlimit_as;
+ rlim.rlim_max = rlimit_as;
+ setrlimit(RLIMIT_AS, &rlim);
+ }
+}
+
/* {{{ main
*/
int main(int argc, char *argv[])
@@ -1693,6 +1713,22 @@ consult the installation file that came with this distribution, or visit \n\
}
}
+ if (getenv("PHP_FCGI_RLIMIT_CPU")) {
+ rlimit_cpu = atoi(getenv("PHP_FCGI_RLIMIT_CPU"));
@dsp
dsp added a note

rlim_t is not necessarily a int. On some systems this might be an unsigned int, so atoi might loose information here.
I think it would make more sense to use strtoul() and then downcast to system specific rlim_t.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if (rlimit_cpu < 0) {
+ fprintf(stderr, "PHP_FCGI_RLIMIT_CPU is not valid\n");
+ return FAILURE;
+ }
+ }
+
+ if (getenv("PHP_FCGI_RLIMIT_AS")) {
+ rlimit_as = atoi(getenv("PHP_FCGI_RLIMIT_AS"));
+ if (rlimit_as < 0) {
+ fprintf(stderr, "PHP_FCGI_RLIMIT_AS is not valid\n");
+ return FAILURE;
+ }
+ }
+
/* make php call us to get _ENV vars */
php_php_import_environment_variables = php_import_environment_variables;
php_import_environment_variables = cgi_php_import_environment_variables;
@@ -1761,6 +1797,7 @@ consult the installation file that came with this distribution, or visit \n\
sigaction(SIGTERM, &old_term, 0);
sigaction(SIGQUIT, &old_quit, 0);
sigaction(SIGINT, &old_int, 0);
+ set_worker_rlimits();
break;
case -1:
perror("php (pre-forking)");
@dsp

I think it's better to use rlim_t here.

@dsp

rlim_t is not necessarily a int. On some systems this might be an unsigned int, so atoi might loose information here.
I think it would make more sense to use strtoul() and then downcast to system specific rlim_t.

Please sign in to comment.
Something went wrong with that request. Please try again.