Skip to content

Commit

Permalink
tree-wide: introduce and use syscall number header
Browse files Browse the repository at this point in the history
This allows us:
- to compile on kernels with outdated headers
- compile on older kernels but shipping on newer kernels

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Mar 18, 2020
1 parent ee555b2 commit bed09c9
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 205 deletions.
6 changes: 6 additions & 0 deletions src/lxc/Makefile.am
Expand Up @@ -43,6 +43,7 @@ noinst_HEADERS = api_extensions.h \
storage/storage_utils.h \
storage/zfs.h \
string_utils.h \
syscall_numbers.h \
syscall_wrappers.h \
terminal.h \
../tests/lxctest.h \
Expand Down Expand Up @@ -145,6 +146,7 @@ liblxc_la_SOURCES = af_unix.c af_unix.h \
storage/zfs.c storage/zfs.h \
string_utils.c string_utils.h \
sync.c sync.h \
syscall_numbers.h \
syscall_wrappers.h \
terminal.c \
utils.c utils.h \
Expand Down Expand Up @@ -360,6 +362,7 @@ lxc_top_SOURCES = tools/lxc_top.c \
lxc_unfreeze_SOURCES = tools/lxc_unfreeze.c \
tools/arguments.c tools/arguments.h
lxc_unshare_SOURCES = tools/lxc_unshare.c \
syscall_numbers.h \
tools/arguments.c tools/arguments.h
lxc_wait_SOURCES = tools/lxc_wait.c \
tools/arguments.c tools/arguments.h
Expand All @@ -381,6 +384,7 @@ init_lxc_SOURCES = cmd/lxc_init.c \
memory_utils.h \
parse.c parse.h \
raw_syscalls.c raw_syscalls.h \
syscall_numbers.h \
string_utils.c string_utils.h

init_lxc_LDFLAGS = -pthread
Expand All @@ -391,6 +395,7 @@ lxc_monitord_SOURCES = cmd/lxc_monitord.c \
mainloop.c mainloop.h \
monitor.c monitor.h \
raw_syscalls.c raw_syscalls.h \
syscall_numbers.h \
utils.c utils.h
lxc_user_nic_SOURCES = cmd/lxc_user_nic.c \
../include/netns_ifaddrs.c ../include/netns_ifaddrs.h \
Expand All @@ -399,6 +404,7 @@ lxc_user_nic_SOURCES = cmd/lxc_user_nic.c \
network.c network.h \
parse.c parse.h \
raw_syscalls.c raw_syscalls.h \
syscall_numbers.h \
file_utils.c file_utils.h \
string_utils.c string_utils.h \
syscall_wrappers.h
Expand Down
40 changes: 7 additions & 33 deletions src/lxc/cgroups/cgroup2_devices.h
Expand Up @@ -17,50 +17,24 @@

#include "conf.h"
#include "config.h"
#include "syscall_numbers.h"

#ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
#include <linux/bpf.h>
#include <linux/filter.h>
#endif

#if !HAVE_BPF
#if !(defined __NR_bpf && __NR_bpf > 0)
#if defined __NR_bpf
#undef __NR_bpf
#endif
#if defined __i386__
#define __NR_bpf 357
#elif defined __x86_64__
#define __NR_bpf 321
#elif defined __aarch64__
#define __NR_bpf 280
#elif defined __arm__
#define __NR_bpf 386
#elif defined __sparc__
#define __NR_bpf 349
#elif defined __s390__
#define __NR_bpf 351
#elif defined __tilegx__
#define __NR_bpf 280
#else
#warning "__NR_bpf not defined for your architecture"
#endif
#endif
#ifndef HAVE_BPF

union bpf_attr;

static inline int missing_bpf(int cmd, union bpf_attr *attr, size_t size)
{
#ifdef __NR_bpf
return (int)syscall(__NR_bpf, cmd, attr, size);
#else
errno = ENOSYS;
return -1;
#endif
return syscall(__NR_bpf, cmd, attr, size);
}

#define bpf missing_bpf
#endif
#endif /* HAVE_BPF */

struct bpf_program {
int device_list_type;
Expand All @@ -70,7 +44,7 @@ struct bpf_program {
size_t n_instructions;
#ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
struct bpf_insn *instructions;
#endif
#endif /* HAVE_STRUCT_BPF_CGROUP_DEV_CTX */

char *attached_path;
int attached_type;
Expand All @@ -97,7 +71,7 @@ static inline void __auto_bpf_program_free__(struct bpf_program **prog)
}
}
int bpf_list_add_device(struct lxc_conf *conf, struct device_item *device);
#else
#else /* !HAVE_STRUCT_BPF_CGROUP_DEV_CTX */
static inline struct bpf_program *bpf_program_new(uint32_t prog_type)
{
errno = ENOSYS;
Expand Down Expand Up @@ -160,7 +134,7 @@ static inline int bpf_list_add_device(struct lxc_conf *conf,
errno = ENOSYS;
return -1;
}
#endif
#endif /* !HAVE_STRUCT_BPF_CGROUP_DEV_CTX */

#define __do_bpf_program_free \
__attribute__((__cleanup__(__auto_bpf_program_free__)))
Expand Down
13 changes: 0 additions & 13 deletions src/lxc/lxccontainer.c
Expand Up @@ -81,19 +81,6 @@
#include "include/strlcpy.h"
#endif

/* Define faccessat() if missing from the C library */
#ifndef HAVE_FACCESSAT
static int faccessat(int __fd, const char *__file, int __type, int __flag)
{
#ifdef __NR_faccessat
return syscall(__NR_faccessat, __fd, __file, __type, __flag);
#else
errno = ENOSYS;
return -1;
#endif
}
#endif

lxc_log_define(lxccontainer, lxc);

static bool do_lxcapi_destroy(struct lxc_container *c);
Expand Down
13 changes: 2 additions & 11 deletions src/lxc/raw_syscalls.c
Expand Up @@ -15,16 +15,12 @@
#include "config.h"
#include "macro.h"
#include "raw_syscalls.h"
#include "syscall_numbers.h"

int lxc_raw_execveat(int dirfd, const char *pathname, char *const argv[],
char *const envp[], int flags)
{
#ifdef __NR_execveat
syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
#else
errno = ENOSYS;
#endif
return -1;
return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
}

/*
Expand Down Expand Up @@ -123,11 +119,6 @@ pid_t lxc_raw_clone_cb(int (*fn)(void *), void *args, unsigned long flags,
return pid;
}

/* For all the architectures we care about it's the same syscall number. */
#ifndef __NR_pidfd_send_signal
#define __NR_pidfd_send_signal 424
#endif

int lxc_raw_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
unsigned int flags)
{
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/raw_syscalls.h
Expand Up @@ -81,7 +81,7 @@ static inline pid_t lxc_raw_getpid(void)

static inline pid_t lxc_raw_gettid(void)
{
#ifdef __NR_gettid
#if __NR_gettid > 0
return syscall(__NR_gettid);
#else
return lxc_raw_getpid();
Expand Down
5 changes: 0 additions & 5 deletions src/lxc/seccomp.c
Expand Up @@ -38,12 +38,7 @@ lxc_log_define(seccomp, lxc);
static inline int __seccomp(unsigned int operation, unsigned int flags,
void *args)
{
#ifdef __NR_seccomp
return syscall(__NR_seccomp, operation, flags, args);
#else
errno = ENOSYS;
return -1;
#endif
}
#endif

Expand Down

0 comments on commit bed09c9

Please sign in to comment.