]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm.c
udevadm,..: make --help output of udev tools more like the output of the various...
[thirdparty/systemd.git] / src / udev / udevadm.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 /*
3 * Copyright (C) 2007-2012 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 <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <getopt.h>
26
27 #include "selinux-util.h"
28 #include "udev.h"
29
30 static int adm_version(struct udev *udev, int argc, char *argv[]) {
31 printf("%s\n", VERSION);
32 return 0;
33 }
34
35 static const struct udevadm_cmd udevadm_version = {
36 .name = "version",
37 .cmd = adm_version,
38 };
39
40 static int adm_help(struct udev *udev, int argc, char *argv[]);
41
42 static const struct udevadm_cmd udevadm_help = {
43 .name = "help",
44 .cmd = adm_help,
45 };
46
47 static const struct udevadm_cmd *udevadm_cmds[] = {
48 &udevadm_info,
49 &udevadm_trigger,
50 &udevadm_settle,
51 &udevadm_control,
52 &udevadm_monitor,
53 &udevadm_hwdb,
54 &udevadm_test,
55 &udevadm_test_builtin,
56 &udevadm_version,
57 &udevadm_help,
58 };
59
60 static int adm_help(struct udev *udev, int argc, char *argv[]) {
61 unsigned int i;
62
63 printf("%s [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n\n"
64 "Send control commands or test the device manager.\n\n"
65 "Commands:\n"
66 , program_invocation_short_name);
67
68 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
69 if (udevadm_cmds[i]->help != NULL)
70 printf(" %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help);
71 return 0;
72 }
73
74 static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) {
75 if (cmd->debug)
76 log_set_max_level(LOG_DEBUG);
77 log_debug("calling: %s", cmd->name);
78 return cmd->cmd(udev, argc, argv);
79 }
80
81 int main(int argc, char *argv[]) {
82 struct udev *udev;
83 static const struct option options[] = {
84 { "debug", no_argument, NULL, 'd' },
85 { "help", no_argument, NULL, 'h' },
86 { "version", no_argument, NULL, 'V' },
87 {}
88 };
89 const char *command;
90 unsigned int i;
91 int rc = 1, c;
92
93 udev = udev_new();
94 if (udev == NULL)
95 goto out;
96
97 log_parse_environment();
98 log_open();
99 mac_selinux_init("/dev");
100
101 while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
102 switch (c) {
103
104 case 'd':
105 log_set_max_level(LOG_DEBUG);
106 break;
107
108 case 'h':
109 rc = adm_help(udev, argc, argv);
110 goto out;
111
112 case 'V':
113 rc = adm_version(udev, argc, argv);
114 goto out;
115
116 default:
117 goto out;
118 }
119
120 command = argv[optind];
121
122 if (command != NULL)
123 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
124 if (streq(udevadm_cmds[i]->name, command)) {
125 argc -= optind;
126 argv += optind;
127 /* we need '0' here to reset the internal state */
128 optind = 0;
129 rc = run_command(udev, udevadm_cmds[i], argc, argv);
130 goto out;
131 }
132
133 fprintf(stderr, "%s: missing or unknown command\n", program_invocation_short_name);
134 rc = 2;
135 out:
136 mac_selinux_finish();
137 udev_unref(udev);
138 log_close();
139 return rc;
140 }