]>
Commit | Line | Data |
---|---|---|
d7867b31 | 1 | /* |
1298001e | 2 | * Copyright (C) 2011 Kay Sievers <kay@vrfy.org> |
d7867b31 KS |
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 <stdlib.h> | |
19 | #include <stddef.h> | |
d7867b31 | 20 | #include <stdio.h> |
d7867b31 | 21 | #include <errno.h> |
d7867b31 | 22 | #include <getopt.h> |
d7867b31 KS |
23 | |
24 | #include "udev.h" | |
25 | ||
9ec6e95b | 26 | static void help(struct udev *udev) { |
5ac0162c LP |
27 | printf("%s builtin [--help] COMMAND SYSPATH\n\n" |
28 | "Test a built-in command.\n\n" | |
29 | " -h --help Print this message\n" | |
30 | " --version Print version of the program\n\n" | |
31 | "Commands:\n" | |
32 | , program_invocation_short_name); | |
33 | ||
912541b0 | 34 | udev_builtin_list(udev); |
d7867b31 KS |
35 | } |
36 | ||
9ec6e95b | 37 | static int adm_builtin(struct udev *udev, int argc, char *argv[]) { |
912541b0 KS |
38 | static const struct option options[] = { |
39 | { "help", no_argument, NULL, 'h' }, | |
40 | {} | |
41 | }; | |
42 | char *command = NULL; | |
43 | char *syspath = NULL; | |
44 | char filename[UTIL_PATH_SIZE]; | |
45 | struct udev_device *dev = NULL; | |
46 | enum udev_builtin_cmd cmd; | |
7643ac9a | 47 | int rc = EXIT_SUCCESS, c; |
912541b0 | 48 | |
7643ac9a ZJS |
49 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
50 | switch (c) { | |
912541b0 KS |
51 | case 'h': |
52 | help(udev); | |
53 | goto out; | |
54 | } | |
912541b0 KS |
55 | |
56 | command = argv[optind++]; | |
57 | if (command == NULL) { | |
58 | fprintf(stderr, "command missing\n"); | |
59 | help(udev); | |
60 | rc = 2; | |
61 | goto out; | |
62 | } | |
63 | ||
64 | syspath = argv[optind++]; | |
65 | if (syspath == NULL) { | |
7643ac9a | 66 | fprintf(stderr, "syspath missing\n"); |
912541b0 KS |
67 | rc = 3; |
68 | goto out; | |
69 | } | |
70 | ||
71 | udev_builtin_init(udev); | |
72 | ||
73 | cmd = udev_builtin_lookup(command); | |
74 | if (cmd >= UDEV_BUILTIN_MAX) { | |
75 | fprintf(stderr, "unknown command '%s'\n", command); | |
76 | help(udev); | |
77 | rc = 5; | |
78 | goto out; | |
79 | } | |
80 | ||
81 | /* add /sys if needed */ | |
33502ffe | 82 | if (!startswith(syspath, "/sys")) |
d5a89d7d | 83 | strscpyl(filename, sizeof(filename), "/sys", syspath, NULL); |
912541b0 | 84 | else |
d5a89d7d | 85 | strscpy(filename, sizeof(filename), syspath); |
912541b0 KS |
86 | util_remove_trailing_chars(filename, '/'); |
87 | ||
88 | dev = udev_device_new_from_syspath(udev, filename); | |
89 | if (dev == NULL) { | |
90 | fprintf(stderr, "unable to open device '%s'\n\n", filename); | |
91 | rc = 4; | |
92 | goto out; | |
93 | } | |
94 | ||
0bb91b50 KS |
95 | rc = udev_builtin_run(dev, cmd, command, true); |
96 | if (rc < 0) { | |
97 | fprintf(stderr, "error executing '%s', exit code %i\n\n", command, rc); | |
912541b0 KS |
98 | rc = 6; |
99 | } | |
d7867b31 | 100 | out: |
912541b0 KS |
101 | udev_device_unref(dev); |
102 | udev_builtin_exit(udev); | |
103 | return rc; | |
d7867b31 KS |
104 | } |
105 | ||
106 | const struct udevadm_cmd udevadm_test_builtin = { | |
912541b0 KS |
107 | .name = "test-builtin", |
108 | .cmd = adm_builtin, | |
5ac0162c | 109 | .help = "Test a built-in command", |
912541b0 | 110 | .debug = true, |
d7867b31 | 111 | }; |