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