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