blob: efcd5b8bfc2c75bedf5af0c8c1dbb11819de9767 [file] [log] [blame]
Jeremy Kerr9326d772016-03-17 17:15:02 +08001/**
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 Kerr2bd05182016-03-10 16:59:43 +080016
Jeremy Kerrce0e68b2016-03-18 13:55:46 +080017#include <assert.h>
Jeremy Kerr2bd05182016-03-10 16:59:43 +080018#include <err.h>
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +080019#include <errno.h>
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093020#include <limits.h>
Jeremy Kerr2bd05182016-03-10 16:59:43 +080021#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 Domancc075302023-03-31 10:20:48 -070031#include <systemd/sd-daemon.h>
Jeremy Kerr2bd05182016-03-10 16:59:43 +080032
Alexander Hansena6b29102024-07-10 15:33:37 +020033#include "console-mux.h"
Jeremy Kerr2bd05182016-03-10 16:59:43 +080034#include "console-server.h"
35
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080036#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 Kerr2bd05182016-03-10 16:59:43 +080040struct client {
Andrew Jefferya72711a2023-04-18 18:19:41 +093041 struct socket_handler *sh;
42 struct poller *poller;
43 struct ringbuffer_consumer *rbc;
44 int fd;
45 bool blocked;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080046};
47
48struct socket_handler {
Andrew Jefferya72711a2023-04-18 18:19:41 +093049 struct handler handler;
50 struct console *console;
51 struct poller *poller;
52 int sd;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080053
Andrew Jefferya72711a2023-04-18 18:19:41 +093054 struct client **clients;
55 int n_clients;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080056};
57
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080058static struct timeval const socket_handler_timeout = {
59 .tv_sec = 0,
60 .tv_usec = SOCKET_HANDLER_PKT_US_TIMEOUT
61};
62
Jeremy Kerr2bd05182016-03-10 16:59:43 +080063static struct socket_handler *to_socket_handler(struct handler *handler)
64{
65 return container_of(handler, struct socket_handler, handler);
66}
67
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080068static void client_close(struct client *client)
Jeremy Kerr2bd05182016-03-10 16:59:43 +080069{
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080070 struct socket_handler *sh = client->sh;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080071 int idx;
72
73 close(client->fd);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093074 if (client->poller) {
Jeremy Kerr55c97122017-02-07 17:06:46 +080075 console_poller_unregister(sh->console, client->poller);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093076 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +080077
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093078 if (client->rbc) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080079 ringbuffer_consumer_unregister(client->rbc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093080 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080081
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093082 for (idx = 0; idx < sh->n_clients; idx++) {
83 if (sh->clients[idx] == client) {
Jeremy Kerrce0e68b2016-03-18 13:55:46 +080084 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093085 }
86 }
Jeremy Kerrce0e68b2016-03-18 13:55:46 +080087
88 assert(idx < sh->n_clients);
89
90 free(client);
91 client = NULL;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080092
93 sh->n_clients--;
Andrew Jeffery91b52172023-04-19 12:42:14 +093094 /*
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 Jefferya72711a2023-04-18 18:19:41 +093099 memmove(&sh->clients[idx], &sh->clients[idx + 1],
100 sizeof(*sh->clients) * (sh->n_clients - idx));
Andrew Jeffery7851a392024-07-09 16:30:22 +0930101 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 Jeffery91b52172023-04-19 12:42:14 +0930108 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800109}
110
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800111static void client_set_blocked(struct client *client, bool blocked)
Jeremy Kerr848fc872017-01-17 13:50:05 +0800112{
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800113 int events;
114
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930115 if (client->blocked == blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800116 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930117 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800118
119 client->blocked = blocked;
120
121 events = POLLIN;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930122 if (client->blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800123 events |= POLLOUT;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930124 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800125
126 console_poller_set_events(client->sh->console, client->poller, events);
127}
128
Andrew Jefferya72711a2023-04-18 18:19:41 +0930129static ssize_t send_all(struct client *client, void *buf, size_t len,
130 bool block)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800131{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930132 int fd;
133 int flags;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930134 ssize_t rc;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800135 size_t pos;
136
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930137 if (len > SSIZE_MAX) {
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930138 return -EINVAL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930139 }
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930140
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800141 fd = client->fd;
142
Jeremy Kerr848fc872017-01-17 13:50:05 +0800143 flags = MSG_NOSIGNAL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930144 if (!block) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800145 flags |= MSG_DONTWAIT;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930146 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800147
148 for (pos = 0; pos < len; pos += rc) {
Andrew Jeffery21f4cb42023-04-18 14:38:37 +0930149 rc = send(fd, (char *)buf + pos, len - pos, flags);
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800150 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930151 if (!block &&
152 (errno == EAGAIN || errno == EWOULDBLOCK)) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800153 client_set_blocked(client, true);
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800154 break;
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800155 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800156
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930157 if (errno == EINTR) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800158 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930159 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800160
161 return -1;
162 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930163 if (rc == 0) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800164 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930165 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800166 }
167
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930168 return (ssize_t)pos;
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800169}
170
Jeremy Kerr848fc872017-01-17 13:50:05 +0800171/* 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 Kerrc9775ce2017-02-07 16:25:34 +0800174static int client_drain_queue(struct client *client, size_t force_len)
Jeremy Kerr848fc872017-01-17 13:50:05 +0800175{
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800176 uint8_t *buf;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800177 ssize_t wlen;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930178 size_t len;
179 size_t total_len;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800180 bool block;
181
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800182 total_len = 0;
183 wlen = 0;
184 block = !!force_len;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800185
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800186 /* if we're already blocked, no need for the write */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930187 if (!block && client->blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800188 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930189 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800190
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800191 for (;;) {
192 len = ringbuffer_dequeue_peek(client->rbc, total_len, &buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930193 if (!len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800194 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930195 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800196
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800197 wlen = send_all(client, buf, len, block);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930198 if (wlen <= 0) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800199 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800201
202 total_len += wlen;
203
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930204 if (force_len && total_len >= force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800205 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930206 }
Jeremy Kerr848fc872017-01-17 13:50:05 +0800207 }
208
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930209 if (wlen < 0) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800210 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930211 }
Jeremy Kerr848fc872017-01-17 13:50:05 +0800212
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930213 if (force_len && total_len < force_len) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800214 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930215 }
Jeremy Kerr848fc872017-01-17 13:50:05 +0800216
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800217 ringbuffer_dequeue_commit(client->rbc, total_len);
Jeremy Kerr848fc872017-01-17 13:50:05 +0800218 return 0;
219}
220
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800221static enum ringbuffer_poll_ret client_ringbuffer_poll(void *arg,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930222 size_t force_len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800223{
224 struct client *client = arg;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930225 size_t len;
226 int rc;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800227
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 Kerrc9775ce2017-02-07 16:25:34 +0800237
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 Jefferya72711a2023-04-18 18:19:41 +0930248static enum poller_ret
249client_timeout(struct handler *handler __attribute__((unused)), void *data)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800250{
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 Jefferya72711a2023-04-18 18:19:41 +0930269static enum poller_ret client_poll(struct handler *handler, int events,
270 void *data)
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800271{
272 struct socket_handler *sh = to_socket_handler(handler);
273 struct client *client = data;
274 uint8_t buf[4096];
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930275 ssize_t rc;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800276
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800277 if (events & POLLIN) {
Jeremy Kerr68a2ece2017-01-17 13:52:41 +0800278 rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT);
Jeremy Kerr848fc872017-01-17 13:50:05 +0800279 if (rc < 0) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930280 if (errno == EAGAIN || errno == EWOULDBLOCK) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800281 return POLLER_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930282 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930283 goto err_close;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800284 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930285 if (rc == 0) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800286 goto err_close;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930287 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800288
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800289 console_data_out(sh->console, buf, rc);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800290 }
291
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800292 if (events & POLLOUT) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800293 client_set_blocked(client, false);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800294 rc = client_drain_queue(client, 0);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930295 if (rc) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800296 goto err_close;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930297 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800298 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800299
300 return POLLER_OK;
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800301
302err_close:
303 client->poller = NULL;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800304 client_close(client);
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800305 return POLLER_REMOVE;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800306}
307
Andrew Jefferya72711a2023-04-18 18:19:41 +0930308static enum poller_ret socket_poll(struct handler *handler, int events,
309 void __attribute__((unused)) * data)
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800310{
311 struct socket_handler *sh = to_socket_handler(handler);
312 struct client *client;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930313 int fd;
314 int n;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800315
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930316 if (!(events & POLLIN)) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800317 return POLLER_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930318 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800319
Jeremy Kerr68a2ece2017-01-17 13:52:41 +0800320 fd = accept(sh->sd, NULL, NULL);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930321 if (fd < 0) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800322 return POLLER_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930323 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800324
Alexander Hansena6b29102024-07-10 15:33:37 +0200325 console_mux_activate(sh->console);
326
Jeremy Kerrce0e68b2016-03-18 13:55:46 +0800327 client = malloc(sizeof(*client));
328 memset(client, 0, sizeof(*client));
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800329
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800330 client->sh = sh;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800331 client->fd = fd;
Jeremy Kerr55c97122017-02-07 17:06:46 +0800332 client->poller = console_poller_register(sh->console, handler,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930333 client_poll, client_timeout,
334 client->fd, POLLIN, client);
335 client->rbc = console_ringbuffer_consumer_register(
336 sh->console, client_ringbuffer_poll, client);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800337
Jeremy Kerrce0e68b2016-03-18 13:55:46 +0800338 n = sh->n_clients++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930339 /*
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 Jefferya72711a2023-04-18 18:19:41 +0930344 sh->clients =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930345 reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients));
346 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrce0e68b2016-03-18 13:55:46 +0800347 sh->clients[n] = client;
348
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800349 return POLLER_OK;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800350}
351
Ninad Palsulebd992c92023-05-09 18:30:01 -0500352/* 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 */
356int 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 Kerre2826c72024-07-05 10:54:21 +0800366 if (strcmp(console->handlers[i]->type->name, "socket") == 0) {
Ninad Palsulebd992c92023-05-09 18:30:01 -0500367 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
419free_client:
420 free(client);
421close_fds:
422 close(fds[0]);
423 close(fds[1]);
424 return rc;
425}
426
Jeremy Kerre2826c72024-07-05 10:54:21 +0800427static struct handler *socket_init(const struct handler_type *type
428 __attribute__((unused)),
429 struct console *console,
430 struct config *config
431 __attribute__((unused)))
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800432{
Jeremy Kerre2826c72024-07-05 10:54:21 +0800433 struct socket_handler *sh;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800434 struct sockaddr_un addr;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030435 size_t addrlen;
436 ssize_t len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800437 int rc;
438
Jeremy Kerre2826c72024-07-05 10:54:21 +0800439 sh = malloc(sizeof(*sh));
440 if (!sh) {
441 return NULL;
442 }
443
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800444 sh->console = console;
445 sh->clients = NULL;
446 sh->n_clients = 0;
Jeremy Kerr5708dfb2017-01-19 15:11:19 +0800447
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800448 memset(&addr, 0, sizeof(addr));
449 addr.sun_family = AF_UNIX;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930450 len = console_socket_path(addr.sun_path, console->console_id);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030451 if (len < 0) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930452 if (errno) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030453 warn("Failed to configure socket: %s", strerror(errno));
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930454 } else {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030455 warn("Socket name length exceeds buffer limits");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930456 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800457 goto err_free;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030458 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800459
Jonathan Domancc075302023-03-31 10:20:48 -0700460 /* Try to take a socket from systemd first */
461 if (sd_listen_fds(0) == 1 &&
Andrew Jefferya72711a2023-04-18 18:19:41 +0930462 sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_STREAM, 1,
463 addr.sun_path, len) > 0) {
Jonathan Domancc075302023-03-31 10:20:48 -0700464 sh->sd = SD_LISTEN_FDS_START;
465 } else {
466 sh->sd = socket(AF_UNIX, SOCK_STREAM, 0);
Andrew Jefferya72711a2023-04-18 18:19:41 +0930467 if (sh->sd < 0) {
Jonathan Domancc075302023-03-31 10:20:48 -0700468 warn("Can't create socket");
Jeremy Kerre2826c72024-07-05 10:54:21 +0800469 goto err_free;
Jonathan Domancc075302023-03-31 10:20:48 -0700470 }
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030471
Jonathan Domancc075302023-03-31 10:20:48 -0700472 addrlen = sizeof(addr) - sizeof(addr.sun_path) + len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800473
Jonathan Domancc075302023-03-31 10:20:48 -0700474 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 Jefferya72711a2023-04-18 18:19:41 +0930479 name);
Jeremy Kerre2826c72024-07-05 10:54:21 +0800480 goto err_close;
Jonathan Domancc075302023-03-31 10:20:48 -0700481 }
482
483 rc = listen(sh->sd, 1);
484 if (rc) {
485 warn("Can't listen for incoming connections");
Jeremy Kerre2826c72024-07-05 10:54:21 +0800486 goto err_close;
Jonathan Domancc075302023-03-31 10:20:48 -0700487 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800488 }
489
Jeremy Kerre2826c72024-07-05 10:54:21 +0800490 sh->poller = console_poller_register(console, &sh->handler, socket_poll,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930491 NULL, sh->sd, POLLIN, NULL);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800492
Jeremy Kerre2826c72024-07-05 10:54:21 +0800493 return &sh->handler;
494
495err_close:
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030496 close(sh->sd);
Jeremy Kerre2826c72024-07-05 10:54:21 +0800497err_free:
498 free(sh);
499 return NULL;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800500}
501
Alexander Hansena6b29102024-07-10 15:33:37 +0200502static 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 Kerr2bd05182016-03-10 16:59:43 +0800513static void socket_fini(struct handler *handler)
514{
515 struct socket_handler *sh = to_socket_handler(handler);
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800516
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930517 while (sh->n_clients) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800518 client_close(sh->clients[0]);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930519 }
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800520
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930521 if (sh->poller) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800522 console_poller_unregister(sh->console, sh->poller);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930523 }
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800524
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800525 close(sh->sd);
Jeremy Kerre2826c72024-07-05 10:54:21 +0800526 free(sh);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800527}
528
Jeremy Kerre2826c72024-07-05 10:54:21 +0800529static const struct handler_type socket_handler = {
530 .name = "socket",
531 .init = socket_init,
Alexander Hansena6b29102024-07-10 15:33:37 +0200532 .deselect = socket_deselect,
Jeremy Kerre2826c72024-07-05 10:54:21 +0800533 .fini = socket_fini,
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800534};
535
Jeremy Kerre2826c72024-07-05 10:54:21 +0800536console_handler_register(&socket_handler);