dbus bugfix - close inherited filedescriptors

Daniel P. Berrange dan at berrange.com
Thu May 15 16:16:44 PDT 2008


On Fri, May 16, 2008 at 12:15:01AM +0200, Markus Rechberger wrote:
> Hi,
> 
> when the dbus library spawns out dbus-launch it
> inherits all the filedescriptors from the parent.
> This causes problems in several ways for example when
> the inherited file is a devicenode, it won't be closed
> until dbus gets shut down.
> As for multimedia devices when mplayer starts up and
> opens for example the video4linux node dbus will
> inherit that filedescriptor and the device will never
> be closed. For devices which support multiple
> devicemodes this lock the device in the certain mode.
> 
> Attached a patch which fixes that problem.
> 
> Markus
Content-Description: 3456086339-dbus-fix-inherited-fds.diff
> --- dbus-sysdeps-unix.c	2008-02-26 17:02:27.000000000 +0100
> +++ /tmp/dbus-sysdeps-unix.c	2008-05-15 23:29:41.000000000 +0200
> @@ -2844,7 +2844,9 @@
>    if (pid == 0)
>      {
>        /* child process */
> +      int maxfds = getdtablesize();

This is function isn't part of POSIX - the standard API for getting
max file descriptor count is

    sysconf(_SC_OPEN_MAX)


>        int fd = open ("/dev/null", O_RDWR);
> +
>        if (fd == -1)
>          /* huh?! can't open /dev/null? */
>          _exit (1);
> @@ -2865,9 +2867,10 @@
>        if (dup2 (errors_pipe[WRITE_END], 2) == -1)
>          _exit (1);
>  
> -      close (fd);
> -      close (address_pipe[WRITE_END]);
> -      close (errors_pipe[WRITE_END]);
> +      /* close all inherited fds */
> +      for( i = 0; i<maxfds; i++)
> +        if (i>2)
> +          close(i);

IMHO, it should close 0, 1, 2 as well, and re-open them to
point to /dev/null.   While DBus is mostly used from GUI
applications, there are CLI based apps using it too and the
DBus auto-launched session bus should not spam their stderr/out,
nor need access to their stdin.

Regards
Daniel,
-- 
|: http://berrange.com/     -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://freshmeat.net/~danielpb/    -o-   http://gtk-vnc.sourceforge.net :|


More information about the dbus mailing list