]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: add a simple test that checks what fexecve_or_execve does
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Jul 2022 09:04:01 +0000 (11:04 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 22 Jul 2022 22:13:30 +0000 (07:13 +0900)
This can be easily used to show that the issue described in
ceedbf8185fc7593366679f02d31da63af8c4bd1 is still reproduced:

$ strace -e execveat build/test-execve /bin/grep Name /proc/self/status
execveat(3, "", ["/bin/grep", "Name", "/proc/self/status"], NULL, AT_EMPTY_PATH) = 0
Name: 3

src/test/meson.build
src/test/test-execve.c [new file with mode: 0644]

index cab9c9aea2848fa08a08c76e168deb59b425810f..31ac149b966f742a057b88377cc05558177f6f0c 100644 (file)
@@ -232,6 +232,8 @@ tests += [
 
         [files('test-exec-util.c')],
 
+        [files('test-execve.c')],
+
         [files('test-hexdecoct.c')],
 
         [files('test-alloc-util.c')],
diff --git a/src/test/test-execve.c b/src/test/test-execve.c
new file mode 100644 (file)
index 0000000..38adc72
--- /dev/null
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "exec-util.h"
+#include "fd-util.h"
+#include "log.h"
+#include "main-func.h"
+#include "strv.h"
+#include "tests.h"
+
+/* This program can be used to call programs through fexecve / execveat(…, "", …, AT_EMPTY_PATH),
+ * when compiled with -Dfexecve=true, and the fallback paths, when -Dfexecve=false.
+ *
+ * Example:
+ * $ strace -e execveat build/test-execve /bin/grep Name /proc/self/status
+ * execveat(3, "", ["/bin/grep", "Name", "/proc/self/status"], NULL, AT_EMPTY_PATH) = 0
+ * Name:   3
+ *
+ * FIXME: use the new kernel api to set COMM properly when the kernel makes that available.
+ * C.f. ceedbf8185fc7593366679f02d31da63af8c4bd1.
+ */
+
+static int run(int argc, char **argv) {
+        _cleanup_close_ int fd;
+        char **args = strv_skip(argv, 1);
+        int r;
+
+        test_setup_logging(LOG_DEBUG);
+
+        args = !strv_isempty(args) ? args : STRV_MAKE("/bin/true");
+
+        fd = open(args[0], O_RDONLY | O_CLOEXEC);
+        if (fd < 0)
+                return log_error_errno(errno, "open(%s) failed: %m", args[0]);
+
+        r = fexecve_or_execve(fd, args[0], args, NULL);
+        assert_se(r < 0);
+        return log_error_errno(r, "fexecve_or_execve(%s) failed: %m", args[0]);
+}
+
+DEFINE_MAIN_FUNCTION(run);