diff --git a/common/util.c b/common/util.c index b72f61836..830b48a72 100644 --- a/common/util.c +++ b/common/util.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "util.h" #include "tokenize.h" @@ -273,4 +274,18 @@ a_strsplit(const char *str, ssize_t len, char delim) return retval; } +/** Mark a file descriptor as close-on-exec. + * \param fd The file descriptor. + * \return The fcntl() return value. + */ +int +fd_set_close_on_exec(int fd) +{ + int flags = fcntl(fd, F_GETFD, 0); + /* ignore errors */ + if(flags < 0) + return flags; + return fcntl(fd, F_SETFD, flags | FD_CLOEXEC); +} + // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/common/util.h b/common/util.h index 7869a9692..a3efbd336 100644 --- a/common/util.h +++ b/common/util.h @@ -339,6 +339,7 @@ void *name_func_lookup(const char *, size_t, const name_func_link_t *); const char * name_func_rlookup(void *, const name_func_link_t *); void a_exec(const char *); char ** a_strsplit(const char *, ssize_t, char); +int fd_set_close_on_exec(int); #define a_asprintf(strp, fmt, ...) \ do { \