Only disconnect from terminal in fork mode (keep stderr/stdout open in nofork mode)

git-svn-id: svn://svn.lighttpd.net/spawn-fcgi/trunk@12 4a9f3682-ca7b-49a8-9a55-ba4640e46f83
This commit is contained in:
Stefan Bühler 2009-02-18 16:14:13 +00:00
parent 3acd1bf80d
commit 5cf8f0b73e
2 changed files with 15 additions and 11 deletions

1
NEWS
View File

@ -9,3 +9,4 @@ NEWS
* Remove limits für php children; per default the PHP_FCGI_CHILDREN var is not changed (php defaults to no children, one worker)
* Modified the log messages format (more details on errors, no source line)
* Only try to connect to unix socket (not tcp) before spawning (fixes again #1575)
* Only disconnect from terminal in fork mode (keep stderr/stdout open in nofork mode)

View File

@ -162,9 +162,6 @@ static int fcgi_spawn_connection(char *appPath, char **appArgv, char *addr, unsi
int i = 0;
/* loose control terminal */
setsid();
if (child_count >= 0) {
snprintf(cgi_childs, sizeof(cgi_childs), "PHP_FCGI_CHILDREN=%d", child_count);
putenv(cgi_childs);
@ -176,15 +173,19 @@ static int fcgi_spawn_connection(char *appPath, char **appArgv, char *addr, unsi
close(fcgi_fd);
}
max_fd = open("/dev/null", O_RDWR);
close(STDERR_FILENO);
dup2(max_fd, STDERR_FILENO);
close(max_fd);
/* loose control terminal */
if (!nofork) {
setsid();
max_fd = open("/dev/null", O_RDWR);
close(STDOUT_FILENO);
dup2(max_fd, STDOUT_FILENO);
close(max_fd);
max_fd = open("/dev/null", O_RDWR);
if (-1 != max_fd) {
if (max_fd != STDOUT_FILENO) dup2(max_fd, STDOUT_FILENO);
if (max_fd != STDERR_FILENO) dup2(max_fd, STDERR_FILENO);
if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO) close(max_fd);
} else {
fprintf(stderr, "spawn-fcgi: couldn't open and redirect stdout/stderr to '/dev/null': %s\n", strerror(errno));
}
}
/* we don't need the client socket */
for (i = 3; i < max_fd; i++) {
@ -204,6 +205,8 @@ static int fcgi_spawn_connection(char *appPath, char **appArgv, char *addr, unsi
execl("/bin/sh", "sh", "-c", b, (char *)NULL);
}
/* in nofork mode stderr is still open */
fprintf(stderr, "spawn-fcgi: exec failed: %s\n", strerror(errno));
exit(errno);
break;