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