]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
25ad3e9361e34660b3da02b31e04ecb7272ba43d
[thirdparty/systemd.git] / src / basic / fd-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <dirent.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7
8 #include "forward.h"
9
10 /* maximum length of fdname */
11 #define FDNAME_MAX 255
12
13 /* Make sure we can distinguish fd 0 and NULL */
14 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
15 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
16
17 /* Useful helpers for initializing pipe(), socketpair() or stdio fd arrays */
18 #define EBADF_PAIR { -EBADF, -EBADF }
19 #define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF }
20
21 /* So O_LARGEFILE is generally implied by glibc, and defined to zero hence, because we only build in LFS
22 * mode. However, when invoking fcntl(F_GETFL) the flag is ORed into the result anyway — glibc does not mask
23 * it away. Which sucks. Let's define the actual value here, so that we can mask it ourselves.
24 *
25 * The precise definition is arch specific, so we use the values defined in the kernel (note that some
26 * are hexa and others are octal; duplicated as-is from the kernel definitions):
27 * - alpha, arm, arm64, m68k, mips, parisc, powerpc, sparc: each has a specific value;
28 * - others: they use the "generic" value (defined in include/uapi/asm-generic/fcntl.h) */
29 #if O_LARGEFILE != 0
30 # define RAW_O_LARGEFILE O_LARGEFILE
31 #else
32 # if defined(__alpha__) || defined(__arm__) || defined(__aarch64__) || defined(__m68k__)
33 # define RAW_O_LARGEFILE 0400000
34 # elif defined(__mips__)
35 # define RAW_O_LARGEFILE 0x2000
36 # elif defined(__parisc__) || defined(__hppa__)
37 # define RAW_O_LARGEFILE 000004000
38 # elif defined(__powerpc__)
39 # define RAW_O_LARGEFILE 0200000
40 # elif defined(__sparc__)
41 # define RAW_O_LARGEFILE 0x40000
42 # else
43 # define RAW_O_LARGEFILE 00100000
44 # endif
45 #endif
46
47 /* On musl, O_ACCMODE is defined as (03|O_SEARCH), unlike glibc which defines it as
48 * (O_RDONLY|O_WRONLY|O_RDWR). Additionally, O_SEARCH is simply defined as O_PATH. This changes the behaviour
49 * of O_ACCMODE in certain situations, which we don't want. This definition is copied from glibc and works
50 * around the problems with musl's definition. */
51 #define O_ACCMODE_STRICT (O_RDONLY|O_WRONLY|O_RDWR)
52
53 int close_nointr(int fd);
54 int safe_close(int fd);
55 void safe_close_pair(int p[static 2]);
56
57 static inline int safe_close_above_stdio(int fd) {
58 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */
59 return -EBADF;
60
61 return safe_close(fd);
62 }
63
64 static inline void* close_fd_ptr(void *p) {
65 safe_close(PTR_TO_FD(p));
66 return NULL;
67 }
68
69 void close_many(const int fds[], size_t n_fds);
70 void close_many_unset(int fds[], size_t n_fds);
71 void close_many_and_free(int *fds, size_t n_fds);
72
73 int fclose_nointr(FILE *f);
74 FILE* safe_fclose(FILE *f);
75 DIR* safe_closedir(DIR *f);
76
77 static inline void closep(int *fd) {
78 safe_close(*fd);
79 }
80
81 static inline void close_pairp(int (*p)[2]) {
82 safe_close_pair(*p);
83 }
84
85 static inline void fclosep(FILE **f) {
86 safe_fclose(*f);
87 }
88
89 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL);
90 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
91
92 #define _cleanup_close_ _cleanup_(closep)
93 #define _cleanup_fclose_ _cleanup_(fclosep)
94 #define _cleanup_pclose_ _cleanup_(pclosep)
95 #define _cleanup_closedir_ _cleanup_(closedirp)
96 #define _cleanup_close_pair_ _cleanup_(close_pairp)
97
98 int fd_nonblock(int fd, bool nonblock);
99 int stdio_disable_nonblock(void);
100
101 int fd_cloexec(int fd, bool cloexec);
102 int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
103
104 int get_max_fd(void);
105
106 int close_all_fds(const int except[], size_t n_except);
107 int close_all_fds_without_malloc(const int except[], size_t n_except);
108
109 int pack_fds(int fds[], size_t n);
110
111 int fd_validate(int fd);
112 int same_fd(int a, int b);
113
114 bool fdname_is_valid(const char *s);
115
116 int fd_get_path(int fd, char **ret);
117
118 int move_fd(int from, int to, int cloexec);
119
120 int fd_move_above_stdio(int fd);
121
122 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
123
124 static inline int make_null_stdio(void) {
125 return rearrange_stdio(-EBADF, -EBADF, -EBADF);
126 }
127
128 /* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
129 #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
130
131 /* Like free_and_replace(), but for file descriptors */
132 #define close_and_replace(a, b) \
133 ({ \
134 int *_fdp_ = &(a); \
135 safe_close(*_fdp_); \
136 *_fdp_ = TAKE_FD(b); \
137 0; \
138 })
139
140 int fd_reopen(int fd, int flags);
141 int fd_reopen_propagate_append_and_position(int fd, int flags);
142 int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd);
143
144 int fd_is_opath(int fd);
145
146 int fd_verify_safe_flags_full(int fd, int extra_flags);
147 static inline int fd_verify_safe_flags(int fd) {
148 return fd_verify_safe_flags_full(fd, 0);
149 }
150
151 int read_nr_open(void);
152 int fd_get_diskseq(int fd, uint64_t *ret);
153
154 int path_is_root_at(int dir_fd, const char *path);
155 static inline int path_is_root(const char *path) {
156 return path_is_root_at(AT_FDCWD, path);
157 }
158 static inline int dir_fd_is_root(int dir_fd) {
159 return path_is_root_at(dir_fd, NULL);
160 }
161 static inline int dir_fd_is_root_or_cwd(int dir_fd) {
162 return dir_fd == AT_FDCWD ? true : path_is_root_at(dir_fd, NULL);
163 }
164
165 int fds_are_same_mount(int fd1, int fd2);
166
167 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
168 #define PROC_FD_PATH_MAX \
169 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
170
171 char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd);
172
173 #define FORMAT_PROC_FD_PATH(fd) \
174 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
175
176 /* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */
177 #define PROC_PID_FD_PATH_MAX \
178 (STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int))
179
180 char* format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd);
181
182 /* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */
183 #define FORMAT_PROC_PID_FD_PATH(pid, fd) \
184 format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd))
185
186 int proc_fd_enoent_errno(void);
187
188 const char* accmode_to_string(int flags);
189
190 /* Like ASSERT_PTR, but for fds */
191 #define ASSERT_FD(fd) \
192 ({ \
193 int _fd_ = (fd); \
194 assert(_fd_ >= 0); \
195 _fd_; \
196 })