diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-02-26 15:41:38 +0100 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2018-05-30 07:59:08 +0200 |
commit | ecc6cb9a01b132b88cdf20272573c142eeb84e8b (patch) | |
tree | 06761bc1094a87d041a9376307d5555550557ce1 /src/basic/fd-util.h | |
parent | 89e3fcd3115e66914561b804009a3b60c574d3c9 (diff) |
util: add new safe_close_above_stdio() wrapper
At various places we only want to close fds if they are not
stdin/stdout/stderr, i.e. fds 0, 1, 2. Let's add a unified helper call
for that, and port everything over.
Diffstat (limited to 'src/basic/fd-util.h')
-rw-r--r-- | src/basic/fd-util.h | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 6a9006421..7c1009773 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -35,6 +35,13 @@ int close_nointr(int fd); int safe_close(int fd); void safe_close_pair(int p[]); +static inline int safe_close_above_stdio(int fd) { + if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */ + return -1; + + return safe_close(fd); +} + void close_many(const int fds[], unsigned n_fd); int fclose_nointr(FILE *f); |