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