]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/missing_syscalls.py
Merge pull request #18886 from anitazha/shutdownconsole
[thirdparty/systemd.git] / src / basic / missing_syscalls.py
CommitLineData
35b42e56 1#!/usr/bin/env python3
cb1f01a5
ZJS
2# SPDX-License-Identifier: LGPL-2.1-or-later
3
35b42e56 4import sys
35b42e56
ZJS
5import functools
6
7# We only generate numbers for a dozen or so syscalls
8SYSCALLS = [
9 'bpf',
10 'close_range',
11 'copy_file_range',
12 'getrandom',
13 'memfd_create',
14 'name_to_handle_at',
15 'pidfd_open',
16 'pidfd_send_signal',
17 'pkey_mprotect',
18 'renameat2',
19 'setns',
420297c9
LP
20 'statx',
21 'epoll_pwait2']
35b42e56
ZJS
22
23def dictify(f):
24 def wrap(*args, **kwargs):
25 return dict(f(*args, **kwargs))
26 return functools.update_wrapper(wrap, f)
27
28@dictify
29def parse_syscall_table(filename):
30 print(f'Reading {filename}…')
31 for line in open(filename):
32 items = line.split()
33 if len(items) >= 2:
34 yield items[0], int(items[1])
35
36def parse_syscall_tables(filenames):
b85ee926 37 return {filename.split('-')[-1][:-4]: parse_syscall_table(filename)
35b42e56
ZJS
38 for filename in filenames}
39
1d73ffea 40DEF_TEMPLATE = '''
35b42e56
ZJS
41#ifndef __IGNORE_{syscall}
42# if defined(__aarch64__)
43# define systemd_NR_{syscall} {nr_arm64}
44# elif defined(__alpha__)
45# define systemd_NR_{syscall} {nr_alpha}
46# elif defined(__arc__) || defined(__tilegx__)
47# define systemd_NR_{syscall} {nr_arc}
48# elif defined(__arm__)
49# define systemd_NR_{syscall} {nr_arm}
50# elif defined(__i386__)
51# define systemd_NR_{syscall} {nr_i386}
52# elif defined(__ia64__)
53# define systemd_NR_{syscall} {nr_ia64}
54# elif defined(__m68k__)
55# define systemd_NR_{syscall} {nr_m68k}
56# elif defined(_MIPS_SIM)
57# if _MIPS_SIM == _MIPS_SIM_ABI32
58# define systemd_NR_{syscall} {nr_mipso32}
59# elif _MIPS_SIM == _MIPS_SIM_NABI32
60# define systemd_NR_{syscall} {nr_mips64n32}
61# elif _MIPS_SIM == _MIPS_SIM_ABI64
62# define systemd_NR_{syscall} {nr_mips64}
63# else
64# error "Unknown MIPS ABI"
65# endif
66# elif defined(__powerpc__)
67# define systemd_NR_{syscall} {nr_powerpc}
68# elif defined(__s390__)
69# define systemd_NR_{syscall} {nr_s390}
70# elif defined(__sparc__)
71# define systemd_NR_{syscall} {nr_sparc}
72# elif defined(__x86_64__)
73# if defined(__ILP32__)
74# define systemd_NR_{syscall} ({nr_x86_64} | /* __X32_SYSCALL_BIT */ 0x40000000)
75# else
76# define systemd_NR_{syscall} {nr_x86_64}
77# endif
78# else
79# warning "{syscall}() syscall number is unknown for your architecture"
80# endif
81
b85ee926 82/* may be an (invalid) negative number due to libseccomp, see PR 13319 */
35b42e56
ZJS
83# if defined __NR_{syscall} && __NR_{syscall} >= 0
84# if defined systemd_NR_{syscall}
85assert_cc(__NR_{syscall} == systemd_NR_{syscall});
86# endif
87# else
88# if defined __NR_{syscall}
89# undef __NR_{syscall}
90# endif
91# if defined systemd_NR_{syscall} && systemd_NR_{syscall} >= 0
92# define __NR_{syscall} systemd_NR_{syscall}
93# endif
94# endif
95#endif
96'''
97
98def print_syscall_def(syscall, tables, out):
99 mappings = {f'nr_{arch}':t.get(syscall, -1)
100 for arch, t in tables.items()}
101 print(DEF_TEMPLATE.format(syscall=syscall, **mappings),
1d73ffea 102 file=out, end='')
35b42e56
ZJS
103
104def print_syscall_defs(syscalls, tables, out):
cb1f01a5
ZJS
105 print('''\
106/* SPDX-License-Identifier: LGPL-2.1-or-later
107 * This file is generated. Do not edit! */
1d73ffea 108''' , file=out, end='')
35b42e56
ZJS
109 for syscall in syscalls:
110 print_syscall_def(syscall, tables, out)
111
112if __name__ == '__main__':
113 output_file = sys.argv[1]
114 arch_files = sys.argv[2:]
115 out = open(output_file, 'wt')
116
117 tables = parse_syscall_tables(arch_files)
118 print_syscall_defs(SYSCALLS, tables, out)
119
120 print(f'Wrote {output_file}')