1 // SPDX-License-Identifier: GPL-2.0-only
3 * System call table mapper
5 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
8 #include "syscalltbl.h"
10 #include <linux/compiler.h>
11 #include <linux/zalloc.h>
13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT
17 #if defined(__x86_64__)
18 #include <asm/syscalls_64.c>
19 const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID;
20 static const char **syscalltbl_native = syscalltbl_x86_64;
21 #elif defined(__s390x__)
22 #include <asm/syscalls_64.c>
23 const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID;
24 static const char **syscalltbl_native = syscalltbl_s390_64;
25 #elif defined(__powerpc64__)
26 #include <asm/syscalls_64.c>
27 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID;
28 static const char **syscalltbl_native = syscalltbl_powerpc_64;
29 #elif defined(__powerpc__)
30 #include <asm/syscalls_32.c>
31 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID;
32 static const char **syscalltbl_native = syscalltbl_powerpc_32;
33 #elif defined(__aarch64__)
34 #include <asm/syscalls.c>
35 const int syscalltbl_native_max_id = SYSCALLTBL_ARM64_MAX_ID;
36 static const char **syscalltbl_native = syscalltbl_arm64;
37 #elif defined(__mips__)
38 #include <asm/syscalls_n64.c>
39 const int syscalltbl_native_max_id = SYSCALLTBL_MIPS_N64_MAX_ID;
40 static const char **syscalltbl_native = syscalltbl_mips_n64;
48 static int syscallcmpname(const void *vkey, const void *ventry)
50 const char *key = vkey;
51 const struct syscall *entry = ventry;
53 return strcmp(key, entry->name);
56 static int syscallcmp(const void *va, const void *vb)
58 const struct syscall *a = va, *b = vb;
60 return strcmp(a->name, b->name);
63 static int syscalltbl__init_native(struct syscalltbl *tbl)
65 int nr_entries = 0, i, j;
66 struct syscall *entries;
68 for (i = 0; i <= syscalltbl_native_max_id; ++i)
69 if (syscalltbl_native[i])
72 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
73 if (tbl->syscalls.entries == NULL)
76 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
77 if (syscalltbl_native[i]) {
78 entries[j].name = syscalltbl_native[i];
84 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
85 tbl->syscalls.nr_entries = nr_entries;
86 tbl->syscalls.max_id = syscalltbl_native_max_id;
90 struct syscalltbl *syscalltbl__new(void)
92 struct syscalltbl *tbl = malloc(sizeof(*tbl));
94 if (syscalltbl__init_native(tbl)) {
102 void syscalltbl__delete(struct syscalltbl *tbl)
104 zfree(&tbl->syscalls.entries);
108 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
110 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
113 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
115 struct syscall *sc = bsearch(name, tbl->syscalls.entries,
116 tbl->syscalls.nr_entries, sizeof(*sc),
119 return sc ? sc->id : -1;
122 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
125 struct syscall *syscalls = tbl->syscalls.entries;
127 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
128 if (strglobmatch(syscalls[i].name, syscall_glob)) {
130 return syscalls[i].id;
137 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
140 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
143 #else /* HAVE_SYSCALL_TABLE_SUPPORT */
145 #include <libaudit.h>
147 struct syscalltbl *syscalltbl__new(void)
149 struct syscalltbl *tbl = zalloc(sizeof(*tbl));
151 tbl->audit_machine = audit_detect_machine();
155 void syscalltbl__delete(struct syscalltbl *tbl)
160 const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
162 return audit_syscall_to_name(id, tbl->audit_machine);
165 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
167 return audit_name_to_syscall(name, tbl->audit_machine);
170 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
171 const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
176 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
178 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
180 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */