Changeset 2163

Show
Ignore:
Timestamp:
04/29/2008 08:59:39 PM (2 weeks ago)
Author:
stbuehler
Message:

Implement a clean way to open /dev/null and use it to close stdin/out/err in the needed places (#624)

  • as stderr gets redirected to /dev/null before exec in childs, we cannot
    write to the log afterwards, so disabled that log messages too.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/lighttpd-1.4.x/NEWS

    r2162 r2163  
    2525  * Do not rely on PATH_MAX (POSIX does not require it) (#580) 
    2626  * Disable logging to access.log if filename is an empty string 
     27  * Implement a clean way to open /dev/null and use it to close stdin/out/err in the needed places (#624) 
    2728 
    2829- 1.4.19 - 2008-03-10 
  • branches/lighttpd-1.4.x/src/log.c

    r1957 r2163  
    3232#endif 
    3333 
     34/* Close fd and _try_ to get a /dev/null for it instead. 
     35 * close() alone may trigger some bugs when a 
     36 * process opens another file and gets fd = STDOUT_FILENO or STDERR_FILENO 
     37 * and later tries to just print on stdout/stderr 
     38 * 
     39 * Returns 0 on success and -1 on failure (fd gets closed in all cases) 
     40 */ 
     41int openDevNull(int fd) { 
     42        int tmpfd; 
     43        close(fd); 
     44#if defined(__WIN32) 
     45        /* Cygwin should work with /dev/null */ 
     46        tmpfd = open("nul", O_RDWR); 
     47#else 
     48        tmpfd = open("/dev/null", O_RDWR); 
     49#endif 
     50        if (tmpfd != -1 && tmpfd != fd) { 
     51                dup2(tmpfd, fd); 
     52                close(tmpfd); 
     53        } 
     54        return (tmpfd != -1) ? 0 : -1; 
     55} 
     56 
    3457/** 
    3558 * open the errorlog 
     
    4568 
    4669int log_error_open(server *srv) { 
    47         int fd; 
    4870        int close_stderr = 1; 
    4971 
     
    79101        if (RUNNING_ON_VALGRIND) close_stderr = 0; 
    80102#endif 
    81         if (srv->errorlog_mode == ERRORLOG_STDERR) close_stderr = 0; 
     103 
     104        if (srv->errorlog_mode == ERRORLOG_STDERR && srv->srvconf.dont_daemonize) { 
     105                /* We can only log to stderr in dont-daemonize mode; 
     106                 * if we do daemonize and no errorlog file is specified, we log into /dev/null 
     107                 */ 
     108                close_stderr = 0; 
     109        } 
    82110 
    83111        /* move stderr to /dev/null */ 
    84         if (close_stderr && 
    85             -1 != (fd = open("/dev/null", O_WRONLY))) { 
    86                 close(STDERR_FILENO); 
    87                 dup2(fd, STDERR_FILENO); 
    88                 close(fd); 
    89         } 
     112        if (close_stderr) openDevNull(STDERR_FILENO); 
    90113        return 0; 
    91114} 
  • branches/lighttpd-1.4.x/src/log.h

    r1371 r2163  
    33 
    44#include "server.h" 
     5 
     6/* Close fd and _try_ to get a /dev/null for it instead. 
     7 * Returns 0 on success and -1 on failure (fd gets closed in all cases) 
     8 */ 
     9int openDevNull(int fd); 
    510 
    611#define WP() log_error_write(srv, __FILE__, __LINE__, ""); 
  • branches/lighttpd-1.4.x/src/mod_accesslog.c

    r2162 r2163  
    499499                                close(to_log_fds[1]); 
    500500 
     501                                openDevNull(STDERR_FILENO); 
     502 
    501503                                /* we don't need the client socket */ 
    502504                                for (i = 3; i < 256; i++) { 
  • branches/lighttpd-1.4.x/src/mod_cgi.c

    r2153 r2163  
    988988                } 
    989989 
     990                openDevNull(STDERR_FILENO); 
     991 
    990992                /* we don't need the client socket */ 
    991993                for (i = 3; i < 256; i++) { 
     
    996998                execve(args[0], args, env.ptr); 
    997999 
    998                 log_error_write(srv, __FILE__, __LINE__, "sss", "CGI failed:", strerror(errno), args[0]); 
     1000                /* log_error_write(srv, __FILE__, __LINE__, "sss", "CGI failed:", strerror(errno), args[0]); */ 
    9991001 
    10001002                /* */ 
  • branches/lighttpd-1.4.x/src/mod_fastcgi.c

    r2150 r2163  
    938938                        } 
    939939 
     940                        openDevNull(STDERR_FILENO); 
     941 
    940942                        /* we don't need the client socket */ 
    941943                        for (i = 3; i < 256; i++) { 
     
    10011003                        execve(arg.ptr[0], arg.ptr, env.ptr); 
    10021004 
    1003                         log_error_write(srv, __FILE__, __LINE__, "sbs", 
    1004                                         "execve failed for:", host->bin_path, strerror(errno)); 
     1005                        /* log_error_write(srv, __FILE__, __LINE__, "sbs", 
     1006                                        "execve failed for:", host->bin_path, strerror(errno)); */ 
    10051007 
    10061008                        exit(errno); 
  • branches/lighttpd-1.4.x/src/mod_rrdtool.c

    r1371 r2163  
    149149                args[i++] = NULL; 
    150150 
     151                openDevNull(STDERR_FILENO); 
     152         
    151153                /* we don't need the client socket */ 
    152154                for (i = 3; i < 256; i++) { 
     
    157159                execv(args[0], args); 
    158160 
    159                 log_error_write(srv, __FILE__, __LINE__, "sss", "spawing rrdtool failed: ", strerror(errno), args[0]); 
     161                /* log_error_write(srv, __FILE__, __LINE__, "sss", "spawing rrdtool failed: ", strerror(errno), args[0]); */ 
    160162 
    161163                /* */ 
  • branches/lighttpd-1.4.x/src/server.c

    r2151 r2163  
    574574 
    575575        /* close stdin and stdout, as they are not needed */ 
    576         /* move stdin to /dev/null */ 
    577         if (-1 != (fd = open("/dev/null", O_RDONLY))) { 
    578                 close(STDIN_FILENO); 
    579                 dup2(fd, STDIN_FILENO); 
    580                 close(fd); 
    581         } 
    582  
    583         /* move stdout to /dev/null */ 
    584         if (-1 != (fd = open("/dev/null", O_WRONLY))) { 
    585                 close(STDOUT_FILENO); 
    586                 dup2(fd, STDOUT_FILENO); 
    587                 close(fd); 
    588         } 
     576        openDevNull(STDIN_FILENO); 
     577        openDevNull(STDOUT_FILENO); 
    589578 
    590579        if (0 != config_set_defaults(srv)) {