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 if (cmd->help != NULL)
67 printf(" %-12s %s\n", cmd->name, cmd->help);
72 static const struct command cmds[] = {
76 .help = "query sysfs or the udev database",
80 .cmd = udevadm_trigger,
81 .help = "request events from the kernel",
85 .cmd = udevadm_settle, "",
86 .help = "wait for the event queue to finish",
90 .cmd = udevadm_control,
91 .help = "control the udev daemon",
95 .cmd = udevadm_monitor,
96 .help = "listen to kernel and udev events",
101 .help = "simulation run",
115 static int run_command(struct udev *udev, const struct command *cmd, int argc, char *argv[])
119 if (udev_get_log_priority(udev) < LOG_INFO)
120 udev_set_log_priority(udev, LOG_INFO);
122 info(udev, "calling: %s\n", cmd->name);
123 return cmd->cmd(udev, argc, argv);
126 int main(int argc, char *argv[])
129 static const struct option options[] = {
130 { "debug", 0, NULL, 'd' },
131 { "help", 0, NULL, 'h' },
132 { "version", 0, NULL, 'V' },
144 logging_init("udevadm");
145 udev_set_log_fn(udev, log_fn);
148 /* see if we are a compat link, this will be removed in a future release */
150 pos = strrchr(command, '/');
154 /* the trailing part of the binary or link name is the command */
155 if (strncmp(command, "udev", 4) == 0)
156 command = &command[4];
158 for (i = 0; cmds[i].cmd != NULL; i++) {
159 if (strcmp(cmds[i].name, command) == 0) {
164 snprintf(path, sizeof(path), "/proc/%lu/exe", (unsigned long) getppid());
165 len = readlink(path, prog, sizeof(prog));
168 fprintf(stderr, "the program '%s' called '%s', it should use 'udevadm %s <options>', "
169 "this will stop working in a future release\n", prog, argv[0], command);
170 info(udev, "the program '%s' called '%s', it should use 'udevadm %s <options>', "
171 "this will stop working in a future release\n", prog, argv[0], command);
173 rc = run_command(udev, &cmds[i], argc, argv);
181 option = getopt_long(argc, argv, "+dhV", options, NULL);
188 if (udev_get_log_priority(udev) < LOG_INFO)
189 udev_set_log_priority(udev, LOG_INFO);
192 rc = help(udev, argc, argv);
195 rc = version(udev, argc, argv);
201 command = argv[optind];
204 for (i = 0; cmds[i].cmd != NULL; i++) {
205 if (strcmp(cmds[i].name, command) == 0) {
207 rc = run_command(udev, &cmds[i], argc, argv);
212 fprintf(stderr, "missing or unknown command\n\n");
213 help(udev, argc, argv);