Debian Codegraph @

src/dwdump.c

raw symbols SCIP
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4
5 #include <errno.h>
6 #include <inttypes.h>
7 #include <getopt.h>
8 #include <pcap.h>
9 #include <signal.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <arpa/inet.h>
16 #include <linux/if_arp.h>
17 #include <linux/if_packet.h>
18 #include <linux/netlink.h>
19 #include <linux/socket.h>
20 #include <netlink/genl/ctrl.h>
21 #include <netlink/genl/genl.h>
22 #include <netlink/socket.h>
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26
27 #include "net_dropmon.h"
28
29 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
30
31 static struct nla_policy net_dm_policy[NET_DM_ATTR_MAX + 1] = {
32 [NET_DM_ATTR_ALERT_MODE] = { .type = NLA_U8 },
33 [NET_DM_ATTR_TRUNC_LEN] = { .type = NLA_U32 },
34 [NET_DM_ATTR_QUEUE_LEN] = { .type = NLA_U32 },
35 [NET_DM_ATTR_STATS] = { .type = NLA_NESTED },
36 [NET_DM_ATTR_HW_STATS] = { .type = NLA_NESTED },
37 };
38
39 static struct nla_policy
40 net_dm_stats_policy[NET_DM_ATTR_STATS_MAX + 1] = {
41 [NET_DM_ATTR_STATS_DROPPED] = { .type = NLA_U64 },
42 };
43
44 static bool stop;
45
46 enum dwdump_pkt_origin {
47 DWDUMP_PKT_ORIGIN_ALL,
48 DWDUMP_PKT_ORIGIN_SW,
49 DWDUMP_PKT_ORIGIN_HW,
50 };
51
52 struct dwdump_options {
53 const char *dumpfile;
54 __u32 trunc_len;
55 bool query;
56 __u32 queue_len;
57 bool passive;
58 bool stats;
59 int rxbuf;
60 bool exit;
61 enum dwdump_pkt_origin origin;
62 bool need_pcap;
63 bool need_mon;
64 };
65
66 struct dwdump {
67 struct nl_sock *csk, *dsk;
68 pcap_t *pcap_handle;
69 pcap_dumper_t *pcap_dumper;
70 struct dwdump_options options;
71 int family;
72 long snaplen;
73 char *pcap_buf, *pkt;
74 };
75
76 /* Based on https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL.html */
77 struct linux_sll {
78 __be16 pkttype;
79 __be16 hatype;
80 __be16 halen;
81 unsigned char addr[8];
82 __be16 family;
83 };
84
85 static int dwdump_data_init(struct dwdump *dwdump)
86 {
87 int fd, optval, family, err;
88 struct nl_sock *sk;
89
90 sk = nl_socket_alloc();
91 if (!sk) {
92 fprintf(stderr, "Failed to allocate data socket\n");
93 return -1;
94 }
95
96 /* Add wiggle room for other netlink attributes in addition to the
97 * payload.
98 */
99 dwdump->snaplen = dwdump->options.trunc_len + 2048;
100 dwdump->pkt = calloc(1, dwdump->snaplen);
101 if (!dwdump->pkt) {
102 perror("calloc");
103 goto err_pkt_alloc;
104 }
105
106 err = genl_connect(sk);
107 if (err) {
108 fprintf(stderr, "Failed to connect data socket\n");
109 goto err_genl_connect;
110 }
111
112 family = genl_ctrl_resolve(sk, "NET_DM");
113 if (family < 0) {
114 fprintf(stderr, "Failed to resolve ID of \"NET_DM\" family\n");
115 goto err_genl_ctrl_resolve;
116 }
117
118 err = nl_socket_set_buffer_size(sk, dwdump->options.rxbuf, 0);
119 if (err < 0) {
120 fprintf(stderr, "Failed to set receive buffer size of data socket\n");
121 goto err_set_buffer_size;
122 }
123
124 optval = 1;
125 fd = nl_socket_get_fd(sk);
126 err = setsockopt(fd, SOL_NETLINK, NETLINK_NO_ENOBUFS, &optval,
127 sizeof(optval));
128 if (err < 0) {
129 fprintf(stderr, "Failed to set NETLINK_NO_ENOBUFS socket option\n");
130 goto err_setsockopt;
131 }
132
133 err = nl_socket_add_memberships(sk, NET_DM_GRP_ALERT, NFNLGRP_NONE);
134 if (err) {
135 fprintf(stderr, "Failed to join multicast group\n");
136 goto err_add_memberships;
137 }
138
139 dwdump->dsk = sk;
140 dwdump->family = family;
141
142 return 0;
143
144 err_add_memberships:
145 err_setsockopt:
146 err_set_buffer_size:
147 err_genl_ctrl_resolve:
148 err_genl_connect:
149 free(dwdump->pkt);
150 err_pkt_alloc:
151 nl_socket_free(sk);
152 return -1;
153 }
154
155 static void dwdump_data_fini(struct dwdump *dwdump)
156 {
157 nl_socket_drop_memberships(dwdump->dsk, NET_DM_GRP_ALERT, NFNLGRP_NONE);
158 free(dwdump->pkt);
159 nl_socket_free(dwdump->dsk);
160 }
161
162 static const char *dwdump_alert_mode(uint8_t alert_mode)
163 {
164 switch (alert_mode) {
165 case NET_DM_ALERT_MODE_SUMMARY:
166 return "summary";
167 case NET_DM_ALERT_MODE_PACKET:
168 return "packet";
169 }
170
171 return "invalid alert mode";
172 }
173
174 static int dwdump_config_set(struct dwdump *dwdump)
175 {
176 struct nl_sock *sk = dwdump->csk;
177 struct nl_msg *msg;
178 int err;
179
180 msg = nlmsg_alloc();
181 if (!msg) {
182 fprintf(stderr, "Failed to allocate netlink message\n");
183 return -1;
184 }
185
186 if (!genlmsg_put(msg, 0, NL_AUTO_SEQ, dwdump->family, 0,
187 NLM_F_REQUEST|NLM_F_ACK, NET_DM_CMD_CONFIG, 0))
188 goto genlmsg_put_failure;
189
190 if (nla_put_u8(msg, NET_DM_ATTR_ALERT_MODE, NET_DM_ALERT_MODE_PACKET))
191 goto nla_put_failure;
192
193 if (nla_put_u32(msg, NET_DM_ATTR_TRUNC_LEN, dwdump->options.trunc_len))
194 goto nla_put_failure;
195
196 if (nla_put_u32(msg, NET_DM_ATTR_QUEUE_LEN, dwdump->options.queue_len))
197 goto nla_put_failure;
198
199 err = nl_send_sync(sk, msg);
200 if (err < 0) {
201 fprintf(stderr, "Failed to configure drop monitor kernel module\n");
202 return err;
203 }
204
205 return 0;
206
207 nla_put_failure:
208 genlmsg_put_failure:
209 nlmsg_free(msg);
210 return -EMSGSIZE;
211 }
212
213 static int dwdump_config_get(struct dwdump *dwdump)
214 {
215 struct nlattr *attrs[NET_DM_ATTR_MAX + 1];
216 struct sockaddr_nl nla;
217 unsigned char *buf;
218 uint8_t alert_mode;
219 int len, err;
220
221 err = genl_send_simple(dwdump->csk, dwdump->family,
222 NET_DM_CMD_CONFIG_GET, 0, NLM_F_REQUEST);
223 if (err < 0) {
224 fprintf(stderr, "Failed to query configuration\n");
225 return -1;
226 }
227
228 len = nl_recv(dwdump->csk, &nla, &buf, NULL);
229 if (len < 0)
230 return -1;
231
232 err = genlmsg_parse((void *) buf, 0, attrs, NET_DM_ATTR_MAX,
233 net_dm_policy);
234 if (err < 0)
235 return -1;
236
237 if (!attrs[NET_DM_ATTR_ALERT_MODE] || !attrs[NET_DM_ATTR_TRUNC_LEN] ||
238 !attrs[NET_DM_ATTR_QUEUE_LEN])
239 return -1;
240
241 alert_mode = nla_get_u8(attrs[NET_DM_ATTR_ALERT_MODE]);
242 printf("Alert mode: %s\n", dwdump_alert_mode(alert_mode));
243
244 printf("Truncation length: %u\n",
245 nla_get_u32(attrs[NET_DM_ATTR_TRUNC_LEN]));
246
247 printf("Queue length: %u\n", nla_get_u32(attrs[NET_DM_ATTR_QUEUE_LEN]));
248
249 return 0;
250 }
251
252 static void dwdump_nested_stats_print(struct nlattr *attr)
253 {
254 struct nlattr *attrs[NET_DM_ATTR_STATS_MAX + 1];
255 int err;
256
257 err = nla_parse_nested(attrs, NET_DM_ATTR_STATS_MAX, attr,
258 net_dm_stats_policy);
259 if (err)
260 return;
261
262 if (attrs[NET_DM_ATTR_STATS_DROPPED])
263 printf("Tail dropped: %" PRIu64 "\n",
264 nla_get_u64(attrs[NET_DM_ATTR_STATS_DROPPED]));
265 }
266
267 static int dwdump_stats_get(struct dwdump *dwdump)
268 {
269 struct nlattr *attrs[NET_DM_ATTR_MAX + 1];
270 struct sockaddr_nl nla;
271 unsigned char *buf;
272 int len, err;
273
274 err = genl_send_simple(dwdump->csk, dwdump->family,
275 NET_DM_CMD_STATS_GET, 0, NLM_F_REQUEST);
276 if (err < 0) {
277 fprintf(stderr, "Failed to query statistics\n");
278 return -1;
279 }
280
281 len = nl_recv(dwdump->csk, &nla, &buf, NULL);
282 if (len < 0)
283 return -1;
284
285 err = genlmsg_parse((void *) buf, 0, attrs, NET_DM_ATTR_MAX,
286 net_dm_policy);
287 if (err < 0)
288 return -1;
289
290 if (attrs[NET_DM_ATTR_STATS]) {
291 printf("Software statistics:\n");
292 dwdump_nested_stats_print(attrs[NET_DM_ATTR_STATS]);
293 }
294
295 if (attrs[NET_DM_ATTR_HW_STATS]) {
296 printf("Hardware statistics:\n");
297 dwdump_nested_stats_print(attrs[NET_DM_ATTR_HW_STATS]);
298 }
299
300 return 0;
301 }
302
303 static int dwdump_monitor_origin_put(const struct dwdump *dwdump,
304 struct nl_msg *msg)
305 {
306 switch (dwdump->options.origin) {
307 case DWDUMP_PKT_ORIGIN_ALL:
308 if (nla_put_flag(msg, NET_DM_ATTR_SW_DROPS) ||
309 nla_put_flag(msg, NET_DM_ATTR_HW_DROPS))
310 return -EMSGSIZE;
311 break;
312 case DWDUMP_PKT_ORIGIN_SW:
313 if (nla_put_flag(msg, NET_DM_ATTR_SW_DROPS))
314 return -EMSGSIZE;
315 break;
316 case DWDUMP_PKT_ORIGIN_HW:
317 if (nla_put_flag(msg, NET_DM_ATTR_HW_DROPS))
318 return -EMSGSIZE;
319 break;
320 }
321
322 return 0;
323 }
324
325 static int dwdump_monitor(struct dwdump *dwdump, bool start)
326 {
327 uint8_t cmd = start ? NET_DM_CMD_START : NET_DM_CMD_STOP;
328 struct nl_sock *sk = dwdump->csk;
329 struct nl_msg *msg;
330 int err;
331
332 msg = nlmsg_alloc();
333 if (!msg) {
334 fprintf(stderr, "Failed to allocate netlink message\n");
335 return -1;
336 }
337
338 if (!genlmsg_put(msg, 0, NL_AUTO_SEQ, dwdump->family, 0,
339 NLM_F_REQUEST|NLM_F_ACK, cmd, 0))
340 goto genlmsg_put_failure;
341
342 err = dwdump_monitor_origin_put(dwdump, msg);
343 if (err < 0)
344 goto genlmsg_put_failure;
345
346 err = nl_send_sync(sk, msg);
347 if (err < 0) {
348 fprintf(stderr, "Failed to %s monitoring\n",
349 start ? "start" : "stop");
350 return err;
351 }
352
353 return 0;
354
355 genlmsg_put_failure:
356 nlmsg_free(msg);
357 return -EMSGSIZE;
358 }
359
360 static void dwdump_handler(int sig)
361 {
362 stop = true;
363 }
364
365 static int dwdump_sighandler_install(void)
366 {
367 static const int signals_catch[] = {
368 SIGINT, SIGQUIT, SIGTERM, SIGPIPE, SIGHUP,
369 };
370 struct sigaction saction;
371 int i, err;
372
373 memset(&saction, 0, sizeof(saction));
374 saction.sa_handler = dwdump_handler;
375
376 for (i = 0; i < ARRAY_SIZE(signals_catch); i++) {
377 err = sigaction(signals_catch[i], &saction, NULL);
378 if (err) {
379 perror("sigaction");
380 return err;
381 }
382 }
383
384 return 0;
385 }
386
387 static int dwdump_ctrl_init(struct dwdump *dwdump)
388 {
389 struct nl_sock *sk;
390 int err;
391
392 sk = nl_socket_alloc();
393 if (!sk) {
394 fprintf(stderr, "Failed to allocate control socket\n");
395 return -1;
396 }
397
398 err = genl_connect(sk);
399 if (err) {
400 fprintf(stderr, "Failed to connect control socket");
401 goto err_genl_connect;
402 }
403
404 dwdump->csk = sk;
405
406 if (!dwdump->options.need_mon)
407 return 0;
408
409 err = dwdump_config_set(dwdump);
410 if (err)
411 goto err_config_set;
412
413 err = dwdump_monitor(dwdump, true);
414 if (err)
415 goto err_monitor;
416
417 return 0;
418
419 err_monitor:
420 err_config_set:
421 err_genl_connect:
422 nl_socket_free(sk);
423 return err;
424 }
425
426 static void dwdump_ctrl_fini(struct dwdump *dwdump)
427 {
428 if (!dwdump->options.need_mon)
429 goto out;
430
431 dwdump_monitor(dwdump, false);
432 out:
433 nl_socket_free(dwdump->csk);
434 }
435
436 static void dwdump_pcap_write(struct dwdump *dwdump, unsigned char *buf,
437 int len)
438 {
439 struct pcap_pkthdr hdr;
440 int pkt_len;
441
442 if (len + sizeof(struct linux_sll) < dwdump->snaplen)
443 pkt_len = len + sizeof(struct linux_sll);
444 else
445 pkt_len = dwdump->snaplen - sizeof(struct linux_sll);
446
447 memcpy(dwdump->pcap_buf + sizeof(struct linux_sll), buf, pkt_len);
448
449 hdr.caplen = pkt_len;
450 hdr.len = pkt_len;
451 gettimeofday(&hdr.ts, NULL);
452
453 pcap_dump((unsigned char *) dwdump->pcap_dumper, &hdr,
454 (const unsigned char *) dwdump->pcap_buf);
455 /* In case packets are written to stdout, make sure each packet is
456 * immediately written and not buffered.
457 */
458 fflush(NULL);
459 }
460
461 static int dwdump_pcap_buf_init(struct dwdump *dwdump)
462 {
463 struct linux_sll sll;
464
465 /* The wireshark netlink dissector expects netlink messages to start
466 * with a Linux cooked header (SLL), so include it before each packet.
467 */
468 memset(&sll, 0, sizeof(sll));
469 sll.pkttype = htons(PACKET_OUTGOING);
470 sll.hatype = htons(ARPHRD_NETLINK);
471 sll.family = htons(AF_NETLINK);
472
473 dwdump->pcap_buf = calloc(1, dwdump->snaplen);
474 if (!dwdump->pcap_buf) {
475 perror("calloc");
476 return -1;
477 }
478
479 memcpy(dwdump->pcap_buf, &sll, sizeof(sll));
480
481 return 0;
482 }
483
484 static void dwdump_pcap_buf_fini(struct dwdump *dwdump)
485 {
486 free(dwdump->pcap_buf);
487 }
488
489 static int dwdump_pcap_genl_init(struct dwdump *dwdump)
490 {
491 struct sockaddr_nl nla;
492 unsigned char *buf;
493 int len, err;
494
495 /* In order for wireshark to be able to invoke the net_dm dissector,
496 * it must learn about the mapping between the generic netlink
497 * family ID and its name from this dump.
498 *
499 * Reference:
500 * https://www.wireshark.org/lists/wireshark-users/201907/msg00027.html
501 */
502 err = genl_send_simple(dwdump->csk, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
503 1, NLM_F_DUMP);
504 if (err < 0) {
505 fprintf(stderr, "Failed to dump generic netlink families\n");
506 return -1;
507 }
508
509 len = nl_recv(dwdump->csk, &nla, &buf, NULL);
510 if (len < 0)
511 return -1;
512
513 dwdump_pcap_write(dwdump, buf, len);
514
515 return 0;
516 }
517
518 static int dwdump_pcap_init(struct dwdump *dwdump)
519 {
520 int err;
521
522 if (!dwdump->options.need_pcap)
523 return 0;
524
525 dwdump->pcap_handle = pcap_open_dead(DLT_NETLINK, dwdump->snaplen);
526 if (!dwdump->pcap_handle) {
527 perror("pcap_open_dead");
528 return -1;
529 }
530
531 dwdump->pcap_dumper = pcap_dump_open(dwdump->pcap_handle,
532 dwdump->options.dumpfile);
533 if (!dwdump->pcap_dumper) {
534 pcap_perror(dwdump->pcap_handle, "pcap_dump_open");
535 goto err_dump_open;
536 }
537
538 err = dwdump_pcap_buf_init(dwdump);
539 if (err)
540 goto err_buf_init;
541
542 err = dwdump_pcap_genl_init(dwdump);
543 if (err)
544 goto err_genl_init;
545
546 return 0;
547
548 err_genl_init:
549 dwdump_pcap_buf_fini(dwdump);
550 err_buf_init:
551 pcap_dump_close(dwdump->pcap_dumper);
552 err_dump_open:
553 pcap_close(dwdump->pcap_handle);
554 return -1;
555 }
556
557 static void dwdump_pcap_fini(struct dwdump *dwdump)
558 {
559 if (!dwdump->options.need_pcap)
560 return;
561
562 dwdump_pcap_buf_fini(dwdump);
563 pcap_dump_close(dwdump->pcap_dumper);
564 pcap_close(dwdump->pcap_handle);
565 }
566
567 static int dwdump_init(struct dwdump *dwdump)
568 {
569 int err;
570
571 err = dwdump_data_init(dwdump);
572 if (err)
573 return err;
574
575 err = dwdump_ctrl_init(dwdump);
576 if (err)
577 goto err_ctrl_init;
578
579 err = dwdump_pcap_init(dwdump);
580 if (err)
581 goto err_pcap_init;
582
583 err = dwdump_sighandler_install();
584 if (err)
585 goto err_sighandler_install;
586
587 return 0;
588
589 err_sighandler_install:
590 dwdump_pcap_fini(dwdump);
591 err_pcap_init:
592 dwdump_ctrl_fini(dwdump);
593 err_ctrl_init:
594 dwdump_data_fini(dwdump);
595 return err;
596 }
597
598 static void dwdump_fini(struct dwdump *dwdump)
599 {
600 dwdump_pcap_fini(dwdump);
601 dwdump_ctrl_fini(dwdump);
602 dwdump_data_fini(dwdump);
603 }
604
605 static int dwdump_main(struct dwdump *dwdump)
606 {
607 int fd = nl_socket_get_fd(dwdump->dsk);
608
609 if (dwdump->options.query)
610 return dwdump_config_get(dwdump);
611
612 if (dwdump->options.stats)
613 return dwdump_stats_get(dwdump);
614
615 if (dwdump->options.exit)
616 return dwdump_monitor(dwdump, false);
617
618 while (!stop) {
619 int len;
620
621 /* Use recv() instead of nl_recv() since interruption of
622 * nl_recv() causes the operation to be retried.
623 */
624 len = recv(fd, dwdump->pkt, dwdump->snaplen, 0);
625 if (len < 0) {
626 switch (errno) {
627 case EINTR:
628 continue;
629 default:
630 perror("recv");
631 return -1;
632 }
633 }
634
635 dwdump_pcap_write(dwdump, (unsigned char *) dwdump->pkt, len);
636 }
637
638 return 0;
639 }
640
641 static int dwdump_origin_parse(struct dwdump *dwdump, const char *origin)
642 {
643 if (strcmp(origin, "sw") == 0) {
644 dwdump->options.origin = DWDUMP_PKT_ORIGIN_SW;
645 return 0;
646 } else if (strcmp(origin, "hw") == 0) {
647 dwdump->options.origin = DWDUMP_PKT_ORIGIN_HW;
648 return 0;
649 } else {
650 fprintf(stderr, "Invalid origin: \'%s\'\n", origin);
651 return -EINVAL;
652 }
653 }
654
655 static void dwdump_usage(FILE *fp)
656 {
657 fprintf(fp, "Usage:\n");
658 fprintf(fp, "dwdump [ -w <file> -t <length> -q -l <limit> -p -s -b <size> -e -o <sw|hw> ]\n");
659 fprintf(fp, " -w <file> dump packets to provided file. defaults to standard output\n");
660 fprintf(fp, " -t <length> truncate packets to provided length. defaults to no truncation\n");
661 fprintf(fp, " -q query the kernel for current configuration and exit\n");
662 fprintf(fp, " -l <limit> set packet queue limit to provided limit\n");
663 fprintf(fp, " -p only listen on notified packets with no configuration\n");
664 fprintf(fp, " -s query kernel for statistics and exit\n");
665 fprintf(fp, " -b <size> set the socket's receive buffer to provided size\n");
666 fprintf(fp, " -o monitor only <sw|hw> originated drops. defaults to all\n");
667 fprintf(fp, " -e ask kernel to stop monitoring and exit\n");
668 }
669
670 static int dwdump_opts_parse(struct dwdump *dwdump, int argc, char **argv)
671 {
672 static const struct option long_options[] = {
673 { "write", required_argument, NULL, 'w' },
674 { "trunc", required_argument, NULL, 't' },
675 { "query", no_argument, NULL, 'q' },
676 { "limit", required_argument, NULL, 'l' },
677 { "passive", no_argument, NULL, 'p' },
678 { "stats", no_argument, NULL, 's' },
679 { "bufsize", required_argument, NULL, 'b' },
680 { "origin", required_argument, NULL, 'o' },
681 { "exit", no_argument, NULL, 'e' },
682 { "help", no_argument, NULL, 'h' },
683 { NULL, 0, NULL, 0 }
684 };
685 static const char optstring[] = "w:t:ql:psb:o:eh";
686 int opt, err;
687
688 /* Default values */
689 dwdump->options.dumpfile = "/dev/stdout";
690 dwdump->options.trunc_len = 0xffff;
691 dwdump->options.queue_len = 1000;
692 dwdump->options.rxbuf = 1024 * 1024;
693 dwdump->options.origin = DWDUMP_PKT_ORIGIN_ALL;
694 dwdump->options.need_pcap = true;
695 dwdump->options.need_mon = true;
696
697 while ((opt = getopt_long(argc, argv, optstring,
698 long_options, NULL)) != -1) {
699 switch (opt) {
700 case 'w':
701 dwdump->options.dumpfile = optarg;
702 break;
703 case 't':
704 dwdump->options.trunc_len = atol(optarg);
705 if (dwdump->options.trunc_len == 0 ||
706 dwdump->options.trunc_len > 0xffff)
707 dwdump->options.trunc_len = 0xffff;
708 break;
709 case 'q':
710 dwdump->options.query = true;
711 dwdump->options.need_pcap = false;
712 dwdump->options.need_mon = false;
713 break;
714 case 'l':
715 dwdump->options.queue_len = atol(optarg);
716 break;
717 case 'p':
718 dwdump->options.passive = true;
719 dwdump->options.need_mon = false;
720 break;
721 case 's':
722 dwdump->options.stats = true;
723 dwdump->options.need_pcap = false;
724 dwdump->options.need_mon = false;
725 break;
726 case 'b':
727 dwdump->options.rxbuf = atol(optarg);
728 break;
729 case 'o':
730 err = dwdump_origin_parse(dwdump, optarg);
731 if (err)
732 return err;
733 break;
734 case 'e':
735 dwdump->options.exit = true;
736 dwdump->options.need_pcap = false;
737 dwdump->options.need_mon = false;
738 break;
739 case 'h':
740 dwdump_usage(stdout);
741 return -1;
742 case '?':
743 dwdump_usage(stderr);
744 return -1;
745 default:
746 fprintf(stderr, "Unknown option: \'%c\'\n", opt);
747 dwdump_usage(stderr);
748 return -1;
749 }
750 }
751
752 return 0;
753 }
754
755 int main(int argc, char **argv)
756 {
757 struct dwdump *dwdump;
758 int err;
759
760 dwdump = calloc(1, sizeof(*dwdump));
761 if (!dwdump) {
762 perror("calloc");
763 goto err_dwdump_alloc;
764 }
765
766 err = dwdump_opts_parse(dwdump, argc, argv);
767 if (err)
768 goto err_opts_parse;
769
770 err = dwdump_init(dwdump);
771 if (err)
772 goto err_dwdump_init;
773
774 err = dwdump_main(dwdump);
775
776 dwdump_fini(dwdump);
777 free(dwdump);
778
779 return err;
780
781 err_dwdump_init:
782 err_opts_parse:
783 free(dwdump);
784 err_dwdump_alloc:
785 exit(EXIT_FAILURE);
786 }