chiark / gitweb /
rules: switch to built-in blkid
[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 bool 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                 va_list args2;
39
40                 va_copy(args2, args);
41                 vfprintf(stderr, format, args2);
42                 va_end(args2);
43                 vsyslog(priority, format, args);
44         }
45 }
46
47 static int adm_version(struct udev *udev, int argc, char *argv[])
48 {
49         printf("%s\n", VERSION);
50         return 0;
51 }
52 static const struct udevadm_cmd udevadm_version = {
53         .name = "version",
54         .cmd = adm_version,
55 };
56
57 static int adm_help(struct udev *udev, int argc, char *argv[]);
58 static const struct udevadm_cmd udevadm_help = {
59         .name = "help",
60         .cmd = adm_help,
61 };
62
63 static const struct udevadm_cmd *udevadm_cmds[] = {
64         &udevadm_info,
65         &udevadm_trigger,
66         &udevadm_settle,
67         &udevadm_control,
68         &udevadm_monitor,
69         &udevadm_test,
70         &udevadm_test_builtin,
71         &udevadm_version,
72         &udevadm_help,
73 };
74
75 static int adm_help(struct udev *udev, int argc, char *argv[])
76 {
77         unsigned int i;
78
79         fprintf(stderr, "Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
80         for (i = 0; i < ARRAY_SIZE(udevadm_cmds); i++)
81                 if (udevadm_cmds[i]->help != NULL)
82                         printf("  %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help);
83         fprintf(stderr, "\n");
84         return 0;
85 }
86
87 static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[])
88 {
89         if (cmd->debug) {
90                 debug = true;
91                 if (udev_get_log_priority(udev) < LOG_INFO)
92                         udev_set_log_priority(udev, LOG_INFO);
93         }
94         info(udev, "calling: %s\n", cmd->name);
95         return cmd->cmd(udev, argc, argv);
96 }
97
98 int main(int argc, char *argv[])
99 {
100         struct udev *udev;
101         static const struct option options[] = {
102                 { "debug", no_argument, NULL, 'd' },
103                 { "help", no_argument, NULL, 'h' },
104                 { "version", no_argument, NULL, 'V' },
105                 {}
106         };
107         const char *command;
108         unsigned int i;
109         int rc = 1;
110
111         udev = udev_new();
112         if (udev == NULL)
113                 goto out;
114
115         udev_log_init("udevadm");
116         udev_set_log_fn(udev, log_fn);
117         udev_selinux_init(udev);
118
119         for (;;) {
120                 int option;
121
122                 option = getopt_long(argc, argv, "+dhV", options, NULL);
123                 if (option == -1)
124                         break;
125
126                 switch (option) {
127                 case 'd':
128                         debug = true;
129                         if (udev_get_log_priority(udev) < LOG_INFO)
130                                 udev_set_log_priority(udev, LOG_INFO);
131                         break;
132                 case 'h':
133                         rc = adm_help(udev, argc, argv);
134                         goto out;
135                 case 'V':
136                         rc = adm_version(udev, argc, argv);
137                         goto out;
138                 default:
139                         goto out;
140                 }
141         }
142         command = argv[optind];
143
144         info(udev, "runtime dir '%s'\n", udev_get_run_path(udev));
145
146         if (command != NULL)
147                 for (i = 0; i < ARRAY_SIZE(udevadm_cmds); i++) {
148                         if (strcmp(udevadm_cmds[i]->name, command) == 0) {
149                                 argc -= optind;
150                                 argv += optind;
151                                 optind = 0;
152                                 rc = run_command(udev, udevadm_cmds[i], argc, argv);
153                                 goto out;
154                         }
155                 }
156
157         fprintf(stderr, "missing or unknown command\n\n");
158         adm_help(udev, argc, argv);
159         rc = 2;
160 out:
161         udev_selinux_exit(udev);
162         udev_unref(udev);
163         udev_log_close();
164         return rc;
165 }