]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-test-builtin.c
f5a39a11c11c7094977f0979f86fedfa6b118b13
[thirdparty/systemd.git] / src / udev / udevadm-test-builtin.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "path-util.h"
26 #include "string-util.h"
27 #include "udev.h"
28 #include "udevadm-util.h"
29
30 static void help(struct udev *udev) {
31 printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
32 "Test a built-in command.\n\n"
33 " -h --help Print this message\n"
34 " -V --version Print version of the program\n\n"
35 "Commands:\n"
36 , program_invocation_short_name);
37
38 udev_builtin_list(udev);
39 }
40
41 static int adm_builtin(struct udev *udev, int argc, char *argv[]) {
42 static const struct option options[] = {
43 { "version", no_argument, NULL, 'V' },
44 { "help", no_argument, NULL, 'h' },
45 {}
46 };
47 char *command = NULL;
48 char *syspath = NULL;
49 char filename[UTIL_PATH_SIZE];
50 struct udev_device *dev = NULL;
51 enum udev_builtin_cmd cmd;
52 int rc = EXIT_SUCCESS, c;
53
54 while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
55 switch (c) {
56 case 'V':
57 print_version();
58 goto out;
59 case 'h':
60 help(udev);
61 goto out;
62 }
63
64 command = argv[optind++];
65 if (command == NULL) {
66 fprintf(stderr, "command missing\n");
67 help(udev);
68 rc = 2;
69 goto out;
70 }
71
72 syspath = argv[optind++];
73 if (syspath == NULL) {
74 fprintf(stderr, "syspath missing\n");
75 rc = 3;
76 goto out;
77 }
78
79 udev_builtin_init(udev);
80
81 cmd = udev_builtin_lookup(command);
82 if (cmd >= UDEV_BUILTIN_MAX) {
83 fprintf(stderr, "unknown command '%s'\n", command);
84 help(udev);
85 rc = 5;
86 goto out;
87 }
88
89 /* add /sys if needed */
90 if (!path_startswith(syspath, "/sys"))
91 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
92 else
93 strscpy(filename, sizeof(filename), syspath);
94 delete_trailing_chars(filename, "/");
95
96 dev = udev_device_new_from_syspath(udev, filename);
97 if (dev == NULL) {
98 fprintf(stderr, "unable to open device '%s'\n\n", filename);
99 rc = 4;
100 goto out;
101 }
102
103 rc = udev_builtin_run(dev, cmd, command, true);
104 if (rc < 0) {
105 fprintf(stderr, "error executing '%s', exit code %i\n\n", command, rc);
106 rc = 6;
107 }
108 out:
109 udev_device_unref(dev);
110 udev_builtin_exit(udev);
111 return rc;
112 }
113
114 const struct udevadm_cmd udevadm_test_builtin = {
115 .name = "test-builtin",
116 .cmd = adm_builtin,
117 .help = "Test a built-in command",
118 .debug = true,
119 };