]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: add fd_cloexec()
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 1 Jul 2018 10:05:31 +0000 (12:05 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 1 Jul 2018 18:00:14 +0000 (20:00 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Cc: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/lxc/utils.c
src/lxc/utils.h

index 2b00f0d42e70c2213cd229fc6572c21edc21f51d..fb7ae20236557c7a8f5ce8eed385495b9da3ed6c 100644 (file)
@@ -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;
+}
index 7494f80f4bf8358e13b556edeab1444435228805..4a748cd1b93546d00537d8f7d51057f8dcc401bf 100644 (file)
@@ -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 */