chiark / gitweb /
udevadm: move udevadm command descriptions into their files
[elogind.git] / udev / udevadm.c
1 /*
2  * Copyright (C) 2007-2009 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
8  *
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/>.
16  */
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25
26 #include "udev.h"
27
28 static bool debug;
29
30 static void log_fn(struct udev *udev, int priority,
31                    const char *file, int line, const char *fn,
32                    const char *format, va_list args)
33 {
34         if (debug) {
35                 fprintf(stderr, "%s: ", fn);
36                 vfprintf(stderr, format, args);
37         } else {
38                 va_list args2;
39
40                 va_copy(args2, args);
41                 vfprintf(stderr, format, args2);
42                 va_end(args2);
43                 vsyslog(priority, format, args);
44         }
45 }
46
47 static int adm_version(struct udev *udev, int argc, char *argv[])
48 {
49         printf("%s\n", VERSION);
50         return 0;
51 }
52 static const struct udevadm_cmd udevadm_version = {
53         .name = "version",
54         .cmd = adm_version,
55 };
56
57 static int adm_help(struct udev *udev, int argc, char *argv[]);
58 static const struct udevadm_cmd udevadm_help = {
59         .name = "help",
60         .cmd = adm_help,
61 };
62
63 static const struct udevadm_cmd *udevadm_cmds[] = {
64         &udevadm_info,
65         &udevadm_trigger,
66         &udevadm_settle,
67         &udevadm_control,
68         &udevadm_monitor,
69         &udevadm_test,
70         &udevadm_version,
71         &udevadm_help,
72 };
73
74 static int adm_help(struct udev *udev, int argc, char *argv[])
75 {
76         unsigned int i;
77
78         printf("Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
79         for (i = 0; i < ARRAY_SIZE(udevadm_cmds); i++)
80                 if (udevadm_cmds[i]->help != NULL)
81                         printf("  %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help);
82         printf("\n");
83         return 0;
84 }
85
86 static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[])
87 {
88         if (cmd->debug) {
89                 debug = true;
90                 if (udev_get_log_priority(udev) < LOG_INFO)
91                         udev_set_log_priority(udev, LOG_INFO);
92         }
93         info(udev, "calling: %s\n", cmd->name);
94         return cmd->cmd(udev, argc, argv);
95 }
96
97 int main(int argc, char *argv[])
98 {
99         struct udev *udev;
100         static const struct option options[] = {
101                 { "debug", no_argument, NULL, 'd' },
102                 { "help", no_argument, NULL, 'h' },
103                 { "version", no_argument, NULL, 'V' },
104                 {}
105         };
106         const char *command;
107         unsigned int i;
108         int rc = 1;
109
110         udev = udev_new();
111         if (udev == NULL)
112                 goto out;
113
114         udev_log_init("udevadm");
115         udev_set_log_fn(udev, log_fn);
116         udev_selinux_init(udev);
117
118         for (;;) {
119                 int option;
120
121                 option = getopt_long(argc, argv, "+dhV", options, NULL);
122                 if (option == -1)
123                         break;
124
125                 switch (option) {
126                 case 'd':
127                         debug = true;
128                         if (udev_get_log_priority(udev) < LOG_INFO)
129                                 udev_set_log_priority(udev, LOG_INFO);
130                         break;
131                 case 'h':
132                         rc = adm_help(udev, argc, argv);
133                         goto out;
134                 case 'V':
135                         rc = adm_version(udev, argc, argv);
136                         goto out;
137                 default:
138                         goto out;
139                 }
140         }
141         command = argv[optind];
142
143         info(udev, "runtime dir '%s'\n", udev_get_run_path(udev));
144
145         if (command != NULL)
146                 for (i = 0; i < ARRAY_SIZE(udevadm_cmds); i++) {
147                         if (strcmp(udevadm_cmds[i]->name, command) == 0) {
148                                 argc -= optind;
149                                 argv += optind;
150                                 optind = 0;
151                                 rc = run_command(udev, udevadm_cmds[i], argc, argv);
152                                 goto out;
153                         }
154                 }
155
156         fprintf(stderr, "missing or unknown command\n\n");
157         adm_help(udev, argc, argv);
158         rc = 2;
159 out:
160         udev_selinux_exit(udev);
161         udev_unref(udev);
162         udev_log_close();
163         return rc;
164 }