Bug 31565

Summary: Won't start correctly if parent closed stdin, stdout and stderr
Product: Apache httpd-2 Reporter: Tom Chan <tchan>
Component: AllAssignee: Apache HTTPD Bugs Mailing List <bugs>
Status: RESOLVED LATER    
Severity: enhancement Keywords: MassUpdate
Priority: P3    
Version: 2.0.51   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Tom Chan 2004-10-06 15:52:17 UTC
I have this test program which starts apache.  The result is the child processes
all die on an accept() call with errno 88 (ENOTSOCK).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

int main (int argc, char *argv[]) {
    int i;
    char *arg;

    if (argc < 2) {
        printf("Must specify the path to the executable\n");
        exit(1);
    }
    close(0);
    close(1);
    close(2);

    arg = malloc(strlen(argv[1]) + 1);
    strcpy(arg, argv[1]);
    for (i = 2; i < argc; i++) {
        arg = realloc(arg, strlen(arg) + strlen(argv[i]) + 2);
        strcat(arg, " ");
        strcat(arg, argv[i]);
    }
    syslog(LOG_ERR, "Calling system(%s)", arg);
    system(arg);
    exit(0);
}

If I comment out the close()'s (or open /dev/null in their place), the child
processes will have no problems.

I think the problem is with the order of these events:
First, apache processes the config file with a LISTEN directive, opens the
listen socket(s) and keeps the file descriptor(s) in its table.  With no stdin,
stdout, etc, the first listen socket will be fd 0 and so on.
Then later, apr_proc_detach() in srclib/apr/threadproc/unix/procsup.c does
freopen() on stdin, stdout and stderr which, regardless of whether fd 0, 1 and 2
are sockets or the std files, may wipe out listen sockets created earlier.
Later, when accept() is called on the first listen socket, it has already been
freopen() to a file, causing ENOTSOCK.
Comment 1 Paul Querna 2004-11-04 02:36:09 UTC
Ouch.

Moving this to a enhancement since the solution involves hacking into APR :(
Comment 2 William A. Rowe Jr. 2018-11-07 21:09:25 UTC
Please help us to refine our list of open and current defects; this is a mass update of old and inactive Bugzilla reports which reflect user error, already resolved defects, and still-existing defects in httpd.

As repeatedly announced, the Apache HTTP Server Project has discontinued all development and patch review of the 2.2.x series of releases. The final release 2.2.34 was published in July 2017, and no further evaluation of bug reports or security risks will be considered or published for 2.2.x releases. All reports older than 2.4.x have been updated to status RESOLVED/LATER; no further action is expected unless the report still applies to a current version of httpd.

If your report represented a question or confusion about how to use an httpd feature, an unexpected server behavior, problems building or installing httpd, or working with an external component (a third party module, browser etc.) we ask you to start by bringing your question to the User Support and Discussion mailing list, see [https://httpd.apache.org/lists.html#http-users] for details. Include a link to this Bugzilla report for completeness with your question.

If your report was clearly a defect in httpd or a feature request, we ask that you retest using a modern httpd release (2.4.33 or later) released in the past year. If it can be reproduced, please reopen this bug and change the Version field above to the httpd version you have reconfirmed with.

Your help in identifying defects or enhancements still applicable to the current httpd server software release is greatly appreciated.