Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
  3
  4#include <ctype.h>
  5#include <errno.h>
  6#include <getopt.h>
  7#include <linux/bpf.h>
  8#include <stdio.h>
  9#include <stdlib.h>
 10#include <string.h>
 11
 12#include <bpf/bpf.h>
 13#include <bpf/btf.h>
 14#include <bpf/hashmap.h>
 15#include <bpf/libbpf.h>
 16
 17#include "main.h"
 18
 19#define BATCH_LINE_LEN_MAX 65536
 20#define BATCH_ARG_NB_MAX 4096
 21
 22const char *bin_name;
 23static int last_argc;
 24static char **last_argv;
 25static int (*last_do_help)(int argc, char **argv);
 26json_writer_t *json_wtr;
 27bool pretty_output;
 28bool json_output;
 29bool show_pinned;
 30bool block_mount;
 31bool verifier_logs;
 32bool relaxed_maps;
 33bool use_loader;
 34struct btf *base_btf;
 35struct hashmap *refs_table;
 36
 37static void __noreturn clean_and_exit(int i)
 38{
 39	if (json_output)
 40		jsonw_destroy(&json_wtr);
 41
 42	exit(i);
 43}
 44
 45void usage(void)
 46{
 47	last_do_help(last_argc - 1, last_argv + 1);
 48
 49	clean_and_exit(-1);
 50}
 51
 52static int do_help(int argc, char **argv)
 53{
 54	if (json_output) {
 55		jsonw_null(json_wtr);
 56		return 0;
 57	}
 58
 59	fprintf(stderr,
 60		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
 61		"       %s batch file FILE\n"
 62		"       %s version\n"
 63		"\n"
 64		"       OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
 65		"       " HELP_SPEC_OPTIONS " |\n"
 66		"                    {-V|--version} }\n"
 67		"",
 68		bin_name, bin_name, bin_name);
 69
 70	return 0;
 71}
 72
 73static int do_batch(int argc, char **argv);
 74static int do_version(int argc, char **argv);
 75
 76static const struct cmd commands[] = {
 77	{ "help",	do_help },
 78	{ "batch",	do_batch },
 79	{ "prog",	do_prog },
 80	{ "map",	do_map },
 81	{ "link",	do_link },
 82	{ "cgroup",	do_cgroup },
 83	{ "perf",	do_perf },
 84	{ "net",	do_net },
 85	{ "feature",	do_feature },
 86	{ "btf",	do_btf },
 87	{ "gen",	do_gen },
 88	{ "struct_ops",	do_struct_ops },
 89	{ "iter",	do_iter },
 90	{ "version",	do_version },
 91	{ 0 }
 92};
 93
 94#ifndef BPFTOOL_VERSION
 95/* bpftool's major and minor version numbers are aligned on libbpf's. There is
 96 * an offset of 6 for the version number, because bpftool's version was higher
 97 * than libbpf's when we adopted this scheme. The patch number remains at 0
 98 * for now. Set BPFTOOL_VERSION to override.
 99 */
