]>
Commit | Line | Data |
---|---|---|
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 | int close_nointr(int fd); | |
22 | int safe_close(int fd); | |
23 | void safe_close_pair(int p[static 2]); | |
24 | ||
25 | static inline int safe_close_above_stdio(int fd) { | |
26 | if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */ | |
27 | return -EBADF; | |
28 | ||
29 | return safe_close(fd); | |
30 | } | |
31 | ||
32 | static inline void* close_fd_ptr(void *p) { | |
33 | safe_close(PTR_TO_FD(p)); | |
34 | return NULL; | |
35 | } | |
36 | ||
37 | void close_many(const int fds[], size_t n_fds); | |
38 | void close_many_unset(int fds[], size_t n_fds); | |
39 | void close_many_and_free(int *fds, size_t n_fds); | |
40 | ||
41 | int fclose_nointr(FILE *f); | |
42 | FILE* safe_fclose(FILE *f); | |
43 | DIR* safe_closedir(DIR *f); | |
44 | ||
45 | static inline void closep(int *fd) { | |
46 | safe_close(*fd); | |
47 | } | |
48 | ||
49 | static inline void close_pairp(int (*p)[2]) { | |
50 | safe_close_pair(*p); | |
51 | } | |
52 | ||
53 | static inline void fclosep(FILE **f) { | |
54 | safe_fclose(*f); | |
55 | } | |
56 | ||
57 | DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL); | |
58 | DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL); | |
59 | ||
60 | #define _cleanup_close_ _cleanup_(closep) | |
61 | #define _cleanup_fclose_ _cleanup_(fclosep) | |
62 | #define _cleanup_pclose_ _cleanup_(pclosep) | |
63 | #define _cleanup_closedir_ _cleanup_(closedirp) | |
64 | #define _cleanup_close_pair_ _cleanup_(close_pairp) | |
65 | ||
66 | int fd_nonblock(int fd, bool nonblock); | |
67 | int stdio_disable_nonblock(void); | |
68 | ||
69 | int fd_cloexec(int fd, bool cloexec); | |
70 | int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec); | |
71 | ||
72 | int get_max_fd(void); | |
73 | ||
74 | int close_all_fds(const int except[], size_t n_except); | |
75 | int close_all_fds_without_malloc(const int except[], size_t n_except); | |
76 | ||
77 | int pack_fds(int fds[], size_t n); | |
78 | ||
79 | int fd_validate(int fd); | |
80 | int same_fd(int a, int b); | |
81 | ||
82 | bool fdname_is_valid(const char *s); | |
83 | ||
84 | int fd_get_path(int fd, char **ret); | |
85 | ||
86 | int move_fd(int from, int to, int cloexec); | |
87 | ||
88 | int fd_move_above_stdio(int fd); | |
89 | ||
90 | int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd); | |
91 | ||
92 | static inline int make_null_stdio(void) { | |
93 | return rearrange_stdio(-EBADF, -EBADF, -EBADF); | |
94 | } | |
95 | ||
96 | /* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */ | |
97 | #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF) | |
98 | ||
99 | /* Like free_and_replace(), but for file descriptors */ | |
100 | #define close_and_replace(a, b) \ | |
101 | ({ \ | |
102 | int *_fdp_ = &(a); \ | |
103 | safe_close(*_fdp_); \ | |
104 | *_fdp_ = TAKE_FD(b); \ | |
105 | 0; \ | |
106 | }) | |
107 | ||
108 | int fd_reopen(int fd, int flags); | |
109 | int fd_reopen_propagate_append_and_position(int fd, int flags); | |
110 | int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd); | |
111 | ||
112 | int fd_is_opath(int fd); | |
113 | ||
114 | int fd_verify_safe_flags_full(int fd, int extra_flags); | |
115 | static inline int fd_verify_safe_flags(int fd) { | |
116 | return fd_verify_safe_flags_full(fd, 0); | |
117 | } | |
118 | ||
119 | int read_nr_open(void); | |
120 | int fd_get_diskseq(int fd, uint64_t *ret); | |
121 | ||
122 | int path_is_root_at(int dir_fd, const char *path); | |
123 | static inline int path_is_root(const char *path) { | |
124 | return path_is_root_at(AT_FDCWD, path); | |
125 | } | |
126 | static inline int dir_fd_is_root(int dir_fd) { | |
127 | return path_is_root_at(dir_fd, NULL); | |
128 | } | |
129 | static inline int dir_fd_is_root_or_cwd(int dir_fd) { | |
130 | return dir_fd == AT_FDCWD ? true : path_is_root_at(dir_fd, NULL); | |
131 | } | |
132 | ||
133 | int fds_are_same_mount(int fd1, int fd2); | |
134 | ||
135 | /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */ | |
136 | #define PROC_FD_PATH_MAX \ | |
137 | (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)) | |
138 | ||
139 | char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd); | |
140 | ||
141 | #define FORMAT_PROC_FD_PATH(fd) \ | |
142 | format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd)) | |
143 | ||
144 | /* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */ | |
145 | #define PROC_PID_FD_PATH_MAX \ | |
146 | (STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int)) | |
147 | ||
148 | char* format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd); | |
149 | ||
150 | /* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */ | |
151 | #define FORMAT_PROC_PID_FD_PATH(pid, fd) \ | |
152 | format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd)) | |
153 | ||
154 | int proc_fd_enoent_errno(void); | |
155 | ||
156 | const char* accmode_to_string(int flags); | |
157 | ||
158 | /* Like ASSERT_PTR, but for fds */ | |
159 | #define ASSERT_FD(fd) \ | |
160 | ({ \ | |
161 | int _fd_ = (fd); \ | |
162 | assert(_fd_ >= 0); \ | |
163 | _fd_; \ | |
164 | }) |