/src/systemd/src/basic/fd-util.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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 | | |
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 | | |
15 | | int close_nointr(int fd); |
16 | | int safe_close(int fd); |
17 | | void safe_close_pair(int p[static 2]); |
18 | | |
19 | 0 | static inline int safe_close_above_stdio(int fd) { |
20 | 0 | if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */ |
21 | 0 | return -1; |
22 | 0 |
|
23 | 0 | return safe_close(fd); |
24 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:safe_close_above_stdio Unexecuted instantiation: link-config.c:safe_close_above_stdio Unexecuted instantiation: sd-dhcp-lease.c:safe_close_above_stdio |
25 | | |
26 | | void close_many(const int fds[], size_t n_fd); |
27 | | |
28 | | int fclose_nointr(FILE *f); |
29 | | FILE* safe_fclose(FILE *f); |
30 | | DIR* safe_closedir(DIR *f); |
31 | | |
32 | 0 | static inline void closep(int *fd) { |
33 | 0 | safe_close(*fd); |
34 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:closep Unexecuted instantiation: link-config.c:closep Unexecuted instantiation: sd-dhcp-lease.c:closep |
35 | | |
36 | 0 | static inline void close_pairp(int (*p)[2]) { |
37 | 0 | safe_close_pair(*p); |
38 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:close_pairp Unexecuted instantiation: link-config.c:close_pairp Unexecuted instantiation: sd-dhcp-lease.c:close_pairp |
39 | | |
40 | 45.7k | static inline void fclosep(FILE **f) { |
41 | 45.7k | safe_fclose(*f); |
42 | 45.7k | } fuzz-link-parser.c:fclosep Line | Count | Source | 40 | 22.8k | static inline void fclosep(FILE **f) { | 41 | 22.8k | safe_fclose(*f); | 42 | 22.8k | } |
Line | Count | Source | 40 | 22.8k | static inline void fclosep(FILE **f) { | 41 | 22.8k | safe_fclose(*f); | 42 | 22.8k | } |
Unexecuted instantiation: sd-dhcp-lease.c:fclosep |
43 | | |
44 | | DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose); |
45 | | DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir); |
46 | | |
47 | | #define _cleanup_close_ _cleanup_(closep) |
48 | 45.7k | #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 | | |
53 | | int fd_nonblock(int fd, bool nonblock); |
54 | | int fd_cloexec(int fd, bool cloexec); |
55 | | |
56 | | int close_all_fds(const int except[], size_t n_except); |
57 | | |
58 | | int same_fd(int a, int b); |
59 | | |
60 | | void cmsg_close_all(struct msghdr *mh); |
61 | | |
62 | | bool fdname_is_valid(const char *s); |
63 | | |
64 | | int fd_get_path(int fd, char **ret); |
65 | | |
66 | | int move_fd(int from, int to, int cloexec); |
67 | | |
68 | | enum { |
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 | | |
76 | | int acquire_data_fd(const void *data, size_t size, unsigned flags); |
77 | | |
78 | | int fd_duplicate_data_fd(int fd); |
79 | | |
80 | | int fd_move_above_stdio(int fd); |
81 | | |
82 | | int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd); |
83 | | |
84 | 0 | static inline int make_null_stdio(void) { |
85 | 0 | return rearrange_stdio(-1, -1, -1); |
86 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:make_null_stdio Unexecuted instantiation: link-config.c:make_null_stdio Unexecuted instantiation: sd-dhcp-lease.c:make_null_stdio |
87 | | |
88 | | /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */ |
89 | | #define TAKE_FD(fd) \ |
90 | | ({ \ |
91 | | int _fd_ = (fd); \ |
92 | | (fd) = -1; \ |
93 | | _fd_; \ |
94 | | }) |
95 | | |
96 | | int fd_reopen(int fd, int flags); |
97 | | |
98 | | int read_nr_open(void); |