]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fd-util.h
tree-wide: use -EBADF for fd initialization
[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
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)
18
3ffd4af2
LP
19int close_nointr(int fd);
20int safe_close(int fd);
3042bbeb 21void safe_close_pair(int p[static 2]);
3ffd4af2 22
e7685a77
LP
23static 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
da6053d0 30void close_many(const int fds[], size_t n_fd);
3ffd4af2
LP
31
32int fclose_nointr(FILE *f);
33FILE* safe_fclose(FILE *f);
34DIR* safe_closedir(DIR *f);
35
36static inline void closep(int *fd) {
37 safe_close(*fd);
38}
39
40static inline void close_pairp(int (*p)[2]) {
41 safe_close_pair(*p);
42}
43
44static inline void fclosep(FILE **f) {
45 safe_fclose(*f);
46}
47
fd421c4a
ZJS
48DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose, NULL);
49DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
3ffd4af2
LP
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
57int fd_nonblock(int fd, bool nonblock);
58int fd_cloexec(int fd, bool cloexec);
ed18c22c 59int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
3ffd4af2 60
73fc0cbc
LP
61int get_max_fd(void);
62
c85cb3bc 63int close_all_fds(const int except[], size_t n_except);
11966552 64int close_all_fds_without_malloc(const int except[], size_t n_except);
3ffd4af2
LP
65
66int same_fd(int a, int b);
67
68void cmsg_close_all(struct msghdr *mh);
4fee3975
LP
69
70bool fdname_is_valid(const char *s);
a1a3f73a 71
4aeb20f5
LP
72int fd_get_path(int fd, char **ret);
73
046a82c1
LP
74int move_fd(int from, int to, int cloexec);
75
a548e14d
LP
76enum {
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
7fe2903c 84int fd_move_above_stdio(int fd);
aa11e28b
LP
85
86int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
8bb2db73
LP
87
88static inline int make_null_stdio(void) {
89 return rearrange_stdio(-1, -1, -1);
90}
c10d6bdb
LP
91
92/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
93#define TAKE_FD(fd) \
94 ({ \
90d59676
LP
95 int *_fd_ = &(fd); \
96 int _ret_ = *_fd_; \
254d1313 97 *_fd_ = -EBADF; \
90d59676 98 _ret_; \
c10d6bdb 99 })
f2324783 100
0706c012 101/* Like free_and_replace(), but for file descriptors */
ee3455cf 102#define close_and_replace(a, b) \
0706c012
ZJS
103 ({ \
104 int *_fdp_ = &(a); \
105 safe_close(*_fdp_); \
106 *_fdp_ = TAKE_FD(b); \
107 0; \
108 })
109
f2324783 110int fd_reopen(int fd, int flags);
5f5865f0 111int fd_reopen_condition(int fd, int flags, int mask, int *ret_new_fd);
9264cc39 112int read_nr_open(void);
7e93a658 113int fd_get_diskseq(int fd, uint64_t *ret);
6e1e4b59
LP
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
119static 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))