2 * Copyright (C) 2007-2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 static void log_fn(struct udev *udev, int priority,
34 const char *file, int line, const char *fn,
35 const char *format, va_list args)
38 fprintf(stderr, "%s: ", fn);
39 vfprintf(stderr, format, args);
41 vsyslog(priority, format, args);
47 int (*cmd)(struct udev *udev, int argc, char *argv[]);
52 static const struct command cmds[];
54 static int version(struct udev *udev, int argc, char *argv[])
56 printf("%s\n", VERSION);
60 static int help(struct udev *udev, int argc, char *argv[])
62 const struct command *cmd;
64 printf("Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
65 for (cmd = cmds; cmd->name != NULL; cmd++)
66 printf(" %-12s %s\n", cmd->name, cmd->help);
71 static const struct command cmds[] = {
75 .help = "query sysfs or the udev database",
79 .cmd = udevadm_trigger,
80 .help = "request events from the kernel",
84 .cmd = udevadm_settle, "",
85 .help = "wait for the event queue to finish",
89 .cmd = udevadm_control,
90 .help = "control the udev daemon",
94 .cmd = udevadm_monitor,
95 .help = "listen to kernel and udev events",
100 .help = "simulation run",
106 .help = "print the version number",
111 .help = "print this help text",
116 static int run_command(struct udev *udev, const struct command *cmd, int argc, char *argv[])
120 if (udev_get_log_priority(udev) < LOG_INFO)
121 udev_set_log_priority(udev, LOG_INFO);
123 info(udev, "calling: %s\n", cmd->name);
124 return cmd->cmd(udev, argc, argv);
127 int main(int argc, char *argv[])
130 static const struct option options[] = {
131 { "debug", 0, NULL, 'd' },
132 { "help", 0, NULL, 'h' },
133 { "version", 0, NULL, 'V' },
145 logging_init("udevadm");
146 udev_set_log_fn(udev, log_fn);
149 /* see if we are a compat link, this will be removed in a future release */
151 pos = strrchr(command, '/');
155 /* the trailing part of the binary or link name is the command */
156 if (strncmp(command, "udev", 4) == 0)
157 command = &command[4];
159 for (i = 0; cmds[i].cmd != NULL; i++) {
160 if (strcmp(cmds[i].name, command) == 0) {
165 snprintf(path, sizeof(path), "/proc/%lu/exe", (unsigned long) getppid());
166 len = readlink(path, prog, sizeof(prog));
169 fprintf(stderr, "the program '%s' called '%s', it should use 'udevadm %s <options>', "
170 "this will stop working in a future release\n", prog, argv[0], command);
171 info(udev, "the program '%s' called '%s', it should use 'udevadm %s <options>', "
172 "this will stop working in a future release\n", prog, argv[0], command);
174 rc = run_command(udev, &cmds[i], argc, argv);
182 option = getopt_long(argc, argv, "+dhV", options, NULL);
189 if (udev_get_log_priority(udev) < LOG_INFO)
190 udev_set_log_priority(udev, LOG_INFO);
193 rc = help(udev, argc, argv);
196 rc = version(udev, argc, argv);
202 command = argv[optind];
205 for (i = 0; cmds[i].cmd != NULL; i++) {
206 if (strcmp(cmds[i].name, command) == 0) {
208 rc = run_command(udev, &cmds[i], argc, argv);
213 fprintf(stderr, "unknown command, try udevadm help\n\n");