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>
16 #include <syscall_table.h>
17 const int syscalltbl_native_max_id
= SYSCALLTBL_MAX_ID
;
18 static const char *const *syscalltbl_native
= syscalltbl
;
25 static int syscallcmpname(const void *vkey
, const void *ventry
)
27 const char *key
= vkey
;
28 const struct syscall
*entry
= ventry
;
30 return strcmp(key
, entry
->name
);
33 static int syscallcmp(const void *va
, const void *vb
)
35 const struct syscall
*a
= va
, *b
= vb
;
37 return strcmp(a
->name
, b
->name
);
40 static int syscalltbl__init_native(struct syscalltbl
*tbl
)
42 int nr_entries
= 0, i
, j
;
43 struct syscall
*entries
;
45 for (i
= 0; i
<= syscalltbl_native_max_id
; ++i
)
46 if (syscalltbl_native
[i
])
49 entries
= tbl
->syscalls
.entries
= malloc(sizeof(struct syscall
) * nr_entries
);
50 if (tbl
->syscalls
.entries
== NULL
)
53 for (i
= 0, j
= 0; i
<= syscalltbl_native_max_id
; ++i
) {
54 if (syscalltbl_native
[i
]) {
55 entries
[j
].name
= syscalltbl_native
[i
];
61 qsort(tbl
->syscalls
.entries
, nr_entries
, sizeof(struct syscall
), syscallcmp
);
62 tbl
->syscalls
.nr_entries
= nr_entries
;
63 tbl
->syscalls
.max_id
= syscalltbl_native_max_id
;
67 struct syscalltbl
*syscalltbl__new(void)
69 struct syscalltbl
*tbl
= malloc(sizeof(*tbl
));
71 if (syscalltbl__init_native(tbl
)) {
79 void syscalltbl__delete(struct syscalltbl
*tbl
)
81 zfree(&tbl
->syscalls
.entries
);
85 const char *syscalltbl__name(const struct syscalltbl
*tbl __maybe_unused
, int id
)
87 return id
<= syscalltbl_native_max_id
? syscalltbl_native
[id
]: NULL
;
90 int syscalltbl__id(struct syscalltbl
*tbl
, const char *name
)
92 struct syscall
*sc
= bsearch(name
, tbl
->syscalls
.entries
,
93 tbl
->syscalls
.nr_entries
, sizeof(*sc
),
96 return sc
? sc
->id
: -1;
99 int syscalltbl__id_at_idx(struct syscalltbl
*tbl
, int idx
)
101 struct syscall
*syscalls
= tbl
->syscalls
.entries
;
103 return idx
< tbl
->syscalls
.nr_entries
? syscalls
[idx
].id
: -1;
106 int syscalltbl__strglobmatch_next(struct syscalltbl
*tbl
, const char *syscall_glob
, int *idx
)
109 struct syscall
*syscalls
= tbl
->syscalls
.entries
;
111 for (i
= *idx
+ 1; i
< tbl
->syscalls
.nr_entries
; ++i
) {
112 if (strglobmatch(syscalls
[i
].name
, syscall_glob
)) {
114 return syscalls
[i
].id
;
121 int syscalltbl__strglobmatch_first(struct syscalltbl
*tbl
, const char *syscall_glob
, int *idx
)
124 return syscalltbl__strglobmatch_next(tbl
, syscall_glob
, idx
);