]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm.c
udev: place opening { at the same line as the function declaration
[thirdparty/systemd.git] / src / udev / udevadm.c
1 /*
2 * Copyright (C) 2007-2012 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 <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25
26 #include "udev.h"
27
28 void udev_main_log(struct udev *udev, int priority,
29 const char *file, int line, const char *fn,
30 const char *format, va_list args) {
31 log_metav(priority, file, line, fn, format, args);
32 }
33
34 static int adm_version(struct udev *udev, int argc, char *argv[]) {
35 printf("%s\n", VERSION);
36 return 0;
37 }
38
39 static const struct udevadm_cmd udevadm_version = {
40 .name = "version",
41 .cmd = adm_version,
42 };
43
44 static int adm_help(struct udev *udev, int argc, char *argv[]);
45
46 static const struct udevadm_cmd udevadm_help = {
47 .name = "help",
48 .cmd = adm_help,
49 };
50
51 static const struct udevadm_cmd *udevadm_cmds[] = {
52 &udevadm_info,
53 &udevadm_trigger,
54 &udevadm_settle,
55 &udevadm_control,
56 &udevadm_monitor,
57 &udevadm_hwdb,
58 &udevadm_test,
59 &udevadm_test_builtin,
60 &udevadm_version,
61 &udevadm_help,
62 };
63
64 static int adm_help(struct udev *udev, int argc, char *argv[]) {
65 unsigned int i;
66
67 fprintf(stderr, "Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
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 fprintf(stderr, "\n");
72 return 0;
73 }
74
75 static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) {
76 if (cmd->debug)
77 log_set_max_level(LOG_DEBUG);
78 log_debug("calling: %s", cmd->name);
79 return cmd->cmd(udev, argc, argv);
80 }
81
82 int main(int argc, char *argv[]) {
83 struct udev *udev;
84 static const struct option options[] = {
85 { "debug", no_argument, NULL, 'd' },
86 { "help", no_argument, NULL, 'h' },
87 { "version", no_argument, NULL, 'V' },
88 {}
89 };
90 const char *command;
91 unsigned int i;
92 int rc = 1;
93
94 udev = udev_new();
95 if (udev == NULL)
96 goto out;
97
98 log_parse_environment();
99 log_open();
100 udev_set_log_fn(udev, udev_main_log);
101 label_init("/dev");
102
103 for (;;) {
104 int option;
105
106 option = getopt_long(argc, argv, "+dhV", options, NULL);
107 if (option == -1)
108 break;
109
110 switch (option) {
111 case 'd':
112 log_set_max_level(LOG_DEBUG);
113 udev_set_log_priority(udev, LOG_DEBUG);
114 break;
115 case 'h':
116 rc = adm_help(udev, argc, argv);
117 goto out;
118 case 'V':
119 rc = adm_version(udev, argc, argv);
120 goto out;
121 default:
122 goto out;
123 }
124 }
125 command = argv[optind];
126
127 if (command != NULL)
128 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++) {
129 if (streq(udevadm_cmds[i]->name, command)) {
130 argc -= optind;
131 argv += optind;
132 /* we need '0' here to reset the internal state */
133 optind = 0;
134 rc = run_command(udev, udevadm_cmds[i], argc, argv);
135 goto out;
136 }
137 }
138
139 fprintf(stderr, "missing or unknown command\n\n");
140 adm_help(udev, argc, argv);
141 rc = 2;
142 out:
143 label_finish();
144 udev_unref(udev);
145 log_close();
146 return rc;
147 }