Use closefrom(2) instead of close(2) in a loop.
MFC after: 1 week
This commit is contained in:
parent
5ad4a7c74a
commit
6c71649c5f
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=214119
@ -88,32 +88,19 @@ static void hook_free(struct hookproc *hp);
|
|||||||
static void
|
static void
|
||||||
descriptors(void)
|
descriptors(void)
|
||||||
{
|
{
|
||||||
long maxfd;
|
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Close all descriptors.
|
* Close all (or almost all) descriptors.
|
||||||
*/
|
*/
|
||||||
maxfd = sysconf(_SC_OPEN_MAX);
|
if (pjdlog_mode_get() == PJDLOG_MODE_STD) {
|
||||||
if (maxfd < 0) {
|
closefrom(MAX(MAX(STDIN_FILENO, STDOUT_FILENO),
|
||||||
pjdlog_errno(LOG_WARNING, "sysconf(_SC_OPEN_MAX) failed");
|
STDERR_FILENO) + 1);
|
||||||
maxfd = 1024;
|
|
||||||
}
|
|
||||||
for (fd = 0; fd <= maxfd; fd++) {
|
|
||||||
switch (fd) {
|
|
||||||
case STDIN_FILENO:
|
|
||||||
case STDOUT_FILENO:
|
|
||||||
case STDERR_FILENO:
|
|
||||||
if (pjdlog_mode_get() == PJDLOG_MODE_STD)
|
|
||||||
break;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
default:
|
|
||||||
close(fd);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (pjdlog_mode_get() == PJDLOG_MODE_STD)
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
closefrom(0);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Redirect stdin, stdout and stderr to /dev/null.
|
* Redirect stdin, stdout and stderr to /dev/null.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user