Merge branch 'master' of git://git.lttng.org/lttng-tools
[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 it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
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 with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18 #include <fcntl.h>
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <config.h>
25
26 #include <common/error.h>
27 #include <common/defaults.h>
28
29 #include "poll.h"
30
31 unsigned int poll_max_size;
32
33 /*
34 * Create epoll set and allocate returned events structure.
35 */
36 int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
37 {
38 int ret;
39
40 if (events == NULL || size <= 0) {
41 goto error;
42 }
43
44 /* Don't bust the limit here */
45 if (size > poll_max_size) {
46 size = poll_max_size;
47 }
48
49 ret = epoll_create1(flags);
50 if (ret < 0) {
51 /* At this point, every error is fatal */
52 perror("epoll_create1");
53 goto error;
54 }
55
56 events->epfd = ret;
57
58 /* This *must* be freed by using lttng_poll_free() */
59 events->events = zmalloc(size * sizeof(struct epoll_event));
60 if (events->events == NULL) {
61 perror("zmalloc epoll set");
62 goto error_close;
63 }
64
65 events->events_size = size;
66 events->nb_fd = 0;
67
68 return 0;
69
70 error_close:
71 close(events->epfd);
72 error:
73 return -1;
74 }
75
76 /*
77 * Add a fd to the epoll set with requesting events.
78 */
79 int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events)
80 {
81 int ret, new_size;
82 struct epoll_event ev, *ptr;
83
84 if (events == NULL || events->events == NULL || fd < 0) {
85 ERR("Bad compat epoll add arguments");
86 goto error;
87 }
88
89 ev.events = req_events;
90 ev.data.fd = fd;
91
92 ret = epoll_ctl(events->epfd, EPOLL_CTL_ADD, fd, &ev);
93 if (ret < 0) {
94 switch (errno) {
95 case EEXIST:
96 /* If exist, it's OK. */
97 goto end;
98 case ENOSPC:
99 case EPERM:
100 /* Print perror and goto end not failing. Show must go on. */
101 perror("epoll_ctl ADD");
102 goto end;
103 default:
104 perror("epoll_ctl ADD fatal");
105 goto error;
106 }
107 }
108
109 events->nb_fd++;
110
111 if (events->nb_fd >= events->events_size) {
112 new_size = 2 * events->events_size;
113 ptr = realloc(events->events, new_size * sizeof(struct epoll_event));
114 if (ptr == NULL) {
115 perror("realloc epoll add");
116 goto error;
117 }
118 events->events = ptr;
119 events->events_size = new_size;
120 }
121
122 end:
123 return 0;
124
125 error:
126 return -1;
127 }
128
129 /*
130 * Remove a fd from the epoll set.
131 */
132 int compat_epoll_del(struct lttng_poll_event *events, int fd)
133 {
134 int ret;
135
136 if (events == NULL || fd < 0) {
137 goto error;
138 }
139
140 ret = epoll_ctl(events->epfd, EPOLL_CTL_DEL, fd, NULL);
141 if (ret < 0) {
142 switch (errno) {
143 case ENOENT:
144 case EPERM:
145 /* Print perror and goto end not failing. Show must go on. */
146 perror("epoll_ctl DEL");
147 goto end;
148 default:
149 perror("epoll_ctl DEL fatal");
150 goto error;
151 }
152 perror("epoll_ctl del");
153 goto error;
154 }
155
156 events->nb_fd--;
157
158 end:
159 return 0;
160
161 error:
162 return -1;
163 }
164
165 /*
166 * Wait on epoll set. This is a blocking call of timeout value.
167 */
168 int compat_epoll_wait(struct lttng_poll_event *events, int timeout)
169 {
170 int ret;
171
172 if (events == NULL || events->events == NULL ||
173 events->events_size < events->nb_fd) {
174 ERR("Wrong arguments in compat_epoll_wait");
175 goto error;
176 }
177
178 do {
179 ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout);
180 } while (ret == -1 && errno == EINTR);
181 if (ret < 0) {
182 /* At this point, every error is fatal */
183 perror("epoll_wait");
184 goto error;
185 }
186
187 return ret;
188
189 error:
190 return -1;
191 }
192
193 /*
194 * Setup poll set maximum size.
195 */
196 void compat_epoll_set_max_size(void)
197 {
198 int ret, fd;
199 char buf[64];
200
201 poll_max_size = DEFAULT_POLL_SIZE;
202
203 fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY);
204 if (fd < 0) {
205 return;
206 }
207
208 ret = read(fd, buf, sizeof(buf));
209 if (ret < 0) {
210 perror("read set max size");
211 goto error;
212 }
213
214 poll_max_size = atoi(buf);
215 if (poll_max_size <= 0) {
216 /* Extra precaution */
217 poll_max_size = DEFAULT_POLL_SIZE;
218 }
219
220 DBG("epoll set max size is %d", poll_max_size);
221
222 error:
223 close(fd);
224 }
This page took 0.034244 seconds and 5 git commands to generate.