]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 /* maximum length of fdname */
11 #define FDNAME_MAX 255
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)
17 /* Useful helpers for initializing pipe(), socketpair() or stdio fd arrays */
18 #define EBADF_PAIR { -EBADF, -EBADF }
19 #define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF }
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.
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) */
30 # define RAW_O_LARGEFILE O_LARGEFILE
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
43 # define RAW_O_LARGEFILE 00100000
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)
53 /* The default, minimum, and maximum values of /proc/sys/fs/nr_open. See kernel's fs/file.c.
54 * These values have been unchanged since kernel-2.6.26:
55 * https://github.com/torvalds/linux/commit/eceea0b3df05ed262ae32e0c6340cc7a3626632d */
56 #define NR_OPEN_DEFAULT ((unsigned) (1024 * 1024))
57 #define NR_OPEN_MINIMUM ((unsigned) (sizeof(long) * 8))
58 #define NR_OPEN_MAXIMUM ((unsigned) (CONST_MIN((size_t) INT_MAX, SIZE_MAX / __SIZEOF_POINTER__) & ~(sizeof(long) * 8 - 1)))
60 int close_nointr(int fd
);
61 int safe_close(int fd
);
62 void safe_close_pair(int p
[static 2]);
64 static inline int safe_close_above_stdio(int fd
) {
65 if (fd
< 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */
68 return safe_close(fd
);
71 static inline void* close_fd_ptr(void *p
) {
72 safe_close(PTR_TO_FD(p
));
76 void close_many(const int fds
[], size_t n_fds
);
77 void close_many_unset(int fds
[], size_t n_fds
);
78 void close_many_and_free(int *fds
, size_t n_fds
);
80 int fclose_nointr(FILE *f
);
81 FILE* safe_fclose(FILE *f
);
82 DIR* safe_closedir(DIR *f
);
84 static inline void closep(int *fd
) {
88 static inline void close_pairp(int (*p
)[2]) {
92 static inline void fclosep(FILE **f
) {
96 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose
, NULL
);
97 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir
, NULL
);
99 #define _cleanup_close_ _cleanup_(closep)
100 #define _cleanup_fclose_ _cleanup_(fclosep)
101 #define _cleanup_pclose_ _cleanup_(pclosep)
102 #define _cleanup_closedir_ _cleanup_(closedirp)
103 #define _cleanup_close_pair_ _cleanup_(close_pairp)
105 int fd_nonblock(int fd
, bool nonblock
);
106 int stdio_disable_nonblock(void);
108 int fd_cloexec(int fd
, bool cloexec
);
109 int fd_cloexec_many(const int fds
[], size_t n_fds
, bool cloexec
);
111 int get_max_fd(void);
113 int close_all_fds(const int except
[], size_t n_except
);
114 int close_all_fds_without_malloc(const int except
[], size_t n_except
);
116 int pack_fds(int fds
[], size_t n
);
118 int fd_validate(int fd
);
119 int same_fd(int a
, int b
);
121 bool fdname_is_valid(const char *s
);
123 int fd_get_path(int fd
, char **ret
);
125 int move_fd(int from
, int to
, int cloexec
);
127 int fd_move_above_stdio(int fd
);
129 int rearrange_stdio(int original_input_fd
, int original_output_fd
, int original_error_fd
);
131 static inline int make_null_stdio(void) {
132 return rearrange_stdio(-EBADF
, -EBADF
, -EBADF
);
135 /* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
136 #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
138 /* Like free_and_replace(), but for file descriptors */
139 #define close_and_replace(a, b) \
142 safe_close(*_fdp_); \
143 *_fdp_ = TAKE_FD(b); \
147 int fd_reopen(int fd
, int flags
);
148 int fd_reopen_propagate_append_and_position(int fd
, int flags
);
149 int fd_reopen_condition(int fd
, int flags
, int mask
, int *ret_new_fd
);
151 int fd_is_opath(int fd
);
153 int fd_verify_safe_flags_full(int fd
, int extra_flags
);
154 static inline int fd_verify_safe_flags(int fd
) {
155 return fd_verify_safe_flags_full(fd
, 0);
158 unsigned read_nr_open(void);
159 int fd_get_diskseq(int fd
, uint64_t *ret
);
161 int path_is_root_at(int dir_fd
, const char *path
);
162 static inline int path_is_root(const char *path
) {
163 return path_is_root_at(AT_FDCWD
, path
);
165 static inline int dir_fd_is_root(int dir_fd
) {
166 return path_is_root_at(dir_fd
, NULL
);
168 static inline int dir_fd_is_root_or_cwd(int dir_fd
) {
169 return dir_fd
== AT_FDCWD
? true : path_is_root_at(dir_fd
, NULL
);
172 int fds_are_same_mount(int fd1
, int fd2
);
174 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
175 #define PROC_FD_PATH_MAX \
176 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
178 char* format_proc_fd_path(char buf
[static PROC_FD_PATH_MAX
], int fd
);
180 #define FORMAT_PROC_FD_PATH(fd) \
181 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
183 /* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */
184 #define PROC_PID_FD_PATH_MAX \
185 (STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int))
187 char* format_proc_pid_fd_path(char buf
[static PROC_PID_FD_PATH_MAX
], pid_t pid
, int fd
);
189 /* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */
190 #define FORMAT_PROC_PID_FD_PATH(pid, fd) \
191 format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd))
193 int proc_fd_enoent_errno(void);
195 const char* accmode_to_string(int flags
);
197 /* Like ASSERT_PTR, but for fds */
198 #define ASSERT_FD(fd) \