Port: add cygwin support to endian compat
[lttng-tools.git] / src / common / compat / compat-poll.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 <stdlib.h>
21#include <sys/resource.h>
22#include <sys/time.h>
23
990570ed 24#include <common/defaults.h>
db758600 25#include <common/error.h>
cfa9a5a2
DG
26#include <common/macros.h>
27#include <common/utils.h>
5eb91c98
DG
28
29#include "poll.h"
30
31unsigned int poll_max_size;
32
d21b0d71
DG
33/*
34 * Resize the epoll events structure of the new size.
35 *
36 * Return 0 on success or else -1 with the current events pointer untouched.
37 */
38static int resize_poll_event(struct compat_poll_event_array *array,
39 uint32_t new_size)
40{
41 struct pollfd *ptr;
42
43 assert(array);
44
ac018a8b
DG
45 /* Refuse to resize the array more than the max size. */
46 if (new_size > poll_max_size) {
47 goto error;
48 }
49
d21b0d71
DG
50 ptr = realloc(array->events, new_size * sizeof(*ptr));
51 if (ptr == NULL) {
52 PERROR("realloc epoll add");
53 goto error;
54 }
53efb85a
MD
55 if (new_size > array->alloc_size) {
56 /* Zero newly allocated memory */
57 memset(ptr + array->alloc_size, 0,
58 (new_size - array->alloc_size) * sizeof(*ptr));
59 }
d21b0d71
DG
60 array->events = ptr;
61 array->alloc_size = new_size;
62
63 return 0;
64
65error:
66 return -1;
67}
68
69/*
70 * Update events with the current events object.
71 */
72static int update_current_events(struct lttng_poll_event *events)
73{
74 int ret;
75 struct compat_poll_event_array *current, *wait;
76
77 assert(events);
78
79 current = &events->current;
80 wait = &events->wait;
81
82 wait->nb_fd = current->nb_fd;
dbe23f45 83 if (current->alloc_size != wait->alloc_size) {
d21b0d71
DG
84 ret = resize_poll_event(wait, current->alloc_size);
85 if (ret < 0) {
86 goto error;
87 }
88 }
89 memcpy(wait->events, current->events,
90 current->nb_fd * sizeof(*current->events));
91
dbe23f45 92 /* Update is done. */
d21b0d71 93 events->need_update = 0;
d21b0d71
DG
94
95 return 0;
96
97error:
98 return -1;
99}
100
5eb91c98
DG
101/*
102 * Create pollfd data structure.
103 */
104int compat_poll_create(struct lttng_poll_event *events, int size)
105{
d21b0d71
DG
106 struct compat_poll_event_array *current, *wait;
107
5eb91c98
DG
108 if (events == NULL || size <= 0) {
109 ERR("Wrong arguments for poll create");
110 goto error;
111 }
112
dbe23f45 113 if (!poll_max_size) {
c607fe03
MJ
114 if (lttng_poll_set_max_size()) {
115 goto error;
116 }
dbe23f45
MD
117 }
118
5eb91c98
DG
119 /* Don't bust the limit here */
120 if (size > poll_max_size) {
121 size = poll_max_size;
122 }
123
d21b0d71
DG
124 /* Reset everything before begining the allocation. */
125 memset(events, 0, sizeof(struct lttng_poll_event));
126
d21b0d71
DG
127 current = &events->current;
128 wait = &events->wait;
129
5eb91c98 130 /* This *must* be freed by using lttng_poll_free() */
d21b0d71
DG
131 wait->events = zmalloc(size * sizeof(struct pollfd));
132 if (wait->events == NULL) {
6f04ed72 133 PERROR("zmalloc struct pollfd");
5eb91c98
DG
134 goto error;
135 }
136
d21b0d71
DG
137 wait->alloc_size = wait->init_size = size;
138
139 current->events = zmalloc(size * sizeof(struct pollfd));
140 if (current->events == NULL) {
6f04ed72 141 PERROR("zmalloc struct current pollfd");
d21b0d71
DG
142 goto error;
143 }
144
145 current->alloc_size = current->init_size = size;
5eb91c98
DG
146
147 return 0;
148
149error:
150 return -1;
151}
152
153/*
154 * Add fd to pollfd data structure with requested events.
155 */
156int compat_poll_add(struct lttng_poll_event *events, int fd,
157 uint32_t req_events)
158{
d21b0d71
DG
159 int new_size, ret, i;
160 struct compat_poll_event_array *current;
5eb91c98 161
d21b0d71 162 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
163 ERR("Bad compat poll add arguments");
164 goto error;
165 }
166
d21b0d71
DG
167 current = &events->current;
168
169 /* Check if fd we are trying to add is already there. */
170 for (i = 0; i < current->nb_fd; i++) {
d21b0d71
DG
171 if (current->events[i].fd == fd) {
172 errno = EEXIST;
5eb91c98
DG
173 goto error;
174 }
5eb91c98
DG
175 }
176
dbe23f45
MD
177 /* Resize array if needed. */
178 new_size = 1U << utils_get_count_order_u32(current->nb_fd + 1);
179 if (new_size != current->alloc_size && new_size >= current->init_size) {
d21b0d71
DG
180 ret = resize_poll_event(current, new_size);
181 if (ret < 0) {
182 goto error;
183 }
d21b0d71 184 }
5eb91c98 185
d21b0d71
DG
186 current->events[current->nb_fd].fd = fd;
187 current->events[current->nb_fd].events = req_events;
188 current->nb_fd++;
189 events->need_update = 1;
190
191 DBG("fd %d of %d added to pollfd", fd, current->nb_fd);
5eb91c98
DG
192
193 return 0;
194
195error:
196 return -1;
197}
198
199/*
200 * Remove a fd from the pollfd structure.
201 */
202int compat_poll_del(struct lttng_poll_event *events, int fd)
203{
d21b0d71
DG
204 int new_size, i, count = 0, ret;
205 struct compat_poll_event_array *current;
5eb91c98 206
d21b0d71 207 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
208 ERR("Wrong arguments for poll del");
209 goto error;
210 }
211
d21b0d71
DG
212 /* Ease our life a bit. */
213 current = &events->current;
5eb91c98 214
d21b0d71 215 for (i = 0; i < current->nb_fd; i++) {
5eb91c98 216 /* Don't put back the fd we want to delete */
d21b0d71
DG
217 if (current->events[i].fd != fd) {
218 current->events[count].fd = current->events[i].fd;
219 current->events[count].events = current->events[i].events;
5eb91c98
DG
220 count++;
221 }
222 }
dbe23f45
MD
223 /* No fd duplicate should be ever added into array. */
224 assert(current->nb_fd - 1 == count);
225 current->nb_fd = count;
226
227 /* Resize array if needed. */
228 new_size = 1U << utils_get_count_order_u32(current->nb_fd);
229 if (new_size != current->alloc_size && new_size >= current->init_size) {
230 ret = resize_poll_event(current, new_size);
231 if (ret < 0) {
232 goto error;
233 }
234 }
5eb91c98 235
d21b0d71 236 events->need_update = 1;
5eb91c98
DG
237
238 return 0;
239
240error:
241 return -1;
242}
243
244/*
245 * Wait on poll() with timeout. Blocking call.
246 */
247int compat_poll_wait(struct lttng_poll_event *events, int timeout)
248{
249 int ret;
250
d21b0d71 251 if (events == NULL || events->current.events == NULL) {
5eb91c98
DG
252 ERR("poll wait arguments error");
253 goto error;
254 }
dbe23f45 255 assert(events->current.nb_fd >= 0);
5eb91c98 256
d21b0d71
DG
257 if (events->current.nb_fd == 0) {
258 /* Return an invalid error to be consistent with epoll. */
259 errno = EINVAL;
dbe23f45 260 events->wait.nb_fd = 0;
d21b0d71
DG
261 goto error;
262 }
263
264 if (events->need_update) {
265 ret = update_current_events(events);
266 if (ret < 0) {
267 errno = ENOMEM;
268 goto error;
269 }
270 }
271
a9b0dbc2
JG
272 do {
273 ret = poll(events->wait.events, events->wait.nb_fd, timeout);
274 } while (ret == -1 && errno == EINTR);
5eb91c98
DG
275 if (ret < 0) {
276 /* At this point, every error is fatal */
6f04ed72 277 PERROR("poll wait");
5eb91c98
DG
278 goto error;
279 }
280
9ddba525
DG
281 /*
282 * poll() should always iterate on all FDs since we handle the pollset in
283 * user space and after poll returns, we have to try every fd for a match.
284 */
d21b0d71 285 return events->wait.nb_fd;
5eb91c98
DG
286
287error:
288 return -1;
289}
290
291/*
292 * Setup poll set maximum size.
293 */
dbe23f45 294int compat_poll_set_max_size(void)
5eb91c98 295{
dbe23f45 296 int ret, retval = 0;
5eb91c98
DG
297 struct rlimit lim;
298
5eb91c98
DG
299 ret = getrlimit(RLIMIT_NOFILE, &lim);
300 if (ret < 0) {
6f04ed72 301 PERROR("getrlimit poll RLIMIT_NOFILE");
dbe23f45
MD
302 retval = -1;
303 goto end;
5eb91c98
DG
304 }
305
306 poll_max_size = lim.rlim_cur;
dbe23f45 307end:
d21b0d71 308 if (poll_max_size == 0) {
990570ed 309 poll_max_size = DEFAULT_POLL_SIZE;
5eb91c98 310 }
5eb91c98 311 DBG("poll set max size set to %u", poll_max_size);
dbe23f45 312 return retval;
5eb91c98 313}
This page took 0.053495 seconds and 4 git commands to generate.