| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* tc_exec.c "tc exec". |
| 4 |
* |
| 5 |
* Authors: Daniel Borkmann <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <dlfcn.h> |
| 11 |
|
| 12 |
#include "utils.h" |
| 13 |
|
| 14 |
#include "tc_util.h" |
| 15 |
#include "tc_common.h" |
| 16 |
|
| 17 |
static struct exec_util *exec_list; |
| 18 |
static void *BODY; |
| 19 |
|
| 20 |
static void usage(void) |
| 21 |
{ |
| 22 |
fprintf(stderr, |
| 23 |
"Usage: tc exec [ EXEC_TYPE ] [ help | OPTIONS ]\n" |
| 24 |
"Where:\n" |
| 25 |
"EXEC_TYPE := { bpf | etc. }\n" |
| 26 |
"OPTIONS := ... try tc exec <desired EXEC_KIND> help\n"); |
| 27 |
} |
| 28 |
|
| 29 |
static int parse_noeopt(const struct exec_util *eu, int argc, char **argv) |
| 30 |
{ |
| 31 |
if (argc) { |
| 32 |
fprintf(stderr, "Unknown exec \"%s\", hence option \"%s\" is unparsable\n", |
| 33 |
eu->id, *argv); |
| 34 |
return -1; |
| 35 |
} |
| 36 |
|
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
static struct exec_util *get_exec_kind(const char *name) |
| 41 |
{ |
| 42 |
struct exec_util *eu; |
| 43 |
char buf[256]; |
| 44 |
void *dlh; |
| 45 |
|
| 46 |
for (eu = exec_list; eu; eu = eu->next) |
| 47 |
if (strcmp(eu->id, name) == 0) |
| 48 |
return eu; |
| 49 |
|
| 50 |
snprintf(buf, sizeof(buf), "%s/e_%s.so", get_tc_lib(), name); |
| 51 |
dlh = dlopen(buf, RTLD_LAZY); |
| 52 |
if (dlh == NULL) { |
| 53 |
dlh = BODY; |
| 54 |
if (dlh == NULL) { |
| 55 |
dlh = BODY = dlopen(NULL, RTLD_LAZY); |
| 56 |
if (dlh == NULL) |
| 57 |
goto noexist; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
snprintf(buf, sizeof(buf), "%s_exec_util", name); |
| 62 |
eu = dlsym(dlh, buf); |
| 63 |
if (eu == NULL) |
| 64 |
goto noexist; |
| 65 |
reg: |
| 66 |
eu->next = exec_list; |
| 67 |
exec_list = eu; |
| 68 |
|
| 69 |
return eu; |
| 70 |
noexist: |
| 71 |
eu = calloc(1, sizeof(*eu)); |
| 72 |
if (eu) { |
| 73 |
strncpy(eu->id, name, sizeof(eu->id) - 1); |
| 74 |
eu->parse_eopt = parse_noeopt; |
| 75 |
goto reg; |
| 76 |
} |
| 77 |
|
| 78 |
return eu; |
| 79 |
} |
| 80 |
|
| 81 |
int do_exec(int argc, char **argv) |
| 82 |
{ |
| 83 |
struct exec_util *eu; |
| 84 |
char kind[FILTER_NAMESZ] = {}; |
| 85 |
|
| 86 |
if (argc < 1) { |
| 87 |
fprintf(stderr, "No command given, try \"tc exec help\".\n"); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
|
| 91 |
if (matches(*argv, "help") == 0) { |
| 92 |
usage(); |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
strncpy(kind, *argv, sizeof(kind) - 1); |
| 97 |
|
| 98 |
eu = get_exec_kind(kind); |
| 99 |
if (eu == NULL) { |
| 100 |
fprintf(stderr, "Allocation failed finding exec\n"); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
|
| 104 |
argc--; |
| 105 |
argv++; |
| 106 |
|
| 107 |
return eu->parse_eopt(eu, argc, argv); |
| 108 |
} |