Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
| 2 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 3 | #define _GNU_SOURCE |
| 4 | |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 5 | #include "config.h" |
Andrew Jeffery | 5ab7825 | 2022-02-17 21:04:59 +1030 | [diff] [blame] | 6 | |
| 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 Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 13 | #include "utils/mctp-capture.h" |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 14 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 15 | #include <assert.h> |
| 16 | #include <err.h> |
Andrew Jeffery | 6896d41 | 2020-03-11 09:25:32 +1030 | [diff] [blame] | 17 | #include <errno.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 18 | #include <getopt.h> |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 19 | #include <limits.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 20 | #include <poll.h> |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 21 | #include <signal.h> |
Andrew Jeffery | 04b81fc | 2020-02-05 13:07:29 +1030 | [diff] [blame] | 22 | #include <stdbool.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 28 | #include <sys/signalfd.h> |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
| 31 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 32 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 33 | |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 34 | #if HAVE_SYSTEMD_SD_DAEMON_H |
| 35 | #include <systemd/sd-daemon.h> |
| 36 | #else |
| 37 | static inline int sd_listen_fds(int i __unused) |
| 38 | { |
| 39 | return -1; |
| 40 | } |
| 41 | #endif |
| 42 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 43 | static const mctp_eid_t local_eid_default = 8; |
| 44 | static char sockname[] = "\0mctp-mux"; |
| 45 | |
| 46 | struct 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 Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 51 | void (*destroy)(struct mctp *mctp, struct binding *binding); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 52 | int (*get_fd)(struct binding *binding); |
| 53 | int (*process)(struct binding *binding); |
| 54 | void *data; |
| 55 | }; |
| 56 | |
| 57 | struct client { |
| 58 | bool active; |
| 59 | int sock; |
| 60 | uint8_t type; |
| 61 | }; |
| 62 | |
| 63 | struct ctx { |
| 64 | struct mctp *mctp; |
| 65 | struct binding *binding; |
| 66 | bool verbose; |
| 67 | int local_eid; |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 68 | void *buf; |
| 69 | size_t buf_size; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 70 | |
| 71 | int sock; |
| 72 | struct pollfd *pollfds; |
| 73 | |
| 74 | struct client *clients; |
| 75 | int n_clients; |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 76 | |
| 77 | struct { |
| 78 | struct capture binding; |
| 79 | struct capture socket; |
| 80 | } pcap; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static void tx_message(struct ctx *ctx, mctp_eid_t eid, void *msg, size_t len) |
| 84 | { |
Andrew Jeffery | 0673505 | 2021-01-27 23:50:55 +1030 | [diff] [blame] | 85 | int rc; |
| 86 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 87 | rc = mctp_message_tx(ctx->mctp, eid, MCTP_MESSAGE_TO_SRC, 0, msg, len); |
Andrew Jeffery | 0673505 | 2021-01-27 23:50:55 +1030 | [diff] [blame] | 88 | if (rc) |
| 89 | warnx("Failed to send message: %d", rc); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static 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 Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 110 | static void |
| 111 | rx_message(uint8_t eid, bool tag_owner __unused, uint8_t msg_tag __unused, |
| 112 | void *data, void *msg, size_t len) |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 113 | { |
| 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 Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 159 | static 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 171 | static 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 Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 192 | mctp_register_bus(mctp, mctp_binding_serial_core(serial), eid); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 193 | |
| 194 | binding->data = serial; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int binding_serial_get_fd(struct binding *binding) |
| 200 | { |
| 201 | return mctp_serial_get_fd(binding->data); |
| 202 | } |
| 203 | |
| 204 | static int binding_serial_process(struct binding *binding) |
| 205 | { |
| 206 | return mctp_serial_read(binding->data); |
| 207 | } |
| 208 | |
| 209 | static 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 Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 220 | astlpc = mctp_astlpc_init_fileio(); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 221 | if (!astlpc) { |
| 222 | warnx("could not initialise astlpc binding"); |
| 223 | return -1; |
| 224 | } |
| 225 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 226 | mctp_register_bus(mctp, mctp_binding_astlpc_core(astlpc), eid); |
| 227 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 228 | binding->data = astlpc; |
| 229 | return 0; |
| 230 | } |
| 231 | |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 232 | static 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 241 | static int binding_astlpc_get_fd(struct binding *binding) |
| 242 | { |
| 243 | return mctp_astlpc_get_fd(binding->data); |
| 244 | } |
| 245 | |
| 246 | static int binding_astlpc_process(struct binding *binding) |
| 247 | { |
| 248 | return mctp_astlpc_poll(binding->data); |
| 249 | } |
| 250 | |
| 251 | struct binding bindings[] = { |
| 252 | { |
Jeremy Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 253 | .name = "null", |
| 254 | .init = binding_null_init, |
| 255 | }, |
| 256 | { |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 257 | .name = "serial", |
| 258 | .init = binding_serial_init, |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 259 | .destroy = NULL, |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 260 | .get_fd = binding_serial_get_fd, |
| 261 | .process = binding_serial_process, |
| 262 | }, |
| 263 | { |
| 264 | .name = "astlpc", |
| 265 | .init = binding_astlpc_init, |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 266 | .destroy = binding_astlpc_destroy, |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 267 | .get_fd = binding_astlpc_get_fd, |
| 268 | .process = binding_astlpc_process, |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | struct 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 | |
| 287 | static 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 | |
| 317 | err_close: |
| 318 | close(ctx->sock); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | static 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 Jeffery | 8676c93 | 2020-01-24 12:22:21 +1030 | [diff] [blame] | 336 | memset(client, 0, sizeof(*client)); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 337 | client->active = true; |
| 338 | client->sock = fd; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int client_process_recv(struct ctx *ctx, int idx) |
| 344 | { |
| 345 | struct client *client = &ctx->clients[idx]; |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 346 | uint8_t eid; |
| 347 | ssize_t len; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 348 | 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 Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 368 | len = recv(client->sock, NULL, 0, MSG_PEEK | MSG_TRUNC); |
| 369 | if (len < 0) { |
Andrew Jeffery | 6896d41 | 2020-03-11 09:25:32 +1030 | [diff] [blame] | 370 | if (errno != ECONNRESET) |
| 371 | warn("can't receive (peek) from client"); |
| 372 | |
Andrew Jeffery | 13a4041 | 2020-02-07 17:05:20 +1030 | [diff] [blame] | 373 | rc = -1; |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 374 | goto out_close; |
| 375 | } |
| 376 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 377 | if ((size_t)len > ctx->buf_size) { |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 378 | void *tmp; |
| 379 | |
| 380 | tmp = realloc(ctx->buf, len); |
| 381 | if (!tmp) { |
| 382 | warn("can't allocate for incoming message"); |
Andrew Jeffery | 13a4041 | 2020-02-07 17:05:20 +1030 | [diff] [blame] | 383 | rc = -1; |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 384 | 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 391 | if (rc < 0) { |
Andrew Jeffery | 6896d41 | 2020-03-11 09:25:32 +1030 | [diff] [blame] | 392 | if (errno != ECONNRESET) |
| 393 | warn("can't receive from client"); |
Andrew Jeffery | 13a4041 | 2020-02-07 17:05:20 +1030 | [diff] [blame] | 394 | rc = -1; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 395 | goto out_close; |
| 396 | } |
| 397 | |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 398 | if (rc <= 0) { |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 399 | rc = -1; |
| 400 | goto out_close; |
| 401 | } |
| 402 | |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 403 | if (ctx->pcap.socket.path) |
| 404 | capture_socket(ctx->pcap.socket.dumper, ctx->buf, rc); |
| 405 | |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 406 | eid = *(uint8_t *)ctx->buf; |
Jeremy Kerr | d690d8e | 2019-08-01 21:16:58 +0800 | [diff] [blame] | 407 | |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 408 | if (ctx->verbose) |
| 409 | fprintf(stderr, |
| 410 | "client[%d] sent message: dest 0x%02x len %d\n", |
| 411 | idx, eid, rc - 1); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 412 | |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 413 | |
| 414 | if (eid == ctx->local_eid) |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 415 | rx_message(eid, MCTP_MESSAGE_TO_DST, 0, ctx, ctx->buf + 1, |
| 416 | rc - 1); |
Jeremy Kerr | 195a7c5 | 2019-06-24 14:24:56 +0800 | [diff] [blame] | 417 | else |
Jeremy Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 418 | tx_message(ctx, eid, ctx->buf + 1, rc - 1); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | |
| 422 | out_close: |
| 423 | client->active = false; |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | static 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 Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 443 | static void binding_destroy(struct ctx *ctx) |
| 444 | { |
| 445 | if (ctx->binding->destroy) |
| 446 | ctx->binding->destroy(ctx->mctp, ctx->binding); |
| 447 | } |
| 448 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 449 | enum { |
| 450 | FD_BINDING = 0, |
| 451 | FD_SOCKET, |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 452 | FD_SIGNAL, |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 453 | FD_NR, |
| 454 | }; |
| 455 | |
| 456 | static int run_daemon(struct ctx *ctx) |
| 457 | { |
| 458 | bool clients_changed = false; |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 459 | sigset_t mask; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 460 | int rc, i; |
| 461 | |
| 462 | ctx->pollfds = malloc(FD_NR * sizeof(struct pollfd)); |
| 463 | |
Jeremy Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 464 | 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 472 | |
Andrew Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 473 | 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 486 | 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 Jeffery | 490e387 | 2021-08-24 21:11:30 +0930 | [diff] [blame] | 516 | 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 535 | if (ctx->pollfds[FD_BINDING].revents) { |
Jeremy Kerr | 34b9b3d | 2019-11-27 11:27:05 +0800 | [diff] [blame] | 536 | rc = 0; |
| 537 | if (ctx->binding->process) |
| 538 | rc = ctx->binding->process(ctx->binding); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 539 | 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | |
| 564 | free(ctx->pollfds); |
| 565 | |
| 566 | return rc; |
| 567 | } |
| 568 | |
| 569 | static const struct option options[] = { |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 570 | { "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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 574 | { "verbose", no_argument, 0, 'v' }, |
| 575 | { "eid", required_argument, 0, 'e' }, |
| 576 | { 0 }, |
| 577 | }; |
| 578 | |
| 579 | static 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 | |
| 589 | int 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 Jeffery | 04b81fc | 2020-02-05 13:07:29 +1030 | [diff] [blame] | 598 | ctx->verbose = false; |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 599 | ctx->pcap.binding.path = NULL; |
Brad Bishop | 95cccae | 2021-10-07 18:03:59 -0400 | [diff] [blame] | 600 | ctx->pcap.binding.linktype = -1; |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 601 | ctx->pcap.socket.path = NULL; |
Brad Bishop | 95cccae | 2021-10-07 18:03:59 -0400 | [diff] [blame] | 602 | ctx->pcap.socket.linktype = -1; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 603 | |
| 604 | for (;;) { |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 605 | rc = getopt_long(argc, argv, "b:es::v", options, NULL); |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 606 | if (rc == -1) |
| 607 | break; |
| 608 | switch (rc) { |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 609 | 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 621 | 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 Bishop | 95cccae | 2021-10-07 18:03:59 -0400 | [diff] [blame] | 639 | 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 Kerr | f49b2ac | 2019-08-02 15:10:51 +0800 | [diff] [blame] | 651 | /* setup initial buffer */ |
| 652 | ctx->buf_size = 4096; |
| 653 | ctx->buf = malloc(ctx->buf_size); |
| 654 | |
Jeremy Kerr | 0b278a6 | 2019-05-30 21:27:21 +0800 | [diff] [blame] | 655 | mctp_set_log_stdio(ctx->verbose ? MCTP_LOG_DEBUG : MCTP_LOG_WARNING); |
| 656 | |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 657 | ctx->mctp = mctp_init(); |
| 658 | assert(ctx->mctp); |
| 659 | |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 660 | 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 Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 688 | rc = binding_init(ctx, argv[optind], argc - optind - 1, argv + optind + 1); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 689 | if (rc) { |
| 690 | fprintf(stderr, "Failed to initialise binding: %d\n", rc); |
| 691 | rc = EXIT_FAILURE; |
| 692 | goto cleanup_pcap_socket; |
| 693 | } |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 694 | |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 695 | rc = sd_listen_fds(true); |
| 696 | if (rc <= 0) { |
| 697 | rc = socket_init(ctx); |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 698 | if (rc) { |
| 699 | fprintf(stderr, "Failed to initialse socket: %d\n", rc); |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 700 | goto cleanup_binding; |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 701 | } |
Andrew Jeffery | d4103f8 | 2021-06-16 14:39:36 +0930 | [diff] [blame] | 702 | } else { |
| 703 | ctx->sock = SD_LISTEN_FDS_START; |
| 704 | } |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 705 | |
| 706 | rc = run_daemon(ctx); |
| 707 | |
Andrew Jeffery | edebe16 | 2021-08-26 12:35:00 +0930 | [diff] [blame] | 708 | cleanup_binding: |
| 709 | binding_destroy(ctx); |
| 710 | |
Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 711 | cleanup_pcap_socket: |
| 712 | if (ctx->pcap.socket.path) |
| 713 | capture_close(&ctx->pcap.socket); |
| 714 | |
| 715 | cleanup_pcap_binding: |
| 716 | if (ctx->pcap.binding.path) |
| 717 | capture_close(&ctx->pcap.binding); |
| 718 | |
| 719 | rc = rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 720 | cleanup_mctp: |
| 721 | |
| 722 | return rc; |
Jeremy Kerr | 8e31cfd | 2019-05-07 17:13:51 +0800 | [diff] [blame] | 723 | |
| 724 | } |