Skip to content

Commit

Permalink
Use the smarter filedescriptor closing in bpipe.
Browse files Browse the repository at this point in the history
Instead of closing the filedescriptors ourself (and a maximum
of 29 (32 - 3)) use fcntl(F_CLOSEM) or closefrom() library call
to perform what we really want e.g. close all unneeded filedescriptors
of the forked process.
  • Loading branch information
Marco van Wieringen committed May 5, 2013
1 parent 9655bb3 commit 2b04522
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/lib/bpipe.c
Expand Up @@ -127,9 +127,23 @@ BPIPE *open_bpipe(char *prog, int wait, const char *mode)
}
/* Note, the close log cause problems, see bug #1536 */
/* closelog(); close syslog if open */
for (i=3; i<=32; i++) { /* close any open file descriptors */

#if defined(HAVE_FCNTL_F_CLOSEM)
/*
* fcntl(fd, F_CLOSEM) needs the lowest filedescriptor to close.
*/
fcntl(3, F_CLOSEM);
#elif defined(HAVE_CLOSEFROM)
/*
* closefrom needs the lowest filedescriptor to close.
*/
closefrom(3);
#else
for (i = 3; i <= 32; i++) { /* close any open file descriptors */
close(i);
}
#endif

execvp(bargv[0], bargv); /* call the program */
/* Convert errno into an exit code for later analysis */
for (i=0; i< num_execvp_errors; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/daemon.c
Expand Up @@ -70,15 +70,15 @@ daemon_start()

#if defined(HAVE_FCNTL_F_CLOSEM)
/*
* fcntl(fd, F_CLOSEM) needs the minimum filedescriptor
* fcntl(fd, F_CLOSEM) needs the lowest filedescriptor
* to close. the current code sets the last one to keep
* open. So increment it with 1 and use that as argument.
*/
low_fd++;
fcntl(low_fd, F_CLOSEM);
#elif defined(HAVE_CLOSEFROM)
/*
* closefrom needs the minimum filedescriptor to close.
* closefrom needs the lowest filedescriptor to close.
* the current code sets the last one to keep open.
* So increment it with 1 and use that as argument.
*/
Expand Down

0 comments on commit 2b04522

Please sign in to comment.