Add the sessiond notification-handling subsystem
[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
27 #include <common/error.h>
28 #include <common/defaults.h>
29 #include <common/macros.h>
30 #include <common/utils.h>
31
32 #include "poll.h"
33
34 unsigned int poll_max_size;
35
36 /*
37 * Resize the epoll events structure of the new size.
38 *
39 * Return 0 on success or else -1 with the current events pointer untouched.
40 */
41 static int resize_poll_event(struct lttng_poll_event *events,
42 uint32_t new_size)
43 {
44 struct epoll_event *ptr;
45
46 assert(events);
47
48 ptr = realloc(events->events, new_size * sizeof(*ptr));
49 if (ptr == NULL) {
50 PERROR("realloc epoll add");
51 goto error;
52 }
53 if (new_size > events->alloc_size) {
54 /* Zero newly allocated memory */
55 memset(ptr + events->alloc_size, 0,
56 (new_size - events->alloc_size) * sizeof(*ptr));
57 }
58 events->events = ptr;
59 events->alloc_size = new_size;
60
61 return 0;
62
63 error:
64 return -1;
65 }
66
67 /*
68 * Create epoll set and allocate returned events structure.
69 */
70 int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
71 {
72 int ret;
73
74 if (events == NULL || size <= 0) {
75 goto error;
76 }
77
78 if (!poll_max_size) {
79 if (lttng_poll_set_max_size()) {
80 goto error;
81 }
82 }
83
84 /* Don't bust the limit here */
85 if (size > poll_max_size) {
86 size = poll_max_size;
87 }
88
89 ret = compat_glibc_epoll_create(size, flags);
90 if (ret < 0) {
91 /* At this point, every error is fatal */
92 PERROR("epoll_create1");
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) {
101 PERROR("zmalloc epoll set");
102 goto error_close;
103 }
104
105 events->alloc_size = events->init_size = size;
106 events->nb_fd = 0;
107
108 return 0;
109
110 error_close:
111 ret = close(events->epfd);
112 if (ret) {
113 PERROR("close");
114 }
115 error:
116 return -1;
117 }
118
119 /*
120 * Add a fd to the epoll set with requesting events.
121 */
122 int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events)
123 {
124 int ret;
125 struct epoll_event ev;
126
127 if (events == NULL || events->events == NULL || fd < 0) {
128 ERR("Bad compat epoll add arguments");
129 goto error;
130 }
131
132 /*
133 * Zero struct epoll_event to ensure all representations of its
134 * union are zeroed.
135 */
136 memset(&ev, 0, sizeof(ev));
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:
144 /* If exist, it's OK. */
145 goto end;
146 case ENOSPC:
147 case EPERM:
148 /* Print PERROR and goto end not failing. Show must go on. */
149 PERROR("epoll_ctl ADD");
150 goto end;
151 default:
152 PERROR("epoll_ctl ADD fatal");
153 goto error;
154 }
155 }
156
157 events->nb_fd++;
158
159 end:
160 return 0;
161
162 error:
163 return -1;
164 }
165
166 /*
167 * Remove a fd from the epoll set.
168 */
169 int compat_epoll_del(struct lttng_poll_event *events, int fd)
170 {
171 int ret;
172
173 if (events == NULL || fd < 0 || events->nb_fd == 0) {
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:
182 /* Print PERROR and goto end not failing. Show must go on. */
183 PERROR("epoll_ctl DEL");
184 goto end;
185 default:
186 PERROR("epoll_ctl DEL fatal");
187 goto error;
188 }
189 }
190
191 events->nb_fd--;
192
193 end:
194 return 0;
195
196 error:
197 return -1;
198 }
199
200 /*
201 * Set an fd's events.
202 */
203 int compat_epoll_mod(struct lttng_poll_event *events, int fd, uint32_t req_events)
204 {
205 int ret;
206 struct epoll_event ev;
207
208 if (events == NULL || fd < 0 || events->nb_fd == 0) {
209 goto error;
210 }
211
212 /*
213 * Zero struct epoll_event to ensure all representations of its
214 * union are zeroed.
215 */
216 memset(&ev, 0, sizeof(ev));
217 ev.events = req_events;
218 ev.data.fd = fd;
219
220 ret = epoll_ctl(events->epfd, EPOLL_CTL_MOD, fd, &ev);
221 if (ret < 0) {
222 switch (errno) {
223 case ENOENT:
224 case EPERM:
225 /* Print PERROR and goto end not failing. Show must go on. */
226 PERROR("epoll_ctl MOD");
227 goto end;
228 default:
229 PERROR("epoll_ctl MOD fatal");
230 goto error;
231 }
232 }
233
234 end:
235 return 0;
236
237 error:
238 return -1;
239 }
240
241 /*
242 * Wait on epoll set. This is a blocking call of timeout value.
243 */
244 int compat_epoll_wait(struct lttng_poll_event *events, int timeout)
245 {
246 int ret;
247 uint32_t new_size;
248
249 if (events == NULL || events->events == NULL) {
250 ERR("Wrong arguments in compat_epoll_wait");
251 goto error;
252 }
253
254 if (events->nb_fd == 0) {
255 errno = EINVAL;
256 return -1;
257 }
258
259 /*
260 * Resize if needed before waiting. We could either expand the array or
261 * shrink it down. It's important to note that after this step, we are
262 * ensured that the events argument of the epoll_wait call will be large
263 * enough to hold every possible returned events.
264 */
265 new_size = 1U << utils_get_count_order_u32(events->nb_fd);
266 if (new_size != events->alloc_size && new_size >= events->init_size) {
267 ret = resize_poll_event(events, new_size);
268 if (ret < 0) {
269 /* ENOMEM problem at this point. */
270 goto error;
271 }
272 }
273
274 do {
275 ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout);
276 } while (ret == -1 && errno == EINTR);
277 if (ret < 0) {
278 /* At this point, every error is fatal */
279 PERROR("epoll_wait");
280 goto error;
281 }
282
283 /*
284 * Since the returned events are set sequentially in the "events" structure
285 * we only need to return the epoll_wait value and iterate over it.
286 */
287 return ret;
288
289 error:
290 return -1;
291 }
292
293 /*
294 * Setup poll set maximum size.
295 */
296 int compat_epoll_set_max_size(void)
297 {
298 int ret, fd, retval = 0;
299 ssize_t size_ret;
300 char buf[64];
301
302 fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY);
303 if (fd < 0) {
304 retval = -1;
305 goto end;
306 }
307
308 size_ret = lttng_read(fd, buf, sizeof(buf));
309 /*
310 * Allow reading a file smaller than buf, but keep space for
311 * final \0.
312 */
313 if (size_ret < 0 || size_ret >= sizeof(buf)) {
314 PERROR("read set max size");
315 retval = -1;
316 goto end_read;
317 }
318 buf[size_ret] = '\0';
319 poll_max_size = atoi(buf);
320 end_read:
321 ret = close(fd);
322 if (ret) {
323 PERROR("close");
324 }
325 end:
326 if (!poll_max_size) {
327 poll_max_size = DEFAULT_POLL_SIZE;
328 }
329 DBG("epoll set max size is %d", poll_max_size);
330 return retval;
331 }
This page took 0.04029 seconds and 5 git commands to generate.