chiark / gitweb /
udev: link-config - only set *name on success
[elogind.git] / src / udev / udevadm.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 /*
3  * Copyright (C) 2007-2012 Kay Sievers <kay@vrfy.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 void udev_main_log(struct udev *udev, int priority,
30                    const char *file, int line, const char *fn,
31                    const char *format, va_list args) {
32         log_metav(priority, file, line, fn, format, args);
33 }
34
35 static int adm_version(struct udev *udev, int argc, char *argv[]) {
36         printf("%s\n", VERSION);
37         return 0;
38 }
39
40 static const struct udevadm_cmd udevadm_version = {
41         .name = "version",
42         .cmd = adm_version,
43 };
44
45 static int adm_help(struct udev *udev, int argc, char *argv[]);
46
47 static const struct udevadm_cmd udevadm_help = {
48         .name = "help",
49         .cmd = adm_help,
50 };
51
52 static const struct udevadm_cmd *udevadm_cmds[] = {
53         &udevadm_info,
54         &udevadm_trigger,
55         &udevadm_settle,
56         &udevadm_control,
57         &udevadm_monitor,
58         &udevadm_hwdb,
59         &udevadm_test,
60         &udevadm_test_builtin,
61         &udevadm_version,
62         &udevadm_help,
63 };
64
65 static int adm_help(struct udev *udev, int argc, char *argv[]) {
66         unsigned int i;
67
68         fprintf(stderr, "Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
69         for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
70                 if (udevadm_cmds[i]->help != NULL)
71                         printf("  %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help);
72         fprintf(stderr, "\n");
73         return 0;
74 }
75
76 static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) {
77         if (cmd->debug)
78                 log_set_max_level(LOG_DEBUG);
79         log_debug("calling: %s", cmd->name);
80         return cmd->cmd(udev, argc, argv);
81 }
82
83 int main(int argc, char *argv[]) {
84         struct udev *udev;
85         static const struct option options[] = {
86                 { "debug", no_argument, NULL, 'd' },
87                 { "help", no_argument, NULL, 'h' },
88                 { "version", no_argument, NULL, 'V' },
89                 {}
90         };
91         const char *command;
92         unsigned int i;
93         int rc = 1, c;
94
95         udev = udev_new();
96         if (udev == NULL)
97                 goto out;
98
99         log_parse_environment();
100         log_open();
101         udev_set_log_fn(udev, udev_main_log);
102         label_init("/dev");
103
104         while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
105                 switch (c) {
106
107                 case 'd':
108                         log_set_max_level(LOG_DEBUG);
109                         udev_set_log_priority(udev, LOG_DEBUG);
110                         break;
111
112                 case 'h':
113                         rc = adm_help(udev, argc, argv);
114                         goto out;
115
116                 case 'V':
117                         rc = adm_version(udev, argc, argv);
118                         goto out;
119
120                 default:
121                         goto out;
122                 }
123
124         command = argv[optind];
125
126         if (command != NULL)
127                 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
128                         if (streq(udevadm_cmds[i]->name, command)) {
129                                 argc -= optind;
130                                 argv += optind;
131                                 /* we need '0' here to reset the internal state */
132                                 optind = 0;
133                                 rc = run_command(udev, udevadm_cmds[i], argc, argv);
134                                 goto out;
135                         }
136
137         fprintf(stderr, "%s: missing or unknown command\n", program_invocation_short_name);
138         rc = 2;
139 out:
140         label_finish();
141         udev_unref(udev);
142         log_close();
143         return rc;
144 }