Skip to content

WIP: main/dbus: port close-all-fds patch from openjdk8

Jean-Louis Fuchs requested to merge jeanlf/aports:dbus into master

The patch is not correct, from_fd should be 3. The previous patch: falling back to dumb for-loop, has been merged on upstream.

The patch to openjdk8 was introduced in f10a27ab

To help reviewing this, the resulting function is:

void
_dbus_close_all (void)
{
  int max_fd, i;
  max_fd = sysconf (_SC_OPEN_MAX);

#ifdef __linux__
    int from_fd = 2;
    struct pollfd pfds[1024];
    int total, nclosed = 0;


    if (max_fd > 0) {
        /* init events */
        total = max_fd - from_fd;
        for (i = 0; i < (total < 1024 ? total : 1024); i++) {
            pfds[i].events = 0;
        }

        while (from_fd < max_fd) {
            int nfds, r = 0;

            total = max_fd - from_fd;
            nfds =  total < 1024 ? total : 1024;

            for (i = 0; i < nfds; i++)
                pfds[i].fd = from_fd + i;

            do {
                r = poll(pfds, nfds, 0);
            } while (r == -1 && errno == EINTR);

            if (r < 0)
                return;

            for (i = 0; i < nfds; i++)
                if (pfds[i].revents != POLLNVAL) {
                    nclosed++;
                    close(pfds[i].fd);
                }
            from_fd += nfds;
        }
        return;
    }
#endif

  /* Pick something reasonable if for some reason sysconf says
   * unlimited.
   */
  if (max_fd < 0)
    max_fd = 1024;

  /* close all inherited fds */
  for (i = 3; i < max_fd; i++)
    close (i);
}
Edited by Jean-Louis Fuchs

Merge request reports

Loading