100#define BPFTOOL_MAJOR_VERSION (LIBBPF_MAJOR_VERSION + 6)
101#define BPFTOOL_MINOR_VERSION LIBBPF_MINOR_VERSION
102#define BPFTOOL_PATCH_VERSION 0
103#endif
104
105static void
106print_feature(const char *feature, bool state, unsigned int *nb_features)
107{
108	if (state) {
109		printf("%s %s", *nb_features ? "," : "", feature);
110		*nb_features = *nb_features + 1;
111	}
112}
113
114static int do_version(int argc, char **argv)
115{
116#ifdef HAVE_LIBBFD_SUPPORT
117	const bool has_libbfd = true;
118#else
119	const bool has_libbfd = false;
120#endif
121#ifdef HAVE_LLVM_SUPPORT
122	const bool has_llvm = true;
123#else
124	const bool has_llvm = false;
125#endif
126#ifdef BPFTOOL_WITHOUT_SKELETONS
127	const bool has_skeletons = false;
128#else
129	const bool has_skeletons = true;
130#endif
131	bool bootstrap = false;
132	int i;
133
134	for (i = 0; commands[i].cmd; i++) {
135		if (!strcmp(commands[i].cmd, "prog")) {
136			/* Assume we run a bootstrap version if "bpftool prog"
137			 * is not available.
138			 */
139			bootstrap = !commands[i].func;
140			break;
141		}
142	}
143
144	if (json_output) {
145		jsonw_start_object(json_wtr);	/* root object */
146
147		jsonw_name(json_wtr, "version");
148#ifdef BPFTOOL_VERSION
149		jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
150#else
151		jsonw_printf(json_wtr, "\"%d.%d.%d\"", BPFTOOL_MAJOR_VERSION,
152			     BPFTOOL_MINOR_VERSION, BPFTOOL_PATCH_VERSION);
153#endif
154		jsonw_name(json_wtr, "libbpf_version");
155		jsonw_printf(json_wtr, "\"%d.%d\"",
156			     libbpf_major_version(), libbpf_minor_version());
157
158		jsonw_name(json_wtr, "features");
159		jsonw_start_object(json_wtr);	/* features */
160		jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
161		jsonw_bool_field(json_wtr, "llvm", has_llvm);
162		jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
163		jsonw_bool_field(json_wtr, "bootstrap", bootstrap);
164		jsonw_end_object(json_wtr);	/* features */
165
166		jsonw_end_object(json_wtr);	/* root object */
167	} else {
168		unsigned int nb_features = 0;
169
170#ifdef BPFTOOL_VERSION
171		printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
172#else
173		printf("%s v%d.%d.%d\n", bin_name, BPFTOOL_MAJOR_VERSION,
174		       BPFTOOL_MINOR_VERSION, BPFTOOL_PATCH_VERSION);
175#endif
176		printf("using libbpf %s\n", libbpf_version_string());
177		printf("features:");
178		print_feature("libbfd", has_libbfd, &nb_features);
179		print_feature("llvm", has_llvm, &nb_features);
180		print_feature("skeletons", has_skeletons, &nb_features);
181		print_feature("bootstrap", bootstrap, &nb_features);
182		printf("\n");
183	}
184	return 0;
185}
186
 
 
 
 
 
 
 
