| 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 | case ENOSPC: |
| 97 | case EPERM: |
| 98 | /* Print perror and goto end not failing. Show must go on. */ |
| 99 | perror("epoll_ctl ADD"); |
| 100 | goto end; |
| 101 | default: |
| 102 | perror("epoll_ctl ADD fatal"); |
| 103 | goto error; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | events->nb_fd++; |
| 108 | |
| 109 | if (events->nb_fd >= events->events_size) { |
| 110 | new_size = 2 * events->events_size; |
| 111 | ptr = realloc(events->events, new_size * sizeof(struct epoll_event)); |
| 112 | if (ptr == NULL) { |
| 113 | perror("realloc epoll add"); |
| 114 | goto error; |
| 115 | } |
| 116 | events->events = ptr; |
| 117 | events->events_size = new_size; |
| 118 | } |
| 119 | |
| 120 | end: |
| 121 | return 0; |
| 122 | |
| 123 | error: |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Remove a fd from the epoll set. |
| 129 | */ |
| 130 | int compat_epoll_del(struct lttng_poll_event *events, int fd) |
| 131 | { |
| 132 | int ret; |
| 133 | |
| 134 | if (events == NULL || fd < 0) { |
| 135 | goto error; |
| 136 | } |
| 137 | |
| 138 | ret = epoll_ctl(events->epfd, EPOLL_CTL_DEL, fd, NULL); |
| 139 | if (ret < 0) { |
| 140 | switch (errno) { |
| 141 | case ENOENT: |
| 142 | case EPERM: |
| 143 | /* Print perror and goto end not failing. Show must go on. */ |
| 144 | perror("epoll_ctl DEL"); |
| 145 | goto end; |
| 146 | default: |
| 147 | perror("epoll_ctl DEL fatal"); |
| 148 | goto error; |
| 149 | } |
| 150 | perror("epoll_ctl del"); |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | events->nb_fd--; |
| 155 | |
| 156 | end: |
| 157 | return 0; |
| 158 | |
| 159 | error: |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Wait on epoll set. This is a blocking call of timeout value. |
| 165 | */ |
| 166 | int compat_epoll_wait(struct lttng_poll_event *events, int timeout) |
| 167 | { |
| 168 | int ret; |
| 169 | |
| 170 | if (events == NULL || events->events == NULL || |
| 171 | events->events_size < events->nb_fd) { |
| 172 | ERR("Wrong arguments in compat_epoll_wait"); |
| 173 | goto error; |
| 174 | } |
| 175 | |
| 176 | do { |
| 177 | ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout); |
| 178 | } while (ret == -1 && errno == EINTR); |
| 179 | if (ret < 0) { |
| 180 | /* At this point, every error is fatal */ |
| 181 | perror("epoll_wait"); |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | return ret; |
| 186 | |
| 187 | error: |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Setup poll set maximum size. |
| 193 | */ |
| 194 | void compat_epoll_set_max_size(void) |
| 195 | { |
| 196 | int ret, fd; |
| 197 | char buf[64]; |
| 198 | |
| 199 | poll_max_size = DEFAULT_POLL_SIZE; |
| 200 | |
| 201 | fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY); |
| 202 | if (fd < 0) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | ret = read(fd, buf, sizeof(buf)); |
| 207 | if (ret < 0) { |
| 208 | perror("read set max size"); |
| 209 | goto error; |
| 210 | } |
| 211 | |
| 212 | poll_max_size = atoi(buf); |
| 213 | if (poll_max_size <= 0) { |
| 214 | /* Extra precaution */ |
| 215 | poll_max_size = DEFAULT_POLL_SIZE; |
| 216 | } |
| 217 | |
| 218 | DBG("epoll set max size is %d", poll_max_size); |
| 219 | |
| 220 | error: |
| 221 | close(fd); |
| 222 | } |