Fix: various compat poll/epoll issues
[lttng-tools.git] / src / common / compat / compat-epoll.c
CommitLineData
5eb91c98
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
5eb91c98
DG
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 *
d14d33bf
AM
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.
5eb91c98
DG
16 */
17
4c462e79 18#define _GNU_SOURCE
d21b0d71 19#include <assert.h>
5eb91c98
DG
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>
3bd1e081 26#include <config.h>
5eb91c98 27
db758600 28#include <common/error.h>
990570ed 29#include <common/defaults.h>
cfa9a5a2
DG
30#include <common/macros.h>
31#include <common/utils.h>
5eb91c98
DG
32
33#include "poll.h"
34
35unsigned int poll_max_size;
36
d21b0d71
DG
37/*
38 * Resize the epoll events structure of the new size.
39 *
40 * Return 0 on success or else -1 with the current events pointer untouched.
41 */
42static int resize_poll_event(struct lttng_poll_event *events,
43 uint32_t new_size)
44{
45 struct epoll_event *ptr;
46
47 assert(events);
48
49 ptr = realloc(events->events, new_size * sizeof(*ptr));
50 if (ptr == NULL) {
51 PERROR("realloc epoll add");
52 goto error;
53 }
53efb85a
MD
54 if (new_size > events->alloc_size) {
55 /* Zero newly allocated memory */
56 memset(ptr + events->alloc_size, 0,
57 (new_size - events->alloc_size) * sizeof(*ptr));
58 }
d21b0d71
DG
59 events->events = ptr;
60 events->alloc_size = new_size;
61
62 return 0;
63
64error:
65 return -1;
66}
67
5eb91c98
DG
68/*
69 * Create epoll set and allocate returned events structure.
70 */
71int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
72{
73 int ret;
74
75 if (events == NULL || size <= 0) {
76 goto error;
77 }
78
2889fda9
MD
79 if (!poll_max_size) {
80 ERR("poll_max_size not initialized yet");
81 goto error;
82 }
83
5eb91c98 84 /* Don't bust the limit here */
2889fda9 85 if (size > poll_max_size) {
5eb91c98
DG
86 size = poll_max_size;
87 }
88
f263b7fd 89 ret = compat_glibc_epoll_create(size, flags);
5eb91c98
DG
90 if (ret < 0) {
91 /* At this point, every error is fatal */
4c462e79 92 PERROR("epoll_create1");
5eb91c98
DG
93 goto error;
94 }
95
96 events->epfd = ret;
97
98 /* This *must* be freed by using lttng_poll_free() */
99 events->events = zmalloc(size * sizeof(struct epoll_event));
100 if (events->events == NULL) {
4c462e79 101 PERROR("zmalloc epoll set");
5eb91c98
DG
102 goto error_close;
103 }
104
d21b0d71 105 events->alloc_size = events->init_size = size;
5eb91c98
DG
106 events->nb_fd = 0;
107
108 return 0;
109
110error_close:
4c462e79
MD
111 ret = close(events->epfd);
112 if (ret) {
113 PERROR("close");
114 }
5eb91c98
DG
115error:
116 return -1;
117}
118
119/*
120 * Add a fd to the epoll set with requesting events.
121 */
122int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events)
123{
d21b0d71
DG
124 int ret;
125 struct epoll_event ev;
5eb91c98
DG
126
127 if (events == NULL || events->events == NULL || fd < 0) {
128 ERR("Bad compat epoll add arguments");
129 goto error;
130 }
131
53efb85a
MD
132 /*
133 * Zero struct epoll_event to ensure all representations of its
134 * union are zeroed.
135 */
136 memset(&ev, 0, sizeof(ev));
5eb91c98
DG
137 ev.events = req_events;
138 ev.data.fd = fd;
139
140 ret = epoll_ctl(events->epfd, EPOLL_CTL_ADD, fd, &ev);
141 if (ret < 0) {
142 switch (errno) {
143 case EEXIST:
b7a6b49f
DG
144 /* If exist, it's OK. */
145 goto end;
5eb91c98
DG
146 case ENOSPC:
147 case EPERM:
4c462e79
MD
148 /* Print PERROR and goto end not failing. Show must go on. */
149 PERROR("epoll_ctl ADD");
5eb91c98
DG
150 goto end;
151 default:
4c462e79 152 PERROR("epoll_ctl ADD fatal");
5eb91c98
DG
153 goto error;
154 }
155 }
156
157 events->nb_fd++;
158
5eb91c98
DG
159end:
160 return 0;
161
162error:
163 return -1;
164}
165
166/*
167 * Remove a fd from the epoll set.
168 */
169int compat_epoll_del(struct lttng_poll_event *events, int fd)
170{
171 int ret;
172
2889fda9 173 if (events == NULL || fd < 0 || events->nb_fd == 0) {
5eb91c98
DG
174 goto error;
175 }
176
177 ret = epoll_ctl(events->epfd, EPOLL_CTL_DEL, fd, NULL);
178 if (ret < 0) {
179 switch (errno) {
180 case ENOENT:
181 case EPERM:
4c462e79
MD
182 /* Print PERROR and goto end not failing. Show must go on. */
183 PERROR("epoll_ctl DEL");
5eb91c98
DG
184 goto end;
185 default:
4c462e79 186 PERROR("epoll_ctl DEL fatal");
5eb91c98
DG
187 goto error;
188 }
5eb91c98
DG
189 }
190
191 events->nb_fd--;
192
193end:
194 return 0;
195
196error:
197 return -1;
198}
199
200/*
201 * Wait on epoll set. This is a blocking call of timeout value.
202 */
203int compat_epoll_wait(struct lttng_poll_event *events, int timeout)
204{
205 int ret;
d21b0d71 206 uint32_t new_size;
5eb91c98 207
d21b0d71 208 if (events == NULL || events->events == NULL) {
5eb91c98
DG
209 ERR("Wrong arguments in compat_epoll_wait");
210 goto error;
211 }
2889fda9
MD
212 assert(events->nb_fd >= 0);
213
214 if (events->nb_fd == 0) {
215 errno = EINVAL;
216 return -1;
217 }
5eb91c98 218
d21b0d71
DG
219 /*
220 * Resize if needed before waiting. We could either expand the array or
221 * shrink it down. It's important to note that after this step, we are
222 * ensured that the events argument of the epoll_wait call will be large
223 * enough to hold every possible returned events.
224 */
2889fda9
MD
225 new_size = 1U << utils_get_count_order_u32(events->nb_fd);
226 if (new_size != events->alloc_size && new_size >= events->init_size) {
d21b0d71
DG
227 ret = resize_poll_event(events, new_size);
228 if (ret < 0) {
229 /* ENOMEM problem at this point. */
230 goto error;
231 }
232 }
233
3ada8405
DG
234 do {
235 ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout);
236 } while (ret == -1 && errno == EINTR);
5eb91c98
DG
237 if (ret < 0) {
238 /* At this point, every error is fatal */
4c462e79 239 PERROR("epoll_wait");
5eb91c98
DG
240 goto error;
241 }
242
9ddba525
DG
243 /*
244 * Since the returned events are set sequentially in the "events" structure
245 * we only need to return the epoll_wait value and iterate over it.
246 */
5eb91c98
DG
247 return ret;
248
249error:
250 return -1;
251}
252
253/*
254 * Setup poll set maximum size.
255 */
2889fda9 256int compat_epoll_set_max_size(void)
5eb91c98 257{
2889fda9 258 int ret, fd, retval = 0;
13021756 259 ssize_t size_ret;
5eb91c98
DG
260 char buf[64];
261
990570ed 262 fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY);
5eb91c98 263 if (fd < 0) {
2889fda9
MD
264 retval = -1;
265 goto end;
5eb91c98
DG
266 }
267
6cd525e8
MD
268 size_ret = lttng_read(fd, buf, sizeof(buf));
269 /*
270 * Allow reading a file smaller than buf, but keep space for
271 * final \0.
272 */
273 if (size_ret < 0 || size_ret >= sizeof(buf)) {
4c462e79 274 PERROR("read set max size");
2889fda9
MD
275 retval = -1;
276 goto end_read;
5eb91c98 277 }
6cd525e8 278 buf[size_ret] = '\0';
5eb91c98 279 poll_max_size = atoi(buf);
2889fda9 280end_read:
4c462e79
MD
281 ret = close(fd);
282 if (ret) {
283 PERROR("close");
284 }
2889fda9
MD
285end:
286 if (!poll_max_size) {
287 poll_max_size = DEFAULT_POLL_SIZE;
288 }
289 DBG("epoll set max size is %d", poll_max_size);
290 return retval;
5eb91c98 291}
This page took 0.047371 seconds and 4 git commands to generate.