From 5e636bc2fca2c5520445bd21391e514e250c0708 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 24 Apr 2020 21:14:23 +0200 Subject: Add a fallback for close_all_files if procfs isn't mounted Signed-off-by: David Oberhollenzer --- initd/runsvc.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'initd') diff --git a/initd/runsvc.c b/initd/runsvc.c index c45f8ab..1963a09 100644 --- a/initd/runsvc.c +++ b/initd/runsvc.c @@ -53,24 +53,37 @@ static int setup_env(void) static int close_all_files(void) { struct dirent *ent; + int fd, n; DIR *dir; - int fd; dir = opendir(PROCFDDIR); + if (dir == NULL) { - perror(PROCFDDIR); - return -1; - } + if (errno != ENOENT) { + perror(PROCFDDIR); + return -1; + } - while ((ent = readdir(dir)) != NULL) { - if (!isdigit(ent->d_name[0])) - continue; + n = sysconf(_SC_OPEN_MAX); + if (n < 0) { + perror("getting maximum file descriptor count"); + return -1; + } + + for (fd = 0; fd < n; ++fd) + close(fd); + } else { + while ((ent = readdir(dir)) != NULL) { + if (!isdigit(ent->d_name[0])) + continue; + + fd = atoi(ent->d_name); + close(fd); + } - fd = atoi(ent->d_name); - close(fd); + closedir(dir); } - closedir(dir); return 0; } -- cgit v1.2.3