diff --git a/src/liblwevent/900_util.c b/src/liblwevent/900_util.c index 997ea58..438ba48 100644 --- a/src/liblwevent/900_util.c +++ b/src/liblwevent/900_util.c @@ -26,6 +26,25 @@ int lwevent_nonblock(int fd) +/* lwevent_cloexec() + * Uses fcntl(2) to set FD_CLOEXEC for `fd'. No-op if it is already set. Does not affect any other + * file status flags. + */ +int lwevent_cloexec(int fd) +{ + long flags; + + flags = TEMP_FAILURE_RETRY( fcntl(fd, F_GETFD) ); + if(flags == -1) return -1; + + if(flags & FD_CLOEXEC) return 0; + + flags |= FD_CLOEXEC; + return TEMP_FAILURE_RETRY( fcntl(fd, F_SETFD, flags) ); +} + + + /* options for text editors kate: replace-trailing-space-save true; space-indent true; tab-width 4; vim: expandtab:ts=4:sw=4 diff --git a/src/liblwevent/900_util.h b/src/liblwevent/900_util.h index bc380cd..1382405 100644 --- a/src/liblwevent/900_util.h +++ b/src/liblwevent/900_util.h @@ -30,6 +30,20 @@ int lwevent_nonblock(int fd) __attribute__((warn_unused_result)); +/*! \brief Set file descriptor to close on exec(). + +\param fd File descriptor. +\retval 0 on success. +\retval -1 on error (and see \a errno). + +This function is provided to switch the file descriptor \a fd into close-on-exec mode using +\c fcntl(2). + +*/ +int lwevent_cloexec(int fd) __attribute__((warn_unused_result)); + + + /*!@}*/ /* options for text editors kate: replace-trailing-space-save true; space-indent true; tab-width 4;