~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/bpf/bpftool/main.c

Version: ~ [ linux-4.19-rc4 ] ~ [ linux-4.18.8 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.70 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.127 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.156 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.122 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.57 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.31.14 ] ~ [ linux-2.6.30.10 ] ~ [ linux-2.6.29.6 ] ~ [ linux-2.6.28.10 ] ~ [ linux-2.6.27.62 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2017-2018 Netronome Systems, Inc.
  3  *
  4  * This software is dual licensed under the GNU General License Version 2,
  5  * June 1991 as shown in the file COPYING in the top-level directory of this
  6  * source tree or the BSD 2-Clause License provided below.  You have the
  7  * option to license this software under the complete terms of either license.
  8  *
  9  * The BSD 2-Clause License:
 10  *
 11  *     Redistribution and use in source and binary forms, with or
 12  *     without modification, are permitted provided that the following
 13  *     conditions are met:
 14  *
 15  *      1. Redistributions of source code must retain the above
 16  *         copyright notice, this list of conditions and the following
 17  *         disclaimer.
 18  *
 19  *      2. Redistributions in binary form must reproduce the above
 20  *         copyright notice, this list of conditions and the following
 21  *         disclaimer in the documentation and/or other materials
 22  *         provided with the distribution.
 23  *
 24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 31  * SOFTWARE.
 32  */
 33 
 34 #include <bfd.h>
 35 #include <ctype.h>
 36 #include <errno.h>
 37 #include <getopt.h>
 38 #include <linux/bpf.h>
 39 #include <stdio.h>
 40 #include <stdlib.h>
 41 #include <string.h>
 42 
 43 #include <bpf.h>
 44 
 45 #include "main.h"
 46 
 47 #define BATCH_LINE_LEN_MAX 65536
 48 #define BATCH_ARG_NB_MAX 4096
 49 
 50 const char *bin_name;
 51 static int last_argc;
 52 static char **last_argv;
 53 static int (*last_do_help)(int argc, char **argv);
 54 json_writer_t *json_wtr;
 55 bool pretty_output;
 56 bool json_output;
 57 bool show_pinned;
 58 struct pinned_obj_table prog_table;
 59 struct pinned_obj_table map_table;
 60 
 61 static void __noreturn clean_and_exit(int i)
 62 {
 63         if (json_output)
 64                 jsonw_destroy(&json_wtr);
 65 
 66         exit(i);
 67 }
 68 
 69 void usage(void)
 70 {
 71         last_do_help(last_argc - 1, last_argv + 1);
 72 
 73         clean_and_exit(-1);
 74 }
 75 
 76 static int do_help(int argc, char **argv)
 77 {
 78         if (json_output) {
 79                 jsonw_null(json_wtr);
 80                 return 0;
 81         }
 82 
 83         fprintf(stderr,
 84                 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
 85                 "       %s batch file FILE\n"
 86                 "       %s version\n"
 87                 "\n"
 88                 "       OBJECT := { prog | map | cgroup | perf }\n"
 89                 "       " HELP_SPEC_OPTIONS "\n"
 90                 "",
 91                 bin_name, bin_name, bin_name);
 92 
 93         return 0;
 94 }
 95 
 96 static int do_version(int argc, char **argv)
 97 {
 98         if (json_output) {
 99                 jsonw_start_object(json_wtr);
100                 jsonw_name(json_wtr, "version");
101                 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
102                 jsonw_end_object(json_wtr);
103         } else {
104                 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
105         }
106         return 0;
107 }
108 
109 int cmd_select(const struct cmd *cmds, int argc, char **argv,
110                int (*help)(int argc, char **argv))
111 {
112         unsigned int i;
113 
114         last_argc = argc;
115         last_argv = argv;
116         last_do_help = help;
117 
118         if (argc < 1 && cmds[0].func)
119                 return cmds[0].func(argc, argv);
120 
121         for (i = 0; cmds[i].func; i++)
122                 if (is_prefix(*argv, cmds[i].cmd))
123                         return cmds[i].func(argc - 1, argv + 1);
124 
125         help(argc - 1, argv + 1);
126 
127         return -1;
128 }
129 
130 bool is_prefix(const char *pfx, const char *str)
131 {
132         if (!pfx)
133                 return false;
134         if (strlen(str) < strlen(pfx))
135                 return false;
136 
137         return !memcmp(str, pfx, strlen(pfx));
138 }
139 
140 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
141 {
142         unsigned char *data = arg;
143         unsigned int i;
144 
145         for (i = 0; i < n; i++) {
146                 const char *pfx = "";
147 
148                 if (!i)
149                         /* nothing */;
150                 else if (!(i % 16))
151                         fprintf(f, "\n");
152                 else if (!(i % 8))
153                         fprintf(f, "  ");
154                 else
155                         pfx = sep;
156 
157                 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
158         }
159 }
160 
161 /* Split command line into argument vector. */
162 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
163 {
164         static const char ws[] = " \t\r\n";
165         char *cp = line;
166         int n_argc = 0;
167 
168         while (*cp) {
169                 /* Skip leading whitespace. */
170                 cp += strspn(cp, ws);
171 
172                 if (*cp == '\0')
173                         break;
174 
175                 if (n_argc >= (maxargs - 1)) {
176                         p_err("too many arguments to command %d", cmd_nb);
177                         return -1;
178                 }
179 
180                 /* Word begins with quote. */
181                 if (*cp == '\'' || *cp == '"') {
182                         char quote = *cp++;
183 
184                         n_argv[n_argc++] = cp;
185                         /* Find ending quote. */
186                         cp = strchr(cp, quote);
187                         if (!cp) {
188                                 p_err("unterminated quoted string in command %d",
189                                       cmd_nb);
190                                 return -1;
191                         }
192                 } else {
193                         n_argv[n_argc++] = cp;
194 
195                         /* Find end of word. */
196                         cp += strcspn(cp, ws);
197                         if (*cp == '\0')
198                                 break;
199                 }
200 
201                 /* Separate words. */
202                 *cp++ = 0;
203         }
204         n_argv[n_argc] = NULL;
205 
206         return n_argc;
207 }
208 
209 static int do_batch(int argc, char **argv);
210 
211 static const struct cmd cmds[] = {
212         { "help",       do_help },
213         { "batch",      do_batch },
214         { "prog",       do_prog },
215         { "map",        do_map },
216         { "cgroup",     do_cgroup },
217         { "perf",       do_perf },
218         { "version",    do_version },
219         { 0 }
220 };
221 
222 static int do_batch(int argc, char **argv)
223 {
224         char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
225         char *n_argv[BATCH_ARG_NB_MAX];
226         unsigned int lines = 0;
227         int n_argc;
228         FILE *fp;
229         char *cp;
230         int err;
231         int i;
232 
233         if (argc < 2) {
234                 p_err("too few parameters for batch");
235                 return -1;
236         } else if (!is_prefix(*argv, "file")) {
237                 p_err("expected 'file', got: %s", *argv);
238                 return -1;
239         } else if (argc > 2) {
240                 p_err("too many parameters for batch");
241                 return -1;
242         }
243         NEXT_ARG();
244 
245         if (!strcmp(*argv, "-"))
246                 fp = stdin;
247         else
248                 fp = fopen(*argv, "r");
249         if (!fp) {
250                 p_err("Can't open file (%s): %s", *argv, strerror(errno));
251                 return -1;
252         }
253 
254         if (json_output)
255                 jsonw_start_array(json_wtr);
256         while (fgets(buf, sizeof(buf), fp)) {
257                 cp = strchr(buf, '#');
258                 if (cp)
259                         *cp = '\0';
260 
261                 if (strlen(buf) == sizeof(buf) - 1) {
262                         errno = E2BIG;
263                         break;
264                 }
265 
266                 /* Append continuation lines if any (coming after a line ending
267                  * with '\' in the batch file).
268                  */
269                 while ((cp = strstr(buf, "\\\n")) != NULL) {
270                         if (!fgets(contline, sizeof(contline), fp) ||
271                             strlen(contline) == 0) {
272                                 p_err("missing continuation line on command %d",
273                                       lines);
274                                 err = -1;
275                                 goto err_close;
276                         }
277 
278                         cp = strchr(contline, '#');
279                         if (cp)
280                                 *cp = '\0';
281 
282                         if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
283                                 p_err("command %d is too long", lines);
284                                 err = -1;
285                                 goto err_close;
286                         }
287                         buf[strlen(buf) - 2] = '\0';
288                         strcat(buf, contline);
289                 }
290 
291                 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
292                 if (!n_argc)
293                         continue;
294                 if (n_argc < 0)
295                         goto err_close;
296 
297                 if (json_output) {
298                         jsonw_start_object(json_wtr);
299                         jsonw_name(json_wtr, "command");
300                         jsonw_start_array(json_wtr);
301                         for (i = 0; i < n_argc; i++)
302                                 jsonw_string(json_wtr, n_argv[i]);
303                         jsonw_end_array(json_wtr);
304                         jsonw_name(json_wtr, "output");
305                 }
306 
307                 err = cmd_select(cmds, n_argc, n_argv, do_help);
308 
309                 if (json_output)
310                         jsonw_end_object(json_wtr);
311 
312                 if (err)
313                         goto err_close;
314 
315                 lines++;
316         }
317 
318         if (errno && errno != ENOENT) {
319                 p_err("reading batch file failed: %s", strerror(errno));
320                 err = -1;
321         } else {
322                 p_info("processed %d commands", lines);
323                 err = 0;
324         }
325 err_close:
326         if (fp != stdin)
327                 fclose(fp);
328 
329         if (json_output)
330                 jsonw_end_array(json_wtr);
331 
332         return err;
333 }
334 
335 int main(int argc, char **argv)
336 {
337         static const struct option options[] = {
338                 { "json",       no_argument,    NULL,   'j' },
339                 { "help",       no_argument,    NULL,   'h' },
340                 { "pretty",     no_argument,    NULL,   'p' },
341                 { "version",    no_argument,    NULL,   'V' },
342                 { "bpffs",      no_argument,    NULL,   'f' },
343                 { 0 }
344         };
345         int opt, ret;
346 
347         last_do_help = do_help;
348         pretty_output = false;
349         json_output = false;
350         show_pinned = false;
351         bin_name = argv[0];
352 
353         hash_init(prog_table.table);
354         hash_init(map_table.table);
355 
356         opterr = 0;
357         while ((opt = getopt_long(argc, argv, "Vhpjf",
358                                   options, NULL)) >= 0) {
359                 switch (opt) {
360                 case 'V':
361                         return do_version(argc, argv);
362                 case 'h':
363                         return do_help(argc, argv);
364                 case 'p':
365                         pretty_output = true;
366                         /* fall through */
367                 case 'j':
368                         if (!json_output) {
369                                 json_wtr = jsonw_new(stdout);
370                                 if (!json_wtr) {
371                                         p_err("failed to create JSON writer");
372                                         return -1;
373                                 }
374                                 json_output = true;
375                         }
376                         jsonw_pretty(json_wtr, pretty_output);
377                         break;
378                 case 'f':
379                         show_pinned = true;
380                         break;
381                 default:
382                         p_err("unrecognized option '%s'", argv[optind - 1]);
383                         if (json_output)
384                                 clean_and_exit(-1);
385                         else
386                                 usage();
387                 }
388         }
389 
390         argc -= optind;
391         argv += optind;
392         if (argc < 0)
393                 usage();
394 
395         bfd_init();
396 
397         ret = cmd_select(cmds, argc, argv, do_help);
398 
399         if (json_output)
400                 jsonw_destroy(&json_wtr);
401 
402         if (show_pinned) {
403                 delete_pinned_obj_table(&prog_table);
404                 delete_pinned_obj_table(&map_table);
405         }
406 
407         return ret;
408 }
409 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp