chiark / gitweb /
autogen.sh: add debug
[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[]);
53         const char *help;
54         int debug;
55 };
56
57 static const struct command cmds[];
58
59 static int version(int argc, char *argv[])
60 {
61         printf("%s\n", VERSION);
62         return 0;
63 }
64
65 static int help(int argc, char *argv[])
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 = udevadm_info,
80                 .help = "query sysfs or the udev database",
81         },
82         {
83                 .name = "trigger",
84                 .cmd = udevadm_trigger,
85                 .help = "request events from the kernel",
86         },
87         {
88                 .name = "settle",
89                 .cmd = udevadm_settle, "",
90                 .help = "wait for the event queue to finish",
91         },
92         {
93                 .name = "control",
94                 .cmd = udevadm_control,
95                 .help = "control the udev daemon",
96         },
97         {
98                 .name = "monitor",
99                 .cmd = udevadm_monitor,
100                 .help = "listen to kernel and udev events",
101         },
102         {
103                 .name = "test",
104                 .cmd = udevadm_test,
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[])
122 {
123         const char *command = argv[1];
124         int i;
125         const char *pos;
126         int rc;
127
128         logging_init("udevadm");
129         udev_config_init();
130         sysfs_init();
131
132         /* find command */
133         if (command != NULL)
134                 for (i = 0; cmds[i].cmd != NULL; i++) {
135                         if (strcmp(cmds[i].name, command) == 0) {
136                                 debug = cmds[i].debug;
137                                 rc = cmds[i].cmd(argc-1, &argv[1]);
138                                 goto out;
139                         }
140                 }
141
142         /* try to find compat link, will be removed in a future release */
143         command = argv[0];
144         pos = strrchr(command, '/');
145         if (pos != NULL)
146                 command = &pos[1];
147
148         /* the trailing part of the binary or link name is the command */
149         if (strncmp(command, "udev", 4) == 0)
150                 command = &command[4];
151
152         for (i = 0; cmds[i].cmd != NULL; i++) {
153                 if (strcmp(cmds[i].name, command) == 0) {
154                         char path[128];
155                         char prog[512];
156                         ssize_t len;
157
158                         snprintf(path, sizeof(path), "/proc/%lu/exe", (unsigned long) getppid());
159                         len = readlink(path, prog, sizeof(prog));
160                         if (len > 0) {
161                                 prog[len] = '\0';
162                                 fprintf(stderr, "the program '%s' called '%s', it should use 'udevadm %s <options>', "
163                                        "this will stop working in a future release\n", prog, argv[0], command);
164                                 info("the program '%s' called '%s', it should use 'udevadm %s <options>', "
165                                      "this will stop working in a future release\n", prog, argv[0], command);
166                         }
167                         debug = cmds[i].debug;
168                         rc = cmds[i].cmd(argc, argv);
169                         goto out;
170                 }
171         }
172
173         fprintf(stderr, "unknown command, try help\n\n");
174         rc = 2;
175 out:
176         sysfs_cleanup();
177         logging_close();
178         return rc;
179 }