Skip to content

Allow FCGI worker processes to have CPU and RAM limits imposed #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions sapi/cgi/cgi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* Process group
*/
Expand Down Expand Up @@ -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[])
Expand Down Expand Up @@ -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"));
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;
Expand Down Expand Up @@ -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)");
Expand Down