blob: aeb2de0739248c5522bb4e9038c25d3a14210234 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +08003#define _GNU_SOURCE
4
Andrew Jefferyd4103f82021-06-16 14:39:36 +09305#include "config.h"
Andrew Jeffery5ab78252022-02-17 21:04:59 +10306
7#define SD_LISTEN_FDS_START 3
8
9#include "compiler.h"
10#include "libmctp.h"
11#include "libmctp-serial.h"
12#include "libmctp-astlpc.h"
Andrew Jefferycad47302021-08-20 21:37:57 +093013#include "utils/mctp-capture.h"
Andrew Jefferyd4103f82021-06-16 14:39:36 +093014
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080015#include <assert.h>
16#include <err.h>
Andrew Jeffery6896d412020-03-11 09:25:32 +103017#include <errno.h>
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080018#include <getopt.h>
Andrew Jefferyb93b6112020-06-05 14:13:44 +093019#include <limits.h>
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080020#include <poll.h>
Andrew Jeffery490e3872021-08-24 21:11:30 +093021#include <signal.h>
Andrew Jeffery04b81fc2020-02-05 13:07:29 +103022#include <stdbool.h>
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
Andrew Jeffery490e3872021-08-24 21:11:30 +093028#include <sys/signalfd.h>
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080029#include <sys/socket.h>
30#include <sys/un.h>
31
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
33
Andrew Jefferyd4103f82021-06-16 14:39:36 +093034#if HAVE_SYSTEMD_SD_DAEMON_H
35#include <systemd/sd-daemon.h>
36#else
37static inline int sd_listen_fds(int i __unused)
38{
39 return -1;
40}
41#endif
42
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080043static const mctp_eid_t local_eid_default = 8;
44static char sockname[] = "\0mctp-mux";
45
46struct binding {
47 const char *name;
48 int (*init)(struct mctp *mctp, struct binding *binding,
49 mctp_eid_t eid, int n_params,
50 char * const * params);
Andrew Jefferyedebe162021-08-26 12:35:00 +093051 void (*destroy)(struct mctp *mctp, struct binding *binding);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080052 int (*get_fd)(struct binding *binding);
53 int (*process)(struct binding *binding);
54 void *data;
55};
56
57struct client {
58 bool active;
59 int sock;
60 uint8_t type;
61};
62
63struct ctx {
64 struct mctp *mctp;
65 struct binding *binding;
66 bool verbose;
67 int local_eid;
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +080068 void *buf;
69 size_t buf_size;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080070
71 int sock;
72 struct pollfd *pollfds;
73
74 struct client *clients;
75 int n_clients;
Andrew Jefferycad47302021-08-20 21:37:57 +093076
77 struct {
78 struct capture binding;
79 struct capture socket;
80 } pcap;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080081};
82
83static void tx_message(struct ctx *ctx, mctp_eid_t eid, void *msg, size_t len)
84{
Andrew Jeffery06735052021-01-27 23:50:55 +103085 int rc;
86
Sumanth Bhatf39c3852022-01-10 17:04:10 +053087 rc = mctp_message_tx(ctx->mctp, eid, MCTP_MESSAGE_TO_SRC, 0, msg, len);
Andrew Jeffery06735052021-01-27 23:50:55 +103088 if (rc)
89 warnx("Failed to send message: %d", rc);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080090}
91
92static void client_remove_inactive(struct ctx *ctx)
93{
94 int i;
95
96 for (i = 0; i < ctx->n_clients; i++) {
97 struct client *client = &ctx->clients[i];
98 if (client->active)
99 continue;
100 close(client->sock);
101
102 ctx->n_clients--;
103 memmove(&ctx->clients[i], &ctx->clients[i+1],
104 (ctx->n_clients - i) * sizeof(*ctx->clients));
105 ctx->clients = realloc(ctx->clients,
106 ctx->n_clients * sizeof(*ctx->clients));
107 }
108}
109
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530110static void
111rx_message(uint8_t eid, bool tag_owner __unused, uint8_t msg_tag __unused,
112 void *data, void *msg, size_t len)
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800113{
114 struct ctx *ctx = data;
115 struct iovec iov[2];
116 struct msghdr msghdr;
117 bool removed;
118 uint8_t type;
119 int i, rc;
120
121 if (len < 2)
122 return;
123
124 type = *(uint8_t *)msg;
125
126 if (ctx->verbose)
127 fprintf(stderr, "MCTP message received: len %zd, type %d\n",
128 len, type);
129
130 memset(&msghdr, 0, sizeof(msghdr));
131 msghdr.msg_iov = iov;
132 msghdr.msg_iovlen = 2;
133 iov[0].iov_base = &eid;
134 iov[0].iov_len = 1;
135 iov[1].iov_base = msg;
136 iov[1].iov_len = len;
137
138 for (i = 0; i < ctx->n_clients; i++) {
139 struct client *client = &ctx->clients[i];
140
141 if (client->type != type)
142 continue;
143
144 if (ctx->verbose)
145 fprintf(stderr, " forwarding to client %d\n", i);
146
147 rc = sendmsg(client->sock, &msghdr, 0);
148 if (rc != (ssize_t)(len + 1)) {
149 client->active = false;
150 removed = true;
151 }
152 }
153
154 if (removed)
155 client_remove_inactive(ctx);
156
157}
158
Jeremy Kerr34b9b3d2019-11-27 11:27:05 +0800159static int binding_null_init(struct mctp *mctp __unused,
160 struct binding *binding __unused,
161 mctp_eid_t eid __unused,
162 int n_params, char * const *params __unused)
163{
164 if (n_params != 0) {
165 warnx("null binding doesn't accept parameters");
166 return -1;
167 }
168 return 0;
169}
170
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800171static int binding_serial_init(struct mctp *mctp, struct binding *binding,
172 mctp_eid_t eid, int n_params, char * const *params)
173{
174 struct mctp_binding_serial *serial;
175 const char *path;
176 int rc;
177
178 if (n_params != 1) {
179 warnx("serial binding requires device param");
180 return -1;
181 }
182
183 path = params[0];
184
185 serial = mctp_serial_init();
186 assert(serial);
187
188 rc = mctp_serial_open_path(serial, path);
189 if (rc)
190 return -1;
191
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800192 mctp_register_bus(mctp, mctp_binding_serial_core(serial), eid);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800193
194 binding->data = serial;
195
196 return 0;
197}
198
199static int binding_serial_get_fd(struct binding *binding)
200{
201 return mctp_serial_get_fd(binding->data);
202}
203
204static int binding_serial_process(struct binding *binding)
205{
206 return mctp_serial_read(binding->data);
207}
208
209static int binding_astlpc_init(struct mctp *mctp, struct binding *binding,
210 mctp_eid_t eid, int n_params,
211 char * const *params __attribute__((unused)))
212{
213 struct mctp_binding_astlpc *astlpc;
214
215 if (n_params) {
216 warnx("astlpc binding does not accept parameters");
217 return -1;
218 }
219
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530220 astlpc = mctp_astlpc_init_fileio();
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800221 if (!astlpc) {
222 warnx("could not initialise astlpc binding");
223 return -1;
224 }
225
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800226 mctp_register_bus(mctp, mctp_binding_astlpc_core(astlpc), eid);
227
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800228 binding->data = astlpc;
229 return 0;
230}
231
Andrew Jefferyedebe162021-08-26 12:35:00 +0930232static void binding_astlpc_destroy(struct mctp *mctp, struct binding *binding)
233{
234 struct mctp_binding_astlpc *astlpc = binding->data;
235
236 mctp_unregister_bus(mctp, mctp_binding_astlpc_core(astlpc));
237
238 mctp_astlpc_destroy(astlpc);
239}
240
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800241static int binding_astlpc_get_fd(struct binding *binding)
242{
243 return mctp_astlpc_get_fd(binding->data);
244}
245
246static int binding_astlpc_process(struct binding *binding)
247{
248 return mctp_astlpc_poll(binding->data);
249}
250
251struct binding bindings[] = {
252 {
Jeremy Kerr34b9b3d2019-11-27 11:27:05 +0800253 .name = "null",
254 .init = binding_null_init,
255 },
256 {
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800257 .name = "serial",
258 .init = binding_serial_init,
Andrew Jefferyedebe162021-08-26 12:35:00 +0930259 .destroy = NULL,
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800260 .get_fd = binding_serial_get_fd,
261 .process = binding_serial_process,
262 },
263 {
264 .name = "astlpc",
265 .init = binding_astlpc_init,
Andrew Jefferyedebe162021-08-26 12:35:00 +0930266 .destroy = binding_astlpc_destroy,
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800267 .get_fd = binding_astlpc_get_fd,
268 .process = binding_astlpc_process,
269 }
270};
271
272struct binding *binding_lookup(const char *name)
273{
274 struct binding *binding;
275 unsigned int i;
276
277 for (i = 0; i < ARRAY_SIZE(bindings); i++) {
278 binding = &bindings[i];
279
280 if (!strcmp(binding->name, name))
281 return binding;
282 }
283
284 return NULL;
285}
286
287static int socket_init(struct ctx *ctx)
288{
289 struct sockaddr_un addr;
290 int namelen, rc;
291
292 namelen = sizeof(sockname) - 1;
293 addr.sun_family = AF_UNIX;
294 memcpy(addr.sun_path, sockname, namelen);
295
296 ctx->sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
297 if (ctx->sock < 0) {
298 warn("can't create socket");
299 return -1;
300 }
301
302 rc = bind(ctx->sock, (struct sockaddr *)&addr,
303 sizeof(addr.sun_family) + namelen);
304 if (rc) {
305 warn("can't bind socket");
306 goto err_close;
307 }
308
309 rc = listen(ctx->sock, 1);
310 if (rc) {
311 warn("can't listen on socket");
312 goto err_close;
313 }
314
315 return 0;
316
317err_close:
318 close(ctx->sock);
319 return -1;
320}
321
322static int socket_process(struct ctx *ctx)
323{
324 struct client *client;
325 int fd;
326
327 fd = accept4(ctx->sock, NULL, 0, SOCK_NONBLOCK);
328 if (fd < 0)
329 return -1;
330
331 ctx->n_clients++;
332 ctx->clients = realloc(ctx->clients,
333 ctx->n_clients * sizeof(struct client));
334
335 client = &ctx->clients[ctx->n_clients-1];
Andrew Jeffery8676c932020-01-24 12:22:21 +1030336 memset(client, 0, sizeof(*client));
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800337 client->active = true;
338 client->sock = fd;
339
340 return 0;
341}
342
343static int client_process_recv(struct ctx *ctx, int idx)
344{
345 struct client *client = &ctx->clients[idx];
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800346 uint8_t eid;
347 ssize_t len;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800348 int rc;
349
350 /* are we waiting for a type message? */
351 if (!client->type) {
352 uint8_t type;
353 rc = read(client->sock, &type, 1);
354 if (rc <= 0)
355 goto out_close;
356
357 if (type == 0) {
358 rc = -1;
359 goto out_close;
360 }
361 if (ctx->verbose)
362 fprintf(stderr, "client[%d] registered for type %u\n",
363 idx, type);
364 client->type = type;
365 return 0;
366 }
367
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800368 len = recv(client->sock, NULL, 0, MSG_PEEK | MSG_TRUNC);
369 if (len < 0) {
Andrew Jeffery6896d412020-03-11 09:25:32 +1030370 if (errno != ECONNRESET)
371 warn("can't receive (peek) from client");
372
Andrew Jeffery13a40412020-02-07 17:05:20 +1030373 rc = -1;
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800374 goto out_close;
375 }
376
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930377 if ((size_t)len > ctx->buf_size) {
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800378 void *tmp;
379
380 tmp = realloc(ctx->buf, len);
381 if (!tmp) {
382 warn("can't allocate for incoming message");
Andrew Jeffery13a40412020-02-07 17:05:20 +1030383 rc = -1;
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800384 goto out_close;
385 }
386 ctx->buf = tmp;
387 ctx->buf_size = len;
388 }
389
390 rc = recv(client->sock, ctx->buf, ctx->buf_size, 0);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800391 if (rc < 0) {
Andrew Jeffery6896d412020-03-11 09:25:32 +1030392 if (errno != ECONNRESET)
393 warn("can't receive from client");
Andrew Jeffery13a40412020-02-07 17:05:20 +1030394 rc = -1;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800395 goto out_close;
396 }
397
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800398 if (rc <= 0) {
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800399 rc = -1;
400 goto out_close;
401 }
402
Andrew Jefferycad47302021-08-20 21:37:57 +0930403 if (ctx->pcap.socket.path)
404 capture_socket(ctx->pcap.socket.dumper, ctx->buf, rc);
405
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800406 eid = *(uint8_t *)ctx->buf;
Jeremy Kerrd690d8e2019-08-01 21:16:58 +0800407
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800408 if (ctx->verbose)
409 fprintf(stderr,
410 "client[%d] sent message: dest 0x%02x len %d\n",
411 idx, eid, rc - 1);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800412
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800413
414 if (eid == ctx->local_eid)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530415 rx_message(eid, MCTP_MESSAGE_TO_DST, 0, ctx, ctx->buf + 1,
416 rc - 1);
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800417 else
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800418 tx_message(ctx, eid, ctx->buf + 1, rc - 1);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800419
420 return 0;
421
422out_close:
423 client->active = false;
424 return rc;
425}
426
427static int binding_init(struct ctx *ctx, const char *name,
428 int argc, char * const *argv)
429{
430 int rc;
431
432 ctx->binding = binding_lookup(name);
433 if (!ctx->binding) {
434 warnx("no such binding '%s'", name);
435 return -1;
436 }
437
438 rc = ctx->binding->init(ctx->mctp, ctx->binding, ctx->local_eid,
439 argc, argv);
440 return rc;
441}
442
Andrew Jefferyedebe162021-08-26 12:35:00 +0930443static void binding_destroy(struct ctx *ctx)
444{
445 if (ctx->binding->destroy)
446 ctx->binding->destroy(ctx->mctp, ctx->binding);
447}
448
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800449enum {
450 FD_BINDING = 0,
451 FD_SOCKET,
Andrew Jeffery490e3872021-08-24 21:11:30 +0930452 FD_SIGNAL,
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800453 FD_NR,
454};
455
456static int run_daemon(struct ctx *ctx)
457{
458 bool clients_changed = false;
Andrew Jeffery490e3872021-08-24 21:11:30 +0930459 sigset_t mask;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800460 int rc, i;
461
462 ctx->pollfds = malloc(FD_NR * sizeof(struct pollfd));
463
Jeremy Kerr34b9b3d2019-11-27 11:27:05 +0800464 if (ctx->binding->get_fd) {
465 ctx->pollfds[FD_BINDING].fd =
466 ctx->binding->get_fd(ctx->binding);
467 ctx->pollfds[FD_BINDING].events = POLLIN;
468 } else {
469 ctx->pollfds[FD_BINDING].fd = -1;
470 ctx->pollfds[FD_BINDING].events = 0;
471 }
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800472
Andrew Jeffery490e3872021-08-24 21:11:30 +0930473 sigemptyset(&mask);
474 sigaddset(&mask, SIGINT);
475 sigaddset(&mask, SIGTERM);
476 sigaddset(&mask, SIGQUIT);
477
478 if ((rc = sigprocmask(SIG_BLOCK, &mask, NULL)) == -1) {
479 warn("sigprocmask");
480 return rc;
481 }
482
483 ctx->pollfds[FD_SIGNAL].fd = signalfd(-1, &mask, 0);
484 ctx->pollfds[FD_SIGNAL].events = POLLIN;
485
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800486 ctx->pollfds[FD_SOCKET].fd = ctx->sock;
487 ctx->pollfds[FD_SOCKET].events = POLLIN;
488
489 mctp_set_rx_all(ctx->mctp, rx_message, ctx);
490
491 for (;;) {
492 if (clients_changed) {
493 int i;
494
495 ctx->pollfds = realloc(ctx->pollfds,
496 (ctx->n_clients + FD_NR) *
497 sizeof(struct pollfd));
498
499 for (i = 0; i < ctx->n_clients; i++) {
500 ctx->pollfds[FD_NR+i].fd =
501 ctx->clients[i].sock;
502 ctx->pollfds[FD_NR+i].events = POLLIN;
503 }
504 clients_changed = false;
505 }
506
507 rc = poll(ctx->pollfds, ctx->n_clients + FD_NR, -1);
508 if (rc < 0) {
509 warn("poll failed");
510 break;
511 }
512
513 if (!rc)
514 continue;
515
Andrew Jeffery490e3872021-08-24 21:11:30 +0930516 if (ctx->pollfds[FD_SIGNAL].revents) {
517 struct signalfd_siginfo si;
518 ssize_t got;
519
520 got = read(ctx->pollfds[FD_SIGNAL].fd, &si, sizeof(si));
521 if (got == sizeof(si)) {
522 warnx("Received %s, quitting",
523 strsignal(si.ssi_signo));
524 rc = 0;
525 break;
526 } else {
527 warnx("Unexpected read result for signalfd: %d",
528 rc);
529 warnx("Quitting on the basis that signalfd became ready");
530 rc = -1;
531 break;
532 }
533 }
534
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800535 if (ctx->pollfds[FD_BINDING].revents) {
Jeremy Kerr34b9b3d2019-11-27 11:27:05 +0800536 rc = 0;
537 if (ctx->binding->process)
538 rc = ctx->binding->process(ctx->binding);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800539 if (rc)
540 break;
541 }
542
543 for (i = 0; i < ctx->n_clients; i++) {
544 if (!ctx->pollfds[FD_NR+i].revents)
545 continue;
546
547 rc = client_process_recv(ctx, i);
548 if (rc)
549 clients_changed = true;
550 }
551
552 if (ctx->pollfds[FD_SOCKET].revents) {
553 rc = socket_process(ctx);
554 if (rc)
555 break;
556 clients_changed = true;
557 }
558
559 if (clients_changed)
560 client_remove_inactive(ctx);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800561 }
562
563
564 free(ctx->pollfds);
565
566 return rc;
567}
568
569static const struct option options[] = {
Andrew Jefferycad47302021-08-20 21:37:57 +0930570 { "capture-binding", required_argument, 0, 'b' },
571 { "capture-socket", required_argument, 0, 's' },
572 { "binding-linktype", required_argument, 0, 'B' },
573 { "socket-linktype", required_argument, 0, 'S' },
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800574 { "verbose", no_argument, 0, 'v' },
575 { "eid", required_argument, 0, 'e' },
576 { 0 },
577};
578
579static void usage(const char *progname)
580{
581 unsigned int i;
582
583 fprintf(stderr, "usage: %s <binding> [params]\n", progname);
584 fprintf(stderr, "Available bindings:\n");
585 for (i = 0; i < ARRAY_SIZE(bindings); i++)
586 fprintf(stderr, " %s\n", bindings[i].name);
587}
588
589int main(int argc, char * const *argv)
590{
591 struct ctx *ctx, _ctx;
592 int rc;
593
594 ctx = &_ctx;
595 ctx->clients = NULL;
596 ctx->n_clients = 0;
597 ctx->local_eid = local_eid_default;
Andrew Jeffery04b81fc2020-02-05 13:07:29 +1030598 ctx->verbose = false;
Andrew Jefferycad47302021-08-20 21:37:57 +0930599 ctx->pcap.binding.path = NULL;
Brad Bishop95cccae2021-10-07 18:03:59 -0400600 ctx->pcap.binding.linktype = -1;
Andrew Jefferycad47302021-08-20 21:37:57 +0930601 ctx->pcap.socket.path = NULL;
Brad Bishop95cccae2021-10-07 18:03:59 -0400602 ctx->pcap.socket.linktype = -1;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800603
604 for (;;) {
Andrew Jefferycad47302021-08-20 21:37:57 +0930605 rc = getopt_long(argc, argv, "b:es::v", options, NULL);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800606 if (rc == -1)
607 break;
608 switch (rc) {
Andrew Jefferycad47302021-08-20 21:37:57 +0930609 case 'b':
610 ctx->pcap.binding.path = optarg;
611 break;
612 case 's':
613 ctx->pcap.socket.path = optarg;
614 break;
615 case 'B':
616 ctx->pcap.binding.linktype = atoi(optarg);
617 break;
618 case 'S':
619 ctx->pcap.socket.linktype = atoi(optarg);
620 break;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800621 case 'v':
622 ctx->verbose = true;
623 break;
624 case 'e':
625 ctx->local_eid = atoi(optarg);
626 break;
627 default:
628 fprintf(stderr, "Invalid argument\n");
629 return EXIT_FAILURE;
630 }
631 }
632
633 if (optind >= argc) {
634 fprintf(stderr, "missing binding argument\n");
635 usage(argv[0]);
636 return EXIT_FAILURE;
637 }
638
Brad Bishop95cccae2021-10-07 18:03:59 -0400639 if (ctx->pcap.binding.linktype < 0 && ctx->pcap.binding.path) {
640 fprintf(stderr, "missing binding-linktype argument\n");
641 usage(argv[0]);
642 return EXIT_FAILURE;
643 }
644
645 if (ctx->pcap.socket.linktype < 0 && ctx->pcap.socket.path) {
646 fprintf(stderr, "missing socket-linktype argument\n");
647 usage(argv[0]);
648 return EXIT_FAILURE;
649 }
650
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800651 /* setup initial buffer */
652 ctx->buf_size = 4096;
653 ctx->buf = malloc(ctx->buf_size);
654
Jeremy Kerr0b278a62019-05-30 21:27:21 +0800655 mctp_set_log_stdio(ctx->verbose ? MCTP_LOG_DEBUG : MCTP_LOG_WARNING);
656
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800657 ctx->mctp = mctp_init();
658 assert(ctx->mctp);
659
Andrew Jefferycad47302021-08-20 21:37:57 +0930660 if (ctx->pcap.binding.path || ctx->pcap.socket.path) {
661 if (capture_init()) {
662 rc = EXIT_FAILURE;
663 goto cleanup_mctp;
664 }
665 }
666
667 if (ctx->pcap.binding.path) {
668 rc = capture_prepare(&ctx->pcap.binding);
669 if (rc == -1) {
670 fprintf(stderr, "Failed to initialise capture: %d\n", rc);
671 rc = EXIT_FAILURE;
672 goto cleanup_mctp;
673 }
674
675 mctp_set_capture_handler(ctx->mctp, capture_binding,
676 ctx->pcap.binding.dumper);
677 }
678
679 if (ctx->pcap.socket.path) {
680 rc = capture_prepare(&ctx->pcap.socket);
681 if (rc == -1) {
682 fprintf(stderr, "Failed to initialise capture: %d\n", rc);
683 rc = EXIT_FAILURE;
684 goto cleanup_pcap_binding;
685 }
686 }
687
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800688 rc = binding_init(ctx, argv[optind], argc - optind - 1, argv + optind + 1);
Andrew Jefferycad47302021-08-20 21:37:57 +0930689 if (rc) {
690 fprintf(stderr, "Failed to initialise binding: %d\n", rc);
691 rc = EXIT_FAILURE;
692 goto cleanup_pcap_socket;
693 }
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800694
Andrew Jefferyd4103f82021-06-16 14:39:36 +0930695 rc = sd_listen_fds(true);
696 if (rc <= 0) {
697 rc = socket_init(ctx);
Andrew Jefferycad47302021-08-20 21:37:57 +0930698 if (rc) {
699 fprintf(stderr, "Failed to initialse socket: %d\n", rc);
Andrew Jefferyedebe162021-08-26 12:35:00 +0930700 goto cleanup_binding;
Andrew Jefferycad47302021-08-20 21:37:57 +0930701 }
Andrew Jefferyd4103f82021-06-16 14:39:36 +0930702 } else {
703 ctx->sock = SD_LISTEN_FDS_START;
704 }
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800705
706 rc = run_daemon(ctx);
707
Andrew Jefferyedebe162021-08-26 12:35:00 +0930708cleanup_binding:
709 binding_destroy(ctx);
710
Andrew Jefferycad47302021-08-20 21:37:57 +0930711cleanup_pcap_socket:
712 if (ctx->pcap.socket.path)
713 capture_close(&ctx->pcap.socket);
714
715cleanup_pcap_binding:
716 if (ctx->pcap.binding.path)
717 capture_close(&ctx->pcap.binding);
718
719 rc = rc ? EXIT_FAILURE : EXIT_SUCCESS;
720cleanup_mctp:
721
722 return rc;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800723
724}