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