]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fd-util.h
basic/include: replace _Static_assert() with static_assert()
[thirdparty/systemd.git] / src / basic / fd-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
3ffd4af2
LP
2#pragma once
3
3ffd4af2 4#include <dirent.h>
194a6901 5#include <fcntl.h>
71d35b6b 6#include <stdio.h>
3ffd4af2 7
0c15577a 8#include "forward.h"
3ffd4af2 9
ae3f4bae
YW
10/* maximum length of fdname */
11#define FDNAME_MAX 255
12
23e096cc
LP
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)
71136404
LP
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 }
23e096cc 20
cefec8f6
D
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
1543c238
D
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
cfba9b9e
YW
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)))
59
3ffd4af2
LP
60int close_nointr(int fd);
61int safe_close(int fd);
3042bbeb 62void safe_close_pair(int p[static 2]);
3ffd4af2 63
e7685a77 64static inline int safe_close_above_stdio(int fd) {
5bb1d7fb
YW
65 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */
66 return -EBADF;
e7685a77
LP
67
68 return safe_close(fd);
69}
70
82775307
MY
71static inline void* close_fd_ptr(void *p) {
72 safe_close(PTR_TO_FD(p));
73 return NULL;
74}
75
1276e633
MY
76void close_many(const int fds[], size_t n_fds);
77void close_many_unset(int fds[], size_t n_fds);
3b444970 78void close_many_and_free(int *fds, size_t n_fds);
3ffd4af2
LP
79
80int fclose_nointr(FILE *f);
81FILE* safe_fclose(FILE *f);
82DIR* safe_closedir(DIR *f);
83
84static inline void closep(int *fd) {
85 safe_close(*fd);
86}
87
88static inline void close_pairp(int (*p)[2]) {
89 safe_close_pair(*p);
90}
91
92static inline void fclosep(FILE **f) {
93 safe_fclose(*f);
94}
95
fd421c4a
ZJS
96DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL);
97DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
3ffd4af2
LP
98
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)
104
105int fd_nonblock(int fd, bool nonblock);
3b1e80f7
LP
106int stdio_disable_nonblock(void);
107
3ffd4af2 108int fd_cloexec(int fd, bool cloexec);
ed18c22c 109int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
3ffd4af2 110
73fc0cbc
LP
111int get_max_fd(void);
112
c85cb3bc 113int close_all_fds(const int except[], size_t n_except);
11966552 114int close_all_fds_without_malloc(const int except[], size_t n_except);
3ffd4af2 115
85f660d4
AV
116int pack_fds(int fds[], size_t n);
117
6056663a 118int fd_validate(int fd);
3ffd4af2
LP
119int same_fd(int a, int b);
120
4fee3975 121bool fdname_is_valid(const char *s);
a1a3f73a 122
4aeb20f5
LP
123int fd_get_path(int fd, char **ret);
124
046a82c1
LP
125int move_fd(int from, int to, int cloexec);
126
7fe2903c 127int fd_move_above_stdio(int fd);
aa11e28b
LP
128
129int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
8bb2db73
LP
130
131static inline int make_null_stdio(void) {
5bb1d7fb 132 return rearrange_stdio(-EBADF, -EBADF, -EBADF);
8bb2db73 133}
c10d6bdb 134
40c5cc2b
DS
135/* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
136#define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
f2324783 137
0706c012 138/* Like free_and_replace(), but for file descriptors */
ee3455cf 139#define close_and_replace(a, b) \
0706c012
ZJS
140 ({ \
141 int *_fdp_ = &(a); \
142 safe_close(*_fdp_); \
143 *_fdp_ = TAKE_FD(b); \
144 0; \
145 })
146
f2324783 147int fd_reopen(int fd, int flags);
b8e25bff 148int fd_reopen_propagate_append_and_position(int fd, int flags);
5f5865f0 149int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd);
14f38d17 150
ea61e2e9 151int fd_is_opath(int fd);
9f65355b
LP
152
153int fd_verify_safe_flags_full(int fd, int extra_flags);
154static inline int fd_verify_safe_flags(int fd) {
155 return fd_verify_safe_flags_full(fd, 0);
156}
14f38d17 157
cfba9b9e 158unsigned read_nr_open(void);
7e93a658 159int fd_get_diskseq(int fd, uint64_t *ret);
6e1e4b59 160
8a65b0b2 161int path_is_root_at(int dir_fd, const char *path);
83c57d8c
YW
162static inline int path_is_root(const char *path) {
163 return path_is_root_at(AT_FDCWD, path);
164}
8a65b0b2
DDM
165static inline int dir_fd_is_root(int dir_fd) {
166 return path_is_root_at(dir_fd, NULL);
167}
e212f422 168static inline int dir_fd_is_root_or_cwd(int dir_fd) {
8a65b0b2 169 return dir_fd == AT_FDCWD ? true : path_is_root_at(dir_fd, NULL);
e212f422 170}
af423b4b 171
5134e546
LB
172int fds_are_same_mount(int fd1, int fd2);
173
7b9da386 174/* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
6e1e4b59 175#define PROC_FD_PATH_MAX \
7b9da386 176 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
6e1e4b59 177
0c15577a 178char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd);
6e1e4b59
LP
179
180#define FORMAT_PROC_FD_PATH(fd) \
181 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
b2b84f4b 182
61c062f8
LP
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))
186
ff3f2953 187char* format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd);
61c062f8
LP
188
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))
192
d19b3c5d
MY
193int proc_fd_enoent_errno(void);
194
bfd5a068 195const char* accmode_to_string(int flags);
6f81bcef
LP
196
197/* Like ASSERT_PTR, but for fds */
198#define ASSERT_FD(fd) \
199 ({ \
200 int _fd_ = (fd); \
201 assert(_fd_ >= 0); \
202 _fd_; \
203 })