1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * System call table mapper 4 * 5 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com> 6 */ 7 8 #include "syscalltbl.h" 9 #include <stdlib.h> 10 #include <asm/bitsperlong.h> 11 #include <linux/compiler.h> 12 #include <linux/zalloc.h> 13 14 #include <string.h> 15 #include "string2.h" 16 17 #if __BITS_PER_LONG == 64 18 #include <asm/syscalls_64.h> 19 #else 20 #include <asm/syscalls_32.h> 21 #endif 22 23 const int syscalltbl_native_max_id = SYSCALLTBL_MAX_ID; 24 static const char *const *syscalltbl_native = syscalltbl; 25 26 struct syscall { 27 int id; 28 const char *name; 29 }; 30 31 static int syscallcmpname(const void *vkey, const void *ventry) 32 { 33 const char *key = vkey; 34 const struct syscall *entry = ventry; 35 36 return strcmp(key, entry->name); 37 } 38 39 static int syscallcmp(const void *va, const void *vb) 40 { 41 const struct syscall *a = va, *b = vb; 42 43 return strcmp(a->name, b->name); 44 } 45 46 static int syscalltbl__init_native(struct syscalltbl *tbl) 47 { 48 int nr_entries = 0, i, j; 49 struct syscall *entries; 50 51 for (i = 0; i <= syscalltbl_native_max_id; ++i) 52 if (syscalltbl_native[i]) 53 ++nr_entries; 54 55 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries); 56 if (tbl->syscalls.entries == NULL) 57 return -1; 58 59 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) { 60 if (syscalltbl_native[i]) { 61 entries[j].name = syscalltbl_native[i]; 62 entries[j].id = i; 63 ++j; 64 } 65 } 66 67 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp); 68 tbl->syscalls.nr_entries = nr_entries; 69 tbl->syscalls.max_id = syscalltbl_native_max_id; 70 return 0; 71 } 72 73 struct syscalltbl *syscalltbl__new(void) 74 { 75 struct syscalltbl *tbl = malloc(sizeof(*tbl)); 76 if (tbl) { 77 if (syscalltbl__init_native(tbl)) { 78 free(tbl); 79 return NULL; 80 } 81 } 82 return tbl; 83 } 84 85 void syscalltbl__delete(struct syscalltbl *tbl) 86 { 87 zfree(&tbl->syscalls.entries); 88 free(tbl); 89 } 90 91 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id) 92 { 93 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL; 94 } 95 96 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 97 { 98 struct syscall *sc = bsearch(name, tbl->syscalls.entries, 99 tbl->syscalls.nr_entries, sizeof(*sc), 100 syscallcmpname); 101 102 return sc ? sc->id : -1; 103 } 104 105 int syscalltbl__id_at_idx(struct syscalltbl *tbl, int idx) 106 { 107 struct syscall *syscalls = tbl->syscalls.entries; 108 109 return idx < tbl->syscalls.nr_entries ? syscalls[idx].id : -1; 110 } 111 112 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 113 { 114 int i; 115 struct syscall *syscalls = tbl->syscalls.entries; 116 117 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) { 118 if (strglobmatch(syscalls[i].name, syscall_glob)) { 119 *idx = i; 120 return syscalls[i].id; 121 } 122 } 123 124 return -1; 125 } 126 127 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 128 { 129 *idx = -1; 130 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 131 } 132