]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/udev/udevadm-test-builtin.c
tree-wide: drop empty lines in comments
[thirdparty/systemd.git] / src / udev / udevadm-test-builtin.c
... / ...
CommitLineData
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 "path-util.h"
10#include "string-util.h"
11#include "udev.h"
12#include "udevadm-util.h"
13
14static void help(struct udev *udev) {
15 printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
16 "Test a built-in command.\n\n"
17 " -h --help Print this message\n"
18 " -V --version Print version of the program\n\n"
19 "Commands:\n"
20 , program_invocation_short_name);
21
22 udev_builtin_list(udev);
23}
24
25static int adm_builtin(struct udev *udev, int argc, char *argv[]) {
26 static const struct option options[] = {
27 { "version", no_argument, NULL, 'V' },
28 { "help", no_argument, NULL, 'h' },
29 {}
30 };
31 char *command = NULL;
32 char *syspath = NULL;
33 char filename[UTIL_PATH_SIZE];
34 struct udev_device *dev = NULL;
35 enum udev_builtin_cmd cmd;
36 int rc = EXIT_SUCCESS, c;
37
38 while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
39 switch (c) {
40 case 'V':
41 print_version();
42 goto out;
43 case 'h':
44 help(udev);
45 goto out;
46 }
47
48 command = argv[optind++];
49 if (command == NULL) {
50 fprintf(stderr, "command missing\n");
51 help(udev);
52 rc = 2;
53 goto out;
54 }
55
56 syspath = argv[optind++];
57 if (syspath == NULL) {
58 fprintf(stderr, "syspath missing\n");
59 rc = 3;
60 goto out;
61 }
62
63 udev_builtin_init(udev);
64
65 cmd = udev_builtin_lookup(command);
66 if (cmd >= UDEV_BUILTIN_MAX) {
67 fprintf(stderr, "unknown command '%s'\n", command);
68 help(udev);
69 rc = 5;
70 goto out;
71 }
72
73 /* add /sys if needed */
74 if (!path_startswith(syspath, "/sys"))
75 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
76 else
77 strscpy(filename, sizeof(filename), syspath);
78 delete_trailing_chars(filename, "/");
79
80 dev = udev_device_new_from_syspath(udev, filename);
81 if (dev == NULL) {
82 fprintf(stderr, "unable to open device '%s'\n\n", filename);
83 rc = 4;
84 goto out;
85 }
86
87 rc = udev_builtin_run(dev, cmd, command, true);
88 if (rc < 0) {
89 fprintf(stderr, "error executing '%s', exit code %i\n\n", command, rc);
90 rc = 6;
91 }
92out:
93 udev_device_unref(dev);
94 udev_builtin_exit(udev);
95 return rc;
96}
97
98const struct udevadm_cmd udevadm_test_builtin = {
99 .name = "test-builtin",
100 .cmd = adm_builtin,
101 .help = "Test a built-in command",
102 .debug = true,
103};