chiark / gitweb /
volume_id: md - add metadata minor version
[elogind.git] / udevadm.c
1 /*
2  * Copyright (C) 2007 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
7  * 
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.
12  * 
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.
16  *
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
29 static int verbose;
30
31 #ifdef USE_LOG
32 void log_message(int priority, const char *format, ...)
33 {
34         va_list args;
35
36         if (priority > udev_log_priority)
37                 return;
38
39         va_start(args, format);
40         if (verbose) {
41                 vprintf(format, args);
42                 printf("\n");
43         } else
44                 vsyslog(priority, format, args);
45         va_end(args);
46 }
47 #endif
48
49 struct command {
50         const char *name;
51         int (*cmd)(int argc, char *argv[], char *envp[]);
52         const char *help;
53         int verbose;
54 };
55
56 static const struct command cmds[];
57
58 static int version(int argc, char *argv[], char *envp[])
59 {
60         printf("%s\n", UDEV_VERSION);
61         return 0;
62 }
63
64 static int help(int argc, char *argv[], char *envp[])
65 {
66         const struct command *cmd;
67
68         printf("Usage: udevadm COMMAND [OPTIONS]\n");
69         for (cmd = cmds; cmd->name != NULL; cmd++)
70                 printf("  %-12s %s\n", cmd->name, cmd->help);
71         printf("\n");
72         return 0;
73 }
74
75 static const struct command cmds[] = {
76         {
77                 .name = "info",
78                 .cmd = udevinfo,
79                 .help = "query sysfs or the udev database",
80         },
81         {
82                 .name = "trigger",
83                 .cmd = udevtrigger,
84                 .help = "request events from the kernel",
85         },
86         {
87                 .name = "settle",
88                 .cmd = udevsettle, "",
89                 .help = "wait for the event queue to finish",
90         },
91         {
92                 .name = "control",
93                 .cmd = udevcontrol,
94                 .help = "control the udev daemon",
95         },
96         {
97                 .name = "monitor",
98                 .cmd = udevmonitor,
99                 .help = "listen to kernel and udev events",
100         },
101         {
102                 .name = "test",
103                 .cmd = udevtest,
104                 .help = "simulation run",
105                 .verbose = 1,
106         },
107         {
108                 .name = "version",
109                 .cmd = version,
110                 .help = "print the version number",
111         },
112         {
113                 .name = "help",
114                 .cmd = help,
115                 .help = "print this help text",
116         },
117         {}
118 };
119
120 int main(int argc, char *argv[], char *envp[])
121 {
122         const char *command;
123         const char *pos;
124         const struct command *cmd;
125         int rc;
126
127         /* get binary or symlink name */
128         pos = strrchr(argv[0], '/');
129         if (pos != NULL)
130                 command = &pos[1];
131         else
132                 command = argv[0];
133
134         /* the trailing part of the binary or symlink name is the command */
135         if (strncmp(command, "udev", 4) == 0)
136                 command = &command[4];
137
138         if (command == NULL || command[0] == '\0')
139                 goto err_unknown;
140
141         /* udevadm itself needs to strip its name from the passed options */
142         if (strcmp(command, "adm") == 0) {
143                 command = argv[1];
144                 argv++;
145                 argc--;
146         }
147
148         if (command == NULL)
149                 goto err_unknown;
150
151         /* allow command to be specified as an option */
152         if (strncmp(command, "--", 2) == 0)
153                 command += 2;
154
155         /* find and execute command */
156         for (cmd = cmds; cmd->name != NULL; cmd++) {
157                 if (strcmp(cmd->name, command) == 0) {
158                         verbose = cmd->verbose;
159                         rc = cmd->cmd(argc, argv, envp);
160                         goto out;
161                 }
162         }
163
164 err_unknown:
165         fprintf(stderr, "unknown command, try help\n\n");
166         rc = 2;
167 out:
168         return rc;
169 }