utils: split null_stdfds() to open_devnull() and set_stdfds()

Signed-off-by: Aleksandr Mezin <mezin.alexander@gmail.com>
This commit is contained in:
Aleksandr Mezin 2016-03-24 23:20:42 +06:00
parent d1ccb562a9
commit f8dd027559
2 changed files with 28 additions and 9 deletions

View File

@ -1752,24 +1752,41 @@ domount:
return 1;
}
int null_stdfds(void)
int open_devnull(void)
{
int fd, ret = -1;
int fd = open("/dev/null", O_RDWR);
fd = open("/dev/null", O_RDWR);
if (fd < 0)
SYSERROR("Can't open /dev/null");
return fd;
}
int set_stdfds(int fd)
{
if (fd < 0)
return -1;
if (dup2(fd, 0) < 0)
goto err;
return -1;
if (dup2(fd, 1) < 0)
goto err;
return -1;
if (dup2(fd, 2) < 0)
goto err;
return -1;
ret = 0;
err:
return 0;
}
int null_stdfds(void)
{
int ret = -1;
int fd = open_devnull();
if (fd >= 0) {
ret = set_stdfds(fd);
close(fd);
}
return ret;
}

View File

@ -283,6 +283,8 @@ int setproctitle(char *title);
int safe_mount(const char *src, const char *dest, const char *fstype,
unsigned long flags, const void *data, const char *rootfs);
int mount_proc_if_needed(const char *rootfs);
int open_devnull(void);
int set_stdfds(int fd);
int null_stdfds(void);
int lxc_count_file_lines(const char *fn);
#endif /* __LXC_UTILS_H */