1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * System call table mapper 4 * 5 * (C) 2016 Arnaldo Carvalho de Melo <acme@red 6 */ 7 8 #include "syscalltbl.h" 9 #include <stdlib.h> 10 #include <linux/compiler.h> 11 #include <linux/zalloc.h> 12 13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT 14 #include <string.h> 15 #include "string2.h" 16 17 #if defined(__x86_64__) 18 #include <asm/syscalls_64.c> 19 const int syscalltbl_native_max_id = SYSCALLTB 20 static const char *const *syscalltbl_native = 21 #elif defined(__i386__) 22 #include <asm/syscalls_32.c> 23 const int syscalltbl_native_max_id = SYSCALLTB 24 static const char *const *syscalltbl_native = 25 #elif defined(__s390x__) 26 #include <asm/syscalls_64.c> 27 const int syscalltbl_native_max_id = SYSCALLTB 28 static const char *const *syscalltbl_native = 29 #elif defined(__powerpc64__) 30 #include <asm/syscalls_64.c> 31 const int syscalltbl_native_max_id = SYSCALLTB 32 static const char *const *syscalltbl_native = 33 #elif defined(__powerpc__) 34 #include <asm/syscalls_32.c> 35 const int syscalltbl_native_max_id = SYSCALLTB 36 static const char *const *syscalltbl_native = 37 #elif defined(__aarch64__) 38 #include <asm/syscalls.c> 39 const int syscalltbl_native_max_id = SYSCALLTB 40 static const char *const *syscalltbl_native = 41 #elif defined(__mips__) 42 #include <asm/syscalls_n64.c> 43 const int syscalltbl_native_max_id = SYSCALLTB 44 static const char *const *syscalltbl_native = 45 #elif defined(__loongarch__) 46 #include <asm/syscalls.c> 47 const int syscalltbl_native_max_id = SYSCALLTB 48 static const char *const *syscalltbl_native = 49 #else 50 const int syscalltbl_native_max_id = 0; 51 static const char *const syscalltbl_native[] = 52 [0] = "unknown", 53 }; 54 #endif 55 56 struct syscall { 57 int id; 58 const char *name; 59 }; 60 61 static int syscallcmpname(const void *vkey, co 62 { 63 const char *key = vkey; 64 const struct syscall *entry = ventry; 65 66 return strcmp(key, entry->name); 67 } 68 69 static int syscallcmp(const void *va, const vo 70 { 71 const struct syscall *a = va, *b = vb; 72 73 return strcmp(a->name, b->name); 74 } 75 76 static int syscalltbl__init_native(struct sysc 77 { 78 int nr_entries = 0, i, j; 79 struct syscall *entries; 80 81 for (i = 0; i <= syscalltbl_native_max 82 if (syscalltbl_native[i]) 83 ++nr_entries; 84 85 entries = tbl->syscalls.entries = mall 86 if (tbl->syscalls.entries == NULL) 87 return -1; 88 89 for (i = 0, j = 0; i <= syscalltbl_nat 90 if (syscalltbl_native[i]) { 91 entries[j].name = sysc 92 entries[j].id = i; 93 ++j; 94 } 95 } 96 97 qsort(tbl->syscalls.entries, nr_entrie 98 tbl->syscalls.nr_entries = nr_entries; 99 tbl->syscalls.max_id = syscalltbl_ 100 return 0; 101 } 102 103 struct syscalltbl *syscalltbl__new(void) 104 { 105 struct syscalltbl *tbl = malloc(sizeof 106 if (tbl) { 107 if (syscalltbl__init_native(tb 108 free(tbl); 109 return NULL; 110 } 111 } 112 return tbl; 113 } 114 115 void syscalltbl__delete(struct syscalltbl *tbl 116 { 117 zfree(&tbl->syscalls.entries); 118 free(tbl); 119 } 120 121 const char *syscalltbl__name(const struct sysc 122 { 123 return id <= syscalltbl_native_max_id 124 } 125 126 int syscalltbl__id(struct syscalltbl *tbl, con 127 { 128 struct syscall *sc = bsearch(name, tbl 129 tbl->sysc 130 syscallcm 131 132 return sc ? sc->id : -1; 133 } 134 135 int syscalltbl__id_at_idx(struct syscalltbl *t 136 { 137 struct syscall *syscalls = tbl->syscal 138 139 return idx < tbl->syscalls.nr_entries 140 } 141 142 int syscalltbl__strglobmatch_next(struct sysca 143 { 144 int i; 145 struct syscall *syscalls = tbl->syscal 146 147 for (i = *idx + 1; i < tbl->syscalls.n 148 if (strglobmatch(syscalls[i].n 149 *idx = i; 150 return syscalls[i].id; 151 } 152 } 153 154 return -1; 155 } 156 157 int syscalltbl__strglobmatch_first(struct sysc 158 { 159 *idx = -1; 160 return syscalltbl__strglobmatch_next(t 161 } 162 163 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 164 165 #include <libaudit.h> 166 167 struct syscalltbl *syscalltbl__new(void) 168 { 169 struct syscalltbl *tbl = zalloc(sizeof 170 if (tbl) 171 tbl->audit_machine = audit_det 172 return tbl; 173 } 174 175 void syscalltbl__delete(struct syscalltbl *tbl 176 { 177 free(tbl); 178 } 179 180 const char *syscalltbl__name(const struct sysc 181 { 182 return audit_syscall_to_name(id, tbl-> 183 } 184 185 int syscalltbl__id(struct syscalltbl *tbl, con 186 { 187 return audit_name_to_syscall(name, tbl 188 } 189 190 int syscalltbl__id_at_idx(struct syscalltbl *t 191 { 192 return idx; 193 } 194 195 int syscalltbl__strglobmatch_next(struct sysca 196 const char * 197 { 198 return -1; 199 } 200 201 int syscalltbl__strglobmatch_first(struct sysc 202 { 203 return syscalltbl__strglobmatch_next(t 204 } 205 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 206
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.