]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/fuzz-systemctl-parse-argv.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "selinux-util.h"
10 #include "static-destruct.h"
11 #include "stdio-util.h"
13 #include "systemctl.h"
14 #include "systemctl-util.h"
16 int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
17 _cleanup_strv_free_
char **argv
= NULL
;
18 _cleanup_close_
int orig_stdout_fd
= -1;
21 /* We don't want to fill the logs with messages about parse errors.
22 * Disable most logging if not running standalone */
23 if (!getenv("SYSTEMD_LOG_LEVEL"))
24 log_set_max_level(LOG_CRIT
);
26 arg_pager_flags
= PAGER_DISABLE
; /* We shouldn't execute the pager */
28 argv
= strv_parse_nulstr((const char *)data
, size
);
33 return 0; /* argv[0] should always be present, but may be zero-length. */
35 if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) {
36 orig_stdout_fd
= fcntl(fileno(stdout
), F_DUPFD_CLOEXEC
, 3);
37 if (orig_stdout_fd
< 0)
38 log_warning_errno(orig_stdout_fd
, "Failed to duplicate fd 1: %m");
40 assert_se(freopen("/dev/null", "w", stdout
));
42 opterr
= 0; /* do not print errors */
45 optind
= 0; /* this tells the getopt machinery to reinitialize */
47 r
= systemctl_dispatch_parse_argv(strv_length(argv
), argv
);
49 log_error_errno(r
, "Failed to parse args: %m");
51 log_info(r
== 0 ? "Done!" : "Action!");
53 if (orig_stdout_fd
>= 0) {
54 char path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
56 xsprintf(path
, "/proc/self/fd/%d", orig_stdout_fd
);
57 assert_se(freopen(path
, "w", stdout
));
60 release_busses(); /* We open the bus for communication with logind.
61 * It needs to be closed to avoid apparent leaks. */
65 /* Call static destructors to do global state cleanup. We do it here, and not in fuzz-main.c so that
66 * any global state is destroyed between fuzzer runs. */