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