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