utils: add fd_cloexec()

Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com> Cc: Wolfgang Bumiller <w.bumiller@proxmox.com>
parent 69b0a768
...@@ -2686,3 +2686,25 @@ void remove_trailing_newlines(char *l) ...@@ -2686,3 +2686,25 @@ void remove_trailing_newlines(char *l)
while (--p >= l && *p == '\n') while (--p >= l && *p == '\n')
*p = '\0'; *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;
}
...@@ -615,5 +615,6 @@ static inline pid_t lxc_raw_gettid(void) ...@@ -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. */ /* Set a signal the child process will receive after the parent has died. */
extern int lxc_set_death_signal(int signal); extern int lxc_set_death_signal(int signal);
extern int fd_cloexec(int fd, bool cloexec);
#endif /* __LXC_UTILS_H */ #endif /* __LXC_UTILS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment