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