// If the file is not a block device, we don't want mkfs to ask
// us about whether to proceed.
- close(0);
- close(1);
- close(2);
- open("/dev/zero", O_RDONLY);
- open("/dev/null", O_RDWR);
- open("/dev/null", O_RDWR);
+ if (null_stdfds() < 0)
+ exit(1);
execlp("mkfs", "mkfs", "-t", fstype, path, NULL);
exit(1);
}
return false;
}
lxc_check_inherited(conf, -1);
- close(0);
- close(1);
- close(2);
- open("/dev/zero", O_RDONLY);
- open("/dev/null", O_RDWR);
- open("/dev/null", O_RDWR);
+ if (null_stdfds() < 0) {
+ ERROR("failed to close fds");
+ return false;
+ }
setsid();
} else {
if (!am_single_threaded()) {
return p;
}
-static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
+static bool create_run_template(struct lxc_container *c, char *tpath, bool need_null_stdfds,
char *const argv[])
{
pid_t pid;
char **newargv;
struct lxc_conf *conf = c->lxc_conf;
- if (quiet) {
- close(0);
- close(1);
- close(2);
- open("/dev/zero", O_RDONLY);
- open("/dev/null", O_RDWR);
- open("/dev/null", O_RDWR);
+ if (need_null_stdfds && null_stdfds() < 0) {
+ exit(1);
}
src = c->lxc_conf->rootfs.path;
exit(EXIT_FAILURE);
}
lxc_check_inherited(NULL, pipefd[1]);
- close(0);
- close(1);
- close(2);
- open("/dev/null", O_RDONLY);
- open("/dev/null", O_RDWR);
- open("/dev/null", O_RDWR);
+ if (null_stdfds() < 0)
+ exit(EXIT_FAILURE);
close(pipefd[0]);
sprintf(pipefd_str, "%d", pipefd[1]);
execvp(args[0], args);
return tpath;
}
+
+int null_stdfds(void)
+{
+ int fd, ret = -1;
+
+ fd = open("/dev/null", O_RDWR);
+ if (fd < 0)
+ return -1;
+
+ if (dup2(fd, 0) < 0)
+ goto err;
+ if (dup2(fd, 1) < 0)
+ goto err;
+ if (dup2(fd, 2) < 0)
+ goto err;
+
+ ret = 0;
+err:
+ close(fd);
+ return ret;
+}
int detect_ramfs_rootfs(void);
char *on_path(char *cmd);
char *get_template_path(const char *t);
+int null_stdfds(void);
#endif /* __LXC_UTILS_H */