Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 16 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 17 | #define _GNU_SOURCE |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 18 | |
| 19 | #include <assert.h> |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 20 | #include <err.h> |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 21 | #include <errno.h> |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 22 | #include <stdbool.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <termios.h> |
| 27 | #include <unistd.h> |
| 28 | #include <endian.h> |
| 29 | |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/un.h> |
| 32 | |
| 33 | #include "console-server.h" |
| 34 | |
| 35 | struct client { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 36 | struct socket_handler *sh; |
| 37 | struct poller *poller; |
| 38 | struct ringbuffer_consumer *rbc; |
| 39 | int fd; |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 40 | bool blocked; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct socket_handler { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 44 | struct handler handler; |
| 45 | struct console *console; |
| 46 | struct poller *poller; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 47 | int sd; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 48 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 49 | struct client **clients; |
| 50 | int n_clients; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static struct socket_handler *to_socket_handler(struct handler *handler) |
| 54 | { |
| 55 | return container_of(handler, struct socket_handler, handler); |
| 56 | } |
| 57 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 58 | static void client_close(struct client *client) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 59 | { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 60 | struct socket_handler *sh = client->sh; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 61 | int idx; |
| 62 | |
| 63 | close(client->fd); |
| 64 | if (client->poller) |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 65 | console_poller_unregister(sh->console, client->poller); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 66 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 67 | if (client->rbc) |
| 68 | ringbuffer_consumer_unregister(client->rbc); |
| 69 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 70 | for (idx = 0; idx < sh->n_clients; idx++) |
| 71 | if (sh->clients[idx] == client) |
| 72 | break; |
| 73 | |
| 74 | assert(idx < sh->n_clients); |
| 75 | |
| 76 | free(client); |
| 77 | client = NULL; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 78 | |
| 79 | sh->n_clients--; |
| 80 | memmove(&sh->clients[idx], &sh->clients[idx+1], |
| 81 | sizeof(*sh->clients) * (sh->n_clients - idx)); |
Joel Stanley | fbff1c6 | 2016-03-17 20:32:24 +1030 | [diff] [blame] | 82 | sh->clients = realloc(sh->clients, |
| 83 | sizeof(*sh->clients) * sh->n_clients); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 86 | static void client_set_blocked(struct client *client, bool blocked) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 87 | { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 88 | int events; |
| 89 | |
| 90 | if (client->blocked == blocked) |
| 91 | return; |
| 92 | |
| 93 | client->blocked = blocked; |
| 94 | |
| 95 | events = POLLIN; |
| 96 | if (client->blocked) |
| 97 | events |= POLLOUT; |
| 98 | |
| 99 | console_poller_set_events(client->sh->console, client->poller, events); |
| 100 | } |
| 101 | |
| 102 | static ssize_t send_all(struct client *client, void *buf, |
| 103 | size_t len, bool block) |
| 104 | { |
| 105 | int fd, rc, flags; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 106 | size_t pos; |
| 107 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 108 | fd = client->fd; |
| 109 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 110 | flags = MSG_NOSIGNAL; |
| 111 | if (!block) |
| 112 | flags |= MSG_DONTWAIT; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 113 | |
| 114 | for (pos = 0; pos < len; pos += rc) { |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 115 | rc = send(fd, buf + pos, len - pos, flags); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 116 | if (rc < 0) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 117 | if (!block && (errno == EAGAIN || |
| 118 | errno == EWOULDBLOCK)) { |
| 119 | client_set_blocked(client, true); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 120 | break; |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 121 | } |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 122 | |
| 123 | if (errno == EINTR) |
| 124 | continue; |
| 125 | |
| 126 | return -1; |
| 127 | } |
| 128 | if (rc == 0) |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | return pos; |
| 133 | } |
| 134 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 135 | /* Drain the queue to the socket and update the queue buffer. If force_len is |
| 136 | * set, send at least that many bytes from the queue, possibly while blocking |
| 137 | */ |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 138 | static int client_drain_queue(struct client *client, size_t force_len) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 139 | { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 140 | uint8_t *buf; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 141 | ssize_t wlen; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 142 | size_t len, total_len; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 143 | bool block; |
| 144 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 145 | total_len = 0; |
| 146 | wlen = 0; |
| 147 | block = !!force_len; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 148 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 149 | /* if we're already blocked, no need for the write */ |
| 150 | if (!block && client->blocked) |
| 151 | return 0; |
| 152 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 153 | for (;;) { |
| 154 | len = ringbuffer_dequeue_peek(client->rbc, total_len, &buf); |
| 155 | if (!len) |
| 156 | break; |
| 157 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 158 | wlen = send_all(client, buf, len, block); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 159 | if (wlen <= 0) |
| 160 | break; |
| 161 | |
| 162 | total_len += wlen; |
| 163 | |
| 164 | if (force_len && total_len >= force_len) |
| 165 | break; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 168 | if (wlen < 0) |
| 169 | return -1; |
| 170 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 171 | if (force_len && total_len < force_len) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 172 | return -1; |
| 173 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 174 | ringbuffer_dequeue_commit(client->rbc, total_len); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 178 | static enum ringbuffer_poll_ret client_ringbuffer_poll(void *arg, |
| 179 | size_t force_len) |
| 180 | { |
| 181 | struct client *client = arg; |
| 182 | int rc; |
| 183 | |
| 184 | rc = client_drain_queue(client, force_len); |
| 185 | if (rc) { |
| 186 | client->rbc = NULL; |
| 187 | client_close(client); |
| 188 | return RINGBUFFER_POLL_REMOVE; |
| 189 | } |
| 190 | |
| 191 | return RINGBUFFER_POLL_OK; |
| 192 | } |
| 193 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 194 | static enum poller_ret client_poll(struct handler *handler, |
| 195 | int events, void *data) |
| 196 | { |
| 197 | struct socket_handler *sh = to_socket_handler(handler); |
| 198 | struct client *client = data; |
| 199 | uint8_t buf[4096]; |
| 200 | int rc; |
| 201 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 202 | if (events & POLLIN) { |
Jeremy Kerr | 68a2ece | 2017-01-17 13:52:41 +0800 | [diff] [blame] | 203 | rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 204 | if (rc < 0) { |
| 205 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 206 | return POLLER_OK; |
| 207 | else |
| 208 | goto err_close; |
| 209 | } |
| 210 | if (rc == 0) |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 211 | goto err_close; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 212 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 213 | console_data_out(sh->console, buf, rc); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 216 | if (events & POLLOUT) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame^] | 217 | client_set_blocked(client, false); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 218 | rc = client_drain_queue(client, 0); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 219 | if (rc) |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 220 | goto err_close; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 221 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 222 | |
| 223 | return POLLER_OK; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 224 | |
| 225 | err_close: |
| 226 | client->poller = NULL; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 227 | client_close(client); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 228 | return POLLER_REMOVE; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 231 | static enum poller_ret socket_poll(struct handler *handler, |
| 232 | int events, void __attribute__((unused)) *data) |
| 233 | { |
| 234 | struct socket_handler *sh = to_socket_handler(handler); |
| 235 | struct client *client; |
| 236 | int fd, n; |
| 237 | |
| 238 | if (!(events & POLLIN)) |
| 239 | return POLLER_OK; |
| 240 | |
Jeremy Kerr | 68a2ece | 2017-01-17 13:52:41 +0800 | [diff] [blame] | 241 | fd = accept(sh->sd, NULL, NULL); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 242 | if (fd < 0) |
| 243 | return POLLER_OK; |
| 244 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 245 | client = malloc(sizeof(*client)); |
| 246 | memset(client, 0, sizeof(*client)); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 247 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 248 | client->sh = sh; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 249 | client->fd = fd; |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 250 | client->poller = console_poller_register(sh->console, handler, |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 251 | client_poll, client->fd, POLLIN, client); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 252 | client->rbc = console_ringbuffer_consumer_register(sh->console, |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 253 | client_ringbuffer_poll, client); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 254 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 255 | n = sh->n_clients++; |
| 256 | sh->clients = realloc(sh->clients, |
| 257 | sizeof(*sh->clients) * sh->n_clients); |
| 258 | sh->clients[n] = client; |
| 259 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 260 | return POLLER_OK; |
| 261 | |
| 262 | } |
| 263 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 264 | static int socket_init(struct handler *handler, struct console *console, |
| 265 | struct config *config __attribute__((unused))) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 266 | { |
| 267 | struct socket_handler *sh = to_socket_handler(handler); |
| 268 | struct sockaddr_un addr; |
| 269 | int rc; |
| 270 | |
| 271 | sh->console = console; |
| 272 | sh->clients = NULL; |
| 273 | sh->n_clients = 0; |
Jeremy Kerr | 5708dfb | 2017-01-19 15:11:19 +0800 | [diff] [blame] | 274 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 275 | sh->sd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 276 | if(sh->sd < 0) { |
| 277 | warn("Can't create socket"); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | memset(&addr, 0, sizeof(addr)); |
| 282 | addr.sun_family = AF_UNIX; |
Jeremy Kerr | 0cff652 | 2016-03-18 09:57:01 +0800 | [diff] [blame] | 283 | memcpy(&addr.sun_path, &console_socket_path, console_socket_path_len); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 284 | |
| 285 | rc = bind(sh->sd, (struct sockaddr *)&addr, sizeof(addr)); |
| 286 | if (rc) { |
| 287 | warn("Can't bind to socket path %s", |
| 288 | console_socket_path_readable); |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | rc = listen(sh->sd, 1); |
| 293 | if (rc) { |
| 294 | warn("Can't listen for incoming connections"); |
| 295 | return -1; |
| 296 | } |
| 297 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 298 | sh->poller = console_poller_register(console, handler, socket_poll, |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 299 | sh->sd, POLLIN, NULL); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 304 | static void socket_fini(struct handler *handler) |
| 305 | { |
| 306 | struct socket_handler *sh = to_socket_handler(handler); |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 307 | |
Jeremy Kerr | abd8e25 | 2017-01-19 15:42:48 +0800 | [diff] [blame] | 308 | while (sh->n_clients) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 309 | client_close(sh->clients[0]); |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 310 | |
| 311 | if (sh->poller) |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 312 | console_poller_unregister(sh->console, sh->poller); |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 313 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 314 | close(sh->sd); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static struct socket_handler socket_handler = { |
| 318 | .handler = { |
| 319 | .name = "socket", |
| 320 | .init = socket_init, |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 321 | .fini = socket_fini, |
| 322 | }, |
| 323 | }; |
| 324 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 325 | console_handler_register(&socket_handler.handler); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 326 | |