1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <dirent.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <sys/socket.h>
8 
9 #include "macro.h"
10 #include "stdio-util.h"
11 
12 /* maximum length of fdname */
13 #define FDNAME_MAX 255
14 
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)
18 
19 int close_nointr(int fd);
20 int safe_close(int fd);
21 void safe_close_pair(int p[static 2]);
22 
safe_close_above_stdio(int fd)23 static inline int safe_close_above_stdio(int fd) {
24         if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */
25                 return -1;
26 
27         return safe_close(fd);
28 }
29 
30 void close_many(const int fds[], size_t n_fd);
31 
32 int fclose_nointr(FILE *f);
33 FILE* safe_fclose(FILE *f);
34 DIR* safe_closedir(DIR *f);
35 
closep(int * fd)36 static inline void closep(int *fd) {
37         safe_close(*fd);
38 }
39 
close_pairp(int (* p)[2])40 static inline void close_pairp(int (*p)[2]) {
41         safe_close_pair(*p);
42 }
43 
fclosep(FILE ** f)44 static inline void fclosep(FILE **f) {
45         safe_fclose(*f);
46 }
47 
48 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL);
49 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
50 
51 #define _cleanup_close_ _cleanup_(closep)
52 #define _cleanup_fclose_ _cleanup_(fclosep)
53 #define _cleanup_pclose_ _cleanup_(pclosep)
54 #define _cleanup_closedir_ _cleanup_(closedirp)
55 #define _cleanup_close_pair_ _cleanup_(close_pairp)
56 
57 int fd_nonblock(int fd, bool nonblock);
58 int fd_cloexec(int fd, bool cloexec);
59 
60 int get_max_fd(void);
61 
62 int close_all_fds(const int except[], size_t n_except);
63 int close_all_fds_without_malloc(const int except[], size_t n_except);
64 
65 int same_fd(int a, int b);
66 
67 void cmsg_close_all(struct msghdr *mh);
68 
69 bool fdname_is_valid(const char *s);
70 
71 int fd_get_path(int fd, char **ret);
72 
73 int move_fd(int from, int to, int cloexec);
74 
75 enum {
76         ACQUIRE_NO_DEV_NULL = 1 << 0,
77         ACQUIRE_NO_MEMFD    = 1 << 1,
78         ACQUIRE_NO_PIPE     = 1 << 2,
79         ACQUIRE_NO_TMPFILE  = 1 << 3,
80         ACQUIRE_NO_REGULAR  = 1 << 4,
81 };
82 
83 int fd_move_above_stdio(int fd);
84 
85 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
86 
make_null_stdio(void)87 static inline int make_null_stdio(void) {
88         return rearrange_stdio(-1, -1, -1);
89 }
90 
91 /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
92 #define TAKE_FD(fd)                             \
93         ({                                      \
94                 int *_fd_ = &(fd);              \
95                 int _ret_ = *_fd_;              \
96                 *_fd_ = -1;                     \
97                 _ret_;                          \
98         })
99 
100 /* Like free_and_replace(), but for file descriptors */
101 #define CLOSE_AND_REPLACE(a, b)                 \
102         ({                                      \
103                 int *_fdp_ = &(a);              \
104                 safe_close(*_fdp_);             \
105                 *_fdp_ = TAKE_FD(b);            \
106                 0;                              \
107         })
108 
109 int fd_reopen(int fd, int flags);
110 int read_nr_open(void);
111 int btrfs_defrag_fd(int fd);
112 int fd_get_diskseq(int fd, uint64_t *ret);
113 
114 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
115 #define PROC_FD_PATH_MAX \
116         (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
117 
format_proc_fd_path(char buf[static PROC_FD_PATH_MAX],int fd)118 static inline char *format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
119         assert(buf);
120         assert(fd >= 0);
121         assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
122         return buf;
123 }
124 
125 #define FORMAT_PROC_FD_PATH(fd) \
126         format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
127