From: Christian Brauner Date: Sun, 1 Jul 2018 10:05:31 +0000 (+0200) Subject: utils: add fd_cloexec() X-Git-Tag: lxc-3.1.0~224^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a9d4ebc1684044eccd7f9f50ad66cacd46e459b1;p=thirdparty%2Flxc.git utils: add fd_cloexec() Signed-off-by: Christian Brauner Cc: Wolfgang Bumiller --- diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 2b00f0d42..fb7ae2023 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2686,3 +2686,25 @@ void remove_trailing_newlines(char *l) while (--p >= l && *p == '\n') *p = '\0'; } + +int fd_cloexec(int fd, bool cloexec) +{ + int oflags, nflags; + + oflags = fcntl(fd, F_GETFD, 0); + if (oflags < 0) + return -errno; + + if (cloexec) + nflags = oflags | FD_CLOEXEC; + else + nflags = oflags & ~FD_CLOEXEC; + + if (nflags == oflags) + return 0; + + if (fcntl(fd, F_SETFD, nflags) < 0) + return -errno; + + return 0; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 7494f80f4..4a748cd1b 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -615,5 +615,6 @@ static inline pid_t lxc_raw_gettid(void) /* Set a signal the child process will receive after the parent has died. */ extern int lxc_set_death_signal(int signal); +extern int fd_cloexec(int fd, bool cloexec); #endif /* __LXC_UTILS_H */