2 * System call table mapper
4 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include "syscalltbl.h"
19 #ifdef HAVE_SYSCALL_TABLE
20 #include <linux/compiler.h>
24 #if defined(__x86_64__)
25 #include <asm/syscalls_64.c>
26 const int syscalltbl_native_max_id
= SYSCALLTBL_x86_64_MAX_ID
;
27 static const char **syscalltbl_native
= syscalltbl_x86_64
;
35 static int syscallcmpname(const void *vkey
, const void *ventry
)
37 const char *key
= vkey
;
38 const struct syscall
*entry
= ventry
;
40 return strcmp(key
, entry
->name
);
43 static int syscallcmp(const void *va
, const void *vb
)
45 const struct syscall
*a
= va
, *b
= vb
;
47 return strcmp(a
->name
, b
->name
);
50 static int syscalltbl__init_native(struct syscalltbl
*tbl
)
52 int nr_entries
= 0, i
, j
;
53 struct syscall
*entries
;
55 for (i
= 0; i
<= syscalltbl_native_max_id
; ++i
)
56 if (syscalltbl_native
[i
])
59 entries
= tbl
->syscalls
.entries
= malloc(sizeof(struct syscall
) * nr_entries
);
60 if (tbl
->syscalls
.entries
== NULL
)
63 for (i
= 0, j
= 0; i
<= syscalltbl_native_max_id
; ++i
) {
64 if (syscalltbl_native
[i
]) {
65 entries
[j
].name
= syscalltbl_native
[i
];
71 qsort(tbl
->syscalls
.entries
, nr_entries
, sizeof(struct syscall
), syscallcmp
);
72 tbl
->syscalls
.nr_entries
= nr_entries
;
76 struct syscalltbl
*syscalltbl__new(void)
78 struct syscalltbl
*tbl
= malloc(sizeof(*tbl
));
80 if (syscalltbl__init_native(tbl
)) {
88 void syscalltbl__delete(struct syscalltbl
*tbl
)
90 zfree(&tbl
->syscalls
.entries
);
94 const char *syscalltbl__name(const struct syscalltbl
*tbl __maybe_unused
, int id
)
96 return id
<= syscalltbl_native_max_id
? syscalltbl_native
[id
]: NULL
;
99 int syscalltbl__id(struct syscalltbl
*tbl
, const char *name
)
101 struct syscall
*sc
= bsearch(name
, tbl
->syscalls
.entries
,
102 tbl
->syscalls
.nr_entries
, sizeof(*sc
),
105 return sc
? sc
->id
: -1;
108 #else /* HAVE_SYSCALL_TABLE */
110 #include <libaudit.h>
112 struct syscalltbl
*syscalltbl__new(void)
114 struct syscalltbl
*tbl
= malloc(sizeof(*tbl
));
116 tbl
->audit_machine
= audit_detect_machine();
120 void syscalltbl__delete(struct syscalltbl
*tbl
)
125 const char *syscalltbl__name(const struct syscalltbl
*tbl
, int id
)
127 return audit_syscall_to_name(id
, tbl
->audit_machine
);
130 int syscalltbl__id(struct syscalltbl
*tbl
, const char *name
)
132 return audit_name_to_syscall(name
, tbl
->audit_machine
);
134 #endif /* HAVE_SYSCALL_TABLE */