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