]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test-builtin.c
Merge pull request #16504 from poettering/read-file-ipc
[thirdparty/systemd.git] / src / udev / udevadm-test-builtin.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stddef.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "log.h"
10 #include "udev-builtin.h"
11 #include "udevadm.h"
12 #include "udevadm-util.h"
13
14 static const char *arg_command = NULL;
15 static const char *arg_syspath = NULL;
16
17 static int help(void) {
18 printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
19 "Test a built-in command.\n\n"
20 " -h --help Print this message\n"
21 " -V --version Print version of the program\n\n"
22 "Commands:\n"
23 , program_invocation_short_name);
24
25 udev_builtin_list();
26
27 return 0;
28 }
29
30 static int parse_argv(int argc, char *argv[]) {
31 static const struct option options[] = {
32 { "version", no_argument, NULL, 'V' },
33 { "help", no_argument, NULL, 'h' },
34 {}
35 };
36
37 int c;
38
39 while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
40 switch (c) {
41 case 'V':
42 return print_version();
43 case 'h':
44 return help();
45 case '?':
46 return -EINVAL;
47 default:
48 assert_not_reached("Unknown option");
49 }
50
51 arg_command = argv[optind++];
52 if (!arg_command)
53 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
54 "Command missing.");
55
56 arg_syspath = argv[optind++];
57 if (!arg_syspath)
58 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
59 "syspath missing.");
60
61 return 1;
62 }
63
64 int builtin_main(int argc, char *argv[], void *userdata) {
65 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
66 UdevBuiltinCommand cmd;
67 int r;
68
69 log_set_max_level(LOG_DEBUG);
70
71 r = parse_argv(argc, argv);
72 if (r <= 0)
73 return r;
74
75 udev_builtin_init();
76
77 cmd = udev_builtin_lookup(arg_command);
78 if (cmd < 0) {
79 log_error("Unknown command '%s'", arg_command);
80 r = -EINVAL;
81 goto finish;
82 }
83
84 r = find_device(arg_syspath, "/sys", &dev);
85 if (r < 0) {
86 log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
87 goto finish;
88 }
89
90 r = udev_builtin_run(dev, cmd, arg_command, true);
91 if (r < 0)
92 log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
93
94 finish:
95 udev_builtin_exit();
96 return r;
97 }