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 | |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 33 | #include "console-mux.h" |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 34 | #include "console-server.h" |
| 35 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 36 | #define SOCKET_HANDLER_PKT_SIZE 512 |
| 37 | /* Set poll() timeout to 4000 uS, or 4 mS */ |
| 38 | #define SOCKET_HANDLER_PKT_US_TIMEOUT 4000 |
| 39 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 40 | struct client { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 41 | struct socket_handler *sh; |
| 42 | struct poller *poller; |
| 43 | struct ringbuffer_consumer *rbc; |
| 44 | int fd; |
| 45 | bool blocked; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | struct socket_handler { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 49 | struct handler handler; |
| 50 | struct console *console; |
| 51 | struct poller *poller; |
| 52 | int sd; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 53 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 54 | struct client **clients; |
| 55 | int n_clients; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 58 | static struct timeval const socket_handler_timeout = { |
| 59 | .tv_sec = 0, |
| 60 | .tv_usec = SOCKET_HANDLER_PKT_US_TIMEOUT |
| 61 | }; |
| 62 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 63 | static struct socket_handler *to_socket_handler(struct handler *handler) |
| 64 | { |
| 65 | return container_of(handler, struct socket_handler, handler); |
| 66 | } |
| 67 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 68 | static void client_close(struct client *client) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 69 | { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 70 | struct socket_handler *sh = client->sh; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 71 | int idx; |
| 72 | |
| 73 | close(client->fd); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 74 | if (client->poller) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 75 | console_poller_unregister(sh->console, client->poller); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 76 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 77 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 78 | if (client->rbc) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 79 | ringbuffer_consumer_unregister(client->rbc); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 80 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 81 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 82 | for (idx = 0; idx < sh->n_clients; idx++) { |
| 83 | if (sh->clients[idx] == client) { |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 84 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 85 | } |
| 86 | } |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 87 | |
| 88 | assert(idx < sh->n_clients); |
| 89 | |
| 90 | free(client); |
| 91 | client = NULL; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 92 | |
| 93 | sh->n_clients--; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 94 | /* |
| 95 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 96 | * pointer type. |
| 97 | */ |
| 98 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 99 | memmove(&sh->clients[idx], &sh->clients[idx + 1], |
| 100 | sizeof(*sh->clients) * (sh->n_clients - idx)); |
Andrew Jeffery | 7851a39 | 2024-07-09 16:30:22 +0930 | [diff] [blame] | 101 | if (sh->n_clients == 0) { |
| 102 | free(sh->clients); |
| 103 | sh->clients = NULL; |
| 104 | } else { |
| 105 | sh->clients = reallocarray(sh->clients, sh->n_clients, |
| 106 | sizeof(*sh->clients)); |
| 107 | } |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 108 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 109 | } |
| 110 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 111 | static void client_set_blocked(struct client *client, bool blocked) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 112 | { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 113 | int events; |
| 114 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 115 | if (client->blocked == blocked) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 116 | return; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 117 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 118 | |
| 119 | client->blocked = blocked; |
| 120 | |
| 121 | events = POLLIN; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 122 | if (client->blocked) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 123 | events |= POLLOUT; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 124 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 125 | |
| 126 | console_poller_set_events(client->sh->console, client->poller, events); |
| 127 | } |
| 128 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 129 | static ssize_t send_all(struct client *client, void *buf, size_t len, |
| 130 | bool block) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 131 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 132 | int fd; |
| 133 | int flags; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 134 | ssize_t rc; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 135 | size_t pos; |
| 136 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 137 | if (len > SSIZE_MAX) { |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 138 | return -EINVAL; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 139 | } |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 140 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 141 | fd = client->fd; |
| 142 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 143 | flags = MSG_NOSIGNAL; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 144 | if (!block) { |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 145 | flags |= MSG_DONTWAIT; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 146 | } |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 147 | |
| 148 | for (pos = 0; pos < len; pos += rc) { |
Andrew Jeffery | 21f4cb4 | 2023-04-18 14:38:37 +0930 | [diff] [blame] | 149 | rc = send(fd, (char *)buf + pos, len - pos, flags); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 150 | if (rc < 0) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 151 | if (!block && |
| 152 | (errno == EAGAIN || errno == EWOULDBLOCK)) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 153 | client_set_blocked(client, true); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 154 | break; |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 155 | } |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 156 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 157 | if (errno == EINTR) { |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 158 | continue; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 159 | } |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 160 | |
| 161 | return -1; |
| 162 | } |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 163 | if (rc == 0) { |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 164 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 165 | } |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 168 | return (ssize_t)pos; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 171 | /* Drain the queue to the socket and update the queue buffer. If force_len is |
| 172 | * set, send at least that many bytes from the queue, possibly while blocking |
| 173 | */ |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 174 | static int client_drain_queue(struct client *client, size_t force_len) |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 175 | { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 176 | uint8_t *buf; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 177 | ssize_t wlen; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 178 | size_t len; |
| 179 | size_t total_len; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 180 | bool block; |
| 181 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 182 | total_len = 0; |
| 183 | wlen = 0; |
| 184 | block = !!force_len; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 185 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 186 | /* if we're already blocked, no need for the write */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 187 | if (!block && client->blocked) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 188 | return 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 189 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 190 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 191 | for (;;) { |
| 192 | len = ringbuffer_dequeue_peek(client->rbc, total_len, &buf); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 193 | if (!len) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 194 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 195 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 196 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 197 | wlen = send_all(client, buf, len, block); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 198 | if (wlen <= 0) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 199 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 200 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 201 | |
| 202 | total_len += wlen; |
| 203 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 204 | if (force_len && total_len >= force_len) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 205 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 206 | } |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 207 | } |
| 208 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 209 | if (wlen < 0) { |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 210 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 211 | } |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 212 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 213 | if (force_len && total_len < force_len) { |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 214 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 215 | } |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 216 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 217 | ringbuffer_dequeue_commit(client->rbc, total_len); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 221 | static enum ringbuffer_poll_ret client_ringbuffer_poll(void *arg, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 222 | size_t force_len) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 223 | { |
| 224 | struct client *client = arg; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 225 | size_t len; |
| 226 | int rc; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 227 | |
| 228 | len = ringbuffer_len(client->rbc); |
| 229 | if (!force_len && (len < SOCKET_HANDLER_PKT_SIZE)) { |
| 230 | /* Do nothing until many small requests have accumulated, or |
| 231 | * the UART is idle for awhile (as determined by the timeout |
| 232 | * value supplied to the poll function call in console_server.c. */ |
| 233 | console_poller_set_timeout(client->sh->console, client->poller, |
| 234 | &socket_handler_timeout); |
| 235 | return RINGBUFFER_POLL_OK; |
| 236 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 237 | |
| 238 | rc = client_drain_queue(client, force_len); |
| 239 | if (rc) { |
| 240 | client->rbc = NULL; |
| 241 | client_close(client); |
| 242 | return RINGBUFFER_POLL_REMOVE; |
| 243 | } |
| 244 | |
| 245 | return RINGBUFFER_POLL_OK; |
| 246 | } |
| 247 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 248 | static enum poller_ret |
| 249 | client_timeout(struct handler *handler __attribute__((unused)), void *data) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 250 | { |
| 251 | struct client *client = data; |
| 252 | int rc = 0; |
| 253 | |
| 254 | if (client->blocked) { |
| 255 | /* nothing to do here, we'll call client_drain_queue when |
| 256 | * we become unblocked */ |
| 257 | return POLLER_OK; |
| 258 | } |
| 259 | |
| 260 | rc = client_drain_queue(client, 0); |
| 261 | if (rc) { |
| 262 | client_close(client); |
| 263 | return POLLER_REMOVE; |
| 264 | } |
| 265 | |
| 266 | return POLLER_OK; |
| 267 | } |
| 268 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 269 | static enum poller_ret client_poll(struct handler *handler, int events, |
| 270 | void *data) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 271 | { |
| 272 | struct socket_handler *sh = to_socket_handler(handler); |
| 273 | struct client *client = data; |
| 274 | uint8_t buf[4096]; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 275 | ssize_t rc; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 276 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 277 | if (events & POLLIN) { |
Jeremy Kerr | 68a2ece | 2017-01-17 13:52:41 +0800 | [diff] [blame] | 278 | rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT); |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 279 | if (rc < 0) { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 280 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 281 | return POLLER_OK; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 282 | } |
Andrew Jeffery | 0b7b047 | 2023-04-19 12:48:51 +0930 | [diff] [blame] | 283 | goto err_close; |
Jeremy Kerr | 848fc87 | 2017-01-17 13:50:05 +0800 | [diff] [blame] | 284 | } |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 285 | if (rc == 0) { |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 286 | goto err_close; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 287 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 288 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 289 | console_data_out(sh->console, buf, rc); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 290 | } |
| 291 | |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 292 | if (events & POLLOUT) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 293 | client_set_blocked(client, false); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 294 | rc = client_drain_queue(client, 0); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 295 | if (rc) { |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 296 | goto err_close; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 297 | } |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 298 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 299 | |
| 300 | return POLLER_OK; |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 301 | |
| 302 | err_close: |
| 303 | client->poller = NULL; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 304 | client_close(client); |
Jeremy Kerr | 4d80a5b | 2016-03-18 13:47:43 +0800 | [diff] [blame] | 305 | return POLLER_REMOVE; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 308 | static enum poller_ret socket_poll(struct handler *handler, int events, |
| 309 | void __attribute__((unused)) * data) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 310 | { |
| 311 | struct socket_handler *sh = to_socket_handler(handler); |
| 312 | struct client *client; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 313 | int fd; |
| 314 | int n; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 315 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 316 | if (!(events & POLLIN)) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 317 | return POLLER_OK; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 318 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 319 | |
Jeremy Kerr | 68a2ece | 2017-01-17 13:52:41 +0800 | [diff] [blame] | 320 | fd = accept(sh->sd, NULL, NULL); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 321 | if (fd < 0) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 322 | return POLLER_OK; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 323 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 324 | |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 325 | console_mux_activate(sh->console); |
| 326 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 327 | client = malloc(sizeof(*client)); |
| 328 | memset(client, 0, sizeof(*client)); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 329 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 330 | client->sh = sh; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 331 | client->fd = fd; |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 332 | client->poller = console_poller_register(sh->console, handler, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 333 | client_poll, client_timeout, |
| 334 | client->fd, POLLIN, client); |
| 335 | client->rbc = console_ringbuffer_consumer_register( |
| 336 | sh->console, client_ringbuffer_poll, client); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 337 | |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 338 | n = sh->n_clients++; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 339 | /* |
| 340 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 341 | * pointer type. |
| 342 | */ |
| 343 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 344 | sh->clients = |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 345 | reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients)); |
| 346 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | ce0e68b | 2016-03-18 13:55:46 +0800 | [diff] [blame] | 347 | sh->clients[n] = client; |
| 348 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 349 | return POLLER_OK; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 350 | } |
| 351 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 352 | /* Create socket pair and register one end as poller/consumer and return |
| 353 | * the other end to the caller. |
| 354 | * Return file descriptor on success and negative value on error. |
| 355 | */ |
| 356 | int dbus_create_socket_consumer(struct console *console) |
| 357 | { |
| 358 | struct socket_handler *sh = NULL; |
| 359 | struct client *client; |
| 360 | int fds[2]; |
| 361 | int i; |
| 362 | int rc = -1; |
| 363 | int n; |
| 364 | |
| 365 | for (i = 0; i < console->n_handlers; i++) { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 366 | if (strcmp(console->handlers[i]->type->name, "socket") == 0) { |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 367 | sh = to_socket_handler(console->handlers[i]); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (!sh) { |
| 373 | return -ENOSYS; |
| 374 | } |
| 375 | |
| 376 | /* Create a socketpair */ |
| 377 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); |
| 378 | if (rc < 0) { |
| 379 | warn("Failed to create socket pair"); |
| 380 | return -errno; |
| 381 | } |
| 382 | |
| 383 | client = malloc(sizeof(*client)); |
| 384 | if (client == NULL) { |
| 385 | warnx("Failed to allocate client structure."); |
| 386 | rc = -ENOMEM; |
| 387 | goto close_fds; |
| 388 | } |
| 389 | memset(client, 0, sizeof(*client)); |
| 390 | |
| 391 | client->sh = sh; |
| 392 | client->fd = fds[0]; |
| 393 | client->poller = console_poller_register(sh->console, &sh->handler, |
| 394 | client_poll, client_timeout, |
| 395 | client->fd, POLLIN, client); |
| 396 | client->rbc = console_ringbuffer_consumer_register( |
| 397 | sh->console, client_ringbuffer_poll, client); |
| 398 | if (client->rbc == NULL) { |
| 399 | warnx("Failed to register a consumer.\n"); |
| 400 | rc = -ENOMEM; |
| 401 | goto free_client; |
| 402 | } |
| 403 | |
| 404 | n = sh->n_clients++; |
| 405 | |
| 406 | /* |
| 407 | * We're managing an array of pointers to aggregates, so don't warn about |
| 408 | * sizeof() on a pointer type. |
| 409 | */ |
| 410 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
| 411 | sh->clients = |
| 412 | reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients)); |
| 413 | /* NOLINTEND(bugprone-sizeof-expression) */ |
| 414 | sh->clients[n] = client; |
| 415 | |
| 416 | /* Return the second FD to caller. */ |
| 417 | return fds[1]; |
| 418 | |
| 419 | free_client: |
| 420 | free(client); |
| 421 | close_fds: |
| 422 | close(fds[0]); |
| 423 | close(fds[1]); |
| 424 | return rc; |
| 425 | } |
| 426 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 427 | static struct handler *socket_init(const struct handler_type *type |
| 428 | __attribute__((unused)), |
| 429 | struct console *console, |
| 430 | struct config *config |
| 431 | __attribute__((unused))) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 432 | { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 433 | struct socket_handler *sh; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 434 | struct sockaddr_un addr; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 435 | size_t addrlen; |
| 436 | ssize_t len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 437 | int rc; |
| 438 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 439 | sh = malloc(sizeof(*sh)); |
| 440 | if (!sh) { |
| 441 | return NULL; |
| 442 | } |
| 443 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 444 | sh->console = console; |
| 445 | sh->clients = NULL; |
| 446 | sh->n_clients = 0; |
Jeremy Kerr | 5708dfb | 2017-01-19 15:11:19 +0800 | [diff] [blame] | 447 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 448 | memset(&addr, 0, sizeof(addr)); |
| 449 | addr.sun_family = AF_UNIX; |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 450 | len = console_socket_path(addr.sun_path, console->console_id); |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 451 | if (len < 0) { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 452 | if (errno) { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 453 | warn("Failed to configure socket: %s", strerror(errno)); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 454 | } else { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 455 | warn("Socket name length exceeds buffer limits"); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 456 | } |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 457 | goto err_free; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 458 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 459 | |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 460 | /* Try to take a socket from systemd first */ |
| 461 | if (sd_listen_fds(0) == 1 && |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 462 | sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_STREAM, 1, |
| 463 | addr.sun_path, len) > 0) { |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 464 | sh->sd = SD_LISTEN_FDS_START; |
| 465 | } else { |
| 466 | sh->sd = socket(AF_UNIX, SOCK_STREAM, 0); |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 467 | if (sh->sd < 0) { |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 468 | warn("Can't create socket"); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 469 | goto err_free; |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 470 | } |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 471 | |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 472 | addrlen = sizeof(addr) - sizeof(addr.sun_path) + len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 473 | |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 474 | rc = bind(sh->sd, (struct sockaddr *)&addr, addrlen); |
| 475 | if (rc) { |
| 476 | socket_path_t name; |
| 477 | console_socket_path_readable(&addr, addrlen, name); |
| 478 | warn("Can't bind to socket path %s (terminated at first null)", |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 479 | name); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 480 | goto err_close; |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | rc = listen(sh->sd, 1); |
| 484 | if (rc) { |
| 485 | warn("Can't listen for incoming connections"); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 486 | goto err_close; |
Jonathan Doman | cc07530 | 2023-03-31 10:20:48 -0700 | [diff] [blame] | 487 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 488 | } |
| 489 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 490 | sh->poller = console_poller_register(console, &sh->handler, socket_poll, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 491 | NULL, sh->sd, POLLIN, NULL); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 492 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 493 | return &sh->handler; |
| 494 | |
| 495 | err_close: |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 496 | close(sh->sd); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 497 | err_free: |
| 498 | free(sh); |
| 499 | return NULL; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 500 | } |
| 501 | |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 502 | static void socket_deselect(struct handler *handler) |
| 503 | { |
| 504 | struct socket_handler *sh = to_socket_handler(handler); |
| 505 | |
| 506 | while (sh->n_clients) { |
| 507 | struct client *c = sh->clients[0]; |
| 508 | client_drain_queue(c, 0); |
| 509 | client_close(c); |
| 510 | } |
| 511 | } |
| 512 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 513 | static void socket_fini(struct handler *handler) |
| 514 | { |
| 515 | struct socket_handler *sh = to_socket_handler(handler); |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 516 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 517 | while (sh->n_clients) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 518 | client_close(sh->clients[0]); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 519 | } |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 520 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 521 | if (sh->poller) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 522 | console_poller_unregister(sh->console, sh->poller); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 523 | } |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 524 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 525 | close(sh->sd); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 526 | free(sh); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 527 | } |
| 528 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 529 | static const struct handler_type socket_handler = { |
| 530 | .name = "socket", |
| 531 | .init = socket_init, |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 532 | .deselect = socket_deselect, |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 533 | .fini = socket_fini, |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 534 | }; |
| 535 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 536 | console_handler_register(&socket_handler); |