chiark / gitweb /
8bc6c01280d730ce2947a90536c19a30fc5481c3
[elogind.git] / src / basic / fd-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <dirent.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <sys/socket.h>
12
13 #include "macro.h"
14
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
19 int close_nointr(int fd);
20 int safe_close(int fd);
21 void safe_close_pair(int p[]);
22
23 static 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
30 void close_many(const int fds[], size_t n_fd);
31
32 int fclose_nointr(FILE *f);
33 FILE* safe_fclose(FILE *f);
34 #if 0 /// UNNEEDED by elogind
35 DIR* safe_closedir(DIR *f);
36 #endif // 0
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
62 int close_all_fds(const int except[], size_t n_except);
63
64 #if 0 /// UNNEEDED by elogind
65 int same_fd(int a, int b);
66
67 void cmsg_close_all(struct msghdr *mh);
68
69 bool fdname_is_valid(const char *s);
70 #endif // 0
71
72 int fd_get_path(int fd, char **ret);
73
74 int move_fd(int from, int to, int cloexec);
75
76 enum {
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
84 int acquire_data_fd(const void *data, size_t size, unsigned flags);
85
86 int fd_duplicate_data_fd(int fd);
87
88 /* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
89 #define ERRNO_IS_DISCONNECT(r) \
90         IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)
91
92 /* Resource exhaustion, could be our fault or general system trouble */
93 #define ERRNO_IS_RESOURCE(r) \
94         IN_SET(r, ENOMEM, EMFILE, ENFILE)
95
96 int fd_move_above_stdio(int fd);
97
98 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
99
100 static inline int make_null_stdio(void) {
101         return rearrange_stdio(-1, -1, -1);
102 }
103
104 /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
105 #define TAKE_FD(fd)                             \
106         ({                                      \
107                 int _fd_ = (fd);                \
108                 (fd) = -1;                      \
109                 _fd_;                           \
110         })
111
112 int fd_reopen(int fd, int flags);
113
114 int read_nr_open(void);