]>
Commit | Line | Data |
---|---|---|
a16e1123 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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 General Public License as published by | |
10 | the Free Software Foundation; either version 2 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 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
23 | #include <dirent.h> | |
24 | #include <fcntl.h> | |
25 | #include <unistd.h> | |
26 | ||
27 | #include "set.h" | |
28 | #include "util.h" | |
29 | #include "macro.h" | |
30 | #include "fdset.h" | |
31 | ||
32 | #define MAKE_SET(s) ((Set*) s) | |
33 | #define MAKE_FDSET(s) ((FDSet*) s) | |
34 | ||
35 | /* Make sure we can distuingish fd 0 and NULL */ | |
36 | #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1) | |
37 | #define PTR_TO_FD(p) (PTR_TO_INT(p)-1) | |
38 | ||
39 | FDSet *fdset_new(void) { | |
40 | return MAKE_FDSET(set_new(trivial_hash_func, trivial_compare_func)); | |
41 | } | |
42 | ||
43 | void fdset_free(FDSet *s) { | |
44 | void *p; | |
45 | ||
46 | while ((p = set_steal_first(MAKE_SET(s)))) { | |
47 | /* Valgrind's fd might have ended up in this set here, | |
48 | * due to fdset_new_fill(). We'll ignore all failures | |
49 | * here, so that the EBADFD that valgrind will return | |
50 | * us on close() doesn't influence us */ | |
51 | ||
52 | log_warning("Closing left-over fd %i", PTR_TO_FD(p)); | |
53 | close_nointr(PTR_TO_FD(p)); | |
54 | } | |
55 | ||
56 | set_free(MAKE_SET(s)); | |
57 | } | |
58 | ||
59 | int fdset_put(FDSet *s, int fd) { | |
60 | assert(s); | |
61 | assert(fd >= 0); | |
62 | ||
63 | return set_put(MAKE_SET(s), FD_TO_PTR(fd)); | |
64 | } | |
65 | ||
66 | int fdset_put_dup(FDSet *s, int fd) { | |
67 | int copy, r; | |
68 | ||
69 | assert(s); | |
70 | assert(fd >= 0); | |
71 | ||
72 | if ((copy = fcntl(fd, F_DUPFD_CLOEXEC, 3)) < 0) | |
73 | return -errno; | |
74 | ||
75 | if ((r = fdset_put(s, copy)) < 0) { | |
76 | close_nointr_nofail(copy); | |
77 | return r; | |
78 | } | |
79 | ||
80 | return copy; | |
81 | } | |
82 | ||
83 | bool fdset_contains(FDSet *s, int fd) { | |
84 | assert(s); | |
85 | assert(fd >= 0); | |
86 | ||
87 | return !!set_get(MAKE_SET(s), FD_TO_PTR(fd)); | |
88 | } | |
89 | ||
90 | int fdset_remove(FDSet *s, int fd) { | |
91 | assert(s); | |
92 | assert(fd >= 0); | |
93 | ||
94 | return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT; | |
95 | } | |
96 | ||
97 | int fdset_new_fill(FDSet **_s) { | |
98 | DIR *d; | |
99 | struct dirent *de; | |
100 | int r = 0; | |
101 | FDSet *s; | |
102 | ||
103 | assert(_s); | |
104 | ||
105 | /* Creates an fdsets and fills in all currently open file | |
106 | * descriptors. */ | |
107 | ||
108 | if (!(d = opendir("/proc/self/fd"))) | |
109 | return -errno; | |
110 | ||
111 | if (!(s = fdset_new())) { | |
112 | r = -ENOMEM; | |
113 | goto finish; | |
114 | } | |
115 | ||
116 | while ((de = readdir(d))) { | |
117 | int fd = -1; | |
118 | ||
119 | if (ignore_file(de->d_name)) | |
120 | continue; | |
121 | ||
122 | if ((r = safe_atoi(de->d_name, &fd)) < 0) | |
123 | goto finish; | |
124 | ||
125 | if (fd < 3) | |
126 | continue; | |
127 | ||
128 | if (fd == dirfd(d)) | |
129 | continue; | |
130 | ||
131 | if ((r = fdset_put(s, fd)) < 0) | |
132 | goto finish; | |
133 | } | |
134 | ||
135 | r = 0; | |
136 | *_s = s; | |
137 | s = NULL; | |
138 | ||
139 | finish: | |
140 | closedir(d); | |
141 | ||
142 | /* We won't close the fds here! */ | |
143 | if (s) | |
144 | set_free(MAKE_SET(s)); | |
145 | ||
146 | return r; | |
147 | ||
148 | } | |
149 | ||
150 | int fdset_cloexec(FDSet *fds, bool b) { | |
151 | Iterator i; | |
152 | void *p; | |
153 | int r; | |
154 | ||
155 | assert(fds); | |
156 | ||
157 | SET_FOREACH(p, MAKE_SET(fds), i) | |
158 | if ((r = fd_cloexec(PTR_TO_FD(p), b)) < 0) | |
159 | return r; | |
160 | ||
161 | return 0; | |
162 | } |