Fix: remove nonblock flag of sessiond socket in consumer
[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
ac018a8b
DG
43 /* Refuse to resize the array more than the max size. */
44 if (new_size > poll_max_size) {
45 goto error;
46 }
47
d21b0d71
DG
48 ptr = realloc(array->events, new_size * sizeof(*ptr));
49 if (ptr == NULL) {
50 PERROR("realloc epoll add");
51 goto error;
52 }
53 array->events = ptr;
54 array->alloc_size = new_size;
55
56 return 0;
57
58error:
59 return -1;
60}
61
62/*
63 * Update events with the current events object.
64 */
65static int update_current_events(struct lttng_poll_event *events)
66{
67 int ret;
68 struct compat_poll_event_array *current, *wait;
69
70 assert(events);
71
72 current = &events->current;
73 wait = &events->wait;
74
75 wait->nb_fd = current->nb_fd;
76 if (events->need_realloc) {
77 ret = resize_poll_event(wait, current->alloc_size);
78 if (ret < 0) {
79 goto error;
80 }
81 }
82 memcpy(wait->events, current->events,
83 current->nb_fd * sizeof(*current->events));
84
85 /* Update is done and realloc as well. */
86 events->need_update = 0;
87 events->need_realloc = 0;
88
89 return 0;
90
91error:
92 return -1;
93}
94
5eb91c98
DG
95/*
96 * Create pollfd data structure.
97 */
98int compat_poll_create(struct lttng_poll_event *events, int size)
99{
d21b0d71
DG
100 struct compat_poll_event_array *current, *wait;
101
5eb91c98
DG
102 if (events == NULL || size <= 0) {
103 ERR("Wrong arguments for poll create");
104 goto error;
105 }
106
107 /* Don't bust the limit here */
108 if (size > poll_max_size) {
109 size = poll_max_size;
110 }
111
d21b0d71
DG
112 /* Reset everything before begining the allocation. */
113 memset(events, 0, sizeof(struct lttng_poll_event));
114
115 /* Ease our life a bit. */
116 current = &events->current;
117 wait = &events->wait;
118
5eb91c98 119 /* This *must* be freed by using lttng_poll_free() */
d21b0d71
DG
120 wait->events = zmalloc(size * sizeof(struct pollfd));
121 if (wait->events == NULL) {
ba7f0ae5 122 perror("zmalloc struct pollfd");
5eb91c98
DG
123 goto error;
124 }
125
d21b0d71
DG
126 wait->alloc_size = wait->init_size = size;
127
128 current->events = zmalloc(size * sizeof(struct pollfd));
129 if (current->events == NULL) {
130 perror("zmalloc struct current pollfd");
131 goto error;
132 }
133
134 current->alloc_size = current->init_size = size;
5eb91c98
DG
135
136 return 0;
137
138error:
139 return -1;
140}
141
142/*
143 * Add fd to pollfd data structure with requested events.
144 */
145int compat_poll_add(struct lttng_poll_event *events, int fd,
146 uint32_t req_events)
147{
d21b0d71
DG
148 int new_size, ret, i;
149 struct compat_poll_event_array *current;
5eb91c98 150
d21b0d71 151 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
152 ERR("Bad compat poll add arguments");
153 goto error;
154 }
155
d21b0d71
DG
156 /* Ease our life a bit. */
157 current = &events->current;
158
159 /* Check if fd we are trying to add is already there. */
160 for (i = 0; i < current->nb_fd; i++) {
161 /* Don't put back the fd we want to delete */
162 if (current->events[i].fd == fd) {
163 errno = EEXIST;
5eb91c98
DG
164 goto error;
165 }
5eb91c98
DG
166 }
167
d21b0d71
DG
168 /* Check for a needed resize of the array. */
169 if (current->nb_fd > current->alloc_size) {
170 /* Expand it by a power of two of the current size. */
171 new_size = current->alloc_size << 1UL;
172 ret = resize_poll_event(current, new_size);
173 if (ret < 0) {
174 goto error;
175 }
176 events->need_realloc = 1;
177 }
5eb91c98 178
d21b0d71
DG
179 current->events[current->nb_fd].fd = fd;
180 current->events[current->nb_fd].events = req_events;
181 current->nb_fd++;
182 events->need_update = 1;
183
184 DBG("fd %d of %d added to pollfd", fd, current->nb_fd);
5eb91c98
DG
185
186 return 0;
187
188error:
189 return -1;
190}
191
192/*
193 * Remove a fd from the pollfd structure.
194 */
195int compat_poll_del(struct lttng_poll_event *events, int fd)
196{
d21b0d71
DG
197 int new_size, i, count = 0, ret;
198 struct compat_poll_event_array *current;
5eb91c98 199
d21b0d71 200 if (events == NULL || events->current.events == NULL || fd < 0) {
5eb91c98
DG
201 ERR("Wrong arguments for poll del");
202 goto error;
203 }
204
d21b0d71
DG
205 /* Ease our life a bit. */
206 current = &events->current;
5eb91c98 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.042752 seconds and 4 git commands to generate.