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