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