chiark / gitweb /
util: add new safe_close_above_stdio() wrapper
[elogind.git] / src / basic / fd-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <dirent.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <sys/socket.h>
27
28 #include "macro.h"
29
30 /* Make sure we can distinguish fd 0 and NULL */
31 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
32 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
33
34 int close_nointr(int fd);
35 int safe_close(int fd);
36 void safe_close_pair(int p[]);
37
38 static inline int safe_close_above_stdio(int fd) {
39         if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */
40                 return -1;
41
42         return safe_close(fd);
43 }
44
45 void close_many(const int fds[], unsigned n_fd);
46
47 int fclose_nointr(FILE *f);
48 FILE* safe_fclose(FILE *f);
49 #if 0 /// UNNEEDED by elogind
50 DIR* safe_closedir(DIR *f);
51 #endif // 0
52
53 static inline void closep(int *fd) {
54         safe_close(*fd);
55 }
56
57 static inline void close_pairp(int (*p)[2]) {
58         safe_close_pair(*p);
59 }
60
61 static inline void fclosep(FILE **f) {
62         safe_fclose(*f);
63 }
64
65 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
66 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
67
68 #define _cleanup_close_ _cleanup_(closep)
69 #define _cleanup_fclose_ _cleanup_(fclosep)
70 #define _cleanup_pclose_ _cleanup_(pclosep)
71 #define _cleanup_closedir_ _cleanup_(closedirp)
72 #define _cleanup_close_pair_ _cleanup_(close_pairp)
73
74 int fd_nonblock(int fd, bool nonblock);
75 int fd_cloexec(int fd, bool cloexec);
76 void stdio_unset_cloexec(void);
77
78 int close_all_fds(const int except[], unsigned n_except);
79
80 #if 0 /// UNNEEDED by elogind
81 int same_fd(int a, int b);
82
83 void cmsg_close_all(struct msghdr *mh);
84
85 bool fdname_is_valid(const char *s);
86
87 int fd_get_path(int fd, char **ret);
88 #endif // 0
89
90 int move_fd(int from, int to, int cloexec);
91
92 enum {
93         ACQUIRE_NO_DEV_NULL = 1 << 0,
94         ACQUIRE_NO_MEMFD    = 1 << 1,
95         ACQUIRE_NO_PIPE     = 1 << 2,
96         ACQUIRE_NO_TMPFILE  = 1 << 3,
97         ACQUIRE_NO_REGULAR  = 1 << 4,
98 };
99
100 int acquire_data_fd(const void *data, size_t size, unsigned flags);
101
102 /* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
103 #define ERRNO_IS_DISCONNECT(r) \
104         IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)
105
106 int fd_move_above_stdio(int fd);