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