1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ 2/* 3 * Copyright (C) 2007-2012 Kay Sievers <kay@vrfy.org> 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19#include <unistd.h> 20#include <stdio.h> 21#include <stdlib.h> 22#include <stddef.h> 23#include <string.h> 24#include <errno.h> 25#include <getopt.h> 26 27#include "udev.h" 28 29static int adm_version(struct udev *udev, int argc, char *argv[]) { 30 printf("%s\n", UDEV_VERSION); 31 return 0; 32} 33 34static const struct udevadm_cmd udevadm_version = { 35 .name = "version", 36 .cmd = adm_version, 37}; 38 39static int adm_help(struct udev *udev, int argc, char *argv[]); 40 41static const struct udevadm_cmd udevadm_help = { 42 .name = "help", 43 .cmd = adm_help, 44}; 45 46static const struct udevadm_cmd *udevadm_cmds[] = { 47 &udevadm_info, 48 &udevadm_trigger, 49 &udevadm_settle, 50 &udevadm_control, 51 &udevadm_monitor, 52 &udevadm_hwdb, 53 &udevadm_test, 54 &udevadm_test_builtin, 55 &udevadm_version, 56 &udevadm_help, 57}; 58 59static int adm_help(struct udev *udev, int argc, char *argv[]) { 60 unsigned int i; 61 62 printf("%s [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n\n" 63 "Send control commands or test the device manager.\n\n" 64 "Commands:\n" 65 , program_invocation_short_name); 66 67 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++) 68 if (udevadm_cmds[i]->help != NULL) 69 printf(" %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help); 70 return 0; 71} 72 73static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) { 74 if (cmd->debug) 75 log_set_max_level(LOG_DEBUG); 76 log_debug("calling: %s", cmd->name); 77 return cmd->cmd(udev, argc, argv); 78} 79 80int main(int argc, char *argv[]) { 81 struct udev *udev; 82 static const struct option options[] = { 83 { "debug", no_argument, NULL, 'd' }, 84 { "help", no_argument, NULL, 'h' }, 85 { "version", no_argument, NULL, 'V' }, 86 {} 87 }; 88 const char *command; 89 unsigned int i; 90 int rc = 1, c; 91 92 udev = udev_new(); 93 if (udev == NULL) 94 goto out; 95 96 log_open(); 97 mac_selinux_init("/dev"); 98 99 while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0) 100 switch (c) { 101 102 case 'd': 103 log_set_max_level(LOG_DEBUG); 104 break; 105 106 case 'h': 107 rc = adm_help(udev, argc, argv); 108 goto out; 109 110 case 'V': 111 rc = adm_version(udev, argc, argv); 112 goto out; 113 114 default: 115 goto out; 116 } 117 118 command = argv[optind]; 119 120 if (command != NULL) 121 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++) 122 if (streq(udevadm_cmds[i]->name, command)) { 123 argc -= optind; 124 argv += optind; 125 /* we need '0' here to reset the internal state */ 126 optind = 0; 127 rc = run_command(udev, udevadm_cmds[i], argc, argv); 128 goto out; 129 } 130 131 fprintf(stderr, "%s: missing or unknown command\n", program_invocation_short_name); 132 rc = 2; 133out: 134 mac_selinux_finish(); 135 udev_unref(udev); 136 log_close(); 137 return rc; 138} 139