GCC Code Coverage Report


Directory: ./
File: src/main.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 132 222 59.5%
Functions: 4 8 50.0%
Branches: 75 116 64.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11 #include <nft.h>
12
13 #include <stddef.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <getopt.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20
21 #include <nftables/libnftables.h>
22 #include <utils.h>
23 #include <cli.h>
24
25 static struct nft_ctx *nft;
26
27 enum opt_indices {
28 /* General options */
29 IDX_HELP,
30 IDX_VERSION,
31 IDX_VERSION_LONG,
32 /* Ruleset input handling */
33 IDX_FILE,
34 #define IDX_RULESET_INPUT_START IDX_FILE
35 IDX_DEFINE,
36 IDX_INTERACTIVE,
37 IDX_INCLUDEPATH,
38 IDX_CHECK,
39 IDX_OPTIMIZE,
40 #define IDX_RULESET_INPUT_END IDX_OPTIMIZE
41 /* Ruleset list formatting */
42 IDX_HANDLE,
43 #define IDX_RULESET_LIST_START IDX_HANDLE
44 IDX_STATELESS,
45 IDX_TERSE,
46 IDX_SERVICE,
47 IDX_REVERSEDNS,
48 IDX_GUID,
49 IDX_NUMERIC,
50 IDX_NUMERIC_PRIO,
51 IDX_NUMERIC_PROTO,
52 IDX_NUMERIC_TIME,
53 #define IDX_RULESET_LIST_END IDX_NUMERIC_TIME
54 /* Command output formatting */
55 IDX_ECHO,
56 #define IDX_CMD_OUTPUT_START IDX_ECHO
57 IDX_JSON,
58 #if HAVE_FUZZER_BUILD
59 IDX_FUZZER,
60 #endif
61 IDX_DEBUG,
62 #define IDX_CMD_OUTPUT_END IDX_DEBUG
63 };
64
65 enum opt_vals {
66 OPT_HELP = 'h',
67 OPT_VERSION = 'v',
68 OPT_VERSION_LONG = 'V',
69 OPT_CHECK = 'c',
70 OPT_FILE = 'f',
71 OPT_DEFINE = 'D',
72 OPT_INTERACTIVE = 'i',
73 OPT_INCLUDEPATH = 'I',
74 OPT_JSON = 'j',
75 OPT_NUMERIC = 'n',
76 OPT_STATELESS = 's',
77 OPT_IP2NAME = 'N',
78 OPT_SERVICE = 'S',
79 OPT_DEBUG = 'd',
80 OPT_HANDLE_OUTPUT = 'a',
81 OPT_ECHO = 'e',
82 OPT_GUID = 'u',
83 OPT_NUMERIC_PRIO = 'y',
84 OPT_NUMERIC_PROTO = 'p',
85 OPT_NUMERIC_TIME = 'T',
86 OPT_TERSE = 't',
87 OPT_OPTIMIZE = 'o',
88 OPT_INVALID = '?',
89 };
90
91 struct nft_opt {
92 const char *name;
93 enum opt_vals val;
94 const char *arg;
95 const char *help;
96 };
97
98 #define NFT_OPT(n, v, a, h) \
99 (struct nft_opt) { .name = n, .val = v, .arg = a, .help = h }
100
101 static const struct nft_opt nft_options[] = {
102 [IDX_HELP] = NFT_OPT("help", OPT_HELP, NULL,
103 "Show this help"),
104 [IDX_VERSION] = NFT_OPT("version", OPT_VERSION, NULL,
105 "Show version information"),
106 [IDX_VERSION_LONG] = NFT_OPT(NULL, OPT_VERSION_LONG, NULL,
107 "Show extended version information"),
108 [IDX_FILE] = NFT_OPT("file", OPT_FILE, "<filename>",
109 "Read input from <filename>"),
110 [IDX_DEFINE] = NFT_OPT("define", OPT_DEFINE, "<name=value>",
111 "Define variable, e.g. --define foo=1.2.3.4"),
112 [IDX_INTERACTIVE] = NFT_OPT("interactive", OPT_INTERACTIVE, NULL,
113 "Read input from interactive CLI"),
114 [IDX_INCLUDEPATH] = NFT_OPT("includepath", OPT_INCLUDEPATH, "<directory>",
115 "Add <directory> to the paths searched for include files. Default is: " DEFAULT_INCLUDE_PATH),
116 [IDX_CHECK] = NFT_OPT("check", OPT_CHECK, NULL,
117 "Check commands validity without actually applying the changes."),
118 [IDX_HANDLE] = NFT_OPT("handle", OPT_HANDLE_OUTPUT, NULL,
119 "Output rule handle."),
120 [IDX_STATELESS] = NFT_OPT("stateless", OPT_STATELESS, NULL,
121 "Omit stateful information of ruleset."),
122 [IDX_TERSE] = NFT_OPT("terse", OPT_TERSE, NULL,
123 "Omit contents of sets."),
124 [IDX_SERVICE] = NFT_OPT("service", OPT_SERVICE, NULL,
125 "Translate ports to service names as described in /etc/services."),
126 [IDX_REVERSEDNS] = NFT_OPT("reversedns", OPT_IP2NAME, NULL,
127 "Translate IP addresses to names."),
128 [IDX_GUID] = NFT_OPT("guid", OPT_GUID, NULL,
129 "Print UID/GID as defined in /etc/passwd and /etc/group."),
130 [IDX_NUMERIC] = NFT_OPT("numeric", OPT_NUMERIC, NULL,
131 "Print fully numerical output."),
132 [IDX_NUMERIC_PRIO] = NFT_OPT("numeric-priority", OPT_NUMERIC_PRIO, NULL,
133 "Print chain priority numerically."),
134 [IDX_NUMERIC_PROTO] = NFT_OPT("numeric-protocol", OPT_NUMERIC_PROTO, NULL,
135 "Print layer 4 protocols numerically."),
136 [IDX_NUMERIC_TIME] = NFT_OPT("numeric-time", OPT_NUMERIC_TIME, NULL,
137 "Print time values numerically."),
138 [IDX_ECHO] = NFT_OPT("echo", OPT_ECHO, NULL,
139 "Echo what has been added, inserted or replaced."),
140 [IDX_JSON] = NFT_OPT("json", OPT_JSON, NULL,
141 "Format output in JSON"),
142 [IDX_DEBUG] = NFT_OPT("debug", OPT_DEBUG, "<level [,level...]>",
143 "Specify debugging level (scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)"),
144 [IDX_OPTIMIZE] = NFT_OPT("optimize", OPT_OPTIMIZE, NULL,
145 "Optimize ruleset"),
146 };
147
148 #define NR_NFT_OPTIONS (sizeof(nft_options) / sizeof(nft_options[0]))
149
150 31968 static const char *get_optstring(void)
151 {
152 static char optstring[2 * NR_NFT_OPTIONS + 2];
153
154
1/2
✓ Branch 0 (2→3) taken 31968 times.
✗ Branch 1 (2→12) not taken.
31968 if (!optstring[0]) {
155 size_t i, j;
156
157 31968 optstring[0] = '+';
158
3/4
✓ Branch 0 (8→9) taken 703296 times.
✓ Branch 1 (8→10) taken 31968 times.
✓ Branch 2 (9→4) taken 703296 times.
✗ Branch 3 (9→10) not taken.
735264 for (i = 0, j = 1; i < NR_NFT_OPTIONS && j < sizeof(optstring); i++)
159 703296 j += snprintf(optstring + j, sizeof(optstring) - j, "%c%s",
160 703296 nft_options[i].val,
161
2/2
✓ Branch 0 (4→5) taken 127872 times.
✓ Branch 1 (4→6) taken 575424 times.
703296 nft_options[i].arg ? ":" : "");
162
163
1/2
✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 31968 times.
31968 assert(j < sizeof(optstring));
164 }
165 31968 return optstring;
166 }
167
168 31968 static const struct option *get_options(void)
169 {
170 static struct option options[NR_NFT_OPTIONS + 1];
171
172
1/2
✓ Branch 0 (2→3) taken 31968 times.
✗ Branch 1 (2→8) not taken.
31968 if (!options[0].name) {
173 size_t i, j;
174
175
2/2
✓ Branch 0 (7→4) taken 703296 times.
✓ Branch 1 (7→8) taken 31968 times.
735264 for (i = 0, j = 0; i < NR_NFT_OPTIONS; ++i) {
176
2/2
✓ Branch 0 (4→5) taken 671328 times.
✓ Branch 1 (4→6) taken 31968 times.
703296 if (nft_options[i].name) {
177 671328 options[j].name = nft_options[i].name;
178 671328 options[j].val = nft_options[i].val;
179 671328 options[j].has_arg = nft_options[i].arg != NULL;
180 671328 j++;
181 }
182 }
183 }
184 31968 return options;
185 }
186
187 ✗ static void print_option(const struct nft_opt *opt)
188 {
189 ✗ char optbuf[35] = "";
190 int i;
191
192 ✗ i = snprintf(optbuf, sizeof(optbuf), " -%c", opt->val);
193 ✗ if (opt->name)
194 ✗ i += snprintf(optbuf + i, sizeof(optbuf) - i, ", --%s",
195 ✗ opt->name);
196 ✗ if (opt->arg)
197 ✗ i += snprintf(optbuf + i, sizeof(optbuf) - i, " %s", opt->arg);
198
199 ✗ printf("%-34s%s\n", optbuf, opt->help);
200 ✗ }
201
202 ✗ static void show_help(const char *name)
203 {
204 int i;
205
206 ✗ printf("Usage: %s [ options ] [ cmds... ]\n"
207 "\n"
208 "Options (general):\n", name);
209
210 ✗ print_option(&nft_options[IDX_HELP]);
211 ✗ print_option(&nft_options[IDX_VERSION]);
212 ✗ print_option(&nft_options[IDX_VERSION_LONG]);
213
214 ✗ printf("\n"
215 "Options (ruleset input handling):"
216 "\n");
217
218 ✗ for (i = IDX_RULESET_INPUT_START; i <= IDX_RULESET_INPUT_END; i++)
219 ✗ print_option(&nft_options[i]);
220
221 ✗ printf("\n"
222 "Options (ruleset list formatting):"
223 "\n");
224
225 ✗ for (i = IDX_RULESET_LIST_START; i <= IDX_RULESET_LIST_END; i++)
226 ✗ print_option(&nft_options[i]);
227
228 ✗ printf("\n"
229 "Options (command output formatting):"
230 "\n");
231
232 ✗ for (i = IDX_CMD_OUTPUT_START; i <= IDX_CMD_OUTPUT_END; i++)
233 ✗ print_option(&nft_options[i]);
234
235 ✗ fputs("\n", stdout);
236 ✗ }
237
238 ✗ static void show_version(void)
239 {
240 const char *cli, *minigmp, *json, *xt;
241
242 #if defined(HAVE_LIBREADLINE)
243 ✗ cli = "readline";
244 #elif defined(HAVE_LIBEDIT)
245 cli = "editline";
246 #elif defined(HAVE_LIBLINENOISE)
247 cli = "linenoise";
248 #else
249 cli = "no";
250 #endif
251
252 #if defined(HAVE_MINIGMP)
253 minigmp = "yes";
254 #else
255 ✗ minigmp = "no";
256 #endif
257
258 #if defined(HAVE_JSON)
259 ✗ json = "yes";
260 #else
261 json = "no";
262 #endif
263
264 #if defined(HAVE_XTABLES)
265 ✗ xt = "yes";
266 #else
267 xt = "no";
268 #endif
269
270 ✗ printf("%s v%s (%s)\n"
271 " cli: %s\n"
272 " json: %s\n"
273 " minigmp: %s\n"
274 " libxtables: %s\n",
275 PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME,
276 cli, json, minigmp, xt);
277
278 ✗ }
279
280 static const struct {
281 const char *name;
282 enum nft_debug_level level;
283 } debug_param[] = {
284 {
285 .name = "scanner",
286 .level = NFT_DEBUG_SCANNER,
287 },
288 {
289 .name = "parser",
290 .level = NFT_DEBUG_PARSER,
291 },
292 {
293 .name = "eval",
294 .level = NFT_DEBUG_EVALUATION,
295 },
296 {
297 .name = "netlink",
298 .level = NFT_DEBUG_NETLINK,
299 },
300 {
301 .name = "mnl",
302 .level = NFT_DEBUG_MNL,
303 },
304 {
305 .name = "proto-ctx",
306 .level = NFT_DEBUG_PROTO_CTX,
307 },
308 {
309 .name = "segtree",
310 .level = NFT_DEBUG_SEGTREE,
311 },
312 {
313 .name = "all",
314 .level = ~0,
315 },
316 };
317
318 ✗ static void nft_options_error(int argc, char * const argv[], int pos)
319 {
320 int i;
321
322 ✗ fprintf(stderr, "Error: syntax error, options must be specified before commands\n");
323 ✗ for (i = 0; i < argc; i++)
324 ✗ fprintf(stderr, "%s ", argv[i]);
325 ✗ printf("\n%4c%*s\n", '^', pos - 2, "~~");
326 ✗ }
327
328 31968 static bool nft_options_check(int argc, char * const argv[])
329 {
330 31968 bool skip = false, nonoption = false;
331 31968 int pos = 0, i;
332 size_t j;
333
334
2/2
✓ Branch 0 (21→3) taken 130857 times.
✓ Branch 1 (21→22) taken 19308 times.
150165 for (i = 1; i < argc; i++) {
335 130857 pos += strlen(argv[i - 1]) + 1;
336
2/2
✓ Branch 0 (3→4) taken 12660 times.
✓ Branch 1 (3→5) taken 118197 times.
130857 if (argv[i][0] == '{') {
337 12660 break;
338
2/2
✓ Branch 0 (5→6) taken 8383 times.
✓ Branch 1 (5→7) taken 109814 times.
118197 } else if (skip) {
339 8383 skip = false;
340 8383 continue;
341
2/2
✓ Branch 0 (7→8) taken 93163 times.
✓ Branch 1 (7→9) taken 16651 times.
109814 } else if (argv[i][0] != '-') {
342 93163 nonoption = true;
343 93163 continue;
344 }
345
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 16651 times.
16651 if (nonoption) {
346 ✗ nft_options_error(argc, argv, pos);
347 ✗ return false;
348 }
349
2/2
✓ Branch 0 (19→13) taken 215945 times.
✓ Branch 1 (19→20) taken 8268 times.
224213 for (j = 0; j < NR_NFT_OPTIONS; j++) {
350
2/2
✓ Branch 0 (13→14) taken 41569 times.
✓ Branch 1 (13→18) taken 174376 times.
215945 if (nft_options[j].arg &&
351
2/2
✓ Branch 0 (14→15) taken 33222 times.
✓ Branch 1 (14→17) taken 8347 times.
41569 (argv[i][1] == (char)nft_options[j].val ||
352
2/2
✓ Branch 0 (15→16) taken 14584 times.
✓ Branch 1 (15→18) taken 18638 times.
33222 (argv[i][1] == '-' &&
353
2/2
✓ Branch 0 (16→17) taken 36 times.
✓ Branch 1 (16→18) taken 14548 times.
14584 !strcmp(argv[i] + 2, nft_options[j].name)))) {
354 8383 skip = true;
355 8383 break;
356 }
357 }
358 }
359
360 31968 return true;
361 }
362
363 31968 int main(int argc, char * const *argv)
364 {
365 31968 const struct option *options = get_options();
366 31968 bool interactive = false, define = false;
367 31968 const char *optstring = get_optstring();
368 31968 unsigned int output_flags = 0;
369 31968 int i, val, rc = EXIT_SUCCESS;
370 unsigned int debug_mask;
371 31968 char *filename = NULL;
372 unsigned int len;
373
374 /* nftables cannot be used with setuid in a safe way. */
375
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 31968 times.
31968 if (getuid() != geteuid())
376 ✗ _exit(111);
377
378
1/2
✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 31968 times.
31968 if (!nft_options_check(argc, argv))
379 ✗ exit(EXIT_FAILURE);
380
381 31968 nft = nft_ctx_new(NFT_CTX_DEFAULT);
382
383 while (1) {
384 48993 val = getopt_long(argc, argv, optstring, options, NULL);
385
2/2
✓ Branch 0 (14→15) taken 31968 times.
✓ Branch 1 (14→16) taken 17025 times.
48993 if (val == -1)
386 31968 break;
387
388
13/24
✗ Branch 0 (16→17) not taken.
✗ Branch 1 (16→19) not taken.
✗ Branch 2 (16→21) not taken.
✓ Branch 3 (16→23) taken 10 times.
✓ Branch 4 (16→28) taken 3648 times.
✓ Branch 5 (16→29) taken 8341 times.
✓ Branch 6 (16→33) taken 21 times.
✓ Branch 7 (16→37) taken 13 times.
✓ Branch 8 (16→41) taken 744 times.
✓ Branch 9 (16→42) taken 4 times.
✗ Branch 10 (16→43) not taken.
✗ Branch 11 (16→44) not taken.
✓ Branch 12 (16→45) taken 28 times.
✓ Branch 13 (16→62) taken 63 times.
✓ Branch 14 (16→63) taken 394 times.
✓ Branch 15 (16→64) taken 3691 times.
✗ Branch 16 (16→65) not taken.
✗ Branch 17 (16→66) not taken.
✗ Branch 18 (16→67) not taken.
✗ Branch 19 (16→68) not taken.
✓ Branch 20 (16→69) taken 8 times.
✓ Branch 21 (16→70) taken 60 times.
✗ Branch 22 (16→72) not taken.
✗ Branch 23 (16→74) not taken.
17025 switch (val) {
389 ✗ case OPT_HELP:
390 ✗ show_help(argv[0]);
391 ✗ goto out;
392 ✗ case OPT_VERSION:
393 ✗ printf("%s v%s (%s)\n",
394 PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME);
395 ✗ goto out;
396 ✗ case OPT_VERSION_LONG:
397 ✗ show_version();
398 ✗ goto out;
399 10 case OPT_DEFINE:
400
1/2
✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→27) taken 10 times.
10 if (nft_ctx_add_var(nft, optarg)) {
401 ✗ fprintf(stderr,
402 "Error: Failed to define variable '%s'\n",
403 optarg);
404 ✗ goto out_fail;
405 }
406 10 define = true;
407 10 break;
408 3648 case OPT_CHECK:
409 3648 nft_ctx_set_dry_run(nft, true);
410 3648 break;
411 8341 case OPT_FILE:
412
1/2
✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→32) taken 8341 times.
8341 if (interactive) {
413 ✗ fprintf(stderr,
414 "Error: -i/--interactive and -f/--file options cannot be combined\n");
415 ✗ goto out_fail;
416 }
417 8341 filename = optarg;
418 8341 break;
419 21 case OPT_INTERACTIVE:
420
1/2
✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→36) taken 21 times.
21 if (filename) {
421 ✗ fprintf(stderr,
422 "Error: -i/--interactive and -f/--file options cannot be combined\n");
423 ✗ goto out_fail;
424 }
425 21 interactive = true;
426 21 break;
427 13 case OPT_INCLUDEPATH:
428
1/2
✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→40) taken 13 times.
13 if (nft_ctx_add_include_path(nft, optarg)) {
429 ✗ fprintf(stderr,
430 "Warning: Cannot include path '%s'\n",
431 optarg);
432 }
433 13 break;
434 744 case OPT_NUMERIC:
435 744 output_flags |= NFT_CTX_OUTPUT_NUMERIC_ALL;
436 744 break;
437 4 case OPT_STATELESS:
438 4 output_flags |= NFT_CTX_OUTPUT_STATELESS;
439 4 break;
440 ✗ case OPT_IP2NAME:
441 ✗ output_flags |= NFT_CTX_OUTPUT_REVERSEDNS;
442 ✗ break;
443 ✗ case OPT_SERVICE:
444 ✗ output_flags |= NFT_CTX_OUTPUT_SERVICE;
445 ✗ break;
446 28 case OPT_DEBUG:
447 28 debug_mask = nft_ctx_output_get_debug(nft);
448 ✗ for (;;) {
449 unsigned int i;
450 char *end;
451
452 28 end = strchr(optarg, ',');
453
1/2
✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→48) taken 28 times.
28 if (end)
454 ✗ *end = '\0';
455
456
1/2
✓ Branch 0 (53→49) taken 110 times.
✗ Branch 1 (53→54) not taken.
110 for (i = 0; i < array_size(debug_param); i++) {
457
2/2
✓ Branch 0 (49→50) taken 82 times.
✓ Branch 1 (49→51) taken 28 times.
110 if (strcmp(debug_param[i].name, optarg))
458 82 continue;
459 28 debug_mask |= debug_param[i].level;
460 28 break;
461 }
462
463
1/2
✗ Branch 0 (54→55) not taken.
✓ Branch 1 (54→57) taken 28 times.
28 if (i == array_size(debug_param)) {
464 ✗ fprintf(stderr, "Error: invalid debug parameter `%s'\n",
465 optarg);
466 ✗ goto out_fail;
467 }
468
469
1/2
✓ Branch 0 (57→58) taken 28 times.
✗ Branch 1 (57→59) not taken.
28 if (end == NULL)
470 28 break;
471 ✗ optarg = end + 1;
472 }
473 28 nft_ctx_output_set_debug(nft, debug_mask);
474 28 break;
475 63 case OPT_HANDLE_OUTPUT:
476 63 output_flags |= NFT_CTX_OUTPUT_HANDLE;
477 63 break;
478 394 case OPT_ECHO:
479 394 output_flags |= NFT_CTX_OUTPUT_ECHO;
480 394 break;
481 3691 case OPT_JSON:
482 #ifdef HAVE_LIBJANSSON
483 3691 output_flags |= NFT_CTX_OUTPUT_JSON;
484 #else
485 fprintf(stderr, "Error: JSON support not compiled-in\n");
486 goto out_fail;
487 #endif
488 3691 break;
489 ✗ case OPT_GUID:
490 ✗ output_flags |= NFT_CTX_OUTPUT_GUID;
491 ✗ break;
492 ✗ case OPT_NUMERIC_PRIO:
493 ✗ output_flags |= NFT_CTX_OUTPUT_NUMERIC_PRIO;
494 ✗ break;
495 ✗ case OPT_NUMERIC_PROTO:
496 ✗ output_flags |= NFT_CTX_OUTPUT_NUMERIC_PROTO;
497 ✗ break;
498 ✗ case OPT_NUMERIC_TIME:
499 ✗ output_flags |= NFT_CTX_OUTPUT_NUMERIC_TIME;
500 ✗ break;
501 8 case OPT_TERSE:
502 8 output_flags |= NFT_CTX_OUTPUT_TERSE;
503 8 break;
504 60 case OPT_OPTIMIZE:
505 60 nft_ctx_set_optimize(nft, 0x1);
506 60 break;
507 ✗ case OPT_INVALID:
508 ✗ goto out_fail;
509 }
510 }
511
512
3/4
✓ Branch 0 (75→76) taken 23627 times.
✓ Branch 1 (75→79) taken 8341 times.
✗ Branch 2 (76→77) not taken.
✓ Branch 3 (76→79) taken 23627 times.
31968 if (!filename && define) {
513 ✗ fprintf(stderr, "Error: -D/--define can only be used with -f/--filename\n");
514 ✗ goto out_fail;
515 }
516
517 31968 nft_ctx_output_set_flags(nft, output_flags);
518
519
2/2
✓ Branch 0 (80→81) taken 23603 times.
✓ Branch 1 (80→94) taken 8365 times.
31968 if (optind != argc) {
520 char *buf;
521
522
2/2
✓ Branch 0 (83→82) taken 122559 times.
✓ Branch 1 (83→84) taken 23603 times.
146162 for (len = 0, i = optind; i < argc; i++)
523 122559 len += strlen(argv[i]) + strlen(" ");
524
525 23603 buf = calloc(1, len);
526
1/2
✗ Branch 0 (84→85) not taken.
✓ Branch 1 (84→87) taken 23603 times.
23603 if (buf == NULL) {
527 ✗ fprintf(stderr, "%s:%u: Memory allocation failure\n",
528 __FILE__, __LINE__);
529 ✗ goto out_fail;
530 }
531
2/2
✓ Branch 0 (91→88) taken 122559 times.
✓ Branch 1 (91→92) taken 23603 times.
146162 for (i = optind; i < argc; i++) {
532 122559 strcat(buf, argv[i]);
533
2/2
✓ Branch 0 (88→89) taken 98956 times.
✓ Branch 1 (88→90) taken 23603 times.
122559 if (i + 1 < argc)
534 98956 strcat(buf, " ");
535 }
536 23603 rc = !!nft_run_cmd_from_buffer(nft, buf);
537 23603 free(buf);
538
2/2
✓ Branch 0 (94→95) taken 8341 times.
✓ Branch 1 (94→97) taken 24 times.
8365 } else if (filename != NULL) {
539 8341 rc = !!nft_run_cmd_from_filename(nft, filename);
540
2/2
✓ Branch 0 (97→98) taken 21 times.
✓ Branch 1 (97→102) taken 3 times.
24 } else if (interactive) {
541
1/2
✗ Branch 0 (99→100) not taken.
✓ Branch 1 (99→104) taken 21 times.
21 if (cli_init(nft) < 0) {
542 ✗ fprintf(stderr, "%s: interactive CLI not supported in this build\n",
543 argv[0]);
544 ✗ goto out_fail;
545 }
546 } else {
547 3 fprintf(stderr, "%s: no command specified\n", argv[0]);
548 3 goto out_fail;
549 }
550
551 31965 out:
552 31965 nft_ctx_free(nft);
553 31965 return rc;
554 3 out_fail:
555 3 nft_ctx_free(nft);
556 3 return EXIT_FAILURE;
557 }
558