| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "exec-util.h" |
| 4 |
#include "fd-util.h" |
| 5 |
#include "log.h" |
| 6 |
#include "main-func.h" |
| 7 |
#include "strv.h" |
| 8 |
#include "tests.h" |
| 9 |
|
| 10 |
/* This program can be used to call programs through fexecve / execveat(…, "", …, AT_EMPTY_PATH), |
| 11 |
* when compiled with -Dfexecve=true, and the fallback paths, when -Dfexecve=false. |
| 12 |
* |
| 13 |
* Example: |
| 14 |
* $ strace -e execveat build/test-execve /bin/grep Name /proc/self/status |
| 15 |
* execveat(3, "", ["/bin/grep", "Name", "/proc/self/status"], NULL, AT_EMPTY_PATH) = 0 |
| 16 |
* Name: 3 |
| 17 |
* |
| 18 |
* FIXME: use the new kernel api to set COMM properly when the kernel makes that available. |
| 19 |
* C.f. ceedbf8185fc7593366679f02d31da63af8c4bd1. |
| 20 |
*/ |
| 21 |
|
| 22 |
static int run(int argc, char **argv) { |
| 23 |
int r; |
| 24 |
|
| 25 |
test_setup_logging(LOG_DEBUG); |
| 26 |
|
| 27 |
char **args = strv_skip(argv, 1) ?: STRV_MAKE("/bin/true"); |
| 28 |
|
| 29 |
_cleanup_close_ int fd = open(args[0], O_RDONLY | O_CLOEXEC); |
| 30 |
if (fd < 0) |
| 31 |
return log_error_errno(errno, "open(%s) failed: %m", args[0]); |
| 32 |
|
| 33 |
r = fexecve_or_execve(fd, args[0], args, NULL); |
| 34 |
assert_se(r < 0); |
| 35 |
return log_error_errno(r, "fexecve_or_execve(%s) failed: %m", args[0]); |
| 36 |
} |
| 37 |
|
| 38 |
DEFINE_MAIN_FUNCTION(run); |