]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
Merge pull request #25578 from mrc0mmand/test-shutdown-tweaks
[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 <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
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
36 static inline void closep(int *fd) {
37 safe_close(*fd);
38 }
39
40 static inline void close_pairp(int (*p)[2]) {
41 safe_close_pair(*p);
42 }
43
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 int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
60
61 int get_max_fd(void);
62
63 int close_all_fds(const int except[], size_t n_except);
64 int close_all_fds_without_malloc(const int except[], size_t n_except);
65
66 int same_fd(int a, int b);
67
68 void cmsg_close_all(struct msghdr *mh);
69
70 bool fdname_is_valid(const char *s);
71
72 int fd_get_path(int fd, char **ret);
73
74 int move_fd(int from, int to, int cloexec);
75
76 enum {
77 ACQUIRE_NO_DEV_NULL = 1 << 0,
78 ACQUIRE_NO_MEMFD = 1 << 1,
79 ACQUIRE_NO_PIPE = 1 << 2,
80 ACQUIRE_NO_TMPFILE = 1 << 3,
81 ACQUIRE_NO_REGULAR = 1 << 4,
82 };
83
84 int fd_move_above_stdio(int fd);
85
86 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
87
88 static inline int make_null_stdio(void) {
89 return rearrange_stdio(-1, -1, -1);
90 }
91
92 /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
93 #define TAKE_FD(fd) \
94 ({ \
95 int *_fd_ = &(fd); \
96 int _ret_ = *_fd_; \
97 *_fd_ = -1; \
98 _ret_; \
99 })
100
101 /* Like free_and_replace(), but for file descriptors */
102 #define close_and_replace(a, b) \
103 ({ \
104 int *_fdp_ = &(a); \
105 safe_close(*_fdp_); \
106 *_fdp_ = TAKE_FD(b); \
107 0; \
108 })
109
110 int fd_reopen(int fd, int flags);
111 int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd);
112 int read_nr_open(void);
113 int fd_get_diskseq(int fd, uint64_t *ret);
114
115 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
116 #define PROC_FD_PATH_MAX \
117 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
118
119 static inline char *format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
120 assert(buf);
121 assert(fd >= 0);
122 assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
123 return buf;
124 }
125
126 #define FORMAT_PROC_FD_PATH(fd) \
127 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))