]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
Merge pull request #32961 from YHNdnzj/starttime-main
[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 <stdbool.h>
7 #include <stdio.h>
8 #include <sys/socket.h>
9
10 #include "macro.h"
11 #include "missing_fcntl.h"
12 #include "stdio-util.h"
13
14 /* maximum length of fdname */
15 #define FDNAME_MAX 255
16
17 /* Make sure we can distinguish fd 0 and NULL */
18 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
19 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
20
21 /* Useful helpers for initializing pipe(), socketpair() or stdio fd arrays */
22 #define EBADF_PAIR { -EBADF, -EBADF }
23 #define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF }
24
25 int close_nointr(int fd);
26 int safe_close(int fd);
27 void safe_close_pair(int p[static 2]);
28
29 static inline int safe_close_above_stdio(int fd) {
30 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */
31 return -EBADF;
32
33 return safe_close(fd);
34 }
35
36 void close_many(const int fds[], size_t n_fds);
37 void close_many_unset(int fds[], size_t n_fds);
38 void close_many_and_free(int *fds, size_t n_fds);
39
40 int fclose_nointr(FILE *f);
41 FILE* safe_fclose(FILE *f);
42 DIR* safe_closedir(DIR *f);
43
44 static inline void closep(int *fd) {
45 safe_close(*fd);
46 }
47
48 static inline void close_pairp(int (*p)[2]) {
49 safe_close_pair(*p);
50 }
51
52 static inline void fclosep(FILE **f) {
53 safe_fclose(*f);
54 }
55
56 static inline void* close_fd_ptr(void *p) {
57 safe_close(PTR_TO_FD(p));
58 return NULL;
59 }
60
61 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL);
62 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
63
64 #define _cleanup_close_ _cleanup_(closep)
65 #define _cleanup_fclose_ _cleanup_(fclosep)
66 #define _cleanup_pclose_ _cleanup_(pclosep)
67 #define _cleanup_closedir_ _cleanup_(closedirp)
68 #define _cleanup_close_pair_ _cleanup_(close_pairp)
69
70 int fd_nonblock(int fd, bool nonblock);
71 int stdio_disable_nonblock(void);
72
73 int fd_cloexec(int fd, bool cloexec);
74 int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
75
76 int get_max_fd(void);
77
78 int close_all_fds(const int except[], size_t n_except);
79 int close_all_fds_without_malloc(const int except[], size_t n_except);
80
81 int pack_fds(int fds[], size_t n);
82
83 int same_fd(int a, int b);
84
85 void cmsg_close_all(struct msghdr *mh);
86
87 bool fdname_is_valid(const char *s);
88
89 int fd_get_path(int fd, char **ret);
90
91 int move_fd(int from, int to, int cloexec);
92
93 int fd_move_above_stdio(int fd);
94
95 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
96
97 static inline int make_null_stdio(void) {
98 return rearrange_stdio(-EBADF, -EBADF, -EBADF);
99 }
100
101 /* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
102 #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
103
104 /* Like free_and_replace(), but for file descriptors */
105 #define close_and_replace(a, b) \
106 ({ \
107 int *_fdp_ = &(a); \
108 safe_close(*_fdp_); \
109 *_fdp_ = TAKE_FD(b); \
110 0; \
111 })
112
113 int fd_reopen(int fd, int flags);
114 int fd_reopen_propagate_append_and_position(int fd, int flags);
115 int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd);
116
117 int fd_is_opath(int fd);
118
119 int fd_verify_safe_flags_full(int fd, int extra_flags);
120 static inline int fd_verify_safe_flags(int fd) {
121 return fd_verify_safe_flags_full(fd, 0);
122 }
123
124 int read_nr_open(void);
125 int fd_get_diskseq(int fd, uint64_t *ret);
126
127 int path_is_root_at(int dir_fd, const char *path);
128 static inline int path_is_root(const char *path) {
129 return path_is_root_at(AT_FDCWD, path);
130 }
131 static inline int dir_fd_is_root(int dir_fd) {
132 return path_is_root_at(dir_fd, NULL);
133 }
134 static inline int dir_fd_is_root_or_cwd(int dir_fd) {
135 return dir_fd == AT_FDCWD ? true : path_is_root_at(dir_fd, NULL);
136 }
137
138 int fds_are_same_mount(int fd1, int fd2);
139
140 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
141 #define PROC_FD_PATH_MAX \
142 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
143
144 static inline char *format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
145 assert(buf);
146 assert(fd >= 0);
147 assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
148 return buf;
149 }
150
151 #define FORMAT_PROC_FD_PATH(fd) \
152 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
153
154 /* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */
155 #define PROC_PID_FD_PATH_MAX \
156 (STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int))
157
158 char *format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd);
159
160 /* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */
161 #define FORMAT_PROC_PID_FD_PATH(pid, fd) \
162 format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd))
163
164 int proc_fd_enoent_errno(void);
165
166 const char *accmode_to_string(int flags);
167
168 /* Like ASSERT_PTR, but for fds */
169 #define ASSERT_FD(fd) \
170 ({ \
171 int _fd_ = (fd); \
172 assert(_fd_ >= 0); \
173 _fd_; \
174 })