]>
Commit | Line | Data |
---|---|---|
225cb03b | 1 | /* |
1298001e | 2 | * Copyright (C) 2007-2012 Kay Sievers <kay@vrfy.org> |
225cb03b | 3 | * |
55e9959b KS |
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. | |
225cb03b | 8 | * |
55e9959b KS |
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/>. | |
225cb03b KS |
16 | */ |
17 | ||
225cb03b KS |
18 | #include <errno.h> |
19 | #include <getopt.h> | |
07630cea LP |
20 | #include <stddef.h> |
21 | #include <stdio.h> | |
225cb03b | 22 | |
d7b8eec7 | 23 | #include "selinux-util.h" |
07630cea | 24 | #include "string-util.h" |
225cb03b | 25 | #include "udev.h" |
b237a168 | 26 | #include "udev-util.h" |
225cb03b | 27 | |
9ec6e95b | 28 | static int adm_version(struct udev *udev, int argc, char *argv[]) { |
948aaa7c | 29 | printf("%s\n", PACKAGE_VERSION); |
912541b0 | 30 | return 0; |
225cb03b | 31 | } |
baa30fbc | 32 | |
1985c76e | 33 | static const struct udevadm_cmd udevadm_version = { |
912541b0 KS |
34 | .name = "version", |
35 | .cmd = adm_version, | |
1985c76e KS |
36 | }; |
37 | ||
38 | static int adm_help(struct udev *udev, int argc, char *argv[]); | |
baa30fbc | 39 | |
1985c76e | 40 | static const struct udevadm_cmd udevadm_help = { |
912541b0 KS |
41 | .name = "help", |
42 | .cmd = adm_help, | |
1985c76e KS |
43 | }; |
44 | ||
45 | static const struct udevadm_cmd *udevadm_cmds[] = { | |
912541b0 KS |
46 | &udevadm_info, |
47 | &udevadm_trigger, | |
48 | &udevadm_settle, | |
49 | &udevadm_control, | |
50 | &udevadm_monitor, | |
796b06c2 | 51 | &udevadm_hwdb, |
912541b0 KS |
52 | &udevadm_test, |
53 | &udevadm_test_builtin, | |
54 | &udevadm_version, | |
55 | &udevadm_help, | |
1985c76e | 56 | }; |
225cb03b | 57 | |
9ec6e95b | 58 | static int adm_help(struct udev *udev, int argc, char *argv[]) { |
912541b0 KS |
59 | unsigned int i; |
60 | ||
5ac0162c LP |
61 | printf("%s [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n\n" |
62 | "Send control commands or test the device manager.\n\n" | |
63 | "Commands:\n" | |
64 | , program_invocation_short_name); | |
65 | ||
8fef0ff2 | 66 | for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++) |
912541b0 | 67 | if (udevadm_cmds[i]->help != NULL) |
5ac0162c | 68 | printf(" %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help); |
912541b0 | 69 | return 0; |
225cb03b KS |
70 | } |
71 | ||
9ec6e95b | 72 | static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) { |
baa30fbc KS |
73 | if (cmd->debug) |
74 | log_set_max_level(LOG_DEBUG); | |
9f6445e3 | 75 | log_debug("calling: %s", cmd->name); |
912541b0 | 76 | return cmd->cmd(udev, argc, argv); |
7d563a17 KS |
77 | } |
78 | ||
9ec6e95b | 79 | int main(int argc, char *argv[]) { |
912541b0 KS |
80 | struct udev *udev; |
81 | static const struct option options[] = { | |
82 | { "debug", no_argument, NULL, 'd' }, | |
83 | { "help", no_argument, NULL, 'h' }, | |
84 | { "version", no_argument, NULL, 'V' }, | |
85 | {} | |
86 | }; | |
87 | const char *command; | |
88 | unsigned int i; | |
601185b4 | 89 | int rc = 1, c; |
912541b0 | 90 | |
b237a168 | 91 | udev_parse_config(); |
baa30fbc | 92 | log_parse_environment(); |
4b261568 | 93 | log_open(); |
b237a168 | 94 | |
c3dacc8b | 95 | mac_selinux_init(); |
912541b0 | 96 | |
b237a168 ZJS |
97 | udev = udev_new(); |
98 | if (udev == NULL) | |
99 | goto out; | |
100 | ||
601185b4 ZJS |
101 | while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0) |
102 | switch (c) { | |
912541b0 | 103 | |
912541b0 | 104 | case 'd': |
baa30fbc | 105 | log_set_max_level(LOG_DEBUG); |
912541b0 | 106 | break; |
601185b4 | 107 | |
912541b0 KS |
108 | case 'h': |
109 | rc = adm_help(udev, argc, argv); | |
110 | goto out; | |
601185b4 | 111 | |
912541b0 KS |
112 | case 'V': |
113 | rc = adm_version(udev, argc, argv); | |
114 | goto out; | |
601185b4 | 115 | |
912541b0 KS |
116 | default: |
117 | goto out; | |
118 | } | |
601185b4 | 119 | |
912541b0 KS |
120 | command = argv[optind]; |
121 | ||
912541b0 | 122 | if (command != NULL) |
601185b4 | 123 | for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++) |
090be865 | 124 | if (streq(udevadm_cmds[i]->name, command)) { |
912541b0 KS |
125 | argc -= optind; |
126 | argv += optind; | |
e5f2783e KS |
127 | /* we need '0' here to reset the internal state */ |
128 | optind = 0; | |
912541b0 KS |
129 | rc = run_command(udev, udevadm_cmds[i], argc, argv); |
130 | goto out; | |
131 | } | |
912541b0 | 132 | |
92e63a51 | 133 | fprintf(stderr, "%s: missing or unknown command\n", program_invocation_short_name); |
912541b0 | 134 | rc = 2; |
225cb03b | 135 | out: |
cc56fafe | 136 | mac_selinux_finish(); |
912541b0 | 137 | udev_unref(udev); |
baa30fbc | 138 | log_close(); |
912541b0 | 139 | return rc; |
225cb03b | 140 | } |