udevadm.c revision 6d837a5348e7b38cb122405290b7bc3c17d069aa
d657c51f14601d0235434ffb78cf6ac0f27cc83cLennart Poettering/*
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * Copyright (C) 2007-2009 Kay Sievers <kay.sievers@vrfy.org>
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering *
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * This program is free software: you can redistribute it and/or modify
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * it under the terms of the GNU General Public License as published by
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * (at your option) any later version.
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering *
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * This program is distributed in the hope that it will be useful,
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * GNU General Public License for more details.
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering *
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * You should have received a copy of the GNU General Public License
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering */
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering#include <unistd.h>
ed28905eecf631916f03edd0a7dfad8b0a177990Kay Sievers#include <stdio.h>
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering#include <stdlib.h>
ed28905eecf631916f03edd0a7dfad8b0a177990Kay Sievers#include <stddef.h>
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering#include <string.h>
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering#include <errno.h>
ed28905eecf631916f03edd0a7dfad8b0a177990Kay Sievers#include <getopt.h>
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
ed28905eecf631916f03edd0a7dfad8b0a177990Kay Sievers#include "udev.h"
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poetteringstatic int debug;
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poetteringstatic void log_fn(struct udev *udev, int priority,
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering const char *file, int line, const char *fn,
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering const char *format, va_list args)
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering{
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering if (debug) {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering fprintf(stderr, "%s: ", fn);
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering vfprintf(stderr, format, args);
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering } else {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering vsyslog(priority, format, args);
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering }
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering}
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poetteringstruct command {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering const char *name;
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering int (*cmd)(struct udev *udev, int argc, char *argv[]);
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering const char *help;
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering int debug;
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering};
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poetteringstatic const struct command cmds[];
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poetteringstatic int version(struct udev *udev, int argc, char *argv[])
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering{
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering printf("%s\n", VERSION);
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering return 0;
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering}
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poetteringstatic int help(struct udev *udev, int argc, char *argv[])
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering{
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering const struct command *cmd;
ed28905eecf631916f03edd0a7dfad8b0a177990Kay Sievers
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering printf("Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering for (cmd = cmds; cmd->name != NULL; cmd++)
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering if (cmd->help != NULL)
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering printf(" %-12s %s\n", cmd->name, cmd->help);
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering printf("\n");
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering return 0;
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering}
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poetteringstatic const struct command cmds[] = {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .name = "info",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .cmd = udevadm_info,
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .help = "query sysfs or the udev database",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering },
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .name = "trigger",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .cmd = udevadm_trigger,
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .help = "request events from the kernel",
ed28905eecf631916f03edd0a7dfad8b0a177990Kay Sievers },
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .name = "settle",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .cmd = udevadm_settle,
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering .help = "wait for the event queue to finish",
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering },
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering {
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering .name = "control",
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering .cmd = udevadm_control,
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering .help = "control the udev daemon",
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering },
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering {
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering .name = "monitor",
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering .cmd = udevadm_monitor,
ed28905eecf631916f03edd0a7dfad8b0a177990Kay Sievers .help = "listen to kernel and udev events",
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering },
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering {
d27893efdf652c6d85ea590fa0b7c2b88f817083Lennart Poettering .name = "test",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .cmd = udevadm_test,
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .help = "simulation run",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .debug = 1,
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering },
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .name = "version",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .cmd = version,
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering },
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering {
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .name = "help",
43c71255b3db02916f4f70afa18bab196c6f4a45Lennart Poettering .cmd = help,
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering },
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering {}
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering};
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. Johnstatic int run_command(struct udev *udev, const struct command *cmd, int argc, char *argv[])
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John{
4670e9d5f23fc39360c086fb58eadf8b157ee205Jan Engelhardt if (cmd->debug) {
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John debug = 1;
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John if (udev_get_log_priority(udev) < LOG_INFO)
4670e9d5f23fc39360c086fb58eadf8b157ee205Jan Engelhardt udev_set_log_priority(udev, LOG_INFO);
1e190502e78cea1f8bcb62e6d390305c89e41e6aZbigniew Jędrzejewski-Szmek }
4670e9d5f23fc39360c086fb58eadf8b157ee205Jan Engelhardt info(udev, "calling: %s\n", cmd->name);
1e190502e78cea1f8bcb62e6d390305c89e41e6aZbigniew Jędrzejewski-Szmek return cmd->cmd(udev, argc, argv);
1e190502e78cea1f8bcb62e6d390305c89e41e6aZbigniew Jędrzejewski-Szmek}
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poetteringint main(int argc, char *argv[])
4c2413bffa7861bd3c4b3589c821ab7e0ac51c83Jan Engelhardt{
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering struct udev *udev;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering static const struct option options[] = {
4c2413bffa7861bd3c4b3589c821ab7e0ac51c83Jan Engelhardt { "debug", no_argument, NULL, 'd' },
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering { "help", no_argument, NULL, 'h' },
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering { "version", no_argument, NULL, 'V' },
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering {}
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering };
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John const char *command;
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John int i;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering int rc = 1;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering udev = udev_new();
4c2413bffa7861bd3c4b3589c821ab7e0ac51c83Jan Engelhardt if (udev == NULL)
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering goto out;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering udev_log_init("udevadm");
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering udev_set_log_fn(udev, log_fn);
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John udev_selinux_init(udev);
4c2413bffa7861bd3c4b3589c821ab7e0ac51c83Jan Engelhardt
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John while (1) {
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering int option;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek option = getopt_long(argc, argv, "+dhV", options, NULL);
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek if (option == -1)
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek break;
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek switch (option) {
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek case 'd':
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek debug = 1;
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek if (udev_get_log_priority(udev) < LOG_INFO)
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek udev_set_log_priority(udev, LOG_INFO);
dfb08b058401d56c395f4f2d20ff214d31a277e5Zbigniew Jędrzejewski-Szmek break;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering case 'h':
4c2413bffa7861bd3c4b3589c821ab7e0ac51c83Jan Engelhardt rc = help(udev, argc, argv);
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering goto out;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering case 'V':
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering rc = version(udev, argc, argv);
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering goto out;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering default:
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering goto out;
4c2413bffa7861bd3c4b3589c821ab7e0ac51c83Jan Engelhardt }
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering }
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering command = argv[optind];
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering if (command != NULL)
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering for (i = 0; cmds[i].cmd != NULL; i++) {
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering if (strcmp(cmds[i].name, command) == 0) {
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering argc -= optind;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering argv += optind;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering optind = 0;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering rc = run_command(udev, &cmds[i], argc, argv);
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering goto out;
8b7d0494a3fe35209d4db0d1b9e065e7e5cc9875Jason St. John }
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering }
1e190502e78cea1f8bcb62e6d390305c89e41e6aZbigniew Jędrzejewski-Szmek
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering fprintf(stderr, "missing or unknown command\n\n");
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering help(udev, argc, argv);
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering rc = 2;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poetteringout:
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering udev_selinux_exit(udev);
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering udev_unref(udev);
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering udev_log_close();
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering return rc;
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering}
e49b5aada0df13c9e8fce7338ae34e075dd7ccd1Lennart Poettering