]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3ffd4af2 LP |
2 | #pragma once |
3 | ||
3ffd4af2 LP |
4 | #include <dirent.h> |
5 | #include <stdbool.h> | |
71d35b6b | 6 | #include <stdio.h> |
3ffd4af2 LP |
7 | #include <sys/socket.h> |
8 | ||
9 | #include "macro.h" | |
6e1e4b59 | 10 | #include "stdio-util.h" |
3ffd4af2 | 11 | |
ae3f4bae YW |
12 | /* maximum length of fdname */ |
13 | #define FDNAME_MAX 255 | |
14 | ||
23e096cc LP |
15 | /* Make sure we can distinguish fd 0 and NULL */ |
16 | #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1) | |
17 | #define PTR_TO_FD(p) (PTR_TO_INT(p)-1) | |
19ee48a6 | 18 | #define PIPE_EBADF { -EBADF, -EBADF } |
23e096cc | 19 | |
3ffd4af2 LP |
20 | int close_nointr(int fd); |
21 | int safe_close(int fd); | |
3042bbeb | 22 | void safe_close_pair(int p[static 2]); |
3ffd4af2 | 23 | |
e7685a77 | 24 | static inline int safe_close_above_stdio(int fd) { |
5bb1d7fb YW |
25 | if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */ |
26 | return -EBADF; | |
e7685a77 LP |
27 | |
28 | return safe_close(fd); | |
29 | } | |
30 | ||
da6053d0 | 31 | void close_many(const int fds[], size_t n_fd); |
3ffd4af2 LP |
32 | |
33 | int fclose_nointr(FILE *f); | |
34 | FILE* safe_fclose(FILE *f); | |
35 | DIR* safe_closedir(DIR *f); | |
36 | ||
37 | static inline void closep(int *fd) { | |
38 | safe_close(*fd); | |
39 | } | |
40 | ||
41 | static inline void close_pairp(int (*p)[2]) { | |
42 | safe_close_pair(*p); | |
43 | } | |
44 | ||
45 | static inline void fclosep(FILE **f) { | |
46 | safe_fclose(*f); | |
47 | } | |
48 | ||
fd421c4a ZJS |
49 | DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL); |
50 | DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL); | |
3ffd4af2 LP |
51 | |
52 | #define _cleanup_close_ _cleanup_(closep) | |
53 | #define _cleanup_fclose_ _cleanup_(fclosep) | |
54 | #define _cleanup_pclose_ _cleanup_(pclosep) | |
55 | #define _cleanup_closedir_ _cleanup_(closedirp) | |
56 | #define _cleanup_close_pair_ _cleanup_(close_pairp) | |
57 | ||
58 | int fd_nonblock(int fd, bool nonblock); | |
59 | int fd_cloexec(int fd, bool cloexec); | |
ed18c22c | 60 | int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec); |
3ffd4af2 | 61 | |
73fc0cbc LP |
62 | int get_max_fd(void); |
63 | ||
c85cb3bc | 64 | int close_all_fds(const int except[], size_t n_except); |
11966552 | 65 | int close_all_fds_without_malloc(const int except[], size_t n_except); |
3ffd4af2 LP |
66 | |
67 | int same_fd(int a, int b); | |
68 | ||
69 | void cmsg_close_all(struct msghdr *mh); | |
4fee3975 LP |
70 | |
71 | bool fdname_is_valid(const char *s); | |
a1a3f73a | 72 | |
4aeb20f5 LP |
73 | int fd_get_path(int fd, char **ret); |
74 | ||
046a82c1 LP |
75 | int move_fd(int from, int to, int cloexec); |
76 | ||
7fe2903c | 77 | int fd_move_above_stdio(int fd); |
aa11e28b LP |
78 | |
79 | int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd); | |
8bb2db73 LP |
80 | |
81 | static inline int make_null_stdio(void) { | |
5bb1d7fb | 82 | return rearrange_stdio(-EBADF, -EBADF, -EBADF); |
8bb2db73 | 83 | } |
c10d6bdb | 84 | |
40c5cc2b DS |
85 | /* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */ |
86 | #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF) | |
f2324783 | 87 | |
0706c012 | 88 | /* Like free_and_replace(), but for file descriptors */ |
ee3455cf | 89 | #define close_and_replace(a, b) \ |
0706c012 ZJS |
90 | ({ \ |
91 | int *_fdp_ = &(a); \ | |
92 | safe_close(*_fdp_); \ | |
93 | *_fdp_ = TAKE_FD(b); \ | |
94 | 0; \ | |
95 | }) | |
96 | ||
f2324783 | 97 | int fd_reopen(int fd, int flags); |
5f5865f0 | 98 | int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd); |
ea61e2e9 | 99 | int fd_is_opath(int fd); |
9264cc39 | 100 | int read_nr_open(void); |
7e93a658 | 101 | int fd_get_diskseq(int fd, uint64_t *ret); |
6e1e4b59 LP |
102 | |
103 | /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */ | |
104 | #define PROC_FD_PATH_MAX \ | |
105 | (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)) | |
106 | ||
107 | static inline char *format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) { | |
108 | assert(buf); | |
109 | assert(fd >= 0); | |
110 | assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd)); | |
111 | return buf; | |
112 | } | |
113 | ||
114 | #define FORMAT_PROC_FD_PATH(fd) \ | |
115 | format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd)) |