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