]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fd-util.h
basic: add fds_are_same_mount() helper
[thirdparty/systemd.git] / src / basic / fd-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
3ffd4af2
LP
2#pragma once
3
3ffd4af2 4#include <dirent.h>
e212f422 5#include <fcntl.h>
3ffd4af2 6#include <stdbool.h>
71d35b6b 7#include <stdio.h>
3ffd4af2
LP
8#include <sys/socket.h>
9
10#include "macro.h"
6e1e4b59 11#include "stdio-util.h"
3ffd4af2 12
ae3f4bae
YW
13/* maximum length of fdname */
14#define FDNAME_MAX 255
15
23e096cc
LP
16/* Make sure we can distinguish fd 0 and NULL */
17#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
18#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
71136404
LP
19
20/* Useful helpers for initializing pipe(), socketpair() or stdio fd arrays */
21#define EBADF_PAIR { -EBADF, -EBADF }
22#define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF }
23e096cc 23
3ffd4af2
LP
24int close_nointr(int fd);
25int safe_close(int fd);
3042bbeb 26void safe_close_pair(int p[static 2]);
3ffd4af2 27
e7685a77 28static inline int safe_close_above_stdio(int fd) {
5bb1d7fb
YW
29 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */
30 return -EBADF;
e7685a77
LP
31
32 return safe_close(fd);
33}
34
da6053d0 35void close_many(const int fds[], size_t n_fd);
d3eb74f8 36void close_many_unset(int fds[], size_t n_fd);
3b444970 37void close_many_and_free(int *fds, size_t n_fds);
3ffd4af2
LP
38
39int fclose_nointr(FILE *f);
40FILE* safe_fclose(FILE *f);
41DIR* safe_closedir(DIR *f);
42
43static inline void closep(int *fd) {
44 safe_close(*fd);
45}
46
47static inline void close_pairp(int (*p)[2]) {
48 safe_close_pair(*p);
49}
50
51static inline void fclosep(FILE **f) {
52 safe_fclose(*f);
53}
54
fd421c4a
ZJS
55DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL);
56DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
3ffd4af2
LP
57
58#define _cleanup_close_ _cleanup_(closep)
59#define _cleanup_fclose_ _cleanup_(fclosep)
60#define _cleanup_pclose_ _cleanup_(pclosep)
61#define _cleanup_closedir_ _cleanup_(closedirp)
62#define _cleanup_close_pair_ _cleanup_(close_pairp)
63
64int fd_nonblock(int fd, bool nonblock);
65int fd_cloexec(int fd, bool cloexec);
ed18c22c 66int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
3ffd4af2 67
73fc0cbc
LP
68int get_max_fd(void);
69
c85cb3bc 70int close_all_fds(const int except[], size_t n_except);
11966552 71int close_all_fds_without_malloc(const int except[], size_t n_except);
3ffd4af2
LP
72
73int same_fd(int a, int b);
74
75void cmsg_close_all(struct msghdr *mh);
4fee3975
LP
76
77bool fdname_is_valid(const char *s);
a1a3f73a 78
4aeb20f5
LP
79int fd_get_path(int fd, char **ret);
80
046a82c1
LP
81int move_fd(int from, int to, int cloexec);
82
7fe2903c 83int fd_move_above_stdio(int fd);
aa11e28b
LP
84
85int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
8bb2db73
LP
86
87static inline int make_null_stdio(void) {
5bb1d7fb 88 return rearrange_stdio(-EBADF, -EBADF, -EBADF);
8bb2db73 89}
c10d6bdb 90
40c5cc2b
DS
91/* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
92#define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
f2324783 93
0706c012 94/* Like free_and_replace(), but for file descriptors */
ee3455cf 95#define close_and_replace(a, b) \
0706c012
ZJS
96 ({ \
97 int *_fdp_ = &(a); \
98 safe_close(*_fdp_); \
99 *_fdp_ = TAKE_FD(b); \
100 0; \
101 })
102
f2324783 103int fd_reopen(int fd, int flags);
5f5865f0 104int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd);
ea61e2e9 105int fd_is_opath(int fd);
9264cc39 106int read_nr_open(void);
7e93a658 107int fd_get_diskseq(int fd, uint64_t *ret);
6e1e4b59 108
8a65b0b2 109int path_is_root_at(int dir_fd, const char *path);
83c57d8c
YW
110static inline int path_is_root(const char *path) {
111 return path_is_root_at(AT_FDCWD, path);
112}
8a65b0b2
DDM
113static inline int dir_fd_is_root(int dir_fd) {
114 return path_is_root_at(dir_fd, NULL);
115}
e212f422 116static inline int dir_fd_is_root_or_cwd(int dir_fd) {
8a65b0b2 117 return dir_fd == AT_FDCWD ? true : path_is_root_at(dir_fd, NULL);
e212f422 118}
af423b4b 119
5134e546
LB
120int fds_are_same_mount(int fd1, int fd2);
121
7b9da386 122/* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
6e1e4b59 123#define PROC_FD_PATH_MAX \
7b9da386 124 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
6e1e4b59
LP
125
126static inline char *format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
127 assert(buf);
128 assert(fd >= 0);
7b9da386 129 assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
6e1e4b59
LP
130 return buf;
131}
132
133#define FORMAT_PROC_FD_PATH(fd) \
134 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
b2b84f4b 135
61c062f8
LP
136/* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */
137#define PROC_PID_FD_PATH_MAX \
138 (STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int))
139
140char *format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd);
141
142/* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */
143#define FORMAT_PROC_PID_FD_PATH(pid, fd) \
144 format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd))
145
b2b84f4b 146const char *accmode_to_string(int flags);
6f81bcef
LP
147
148/* Like ASSERT_PTR, but for fds */
149#define ASSERT_FD(fd) \
150 ({ \
151 int _fd_ = (fd); \
152 assert(_fd_ >= 0); \
153 _fd_; \
154 })