187int cmd_select(const struct cmd *cmds, int argc, char **argv,
188	       int (*help)(int argc, char **argv))
189{
190	unsigned int i;
191
192	last_argc = argc;
193	last_argv = argv;
194	last_do_help = help;
195
196	if (argc < 1 && cmds[0].func)
197		return cmds[0].func(argc, argv);
198
199	for (i = 0; cmds[i].cmd; i++) {
200		if (is_prefix(*argv, cmds[i].cmd)) {
201			if (!cmds[i].func) {
202				p_err("command '%s' is not supported in bootstrap mode",
203				      cmds[i].cmd);
204				return -1;
205			}
206			return cmds[i].func(argc - 1, argv + 1);
207		}
208	}
209
210	help(argc - 1, argv + 1);
211
212	return -1;
213}
214
215bool is_prefix(const char *pfx, const char *str)
216{
217	if (!pfx)
218		return false;
219	if (strlen(str) < strlen(pfx))
220		return false;
221
222	return !memcmp(str, pfx, strlen(pfx));
223}
224
225/* Last argument MUST be NULL pointer */
226int detect_common_prefix(const char *arg, ...)
227{
228	unsigned int count = 0;
229	const char *ref;
230	char msg[256];
231	va_list ap;
232
233	snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
234	va_start(ap, arg);
235	while ((ref = va_arg(ap, const char *))) {
236		if (!is_prefix(arg, ref))
237			continue;
238		count++;
239		if (count > 1)
240			strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
241		strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
242	}
243	va_end(ap);
244	strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
245
246	if (count >= 2) {
247		p_err("%s", msg);
248		return -1;
249	}
250
251	return 0;
252}
253
254void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
255{
256	unsigned char *data = arg;
257	unsigned int i;
258
259	for (i = 0; i < n; i++) {
260		const char *pfx = "";
261
262		if (!i)
263			/* nothing */;
264		else if (!(i % 16))
265			fprintf(f, "\n");
266		else if (!(i % 8))
267			fprintf(f, "  ");
268		else
269			pfx = sep;
270
271		fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
272	}
273}
274
275/* Split command line into argument vector. */
276static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
277{
278	static const char ws[] = " \t\r\n";
279	char *cp = line;
280	int n_argc = 0;
281
282	while (*cp) {
283		/* Skip leading whitespace. */
284		cp += strspn(cp, ws);
285
286		if (*cp == '\0')
287			break;
288
289		if (n_argc >= (maxargs - 1)) {
290			p_err("too many arguments to command %d", cmd_nb);
291			return -1;
292		}
293
294		/* Word begins with quote. */
295		if (*cp == '\'' || *cp == '"') {
296			char quote = *cp++;
297
298			n_argv[n_argc++] = cp;
299			/* Find ending quote. */
300			cp = strchr(cp, quote);
301			if (!cp) {
302				p_err("unterminated quoted string in command %d",
303				      cmd_nb);
304				return -1;
305			}
306		} else {
307			n_argv[n_argc++] = cp;
308
309			/* Find end of word. */
310			cp += strcspn(cp, ws);
311			if (*cp == '\0')
312				break;
313		}
314
315		/* Separate words. */
316		*cp++ = 0;
317	}
318	n_argv[n_argc] = NULL;
319
320	return n_argc;
321}
322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323static int do_batch(int argc, char **argv)
324{
325	char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
326	char *n_argv[BATCH_ARG_NB_MAX];
327	unsigned int lines = 0;
328	int n_argc;
329	FILE *fp;
330	char *cp;
331	int err = 0;
332	int i;
333
334	if (argc < 2) {
335		p_err("too few parameters for batch");
336		return -1;
337	} else if (argc > 2) {
338		p_err("too many parameters for batch");
339		return -1;
340	} else if (!is_prefix(*argv, "file")) {
341		p_err("expected 'file', got: %s", *argv);
342		return -1;
 
 
 
343	}
344	NEXT_ARG();
345
346	if (!strcmp(*argv, "-"))
347		fp = stdin;
348	else
349		fp = fopen(*argv, "r");
350	if (!fp) {
351		p_err("Can't open file (%s): %s", *argv, strerror(errno));
352		return -1;
353	}
354
355	if (json_output)
356		jsonw_start_array(json_wtr);
357	while (fgets(buf, sizeof(buf), fp)) {
358		cp = strchr(buf, '#');
359		if (cp)
360			*cp = '\0';
361
362		if (strlen(buf) == sizeof(buf) - 1) {
363			errno = E2BIG;
364			break;
365		}
366
367		/* Append continuation lines if any (coming after a line ending
368		 * with '\' in the batch file).
369		 */
370		while ((cp = strstr(buf, "\\\n")) != NULL) {
371			if (!fgets(contline, sizeof(contline), fp) ||
372			    strlen(contline) == 0) {
373				p_err("missing continuation line on command %d",
374				      lines);
375				err = -1;
376				goto err_close;
377			}
378
379			cp = strchr(contline, '#');
380			if (cp)
381				*cp = '\0';
382
383			if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
384				p_err("command %d is too long", lines);
385				err = -1;
386				goto err_close;
387			}
388			buf[strlen(buf) - 2] = '\0';
389			strcat(buf, contline);
390		}
391
392		n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
393		if (!n_argc)
394			continue;
395		if (n_argc < 0) {
396			err = n_argc;
397			goto err_close;
398		}
399
400		if (json_output) {
401			jsonw_start_object(json_wtr);
402			jsonw_name(json_wtr, "command");
403			jsonw_start_array(json_wtr);
404			for (i = 0; i < n_argc; i++)
405				jsonw_string(json_wtr, n_argv[i]);
406			jsonw_end_array(json_wtr);
407			jsonw_name(json_wtr, "output");
408		}
409
410		err = cmd_select(commands, n_argc, n_argv, do_help);
411
412		if (json_output)
413			jsonw_end_object(json_wtr);
414
415		if (err)
416			goto err_close;
417
418		lines++;
419	}
420
421	if (errno && errno != ENOENT) {
422		p_err("reading batch file failed: %s", strerror(errno));
423		err = -1;
424	} else {
425		if (!json_output)
426			printf("processed %d commands\n", lines);
 
427	}
428err_close:
429	if (fp != stdin)
430		fclose(fp);
431
432	if (json_output)
433		jsonw_end_array(json_wtr);
434
435	return err;
436}
437
438int main(int argc, char **argv)
439{
440	static const struct option options[] = {
441		{ "json",	no_argument,	NULL,	'j' },
442		{ "help",	no_argument,	NULL,	'h' },
443		{ "pretty",	no_argument,	NULL,	'p' },
444		{ "version",	no_argument,	NULL,	'V' },
445		{ "bpffs",	no_argument,	NULL,	'f' },
446		{ "mapcompat",	no_argument,	NULL,	'm' },
447		{ "nomount",	no_argument,	NULL,	'n' },
448		{ "debug",	no_argument,	NULL,	'd' },
449		{ "use-loader",	no_argument,	NULL,	'L' },
450		{ "base-btf",	required_argument, NULL, 'B' },
451		{ 0 }
452	};
453	bool version_requested = false;
454	int opt, ret;
455
456	setlinebuf(stdout);
457
458#ifdef USE_LIBCAP
459	/* Libcap < 2.63 hooks before main() to compute the number of
460	 * capabilities of the running kernel, and doing so it calls prctl()
461	 * which may fail and set errno to non-zero.
462	 * Let's reset errno to make sure this does not interfere with the
463	 * batch mode.
464	 */
465	errno = 0;
466#endif
467
468	last_do_help = do_help;
469	pretty_output = false;
470	json_output = false;
471	show_pinned = false;
472	block_mount = false;
473	bin_name = "bpftool";
 
 
 
474
475	opterr = 0;
476	while ((opt = getopt_long(argc, argv, "VhpjfLmndB:l",
477				  options, NULL)) >= 0) {
478		switch (opt) {
479		case 'V':
480			version_requested = true;
481			break;
482		case 'h':
483			return do_help(argc, argv);
484		case 'p':
485			pretty_output = true;
486			/* fall through */
487		case 'j':
488			if (!json_output) {
489				json_wtr = jsonw_new(stdout);
490				if (!json_wtr) {
491					p_err("failed to create JSON writer");
492					return -1;
493				}
494				json_output = true;
495			}
496			jsonw_pretty(json_wtr, pretty_output);
497			break;
498		case 'f':
499			show_pinned = true;
500			break;
501		case 'm':
502			relaxed_maps = true;
503			break;
504		case 'n':
505			block_mount = true;
506			break;
507		case 'd':
508			libbpf_set_print(print_all_levels);
509			verifier_logs = true;
510			break;
511		case 'B':
512			base_btf = btf__parse(optarg, NULL);
513			if (!base_btf) {
514				p_err("failed to parse base BTF at '%s': %d\n",
515				      optarg, -errno);
516				return -1;
517			}
518			break;
519		case 'L':
520			use_loader = true;
521			break;
522		default:
523			p_err("unrecognized option '%s'", argv[optind - 1]);
524			if (json_output)
525				clean_and_exit(-1);
526			else
527				usage();
528		}
529	}
530
531	argc -= optind;
532	argv += optind;
533	if (argc < 0)
534		usage();
535
536	if (version_requested)
537		return do_version(argc, argv);
538
539	ret = cmd_select(commands, argc, argv, do_help);
540
541	if (json_output)
542		jsonw_destroy(&json_wtr);
543
544	btf__free(base_btf);
 
 
 
545
546	return ret;
547}
v5.4
  1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
  3
  4#include <ctype.h>
  5#include <errno.h>
  6#include <getopt.h>
  7#include <linux/bpf.h>
  8#include <stdio.h>
  9#include <stdlib.h>
 10#include <string.h>
 11
 12#include <bpf.h>
 13#include <libbpf.h>
 
 
 14
 15#include "main.h"
 16
 17#define BATCH_LINE_LEN_MAX 65536
 18#define BATCH_ARG_NB_MAX 4096
 19
 20const char *bin_name;
 21static int last_argc;
 22static char **last_argv;
 23static int (*last_do_help)(int argc, char **argv);
 24json_writer_t *json_wtr;
 25bool pretty_output;
 26bool json_output;
 27bool show_pinned;
 28bool block_mount;
 29bool verifier_logs;
 30int bpf_flags;
 31struct pinned_obj_table prog_table;
 32struct pinned_obj_table map_table;
 
 33
 34static void __noreturn clean_and_exit(int i)
 35{
 36	if (json_output)
 37		jsonw_destroy(&json_wtr);
 38
 39	exit(i);
 40}
 41
 42void usage(void)
 43{
 44	last_do_help(last_argc - 1, last_argv + 1);
 45
 46	clean_and_exit(-1);
 47}
 48
 49static int do_help(int argc, char **argv)
 50{
 51	if (json_output) {
 52		jsonw_null(json_wtr);
 53		return 0;
 54	}
 55
 56	fprintf(stderr,
 57		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
 58		"       %s batch file FILE\n"
 59		"       %s version\n"
 60		"\n"
 61		"       OBJECT := { prog | map | cgroup | perf | net | feature | btf }\n"
 62		"       " HELP_SPEC_OPTIONS "\n"
 
 63		"",
 64		bin_name, bin_name, bin_name);
 65
 66	return 0;
 67}
 68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 69static int do_version(int argc, char **argv)
 70{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 71	if (json_output) {
 72		jsonw_start_object(json_wtr);
 
 73		jsonw_name(json_wtr, "version");
 
 74		jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
 75		jsonw_end_object(json_wtr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 76	} else {
 
 
 
 77		printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
 
 
 
 
 
 
 
 
 
 
 
 78	}
 79	return 0;
 80}
 81
 82static int __printf(2, 0)
 83print_all_levels(__maybe_unused enum libbpf_print_level level,
 84		 const char *format, va_list args)
 85{
 86	return vfprintf(stderr, format, args);
 87}
 88
 89int cmd_select(const struct cmd *cmds, int argc, char **argv,
 90	       int (*help)(int argc, char **argv))
 91{
 92	unsigned int i;
 93
 94	last_argc = argc;
 95	last_argv = argv;
 96	last_do_help = help;
 97
 98	if (argc < 1 && cmds[0].func)
 99		return cmds[0].func(argc, argv);
100
101	for (i = 0; cmds[i].func; i++)
102		if (is_prefix(*argv, cmds[i].cmd))
 
 
 
 
 
103			return cmds[i].func(argc - 1, argv + 1);
 
 
104
105	help(argc - 1, argv + 1);
106
107	return -1;
108}
109
110bool is_prefix(const char *pfx, const char *str)
111{
112	if (!pfx)
113		return false;
114	if (strlen(str) < strlen(pfx))
115		return false;
116
117	return !memcmp(str, pfx, strlen(pfx));
118}
119
120/* Last argument MUST be NULL pointer */
121int detect_common_prefix(const char *arg, ...)
122{
123	unsigned int count = 0;
124	const char *ref;
125	char msg[256];
126	va_list ap;
127
128	snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
129	va_start(ap, arg);
130	while ((ref = va_arg(ap, const char *))) {
131		if (!is_prefix(arg, ref))
132			continue;
133		count++;
134		if (count > 1)
135			strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
136		strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
137	}
138	va_end(ap);
139	strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
140
141	if (count >= 2) {
142		p_err("%s", msg);
143		return -1;
144	}
145
146	return 0;
147}
148
149void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
150{
151	unsigned char *data = arg;
152	unsigned int i;
153
154	for (i = 0; i < n; i++) {
155		const char *pfx = "";
156
157		if (!i)
158			/* nothing */;
159		else if (!(i % 16))
160			fprintf(f, "\n");
161		else if (!(i % 8))
162			fprintf(f, "  ");
163		else
164			pfx = sep;
165
166		fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
167	}
168}
169
170/* Split command line into argument vector. */
171static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
172{
173	static const char ws[] = " \t\r\n";
174	char *cp = line;
175	int n_argc = 0;
176
177	while (*cp) {
178		/* Skip leading whitespace. */
179		cp += strspn(cp, ws);
180
181		if (*cp == '\0')
182			break;
183
184		if (n_argc >= (maxargs - 1)) {
185			p_err("too many arguments to command %d", cmd_nb);
186			return -1;
187		}
188
189		/* Word begins with quote. */
190		if (*cp == '\'' || *cp == '"') {
191			char quote = *cp++;
192
193			n_argv[n_argc++] = cp;
194			/* Find ending quote. */
195			cp = strchr(cp, quote);
196			if (!cp) {
197				p_err("unterminated quoted string in command %d",
198				      cmd_nb);
199				return -1;
200			}
201		} else {
202			n_argv[n_argc++] = cp;
203
204			/* Find end of word. */
205			cp += strcspn(cp, ws);
206			if (*cp == '\0')
207				break;
208		}
209
210		/* Separate words. */
211		*cp++ = 0;
212	}
213	n_argv[n_argc] = NULL;
214
215	return n_argc;
216}
217
218static int do_batch(int argc, char **argv);
219
220static const struct cmd cmds[] = {
221	{ "help",	do_help },
222	{ "batch",	do_batch },
223	{ "prog",	do_prog },
224	{ "map",	do_map },
225	{ "cgroup",	do_cgroup },
226	{ "perf",	do_perf },
227	{ "net",	do_net },
228	{ "feature",	do_feature },
229	{ "btf",	do_btf },
230	{ "version",	do_version },
231	{ 0 }
232};
233
234static int do_batch(int argc, char **argv)
235{
236	char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
237	char *n_argv[BATCH_ARG_NB_MAX];
238	unsigned int lines = 0;
239	int n_argc;
240	FILE *fp;
241	char *cp;
242	int err;
243	int i;
244
245	if (argc < 2) {
246		p_err("too few parameters for batch");
247		return -1;
 
 
 
248	} else if (!is_prefix(*argv, "file")) {
249		p_err("expected 'file', got: %s", *argv);
250		return -1;
251	} else if (argc > 2) {
252		p_err("too many parameters for batch");
253		return -1;
254	}
255	NEXT_ARG();
256
257	if (!strcmp(*argv, "-"))
258		fp = stdin;
259	else
260		fp = fopen(*argv, "r");
261	if (!fp) {
262		p_err("Can't open file (%s): %s", *argv, strerror(errno));
263		return -1;
264	}
265
266	if (json_output)
267		jsonw_start_array(json_wtr);
268	while (fgets(buf, sizeof(buf), fp)) {
269		cp = strchr(buf, '#');
270		if (cp)
271			*cp = '\0';
272
273		if (strlen(buf) == sizeof(buf) - 1) {
274			errno = E2BIG;
275			break;
276		}
277
278		/* Append continuation lines if any (coming after a line ending
279		 * with '\' in the batch file).
280		 */
281		while ((cp = strstr(buf, "\\\n")) != NULL) {
282			if (!fgets(contline, sizeof(contline), fp) ||
283			    strlen(contline) == 0) {
284				p_err("missing continuation line on command %d",
285				      lines);
286				err = -1;
287				goto err_close;
288			}
289
290			cp = strchr(contline, '#');
291			if (cp)
292				*cp = '\0';
293
294			if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
295				p_err("command %d is too long", lines);
296				err = -1;
297				goto err_close;
298			}
299			buf[strlen(buf) - 2] = '\0';
300			strcat(buf, contline);
301		}
302
303		n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
304		if (!n_argc)
305			continue;
306		if (n_argc < 0)
 
307			goto err_close;
 
308
309		if (json_output) {
310			jsonw_start_object(json_wtr);
311			jsonw_name(json_wtr, "command");
312			jsonw_start_array(json_wtr);
313			for (i = 0; i < n_argc; i++)
314				jsonw_string(json_wtr, n_argv[i]);
315			jsonw_end_array(json_wtr);
316			jsonw_name(json_wtr, "output");
317		}
318
319		err = cmd_select(cmds, n_argc, n_argv, do_help);
320
321		if (json_output)
322			jsonw_end_object(json_wtr);
323
324		if (err)
325			goto err_close;
326
327		lines++;
328	}
329
330	if (errno && errno != ENOENT) {
331		p_err("reading batch file failed: %s", strerror(errno));
332		err = -1;
333	} else {
334		if (!json_output)
335			printf("processed %d commands\n", lines);
336		err = 0;
337	}
338err_close:
339	if (fp != stdin)
340		fclose(fp);
341
342	if (json_output)
343		jsonw_end_array(json_wtr);
344
345	return err;
346}
347
348int main(int argc, char **argv)
349{
350	static const struct option options[] = {
351		{ "json",	no_argument,	NULL,	'j' },
352		{ "help",	no_argument,	NULL,	'h' },
353		{ "pretty",	no_argument,	NULL,	'p' },
354		{ "version",	no_argument,	NULL,	'V' },
355		{ "bpffs",	no_argument,	NULL,	'f' },
356		{ "mapcompat",	no_argument,	NULL,	'm' },
357		{ "nomount",	no_argument,	NULL,	'n' },
358		{ "debug",	no_argument,	NULL,	'd' },
 
 
359		{ 0 }
360	};
 
361	int opt, ret;
362
 
 
 
 
 
 
 
 
 
 
 
 
363	last_do_help = do_help;
364	pretty_output = false;
365	json_output = false;
366	show_pinned = false;
367	block_mount = false;
368	bin_name = argv[0];
369
370	hash_init(prog_table.table);
371	hash_init(map_table.table);
372
373	opterr = 0;
374	while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
375				  options, NULL)) >= 0) {
376		switch (opt) {
377		case 'V':
378			return do_version(argc, argv);
 
379		case 'h':
380			return do_help(argc, argv);
381		case 'p':
382			pretty_output = true;
383			/* fall through */
384		case 'j':
385			if (!json_output) {
386				json_wtr = jsonw_new(stdout);
387				if (!json_wtr) {
388					p_err("failed to create JSON writer");
389					return -1;
390				}
391				json_output = true;
392			}
393			jsonw_pretty(json_wtr, pretty_output);
394			break;
395		case 'f':
396			show_pinned = true;
397			break;
398		case 'm':
399			bpf_flags = MAPS_RELAX_COMPAT;
400			break;
401		case 'n':
402			block_mount = true;
403			break;
404		case 'd':
405			libbpf_set_print(print_all_levels);
406			verifier_logs = true;
407			break;
 
 
 
 
 
 
 
 
 
 
 
408		default:
409			p_err("unrecognized option '%s'", argv[optind - 1]);
410			if (json_output)
411				clean_and_exit(-1);
412			else
413				usage();
414		}
415	}
416
417	argc -= optind;
418	argv += optind;
419	if (argc < 0)
420		usage();
421
422	ret = cmd_select(cmds, argc, argv, do_help);
 
 
 
423
424	if (json_output)
425		jsonw_destroy(&json_wtr);
426
427	if (show_pinned) {
428		delete_pinned_obj_table(&prog_table);
429		delete_pinned_obj_table(&map_table);
430	}
431
432	return ret;
433}