]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fd-util.h
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / basic / fd-util.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
10
23e096cc
LP
11/* Make sure we can distinguish fd 0 and NULL */
12#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
13#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
14
3ffd4af2
LP
15int close_nointr(int fd);
16int safe_close(int fd);
17void safe_close_pair(int p[]);
18
e7685a77
LP
19static inline int safe_close_above_stdio(int fd) {
20 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */
21 return -1;
22
23 return safe_close(fd);
24}
25
da6053d0 26void close_many(const int fds[], size_t n_fd);
3ffd4af2
LP
27
28int fclose_nointr(FILE *f);
29FILE* safe_fclose(FILE *f);
30DIR* safe_closedir(DIR *f);
31
32static inline void closep(int *fd) {
33 safe_close(*fd);
34}
35
36static inline void close_pairp(int (*p)[2]) {
37 safe_close_pair(*p);
38}
39
40static inline void fclosep(FILE **f) {
41 safe_fclose(*f);
42}
43
44DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
45DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
46
47#define _cleanup_close_ _cleanup_(closep)
48#define _cleanup_fclose_ _cleanup_(fclosep)
49#define _cleanup_pclose_ _cleanup_(pclosep)
50#define _cleanup_closedir_ _cleanup_(closedirp)
51#define _cleanup_close_pair_ _cleanup_(close_pairp)
52
53int fd_nonblock(int fd, bool nonblock);
54int fd_cloexec(int fd, bool cloexec);
55
da6053d0 56int close_all_fds(const int except[], size_t n_except);
3ffd4af2
LP
57
58int same_fd(int a, int b);
59
60void cmsg_close_all(struct msghdr *mh);
4fee3975
LP
61
62bool fdname_is_valid(const char *s);
a1a3f73a 63
4aeb20f5
LP
64int fd_get_path(int fd, char **ret);
65
046a82c1
LP
66int move_fd(int from, int to, int cloexec);
67
a548e14d
LP
68enum {
69 ACQUIRE_NO_DEV_NULL = 1 << 0,
70 ACQUIRE_NO_MEMFD = 1 << 1,
71 ACQUIRE_NO_PIPE = 1 << 2,
72 ACQUIRE_NO_TMPFILE = 1 << 3,
73 ACQUIRE_NO_REGULAR = 1 << 4,
74};
75
76int acquire_data_fd(const void *data, size_t size, unsigned flags);
77
4960ce43
LP
78int fd_duplicate_data_fd(int fd);
79
0791110f 80/* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
a1a3f73a 81#define ERRNO_IS_DISCONNECT(r) \
0791110f 82 IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)
7fe2903c 83
bb9947be
ZJS
84/* Resource exhaustion, could be our fault or general system trouble */
85#define ERRNO_IS_RESOURCE(r) \
86 IN_SET(r, ENOMEM, EMFILE, ENFILE)
87
7fe2903c 88int fd_move_above_stdio(int fd);
aa11e28b
LP
89
90int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
8bb2db73
LP
91
92static inline int make_null_stdio(void) {
93 return rearrange_stdio(-1, -1, -1);
94}
c10d6bdb
LP
95
96/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
97#define TAKE_FD(fd) \
98 ({ \
99 int _fd_ = (fd); \
100 (fd) = -1; \
101 _fd_; \
102 })
f2324783
LP
103
104int fd_reopen(int fd, int flags);
9264cc39
LP
105
106int read_nr_open(void);