]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fd-util.h
tree-wide: remove unused variables (#8612)
[thirdparty/systemd.git] / src / basic / fd-util.h
CommitLineData
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
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
3ffd4af2
LP
23#include <dirent.h>
24#include <stdbool.h>
71d35b6b 25#include <stdio.h>
3ffd4af2
LP
26#include <sys/socket.h>
27
28#include "macro.h"
29
23e096cc
LP
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
3ffd4af2
LP
34int close_nointr(int fd);
35int safe_close(int fd);
36void safe_close_pair(int p[]);
37
e7685a77
LP
38static 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
3ffd4af2
LP
45void close_many(const int fds[], unsigned n_fd);
46
47int fclose_nointr(FILE *f);
48FILE* safe_fclose(FILE *f);
49DIR* safe_closedir(DIR *f);
50
51static inline void closep(int *fd) {
52 safe_close(*fd);
53}
54
55static inline void close_pairp(int (*p)[2]) {
56 safe_close_pair(*p);
57}
58
59static inline void fclosep(FILE **f) {
60 safe_fclose(*f);
61}
62
63DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
64DEFINE_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
72int fd_nonblock(int fd, bool nonblock);
73int fd_cloexec(int fd, bool cloexec);
74
75int close_all_fds(const int except[], unsigned n_except);
76
77int same_fd(int a, int b);
78
79void cmsg_close_all(struct msghdr *mh);
4fee3975
LP
80
81bool fdname_is_valid(const char *s);
a1a3f73a 82
4aeb20f5
LP
83int fd_get_path(int fd, char **ret);
84
046a82c1
LP
85int move_fd(int from, int to, int cloexec);
86
a548e14d
LP
87enum {
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
95int acquire_data_fd(const void *data, size_t size, unsigned flags);
96
0791110f 97/* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
a1a3f73a 98#define ERRNO_IS_DISCONNECT(r) \
0791110f 99 IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)
7fe2903c
LP
100
101int fd_move_above_stdio(int fd);
aa11e28b
LP
102
103int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
8bb2db73
LP
104
105static inline int make_null_stdio(void) {
106 return rearrange_stdio(-1, -1, -1);
107}
c10d6bdb
LP
108
109/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
110#define TAKE_FD(fd) \
111 ({ \
112 int _fd_ = (fd); \
113 (fd) = -1; \
114 _fd_; \
115 })