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