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 | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 17 | #include <assert.h> |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 18 | #include <err.h> |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 19 | #include <errno.h> |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 20 | #include <limits.h> |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 21 | #include <stdbool.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <termios.h> |
| 26 | #include <unistd.h> |
| 27 | #include <endian.h> |
| 28 | |
| 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 31 | #include <systemd/sd-daemon.h> |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 32 | |
| 33 | #include "console-server.h" |
| 34 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 35 | #define SOCKET_HANDLER_PKT_SIZE 512 |
| 36 | /* Set poll() timeout to 4000 uS, or 4 mS */ |
| 37 | #define SOCKET_HANDLER_PKT_US_TIMEOUT 4000 |
| 38 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 39 | struct client { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 40 | struct socket_handler *sh; |
| 41 | struct poller *poller; |
| 42 | struct ringbuffer_consumer *rbc; |
| 43 | int fd; |
| 44 | bool blocked; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct socket_handler { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 48 | struct handler handler; |
| 49 | struct console *console; |
| 50 | struct poller *poller; |
| 51 | int sd; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 52 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 53 | struct client **clients; |
| 54 | int n_clients; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 55 | }; |
| 56 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 57 | static struct timeval const socket_handler_timeout = { |
| 58 | .tv_sec = 0, |
| 59 | .tv_usec = SOCKET_HANDLER_PKT_US_TIMEOUT |
| 60 | }; |
| 61 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 62 | static struct socket_handler *to_socket_handler(struct handler *handler) |
| 63 | { |
| 64 | return container_of(handler, struct socket_handler, handler); |
| 65 | } |
| 66 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 67 | static void client_close(struct client *client) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 68 | { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 69 | struct socket_handler *sh = client->sh; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 70 | int idx; |
| 71 | |
| 72 | close(client->fd); |
| 73 | if (client->poller) |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 74 | console_poller_unregister(sh->console, client->poller); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 75 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 76 | if (client->rbc) |
| 77 | ringbuffer_consumer_unregister(client->rbc); |
| 78 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 79 | for (idx = 0; idx < sh->n_clients; idx++) |
| 80 | if (sh->clients[idx] == client) |
| 81 | break; |
| 82 | |
| 83 | assert(idx < sh->n_clients); |
| 84 | |
| 85 | free(client); |
| 86 | client = NULL; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 87 | |
| 88 | sh->n_clients--; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame^] | 89 | /* |
| 90 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 91 | * pointer type. |
| 92 | */ |
| 93 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 94 | memmove(&sh->clients[idx], &sh->clients[idx + 1], |
| 95 | sizeof(*sh->clients) * (sh->n_clients - idx)); |
| 96 | sh->clients = |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame^] | 97 | reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients)); |
| 98 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 99 | } |
| 100 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 101 | static void client_set_blocked(struct client *client, bool blocked) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 102 | { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 103 | int events; |
| 104 | |
| 105 | if (client->blocked == blocked) |
| 106 | return; |
| 107 | |
| 108 | client->blocked = blocked; |
| 109 | |
| 110 | events = POLLIN; |
| 111 | if (client->blocked) |
| 112 | events |= POLLOUT; |
| 113 | |
| 114 | console_poller_set_events(client->sh->console, client->poller, events); |
| 115 | } |
| 116 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 117 | static ssize_t send_all(struct client *client, void *buf, size_t len, |
| 118 | bool block) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 119 | { |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 120 | int fd, flags; |
| 121 | ssize_t rc; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 122 | size_t pos; |
| 123 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 124 | if (len > SSIZE_MAX) |
| 125 | return -EINVAL; |
| 126 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 127 | fd = client->fd; |
| 128 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 129 | flags = MSG_NOSIGNAL; |
| 130 | if (!block) |
| 131 | flags |= MSG_DONTWAIT; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 132 | |
| 133 | for (pos = 0; pos < len; pos += rc) { |
Andrew Jeffery | 21f4cb4 | 2023-04-18 14:38:37 +0930 | [diff] [blame] | 134 | rc = send(fd, (char *)buf + pos, len - pos, flags); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 135 | if (rc < 0) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 136 | if (!block && |
| 137 | (errno == EAGAIN || errno == EWOULDBLOCK)) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 138 | client_set_blocked(client, true); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 139 | break; |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 140 | } |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 141 | |
| 142 | if (errno == EINTR) |
| 143 | continue; |
| 144 | |
| 145 | return -1; |
| 146 | } |
| 147 | if (rc == 0) |
| 148 | return -1; |
| 149 | } |
| 150 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 151 | return (ssize_t)pos; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 152 | } |
| 153 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 154 | /* Drain the queue to the socket and update the queue buffer. If force_len is |
| 155 | * set, send at least that many bytes from the queue, possibly while blocking |
| 156 | */ |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 157 | static int client_drain_queue(struct client *client, size_t force_len) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 158 | { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 159 | uint8_t *buf; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 160 | ssize_t wlen; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 161 | size_t len, total_len; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 162 | bool block; |
| 163 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 164 | total_len = 0; |
| 165 | wlen = 0; |
| 166 | block = !!force_len; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 167 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 168 | /* if we're already blocked, no need for the write */ |
| 169 | if (!block && client->blocked) |
| 170 | return 0; |
| 171 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 172 | for (;;) { |
| 173 | len = ringbuffer_dequeue_peek(client->rbc, total_len, &buf); |
| 174 | if (!len) |
| 175 | break; |
| 176 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 177 | wlen = send_all(client, buf, len, block); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 178 | if (wlen <= 0) |
| 179 | break; |
| 180 | |
| 181 | total_len += wlen; |
| 182 | |
| 183 | if (force_len && total_len >= force_len) |
| 184 | break; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 187 | if (wlen < 0) |
| 188 | return -1; |
| 189 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 190 | if (force_len && total_len < force_len) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 191 | return -1; |
| 192 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 193 | ringbuffer_dequeue_commit(client->rbc, total_len); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 197 | static enum ringbuffer_poll_ret client_ringbuffer_poll(void *arg, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 198 | size_t force_len) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 199 | { |
| 200 | struct client *client = arg; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 201 | size_t len; |
| 202 | int rc; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 203 | |
| 204 | len = ringbuffer_len(client->rbc); |
| 205 | if (!force_len && (len < SOCKET_HANDLER_PKT_SIZE)) { |
| 206 | /* Do nothing until many small requests have accumulated, or |
| 207 | * the UART is idle for awhile (as determined by the timeout |
| 208 | * value supplied to the poll function call in console_server.c. */ |
| 209 | console_poller_set_timeout(client->sh->console, client->poller, |
| 210 | &socket_handler_timeout); |
| 211 | return RINGBUFFER_POLL_OK; |
| 212 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 213 | |
| 214 | rc = client_drain_queue(client, force_len); |
| 215 | if (rc) { |
| 216 | client->rbc = NULL; |
| 217 | client_close(client); |
| 218 | return RINGBUFFER_POLL_REMOVE; |
| 219 | } |
| 220 | |
| 221 | return RINGBUFFER_POLL_OK; |
| 222 | } |
| 223 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 224 | static enum poller_ret |
| 225 | client_timeout(struct handler *handler __attribute__((unused)), void *data) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 226 | { |
| 227 | struct client *client = data; |
| 228 | int rc = 0; |
| 229 | |
| 230 | if (client->blocked) { |
| 231 | /* nothing to do here, we'll call client_drain_queue when |
| 232 | * we become unblocked */ |
| 233 | return POLLER_OK; |
| 234 | } |
| 235 | |
| 236 | rc = client_drain_queue(client, 0); |
| 237 | if (rc) { |
| 238 | client_close(client); |
| 239 | return POLLER_REMOVE; |
| 240 | } |
| 241 | |
| 242 | return POLLER_OK; |
| 243 | } |
| 244 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 245 | static enum poller_ret client_poll(struct handler *handler, int events, |
| 246 | void *data) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 247 | { |
| 248 | struct socket_handler *sh = to_socket_handler(handler); |
| 249 | struct client *client = data; |
| 250 | uint8_t buf[4096]; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 251 | ssize_t rc; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 252 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 253 | if (events & POLLIN) { |
Jeremy Kerr | 68a2ece | 2017-01-17 13:52:41 +0800 | [diff] [blame] | 254 | rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 255 | if (rc < 0) { |
| 256 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 257 | return POLLER_OK; |
| 258 | else |
| 259 | goto err_close; |
| 260 | } |
| 261 | if (rc == 0) |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 262 | goto err_close; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 263 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 264 | console_data_out(sh->console, buf, rc); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 267 | if (events & POLLOUT) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 268 | client_set_blocked(client, false); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 269 | rc = client_drain_queue(client, 0); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 270 | if (rc) |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 271 | goto err_close; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 272 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 273 | |
| 274 | return POLLER_OK; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 275 | |
| 276 | err_close: |
| 277 | client->poller = NULL; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 278 | client_close(client); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 279 | return POLLER_REMOVE; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 280 | } |
| 281 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 282 | static enum poller_ret socket_poll(struct handler *handler, int events, |
| 283 | void __attribute__((unused)) * data) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 284 | { |
| 285 | struct socket_handler *sh = to_socket_handler(handler); |
| 286 | struct client *client; |
| 287 | int fd, n; |
| 288 | |
| 289 | if (!(events & POLLIN)) |
| 290 | return POLLER_OK; |
| 291 | |
Jeremy Kerr | 68a2ece | 2017-01-17 13:52:41 +0800 | [diff] [blame] | 292 | fd = accept(sh->sd, NULL, NULL); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 293 | if (fd < 0) |
| 294 | return POLLER_OK; |
| 295 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 296 | client = malloc(sizeof(*client)); |
| 297 | memset(client, 0, sizeof(*client)); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 298 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 299 | client->sh = sh; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 300 | client->fd = fd; |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 301 | client->poller = console_poller_register(sh->console, handler, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 302 | client_poll, client_timeout, |
| 303 | client->fd, POLLIN, client); |
| 304 | client->rbc = console_ringbuffer_consumer_register( |
| 305 | sh->console, client_ringbuffer_poll, client); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 306 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 307 | n = sh->n_clients++; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame^] | 308 | /* |
| 309 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 310 | * pointer type. |
| 311 | */ |
| 312 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 313 | sh->clients = |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame^] | 314 | reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients)); |
| 315 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 316 | sh->clients[n] = client; |
| 317 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 318 | return POLLER_OK; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 319 | } |
| 320 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 321 | static int socket_init(struct handler *handler, struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 322 | struct config *config) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 323 | { |
| 324 | struct socket_handler *sh = to_socket_handler(handler); |
| 325 | struct sockaddr_un addr; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 326 | size_t addrlen; |
| 327 | ssize_t len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 328 | int rc; |
| 329 | |
| 330 | sh->console = console; |
| 331 | sh->clients = NULL; |
| 332 | sh->n_clients = 0; |
Jeremy Kerr | 5708dfb | 2017-01-19 15:11:19 +0800 | [diff] [blame] | 333 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 334 | memset(&addr, 0, sizeof(addr)); |
| 335 | addr.sun_family = AF_UNIX; |
Andrew Jeffery | ddf2ab7 | 2020-02-10 12:36:09 +1030 | [diff] [blame] | 336 | len = console_socket_path(&addr, config_get_value(config, "socket-id")); |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 337 | if (len < 0) { |
| 338 | if (errno) |
| 339 | warn("Failed to configure socket: %s", strerror(errno)); |
| 340 | else |
| 341 | warn("Socket name length exceeds buffer limits"); |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 342 | return -1; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 343 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 344 | |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 345 | /* Try to take a socket from systemd first */ |
| 346 | if (sd_listen_fds(0) == 1 && |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 347 | sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_STREAM, 1, |
| 348 | addr.sun_path, len) > 0) { |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 349 | sh->sd = SD_LISTEN_FDS_START; |
| 350 | } else { |
| 351 | sh->sd = socket(AF_UNIX, SOCK_STREAM, 0); |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 352 | if (sh->sd < 0) { |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 353 | warn("Can't create socket"); |
| 354 | return -1; |
| 355 | } |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 356 | |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 357 | addrlen = sizeof(addr) - sizeof(addr.sun_path) + len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 358 | |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 359 | rc = bind(sh->sd, (struct sockaddr *)&addr, addrlen); |
| 360 | if (rc) { |
| 361 | socket_path_t name; |
| 362 | console_socket_path_readable(&addr, addrlen, name); |
| 363 | warn("Can't bind to socket path %s (terminated at first null)", |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 364 | name); |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 365 | goto cleanup; |
| 366 | } |
| 367 | |
| 368 | rc = listen(sh->sd, 1); |
| 369 | if (rc) { |
| 370 | warn("Can't listen for incoming connections"); |
| 371 | goto cleanup; |
| 372 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 373 | } |
| 374 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 375 | sh->poller = console_poller_register(console, handler, socket_poll, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 376 | NULL, sh->sd, POLLIN, NULL); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 377 | |
| 378 | return 0; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 379 | cleanup: |
| 380 | close(sh->sd); |
| 381 | return -1; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 382 | } |
| 383 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 384 | static void socket_fini(struct handler *handler) |
| 385 | { |
| 386 | struct socket_handler *sh = to_socket_handler(handler); |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 387 | |
Jeremy Kerr | abd8e25 | 2017-01-19 15:42:48 +0800 | [diff] [blame] | 388 | while (sh->n_clients) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 389 | client_close(sh->clients[0]); |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 390 | |
| 391 | if (sh->poller) |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 392 | console_poller_unregister(sh->console, sh->poller); |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 393 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 394 | close(sh->sd); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static struct socket_handler socket_handler = { |
| 398 | .handler = { |
| 399 | .name = "socket", |
| 400 | .init = socket_init, |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 401 | .fini = socket_fini, |
| 402 | }, |
| 403 | }; |
| 404 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 405 | console_handler_register(&socket_handler.handler); |