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