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