aboutgitcodelistschat:MatrixIRC
path: root/scripts/nr_syscalls.sh
blob: 380d6f15ddb9cc2115a9df2df84bbc0115fb10c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/sh -eu
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# SEITAN - Syscall Expressive Interpreter, Transformer and Notifier
#
# filter.sh - Build binary-search tree BPF program with SECCOMP_RET_USER_NOTIF
#
# Copyright (c) 2022 Red Hat GmbH
# Author: Stefano Brivio <sbrivio@redhat.com>

IN="${@}"
OUT_NUMBERS="common/numbers.h"

HEADER_NUMBERS="/* This file was automatically generated by $(basename ${0}) */
#ifndef NUMBERS_H_
#define NUMBERS_H_

struct syscall_numbers {
        char name[1024];
        long number;
};

struct syscall_numbers numbers[] = {"

FOOTER_NUMBERS="};

#endif"

syscalls=(
	"accept"
	"bind"
	"connect"
	"getpeername"
	"getsockname"
	"getsockopt"
	"listen"
	"mount"
	"openat"
	"recvfrom"
	"recvmmsg"
	"recvmsg"
	"sendmmsg"
	"sendmsg"
	"sendto"
	"setsockopt"
	"shutdown"
	"socket"
	"socketpair"
)

printf '%s\n' "${HEADER_NUMBERS}" > "${OUT_NUMBERS}"
# syscall_nr - Get syscall number from compiler, also note in numbers.h
__in="$(printf "#include <asm-generic/unistd.h>\n#include <sys/syscall.h>\n__NR_%s" ${syscalls[@]})"
__out="$(echo "${__in}" |cc -E -xc - -o - |sed -n '/\#.*$/!p'| sed '/^$/d')"

awk  -v AS="${syscalls[*]}" -v BS="${__out[*]}" \
'BEGIN { MAX=split(AS,a, / /); split(BS,b, / /);
for (x = 1; x <= MAX; x++)
         { printf "\t{\"%s\",\t%d},\n", a[x], b[x] }
 }' >> "${OUT_NUMBERS}"

printf '%s\n' "${FOOTER_NUMBERS}" >> "${OUT_NUMBERS}"