]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fd-util.h
Merge pull request #5276 from poettering/resolved-cname
[thirdparty/systemd.git] / src / basic / fd-util.h
CommitLineData
3ffd4af2
LP
1#pragma once
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
3ffd4af2
LP
22#include <dirent.h>
23#include <stdbool.h>
71d35b6b 24#include <stdio.h>
3ffd4af2
LP
25#include <sys/socket.h>
26
27#include "macro.h"
28
23e096cc
LP
29/* Make sure we can distinguish fd 0 and NULL */
30#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
31#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
32
3ffd4af2
LP
33int close_nointr(int fd);
34int safe_close(int fd);
35void safe_close_pair(int p[]);
36
37void close_many(const int fds[], unsigned n_fd);
38
39int fclose_nointr(FILE *f);
40FILE* safe_fclose(FILE *f);
41DIR* safe_closedir(DIR *f);
42
43static inline void closep(int *fd) {
44 safe_close(*fd);
45}
46
47static inline void close_pairp(int (*p)[2]) {
48 safe_close_pair(*p);
49}
50
51static inline void fclosep(FILE **f) {
52 safe_fclose(*f);
53}
54
55DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
56DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
57
58#define _cleanup_close_ _cleanup_(closep)
59#define _cleanup_fclose_ _cleanup_(fclosep)
60#define _cleanup_pclose_ _cleanup_(pclosep)
61#define _cleanup_closedir_ _cleanup_(closedirp)
62#define _cleanup_close_pair_ _cleanup_(close_pairp)
63
64int fd_nonblock(int fd, bool nonblock);
65int fd_cloexec(int fd, bool cloexec);
3b9a1d87 66void stdio_unset_cloexec(void);
3ffd4af2
LP
67
68int close_all_fds(const int except[], unsigned n_except);
69
70int same_fd(int a, int b);
71
72void cmsg_close_all(struct msghdr *mh);
4fee3975
LP
73
74bool fdname_is_valid(const char *s);
a1a3f73a 75
4aeb20f5
LP
76int fd_get_path(int fd, char **ret);
77
0791110f 78/* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
a1a3f73a 79#define ERRNO_IS_DISCONNECT(r) \
0791110f 80 IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)