Use closefrom if available

This commit is contained in:
Ed Maste 2019-03-13 09:58:32 -04:00
parent 09178676ce
commit d529026f11
4 changed files with 31 additions and 0 deletions

View File

@ -206,9 +206,13 @@ Process(void)
/* Close open files /* Close open files
*/ */
#if HAVE_CLOSEFROM
closefrom((char *)0 == pcTty ? 3 : 0);
#else
for (i = (char *)0 == pcTty ? 3 : 0; i < getdtablesize(); ++i) { for (i = (char *)0 == pcTty ? 3 : 0; i < getdtablesize(); ++i) {
(void)close(i); (void)close(i);
} }
#endif
/* Make us a session leader so that when we open /dev/tty /* Make us a session leader so that when we open /dev/tty
* it will become our controlling terminal. * it will become our controlling terminal.

View File

@ -454,11 +454,19 @@ StartInit(CONSENT *pCE)
/* setup new process with clean file descriptors /* setup new process with clean file descriptors
*/ */
#if HAVE_CLOSEFROM
for (i = 3; i <= pout[0] || i <= pin[1]; i++) {
if (i != pout[0] && i != pin[1])
close(i);
}
closefrom(i);
#else
i = GetMaxFiles(); i = GetMaxFiles();
for ( /* i above */ ; --i > 2;) { for ( /* i above */ ; --i > 2;) {
if (i != pout[0] && i != pin[1]) if (i != pout[0] && i != pin[1])
close(i); close(i);
} }
#endif
/* leave 2 until we have to close it */ /* leave 2 until we have to close it */
close(1); close(1);
close(0); close(0);
@ -592,11 +600,18 @@ VirtDev(CONSENT *pCE)
/* setup new process with clean filew descriptors /* setup new process with clean filew descriptors
*/ */
#if HAVE_CLOSEFROM
for (i = 3; i < pCE->execSlaveFD; i++)
close(i);
i++;
closefrom(i);
#else
i = GetMaxFiles(); i = GetMaxFiles();
for ( /* i above */ ; --i > 2;) { for ( /* i above */ ; --i > 2;) {
if (i != pCE->execSlaveFD) if (i != pCE->execSlaveFD)
close(i); close(i);
} }
#endif
/* leave 2 until we *have to close it* /* leave 2 until we *have to close it*
*/ */
close(1); close(1);

View File

@ -1810,10 +1810,14 @@ StartTask(CONSENT *pCE, char *cmd, uid_t uid, gid_t gid)
/* setup new process with clean file descriptors /* setup new process with clean file descriptors
*/ */
#if HAVE_CLOSEFROM
closefrom(3);
#else
i = GetMaxFiles(); i = GetMaxFiles();
for ( /* i above */ ; --i > 2;) { for ( /* i above */ ; --i > 2;) {
close(i); close(i);
} }
#endif
if (geteuid() == 0) { if (geteuid() == 0) {
if (gid != 0) if (gid != 0)

View File

@ -910,11 +910,19 @@ ExecCmd(void)
/* setup new process with clean file descriptors /* setup new process with clean file descriptors
* stderr still goes to stderr...so user sees it * stderr still goes to stderr...so user sees it
*/ */
#ifdef HAVE_CLOSEFROM
for (i = 3; i <= pout[0] || i <= pin[1]; i++) {
if (i != pout[0] && i != pin[1])
close(i);
}
closefrom(i);
#else
i = GetMaxFiles(); i = GetMaxFiles();
for ( /* i above */ ; --i > 3;) { for ( /* i above */ ; --i > 3;) {
if (i != pout[0] && i != pin[1]) if (i != pout[0] && i != pin[1])
close(i); close(i);
} }
#endif
close(1); close(1);
close(0); close(0);