]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
Merge pull request #8617 from keszybz/tmpfiles-relax
[thirdparty/systemd.git] / src / basic / fd-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <dirent.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <sys/socket.h>
27
28 #include "macro.h"
29
30 /* Make sure we can distinguish fd 0 and NULL */
31 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
32 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
33
34 int close_nointr(int fd);
35 int safe_close(int fd);
36 void safe_close_pair(int p[]);
37
38 static inline int safe_close_above_stdio(int fd) {
39 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */
40 return -1;
41
42 return safe_close(fd);
43 }
44
45 void close_many(const int fds[], unsigned n_fd);
46
47 int fclose_nointr(FILE *f);
48 FILE* safe_fclose(FILE *f);
49 DIR* safe_closedir(DIR *f);
50
51 static inline void closep(int *fd) {
52 safe_close(*fd);
53 }
54
55 static inline void close_pairp(int (*p)[2]) {
56 safe_close_pair(*p);
57 }
58
59 static inline void fclosep(FILE **f) {
60 safe_fclose(*f);
61 }
62
63 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
64 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
65
66 #define _cleanup_close_ _cleanup_(closep)
67 #define _cleanup_fclose_ _cleanup_(fclosep)
68 #define _cleanup_pclose_ _cleanup_(pclosep)
69 #define _cleanup_closedir_ _cleanup_(closedirp)
70 #define _cleanup_close_pair_ _cleanup_(close_pairp)
71
72 int fd_nonblock(int fd, bool nonblock);
73 int fd_cloexec(int fd, bool cloexec);
74
75 int close_all_fds(const int except[], unsigned n_except);
76
77 int same_fd(int a, int b);
78
79 void cmsg_close_all(struct msghdr *mh);
80
81 bool fdname_is_valid(const char *s);
82
83 int fd_get_path(int fd, char **ret);
84
85 int move_fd(int from, int to, int cloexec);
86
87 enum {
88 ACQUIRE_NO_DEV_NULL = 1 << 0,
89 ACQUIRE_NO_MEMFD = 1 << 1,
90 ACQUIRE_NO_PIPE = 1 << 2,
91 ACQUIRE_NO_TMPFILE = 1 << 3,
92 ACQUIRE_NO_REGULAR = 1 << 4,
93 };
94
95 int acquire_data_fd(const void *data, size_t size, unsigned flags);
96
97 /* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
98 #define ERRNO_IS_DISCONNECT(r) \
99 IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)
100
101 /* Resource exhaustion, could be our fault or general system trouble */
102 #define ERRNO_IS_RESOURCE(r) \
103 IN_SET(r, ENOMEM, EMFILE, ENFILE)
104
105 int fd_move_above_stdio(int fd);
106
107 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
108
109 static inline int make_null_stdio(void) {
110 return rearrange_stdio(-1, -1, -1);
111 }
112
113 /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
114 #define TAKE_FD(fd) \
115 ({ \
116 int _fd_ = (fd); \
117 (fd) = -1; \
118 _fd_; \
119 })
120
121 int fd_reopen(int fd, int flags);