/* Check for any fds we need to close. * - If fd_to_ignore != -1, then if we find that fd open we will ignore it. * - By default we warn about open fds we find. * - If closeall is true, we will close open fds. * - If lxc-start was passed "-C", then conf->close_all_fds will be true, in * which case we also close all open fds. * - A daemonized container will always pass closeall=true. */ int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore) { struct dirent *direntp; int fd, fddir; DIR *dir; if (conf && conf->close_all_fds) closeall = true; restart: dir = opendir("/proc/self/fd"); if (!dir) { WARN("Failed to open directory: %m."); return -1; } fddir = dirfd(dir); while ((direntp = readdir(dir))) { if (!direntp) break; if (!strcmp(direntp->d_name, ".")) continue; if (!strcmp(direntp->d_name, "..")) continue; if (lxc_safe_int(direntp->d_name, &fd) < 0) { INFO("Could not parse file descriptor for: %s", direntp->d_name); continue; } if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) continue; if (current_config && fd == current_config->logfd) continue; if (match_fd(fd)) continue; if (closeall) { close(fd); closedir(dir); INFO("Closed inherited fd: %d.", fd); goto restart; } WARN("Inherited fd: %d.", fd); } /* Only enable syslog at this point to avoid the above logging function * to open a new fd and make the check_inherited function enter an * infinite loop. */ lxc_log_enable_syslog(); closedir(dir); /* cannot fail */ return 0; }
/* * Check for any fds we need to close * * if fd_to_ignore != -1, then if we find that fd open we will ignore it. * * By default we warn about open fds we find. * * If closeall is true, we will close open fds. * * If lxc-start was passed "-C", then conf->close_all_fds will be true, * in which case we also close all open fds. * * A daemonized container will always pass closeall=true. */ int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore) { struct dirent dirent, *direntp; int fd, fddir; DIR *dir; if (conf && conf->close_all_fds) closeall = true; restart: dir = opendir("/proc/self/fd"); if (!dir) { WARN("failed to open directory: %m"); return -1; } fddir = dirfd(dir); while (!readdir_r(dir, &dirent, &direntp)) { if (!direntp) break; if (!strcmp(direntp->d_name, ".")) continue; if (!strcmp(direntp->d_name, "..")) continue; fd = atoi(direntp->d_name); if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) continue; if (current_config && fd == current_config->logfd) continue; if (match_fd(fd)) continue; if (closeall) { close(fd); closedir(dir); INFO("closed inherited fd %d", fd); goto restart; } WARN("inherited fd %d", fd); } closedir(dir); /* cannot fail */ return 0; }
int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore) { struct dirent *direntp; int fd, fddir; DIR *dir; restart: dir = opendir("/proc/self/fd"); if (!dir) { WARN("failed to open directory: %m"); return -1; } fddir = dirfd(dir); while ((direntp = readdir(dir))) { if (!direntp) break; if (!strcmp(direntp->d_name, ".")) continue; if (!strcmp(direntp->d_name, "..")) continue; fd = atoi(direntp->d_name); if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) continue; if (match_fd(fd)) continue; if (conf == NULL || conf->close_all_fds) { close(fd); closedir(dir); INFO("closed inherited fd %d", fd); goto restart; } WARN("inherited fd %d", fd); } closedir(dir); /* cannot fail */ return 0; }