You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bugs@apr.apache.org by bu...@apache.org on 2015/03/06 11:13:25 UTC

[Bug 57671] New: apr_proc_detach() should close ALL file descriptors

https://bz.apache.org/bugzilla/show_bug.cgi?id=57671

            Bug ID: 57671
           Summary: apr_proc_detach() should close ALL file descriptors
           Product: APR
           Version: 1.4.5
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: APR
          Assignee: bugs@apr.apache.org
          Reporter: hahn@univention.de

apr_proc_detach() (as called by apache-2.2.22) does only close STDIN, STDOUT,
STDERR, but not all other (open) file descriptors.

This is problematic when apache2 is restarted from an unclean environment,
where other file descriptors are opened and are pipes, as this happens for
example with <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=713967>: As the
forked Apache process inherits the pipe, it is considered a potential writer,
so any reading will block indefinitely instead of getting an EOF.

(Daemons not closing all file descriptors is a common problem, for which
DebConf has an extra FQA entry:
<http://www.fifi.org/doc/debconf-doc/tutorial.html#AEN198>)

As far as I know daemons should close all file descriptors:
- <http://linux.die.net/man/1/daemonize>
- <http://stackoverflow.com/questions/17954432/creating-a-daemon-in-linux>
- <http://en.wikipedia.org/wiki/Daemon_(computing)#Creating>

Closing all open file descriptors is somehow problematic, as there is no
portable function for it. Because of that daemons normally implement a loop to
close all FDs:

    int x;
    for (x = sysconf(_SC_OPEN_MAX); x>0; x--)
        close (x);

For Linux one can optimize that by using apr_dir_open(..., "/proc/self/fd",
...) to only close the open FDs.


IMHO apr_proc_detach() should implement the generic close-all-FDs semantic
instead of only Apache-httpd doing it.

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


[Bug 57671] New function for apr_proc_detach() + close ALL file descriptors

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=57671

Yann Ylavic <yl...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|1.4.5                       |HEAD

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


[Bug 57671] apr_proc_detach() should close ALL file descriptors

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=57671

--- Comment #1 from Philipp Hahn <ha...@univention.de> ---
Created attachment 32548
  --> https://bz.apache.org/bugzilla/attachment.cgi?id=32548&action=edit
Demo-implementation of close_fds()

gcc -Wall -o apache-closefd apache-closefd.c && ./apache-closefd 42>/dev/null

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


[Bug 57671] apr_proc_detach() should close ALL file descriptors

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=57671

--- Comment #2 from Yann Ylavic <yl...@gmail.com> ---
apr_proc_detach() does exactly what it is meant to: make the current process
detach from the terminal (close STDIN/OUT/ERR) and, if daemonize is specified
(not the daemonize tool!), make the current process have its own session (go
background).

Should it close all open descriptors now that could/would break existing
applications, those which currently open file descriptors before
apr_proc_detach() and use them after (note that apr_proc_detach() already takes
care of not closing the standard descriptors, but rather reopen them so that
next opened descriptors won't "hijack" STDIN/OUT/ERR).

Maybe the APR could introduce a new function for this, but IMHO this already as
simple as opening fds on a dedicated pool which could then be cleared before
apr_proc_detach() is called. For fds that are not handled by the APR, it seems
quite tricky to let the APR close them...

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


[Bug 57671] New function for apr_proc_detach() + close ALL file descriptors

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=57671

Jeff Trawick <tr...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |WONTFIX
             Status|NEW                         |RESOLVED

--- Comment #3 from Jeff Trawick <tr...@apache.org> ---
I also don't think apr has a role here to free arbitrary handles (other devs
can of course disagree and re-open).

The best place to address this issue is in the caller of httpd, but maybe httpd
could have a role, controlled by some -Dxxx command-line arg.  (Does httpd and
its arbitrary plug-in modules have no fds it needs to preserve across the
detach?  I don't understand how any code would know, but that's not a concern
for this apr bug ;) )

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org


[Bug 57671] New function for apr_proc_detach() + close ALL file descriptors

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=57671

Yann Ylavic <yl...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ylavic.dev@gmail.com
           Severity|normal                      |enhancement
            Summary|apr_proc_detach() should    |New function for
                   |close ALL file descriptors  |apr_proc_detach() + close
                   |                            |ALL file descriptors

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: bugs-unsubscribe@apr.apache.org
For additional commands, e-mail: bugs-help@apr.apache.org