chiark / gitweb /
remove UDEV_RUN environment variable
[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 int 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                 vfprintf(stderr, format, args);
39                 vsyslog(priority, format, args);
40         }
41 }
42
43 struct command {
44         const char *name;
45         int (*cmd)(struct udev *udev, int argc, char *argv[]);
46         const char *help;
47         int debug;
48 };
49
50 static const struct command cmds[];
51
52 static int version(struct udev *udev, int argc, char *argv[])
53 {
54         printf("%s\n", VERSION);
55         return 0;
56 }
57
58 static int help(struct udev *udev, int argc, char *argv[])
59 {
60         const struct command *cmd;
61
62         printf("Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
63         for (cmd = cmds; cmd->name != NULL; cmd++)
64                 if (cmd->help != NULL)
65                         printf("  %-12s %s\n", cmd->name, cmd->help);
66         printf("\n");
67         return 0;
68 }
69
70 static const struct command cmds[] = {
71         {
72                 .name = "info",
73                 .cmd = udevadm_info,
74                 .help = "query sysfs or the udev database",
75         },
76         {
77                 .name = "trigger",
78                 .cmd = udevadm_trigger,
79                 .help = "request events from the kernel",
80         },
81         {
82                 .name = "settle",
83                 .cmd = udevadm_settle,
84                 .help = "wait for the event queue to finish",
85         },
86         {
87                 .name = "control",
88                 .cmd = udevadm_control,
89                 .help = "control the udev daemon",
90         },
91         {
92                 .name = "monitor",
93                 .cmd = udevadm_monitor,
94                 .help = "listen to kernel and udev events",
95         },
96         {
97                 .name = "test",
98                 .cmd = udevadm_test,
99                 .help = "simulation run",
100                 .debug = 1,
101         },
102         {
103                 .name = "version",
104                 .cmd = version,
105         },
106         {
107                 .name = "help",
108                 .cmd = help,
109         },
110         {}
111 };
112
113 static int run_command(struct udev *udev, const struct command *cmd, int argc, char *argv[])
114 {
115         if (cmd->debug) {
116                 debug = 1;
117                 if (udev_get_log_priority(udev) < LOG_INFO)
118                         udev_set_log_priority(udev, LOG_INFO);
119         }
120         info(udev, "calling: %s\n", cmd->name);
121         return cmd->cmd(udev, argc, argv);
122 }
123
124 int main(int argc, char *argv[])
125 {
126         struct udev *udev;
127         static const struct option options[] = {
128                 { "debug", no_argument, NULL, 'd' },
129                 { "help", no_argument, NULL, 'h' },
130                 { "version", no_argument, NULL, 'V' },
131                 {}
132         };
133         const char *command;
134         int i;
135         int rc = 1;
136
137         udev = udev_new();
138         if (udev == NULL)
139                 goto out;
140
141         udev_log_init("udevadm");
142         udev_set_log_fn(udev, log_fn);
143         udev_selinux_init(udev);
144
145         while (1) {
146                 int option;
147
148                 option = getopt_long(argc, argv, "+dhV", options, NULL);
149                 if (option == -1)
150                         break;
151
152                 switch (option) {
153                 case 'd':
154                         debug = 1;
155                         if (udev_get_log_priority(udev) < LOG_INFO)
156                                 udev_set_log_priority(udev, LOG_INFO);
157                         break;
158                 case 'h':
159                         rc = help(udev, argc, argv);
160                         goto out;
161                 case 'V':
162                         rc = version(udev, argc, argv);
163                         goto out;
164                 default:
165                         goto out;
166                 }
167         }
168         command = argv[optind];
169
170         if (command != NULL)
171                 for (i = 0; cmds[i].cmd != NULL; i++) {
172                         if (strcmp(cmds[i].name, command) == 0) {
173                                 argc -= optind;
174                                 argv += optind;
175                                 optind = 0;
176                                 rc = run_command(udev, &cmds[i], argc, argv);
177                                 goto out;
178                         }
179                 }
180
181         fprintf(stderr, "missing or unknown command\n\n");
182         help(udev, argc, argv);
183         rc = 2;
184 out:
185         udev_selinux_exit(udev);
186         udev_unref(udev);
187         udev_log_close();
188         return rc;
189 }