[Exim] Re: Exim daemon problem; not closing inherited fds

Top Pagina
Delete this message
Reply to this message
Auteur: Miquel van Smoorenburg
Datum:  
Aan: exim-users
Onderwerp: [Exim] Re: Exim daemon problem; not closing inherited fds
In article <Pine.SOL.4.44.0209022101190.22548-100000@???>,
Philip Hazel <ph10@???> wrote:
>If anybody can find a convenient, fast, and portable way to close all
>open descriptors, please tell me about it.


Well, there is a way if the system has poll(). You still have to
give some upper limit on the FDS, say 1024 - open FDS above
that will not be closed.

You probably also want to reopen filedescriptors 0, 1 and 2
with /dev/null to be sure stdin/stdout/stderr are valid. If
exim doesn't do that already - I seem to recall that was talked
about earlier on this list.

#include <malloc.h>
#include <sys/poll.h>

void closeall(int maxfds) {

        struct pollfd   *pfd;
        int             i;


        pfd = calloc(maxfds, sizeof(struct pollfd));
        for (i = 0; i < maxfds; i++) {
                pfd[i].fd = i;
                pfd[i].events = POLLNVAL;
        }
        poll(pfd, maxfds, 0);


        for (i = 0; i < maxfds; i++) {
                if (!(pfd[i].revents & POLLNVAL)) {
                        close(i);
                }
        }


        free(pfd);
}