blob: 5f9d383bc64093d84d30bcd52059c67dfa54233e [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
33#include "console-server.h"
34
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080035#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 Kerr2bd05182016-03-10 16:59:43 +080039struct client {
Andrew Jefferya72711a2023-04-18 18:19:41 +093040 struct socket_handler *sh;
41 struct poller *poller;
42 struct ringbuffer_consumer *rbc;
43 int fd;
44 bool blocked;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080045};
46
47struct socket_handler {
Andrew Jefferya72711a2023-04-18 18:19:41 +093048 struct handler handler;
49 struct console *console;
50 struct poller *poller;
51 int sd;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080052
Andrew Jefferya72711a2023-04-18 18:19:41 +093053 struct client **clients;
54 int n_clients;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080055};
56
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080057static struct timeval const socket_handler_timeout = {
58 .tv_sec = 0,
59 .tv_usec = SOCKET_HANDLER_PKT_US_TIMEOUT
60};
61
Jeremy Kerr2bd05182016-03-10 16:59:43 +080062static struct socket_handler *to_socket_handler(struct handler *handler)
63{
64 return container_of(handler, struct socket_handler, handler);
65}
66
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080067static void client_close(struct client *client)
Jeremy Kerr2bd05182016-03-10 16:59:43 +080068{
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080069 struct socket_handler *sh = client->sh;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080070 int idx;
71
72 close(client->fd);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093073 if (client->poller) {
Jeremy Kerr55c97122017-02-07 17:06:46 +080074 console_poller_unregister(sh->console, client->poller);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093075 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +080076
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093077 if (client->rbc) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080078 ringbuffer_consumer_unregister(client->rbc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093079 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080080
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093081 for (idx = 0; idx < sh->n_clients; idx++) {
82 if (sh->clients[idx] == client) {
Jeremy Kerrce0e68b2016-03-18 13:55:46 +080083 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093084 }
85 }
Jeremy Kerrce0e68b2016-03-18 13:55:46 +080086
87 assert(idx < sh->n_clients);
88
89 free(client);
90 client = NULL;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080091
92 sh->n_clients--;
Andrew Jeffery91b52172023-04-19 12:42:14 +093093 /*
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 Jefferya72711a2023-04-18 18:19:41 +093098 memmove(&sh->clients[idx], &sh->clients[idx + 1],
99 sizeof(*sh->clients) * (sh->n_clients - idx));
100 sh->clients =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930101 reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients));
102 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800103}
104
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800105static void client_set_blocked(struct client *client, bool blocked)
Jeremy Kerr848fc872017-01-17 13:50:05 +0800106{
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800107 int events;
108
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930109 if (client->blocked == blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800110 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930111 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800112
113 client->blocked = blocked;
114
115 events = POLLIN;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930116 if (client->blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800117 events |= POLLOUT;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930118 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800119
120 console_poller_set_events(client->sh->console, client->poller, events);
121}
122
Andrew Jefferya72711a2023-04-18 18:19:41 +0930123static ssize_t send_all(struct client *client, void *buf, size_t len,
124 bool block)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800125{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930126 int fd;
127 int flags;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930128 ssize_t rc;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800129 size_t pos;
130
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930131 if (len > SSIZE_MAX) {
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930132 return -EINVAL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930133 }
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930134
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800135 fd = client->fd;
136
Jeremy Kerr848fc872017-01-17 13:50:05 +0800137 flags = MSG_NOSIGNAL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930138 if (!block) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800139 flags |= MSG_DONTWAIT;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930140 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800141
142 for (pos = 0; pos < len; pos += rc) {
Andrew Jeffery21f4cb42023-04-18 14:38:37 +0930143 rc = send(fd, (char *)buf + pos, len - pos, flags);
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800144 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930145 if (!block &&
146 (errno == EAGAIN || errno == EWOULDBLOCK)) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800147 client_set_blocked(client, true);
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800148 break;
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800149 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800150
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930151 if (errno == EINTR) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800152 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930153 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800154
155 return -1;
156 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930157 if (rc == 0) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800158 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930159 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800160 }
161
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930162 return (ssize_t)pos;
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800163}
164
Jeremy Kerr848fc872017-01-17 13:50:05 +0800165/* Drain the queue to the socket and update the queue buffer. If force_len is
166 * set, send at least that many bytes from the queue, possibly while blocking
167 */
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800168static int client_drain_queue(struct client *client, size_t force_len)
Jeremy Kerr848fc872017-01-17 13:50:05 +0800169{
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800170 uint8_t *buf;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800171 ssize_t wlen;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930172 size_t len;
173 size_t total_len;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800174 bool block;
175
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800176 total_len = 0;
177 wlen = 0;
178 block = !!force_len;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800179
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800180 /* if we're already blocked, no need for the write */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930181 if (!block && client->blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800182 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930183 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800184
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800185 for (;;) {
186 len = ringbuffer_dequeue_peek(client->rbc, total_len, &buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930187 if (!len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800188 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930189 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800190
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800191 wlen = send_all(client, buf, len, block);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930192 if (wlen <= 0) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800193 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930194 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800195
196 total_len += wlen;
197
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930198 if (force_len && total_len >= force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800199 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 }
Jeremy Kerr848fc872017-01-17 13:50:05 +0800201 }
202
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930203 if (wlen < 0) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800204 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930205 }
Jeremy Kerr848fc872017-01-17 13:50:05 +0800206
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930207 if (force_len && total_len < force_len) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800208 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930209 }
Jeremy Kerr848fc872017-01-17 13:50:05 +0800210
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800211 ringbuffer_dequeue_commit(client->rbc, total_len);
Jeremy Kerr848fc872017-01-17 13:50:05 +0800212 return 0;
213}
214
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800215static enum ringbuffer_poll_ret client_ringbuffer_poll(void *arg,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930216 size_t force_len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800217{
218 struct client *client = arg;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930219 size_t len;
220 int rc;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800221
222 len = ringbuffer_len(client->rbc);
223 if (!force_len && (len < SOCKET_HANDLER_PKT_SIZE)) {
224 /* Do nothing until many small requests have accumulated, or
225 * the UART is idle for awhile (as determined by the timeout
226 * value supplied to the poll function call in console_server.c. */
227 console_poller_set_timeout(client->sh->console, client->poller,
228 &socket_handler_timeout);
229 return RINGBUFFER_POLL_OK;
230 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800231
232 rc = client_drain_queue(client, force_len);
233 if (rc) {
234 client->rbc = NULL;
235 client_close(client);
236 return RINGBUFFER_POLL_REMOVE;
237 }
238
239 return RINGBUFFER_POLL_OK;
240}
241
Andrew Jefferya72711a2023-04-18 18:19:41 +0930242static enum poller_ret
243client_timeout(struct handler *handler __attribute__((unused)), void *data)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800244{
245 struct client *client = data;
246 int rc = 0;
247
248 if (client->blocked) {
249 /* nothing to do here, we'll call client_drain_queue when
250 * we become unblocked */
251 return POLLER_OK;
252 }
253
254 rc = client_drain_queue(client, 0);
255 if (rc) {
256 client_close(client);
257 return POLLER_REMOVE;
258 }
259
260 return POLLER_OK;
261}
262
Andrew Jefferya72711a2023-04-18 18:19:41 +0930263static enum poller_ret client_poll(struct handler *handler, int events,
264 void *data)
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800265{
266 struct socket_handler *sh = to_socket_handler(handler);
267 struct client *client = data;
268 uint8_t buf[4096];
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930269 ssize_t rc;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800270
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800271 if (events & POLLIN) {
Jeremy Kerr68a2ece2017-01-17 13:52:41 +0800272 rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT);
Jeremy Kerr848fc872017-01-17 13:50:05 +0800273 if (rc < 0) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930274 if (errno == EAGAIN || errno == EWOULDBLOCK) {
Jeremy Kerr848fc872017-01-17 13:50:05 +0800275 return POLLER_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930276 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930277 goto err_close;
Jeremy Kerr848fc872017-01-17 13:50:05 +0800278 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930279 if (rc == 0) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800280 goto err_close;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930281 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800282
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800283 console_data_out(sh->console, buf, rc);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800284 }
285
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800286 if (events & POLLOUT) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800287 client_set_blocked(client, false);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800288 rc = client_drain_queue(client, 0);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930289 if (rc) {
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800290 goto err_close;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930291 }
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800292 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800293
294 return POLLER_OK;
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800295
296err_close:
297 client->poller = NULL;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800298 client_close(client);
Jeremy Kerr4d80a5b2016-03-18 13:47:43 +0800299 return POLLER_REMOVE;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800300}
301
Andrew Jefferya72711a2023-04-18 18:19:41 +0930302static enum poller_ret socket_poll(struct handler *handler, int events,
303 void __attribute__((unused)) * data)
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800304{
305 struct socket_handler *sh = to_socket_handler(handler);
306 struct client *client;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930307 int fd;
308 int n;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800309
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930310 if (!(events & POLLIN)) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800311 return POLLER_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930312 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800313
Jeremy Kerr68a2ece2017-01-17 13:52:41 +0800314 fd = accept(sh->sd, NULL, NULL);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930315 if (fd < 0) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800316 return POLLER_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930317 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800318
Jeremy Kerrce0e68b2016-03-18 13:55:46 +0800319 client = malloc(sizeof(*client));
320 memset(client, 0, sizeof(*client));
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800321
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800322 client->sh = sh;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800323 client->fd = fd;
Jeremy Kerr55c97122017-02-07 17:06:46 +0800324 client->poller = console_poller_register(sh->console, handler,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930325 client_poll, client_timeout,
326 client->fd, POLLIN, client);
327 client->rbc = console_ringbuffer_consumer_register(
328 sh->console, client_ringbuffer_poll, client);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800329
Jeremy Kerrce0e68b2016-03-18 13:55:46 +0800330 n = sh->n_clients++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930331 /*
332 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
333 * pointer type.
334 */
335 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930336 sh->clients =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930337 reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients));
338 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrce0e68b2016-03-18 13:55:46 +0800339 sh->clients[n] = client;
340
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800341 return POLLER_OK;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800342}
343
Ninad Palsulebd992c92023-05-09 18:30:01 -0500344/* Create socket pair and register one end as poller/consumer and return
345 * the other end to the caller.
346 * Return file descriptor on success and negative value on error.
347 */
348int dbus_create_socket_consumer(struct console *console)
349{
350 struct socket_handler *sh = NULL;
351 struct client *client;
352 int fds[2];
353 int i;
354 int rc = -1;
355 int n;
356
357 for (i = 0; i < console->n_handlers; i++) {
358 if (strcmp(console->handlers[i]->name, "socket") == 0) {
359 sh = to_socket_handler(console->handlers[i]);
360 break;
361 }
362 }
363
364 if (!sh) {
365 return -ENOSYS;
366 }
367
368 /* Create a socketpair */
369 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
370 if (rc < 0) {
371 warn("Failed to create socket pair");
372 return -errno;
373 }
374
375 client = malloc(sizeof(*client));
376 if (client == NULL) {
377 warnx("Failed to allocate client structure.");
378 rc = -ENOMEM;
379 goto close_fds;
380 }
381 memset(client, 0, sizeof(*client));
382
383 client->sh = sh;
384 client->fd = fds[0];
385 client->poller = console_poller_register(sh->console, &sh->handler,
386 client_poll, client_timeout,
387 client->fd, POLLIN, client);
388 client->rbc = console_ringbuffer_consumer_register(
389 sh->console, client_ringbuffer_poll, client);
390 if (client->rbc == NULL) {
391 warnx("Failed to register a consumer.\n");
392 rc = -ENOMEM;
393 goto free_client;
394 }
395
396 n = sh->n_clients++;
397
398 /*
399 * We're managing an array of pointers to aggregates, so don't warn about
400 * sizeof() on a pointer type.
401 */
402 /* NOLINTBEGIN(bugprone-sizeof-expression) */
403 sh->clients =
404 reallocarray(sh->clients, sh->n_clients, sizeof(*sh->clients));
405 /* NOLINTEND(bugprone-sizeof-expression) */
406 sh->clients[n] = client;
407
408 /* Return the second FD to caller. */
409 return fds[1];
410
411free_client:
412 free(client);
413close_fds:
414 close(fds[0]);
415 close(fds[1]);
416 return rc;
417}
418
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800419static int socket_init(struct handler *handler, struct console *console,
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930420 struct config *config __attribute__((unused)))
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800421{
422 struct socket_handler *sh = to_socket_handler(handler);
423 struct sockaddr_un addr;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030424 size_t addrlen;
425 ssize_t len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800426 int rc;
427
428 sh->console = console;
429 sh->clients = NULL;
430 sh->n_clients = 0;
Jeremy Kerr5708dfb2017-01-19 15:11:19 +0800431
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800432 memset(&addr, 0, sizeof(addr));
433 addr.sun_family = AF_UNIX;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930434 len = console_socket_path(addr.sun_path, console->console_id);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030435 if (len < 0) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930436 if (errno) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030437 warn("Failed to configure socket: %s", strerror(errno));
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930438 } else {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030439 warn("Socket name length exceeds buffer limits");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930440 }
Jonathan Domancc075302023-03-31 10:20:48 -0700441 return -1;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030442 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800443
Jonathan Domancc075302023-03-31 10:20:48 -0700444 /* Try to take a socket from systemd first */
445 if (sd_listen_fds(0) == 1 &&
Andrew Jefferya72711a2023-04-18 18:19:41 +0930446 sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_STREAM, 1,
447 addr.sun_path, len) > 0) {
Jonathan Domancc075302023-03-31 10:20:48 -0700448 sh->sd = SD_LISTEN_FDS_START;
449 } else {
450 sh->sd = socket(AF_UNIX, SOCK_STREAM, 0);
Andrew Jefferya72711a2023-04-18 18:19:41 +0930451 if (sh->sd < 0) {
Jonathan Domancc075302023-03-31 10:20:48 -0700452 warn("Can't create socket");
453 return -1;
454 }
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030455
Jonathan Domancc075302023-03-31 10:20:48 -0700456 addrlen = sizeof(addr) - sizeof(addr.sun_path) + len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800457
Jonathan Domancc075302023-03-31 10:20:48 -0700458 rc = bind(sh->sd, (struct sockaddr *)&addr, addrlen);
459 if (rc) {
460 socket_path_t name;
461 console_socket_path_readable(&addr, addrlen, name);
462 warn("Can't bind to socket path %s (terminated at first null)",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930463 name);
Jonathan Domancc075302023-03-31 10:20:48 -0700464 goto cleanup;
465 }
466
467 rc = listen(sh->sd, 1);
468 if (rc) {
469 warn("Can't listen for incoming connections");
470 goto cleanup;
471 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800472 }
473
Jeremy Kerr55c97122017-02-07 17:06:46 +0800474 sh->poller = console_poller_register(console, handler, socket_poll,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930475 NULL, sh->sd, POLLIN, NULL);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800476
477 return 0;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030478cleanup:
479 close(sh->sd);
480 return -1;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800481}
482
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800483static void socket_fini(struct handler *handler)
484{
485 struct socket_handler *sh = to_socket_handler(handler);
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800486
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930487 while (sh->n_clients) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800488 client_close(sh->clients[0]);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930489 }
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800490
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930491 if (sh->poller) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800492 console_poller_unregister(sh->console, sh->poller);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930493 }
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800494
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800495 close(sh->sd);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800496}
497
498static struct socket_handler socket_handler = {
499 .handler = {
500 .name = "socket",
501 .init = socket_init,
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800502 .fini = socket_fini,
503 },
504};
505
Jeremy Kerr55c97122017-02-07 17:06:46 +0800506console_handler_register(&socket_handler.handler);