Fix: relayd: hostname check is too restrictive
[lttng-tools.git] / src / common / compat / compat-epoll.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <stdbool.h>
27
28 #include <common/error.h>
29 #include <common/defaults.h>
30 #include <common/macros.h>
31 #include <common/utils.h>
32
33 #include "poll.h"
34
35 /*
36 * Maximum number of fd we can monitor.
37 *
38 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
39 * be used for the maximum size of the poll set. If this interface is not
40 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
41 * of the available low memory divided by the registration cost in bytes which
42 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
43 *
44 */
45 static unsigned int poll_max_size;
46
47 /*
48 * Resize the epoll events structure of the new size.
49 *
50 * Return 0 on success or else -1 with the current events pointer untouched.
51 */
52 static int resize_poll_event(struct lttng_poll_event *events,
53 uint32_t new_size)
54 {
55 struct epoll_event *ptr;
56
57 assert(events);
58
59 ptr = realloc(events->events, new_size * sizeof(*ptr));
60 if (ptr == NULL) {
61 PERROR("realloc epoll add");
62 goto error;
63 }
64 if (new_size > events->alloc_size) {
65 /* Zero newly allocated memory */
66 memset(ptr + events->alloc_size, 0,
67 (new_size - events->alloc_size) * sizeof(*ptr));
68 }
69 events->events = ptr;
70 events->alloc_size = new_size;
71
72 return 0;
73
74 error:
75 return -1;
76 }
77
78 /*
79 * Create epoll set and allocate returned events structure.
80 */
81 LTTNG_HIDDEN
82 int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
83 {
84 int ret;
85
86 if (events == NULL || size <= 0) {
87 goto error;
88 }
89
90 if (!poll_max_size) {
91 if (lttng_poll_set_max_size()) {
92 goto error;
93 }
94 }
95
96 /* Don't bust the limit here */
97 if (size > poll_max_size) {
98 size = poll_max_size;
99 }
100
101 ret = compat_glibc_epoll_create(size, flags);
102 if (ret < 0) {
103 /* At this point, every error is fatal */
104 PERROR("epoll_create1");
105 goto error;
106 }
107
108 events->epfd = ret;
109
110 /* This *must* be freed by using lttng_poll_free() */
111 events->events = zmalloc(size * sizeof(struct epoll_event));
112 if (events->events == NULL) {
113 PERROR("zmalloc epoll set");
114 goto error_close;
115 }
116
117 events->alloc_size = events->init_size = size;
118 events->nb_fd = 0;
119
120 return 0;
121
122 error_close:
123 ret = close(events->epfd);
124 if (ret) {
125 PERROR("close");
126 }
127 error:
128 return -1;
129 }
130
131 /*
132 * Add a fd to the epoll set with requesting events.
133 */
134 LTTNG_HIDDEN
135 int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events)
136 {
137 int ret;
138 struct epoll_event ev;
139
140 if (events == NULL || events->events == NULL || fd < 0) {
141 ERR("Bad compat epoll add arguments");
142 goto error;
143 }
144
145 /*
146 * Zero struct epoll_event to ensure all representations of its
147 * union are zeroed.
148 */
149 memset(&ev, 0, sizeof(ev));
150 ev.events = req_events;
151 ev.data.fd = fd;
152
153 ret = epoll_ctl(events->epfd, EPOLL_CTL_ADD, fd, &ev);
154 if (ret < 0) {
155 switch (errno) {
156 case EEXIST:
157 /* If exist, it's OK. */
158 goto end;
159 case ENOSPC:
160 case EPERM:
161 /* Print PERROR and goto end not failing. Show must go on. */
162 PERROR("epoll_ctl ADD");
163 goto end;
164 default:
165 PERROR("epoll_ctl ADD fatal");
166 goto error;
167 }
168 }
169
170 events->nb_fd++;
171
172 end:
173 return 0;
174
175 error:
176 return -1;
177 }
178
179 /*
180 * Remove a fd from the epoll set.
181 */
182 LTTNG_HIDDEN
183 int compat_epoll_del(struct lttng_poll_event *events, int fd)
184 {
185 int ret;
186
187 if (events == NULL || fd < 0 || events->nb_fd == 0) {
188 goto error;
189 }
190
191 ret = epoll_ctl(events->epfd, EPOLL_CTL_DEL, fd, NULL);
192 if (ret < 0) {
193 switch (errno) {
194 case ENOENT:
195 case EPERM:
196 /* Print PERROR and goto end not failing. Show must go on. */
197 PERROR("epoll_ctl DEL");
198 goto end;
199 default:
200 PERROR("epoll_ctl DEL fatal");
201 goto error;
202 }
203 }
204
205 events->nb_fd--;
206
207 end:
208 return 0;
209
210 error:
211 return -1;
212 }
213
214 /*
215 * Set an fd's events.
216 */
217 LTTNG_HIDDEN
218 int compat_epoll_mod(struct lttng_poll_event *events, int fd, uint32_t req_events)
219 {
220 int ret;
221 struct epoll_event ev;
222
223 if (events == NULL || fd < 0 || events->nb_fd == 0) {
224 goto error;
225 }
226
227 /*
228 * Zero struct epoll_event to ensure all representations of its
229 * union are zeroed.
230 */
231 memset(&ev, 0, sizeof(ev));
232 ev.events = req_events;
233 ev.data.fd = fd;
234
235 ret = epoll_ctl(events->epfd, EPOLL_CTL_MOD, fd, &ev);
236 if (ret < 0) {
237 switch (errno) {
238 case ENOENT:
239 case EPERM:
240 /* Print PERROR and goto end not failing. Show must go on. */
241 PERROR("epoll_ctl MOD");
242 goto end;
243 default:
244 PERROR("epoll_ctl MOD fatal");
245 goto error;
246 }
247 }
248
249 end:
250 return 0;
251
252 error:
253 return -1;
254 }
255
256 /*
257 * Wait on epoll set. This is a blocking call of timeout value.
258 */
259 LTTNG_HIDDEN
260 int compat_epoll_wait(struct lttng_poll_event *events, int timeout,
261 bool interruptible)
262 {
263 int ret;
264 uint32_t new_size;
265
266 if (events == NULL || events->events == NULL) {
267 ERR("Wrong arguments in compat_epoll_wait");
268 goto error;
269 }
270
271 if (events->nb_fd == 0) {
272 errno = EINVAL;
273 return -1;
274 }
275
276 /*
277 * Resize if needed before waiting. We could either expand the array or
278 * shrink it down. It's important to note that after this step, we are
279 * ensured that the events argument of the epoll_wait call will be large
280 * enough to hold every possible returned events.
281 */
282 new_size = 1U << utils_get_count_order_u32(events->nb_fd);
283 if (new_size != events->alloc_size && new_size >= events->init_size) {
284 ret = resize_poll_event(events, new_size);
285 if (ret < 0) {
286 /* ENOMEM problem at this point. */
287 goto error;
288 }
289 }
290
291 do {
292 ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout);
293 } while (!interruptible && ret == -1 && errno == EINTR);
294 if (ret < 0) {
295 if (errno != EINTR) {
296 PERROR("epoll_wait");
297 }
298 goto error;
299 }
300
301 /*
302 * Since the returned events are set sequentially in the "events" structure
303 * we only need to return the epoll_wait value and iterate over it.
304 */
305 return ret;
306
307 error:
308 return -1;
309 }
310
311 /*
312 * Setup poll set maximum size.
313 */
314 LTTNG_HIDDEN
315 int compat_epoll_set_max_size(void)
316 {
317 int ret, fd, retval = 0;
318 ssize_t size_ret;
319 char buf[64];
320
321 fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY);
322 if (fd < 0) {
323 /*
324 * Failing on opening [1] is not an error per see. [1] was
325 * introduced in Linux 2.6.28 but epoll is available since
326 * 2.5.44. Hence, goto end and set a default value without
327 * setting an error return value.
328 *
329 * [1] /proc/sys/fs/epoll/max_user_watches
330 */
331 retval = 0;
332 goto end;
333 }
334
335 size_ret = lttng_read(fd, buf, sizeof(buf));
336 /*
337 * Allow reading a file smaller than buf, but keep space for
338 * final \0.
339 */
340 if (size_ret < 0 || size_ret >= sizeof(buf)) {
341 PERROR("read set max size");
342 retval = -1;
343 goto end_read;
344 }
345 buf[size_ret] = '\0';
346 poll_max_size = atoi(buf);
347 end_read:
348 ret = close(fd);
349 if (ret) {
350 PERROR("close");
351 }
352 end:
353 if (!poll_max_size) {
354 poll_max_size = DEFAULT_POLL_SIZE;
355 }
356 DBG("epoll set max size is %d", poll_max_size);
357 return retval;
358 }
This page took 0.038128 seconds and 5 git commands to generate.