diff mbox series

[pseudo,1/3] test-syscall: Add a syscall test

Message ID 20260113231729.2698563-1-richard.purdie@linuxfoundation.org
State New
Headers show
Series [pseudo,1/3] test-syscall: Add a syscall test | expand

Commit Message

Richard Purdie Jan. 13, 2026, 11:17 p.m. UTC
From: "mark.hatle" <mark.hatle@kernel.crashing.org>

Verify the syscalls we intercept are being handled properly.  This usually
means ENOSYS.

In the future we may catch and forward them to wrappers, if that happens
the test can be improved.

Signed-off-by: mark.hatle <mark.hatle@kernel.crashing.org>
---
 test/test-syscall.c  | 87 ++++++++++++++++++++++++++++++++++++++++++++
 test/test-syscall.sh |  5 +++
 2 files changed, 92 insertions(+)
 create mode 100644 test/test-syscall.c
 create mode 100755 test/test-syscall.sh
diff mbox series

Patch

diff --git a/test/test-syscall.c b/test/test-syscall.c
new file mode 100644
index 0000000..1e21525
--- /dev/null
+++ b/test/test-syscall.c
@@ -0,0 +1,87 @@ 
+/* This test case must match the implementation in:
+ *   ports/linux/pseudo_wrappers.c
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+
+int main(int argc, char *argv[]) {
+    long rc = 0;
+
+#ifdef SYS_renameat2
+    int retval;
+
+    #ifndef RENAME_NOREPLACE
+      # define RENAME_NOREPLACE (1 << 0)
+    #endif
+
+    /* The two paths don't exist, but we're just checking on error what the errno is. */
+    retval = syscall(SYS_renameat2, AT_FDCWD, "empty_path_1", AT_FDCWD, "empty_path_2", RENAME_NOREPLACE);
+    if (retval != -1) {
+        errno = 0;
+        printf("renameat2: fail: function implemented\n");
+        rc++;
+    } else {
+        if (errno != ENOSYS) {
+            printf("renameat2: fail: function implemented: %s\n", strerror(errno));
+            rc++;
+        }
+        else
+            printf("renameat2: pass\n");
+    }
+
+#endif
+
+#ifdef SYS_seccomp
+    /* We should verify this was intercepted, but at present we're not able to. */
+    errno = 0;
+    printf("seccomp: skip\n");
+#endif
+
+#ifdef SYS_openat2
+    struct open_how
+    {
+    #  ifdef __UINT64_TYPE__
+      __UINT64_TYPE__ flags, mode, resolve;
+    #  else
+      unsigned long long int flags, mode, resolve;
+    #  endif
+    };
+    #  define RESOLVE_NO_XDEV	0x01
+    #  define RESOLVE_NO_MAGICLINKS	0x02
+    #  define RESOLVE_NO_SYMLINKS	0x04
+    #  define RESOLVE_BENEATH	0x08
+    #  define RESOLVE_IN_ROOT	0x10
+    #  define RESOLVE_CACHED	0x20
+
+    struct open_how how;
+    memset(&how, 0, sizeof(how));
+    how.flags = O_DIRECTORY;
+    how.resolve = RESOLVE_IN_ROOT;
+
+    int fd;
+    fd = syscall(SYS_openat2, AT_FDCWD, ".", &how, sizeof(how));
+    if (fd == -1) {
+        if (errno != ENOSYS) {
+            printf("openat2: fail: function implemented: %s\n", strerror(errno));
+            rc++;
+        }
+        else
+            printf("openat2: pass\n");
+    }
+    else {
+        printf("openat2: fail: function implemented\n");
+        rc++;
+    }
+
+    close(fd);
+#endif
+
+    return rc;
+}
diff --git a/test/test-syscall.sh b/test/test-syscall.sh
new file mode 100755
index 0000000..69774db
--- /dev/null
+++ b/test/test-syscall.sh
@@ -0,0 +1,5 @@ 
+#! /bin/sh
+
+mypath=$(dirname $0)
+
+exec ${mypath}/test-syscall