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