]>
Commit | Line | Data |
---|---|---|
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 |
33 | int close_nointr(int fd); |
34 | int safe_close(int fd); | |
35 | void safe_close_pair(int p[]); | |
36 | ||
37 | void close_many(const int fds[], unsigned n_fd); | |
38 | ||
39 | int fclose_nointr(FILE *f); | |
40 | FILE* safe_fclose(FILE *f); | |
41 | DIR* safe_closedir(DIR *f); | |
42 | ||
43 | static inline void closep(int *fd) { | |
44 | safe_close(*fd); | |
45 | } | |
46 | ||
47 | static inline void close_pairp(int (*p)[2]) { | |
48 | safe_close_pair(*p); | |
49 | } | |
50 | ||
51 | static inline void fclosep(FILE **f) { | |
52 | safe_fclose(*f); | |
53 | } | |
54 | ||
55 | DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose); | |
56 | DEFINE_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 | ||
64 | int fd_nonblock(int fd, bool nonblock); | |
65 | int fd_cloexec(int fd, bool cloexec); | |
3b9a1d87 | 66 | void stdio_unset_cloexec(void); |
3ffd4af2 LP |
67 | |
68 | int close_all_fds(const int except[], unsigned n_except); | |
69 | ||
70 | int same_fd(int a, int b); | |
71 | ||
72 | void cmsg_close_all(struct msghdr *mh); | |
4fee3975 LP |
73 | |
74 | bool fdname_is_valid(const char *s); | |
a1a3f73a | 75 | |
4aeb20f5 LP |
76 | int 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) |