diff options
author | David Oberhollenzer <goliath@infraroot.at> | 2020-04-24 21:14:23 +0200 |
---|---|---|
committer | David Oberhollenzer <goliath@infraroot.at> | 2020-04-24 21:14:23 +0200 |
commit | 5e636bc2fca2c5520445bd21391e514e250c0708 (patch) | |
tree | a909d17ce4049dd5e12c281f22486e2cb536dd13 /initd | |
parent | 5307b95b93be495e40e4770fa6194db6ee27533a (diff) |
Add a fallback for close_all_files if procfs isn't mounted
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
Diffstat (limited to 'initd')
-rw-r--r-- | initd/runsvc.c | 33 |
1 files changed, 23 insertions, 10 deletions
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; } |