chiark / gitweb /
tmpfiles: add new line type 'v' for creating btrfs subvolumes
[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 "selinux-util.h"
28 #include "udev.h"
29
30 static int adm_version(struct udev *udev, int argc, char *argv[]) {
31         printf("%s\n", VERSION);
32         return 0;
33 }
34
35 static const struct udevadm_cmd udevadm_version = {
36         .name = "version",
37         .cmd = adm_version,
38 };
39
40 static int adm_help(struct udev *udev, int argc, char *argv[]);
41
42 static const struct udevadm_cmd udevadm_help = {
43         .name = "help",
44         .cmd = adm_help,
45 };
46
47 static const struct udevadm_cmd *udevadm_cmds[] = {
48         &udevadm_info,
49         &udevadm_trigger,
50         &udevadm_settle,
51         &udevadm_control,
52         &udevadm_monitor,
53         &udevadm_hwdb,
54         &udevadm_test,
55         &udevadm_test_builtin,
56         &udevadm_version,
57         &udevadm_help,
58 };
59
60 static int adm_help(struct udev *udev, int argc, char *argv[]) {
61         unsigned int i;
62
63         fprintf(stderr, "Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
64         for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
65                 if (udevadm_cmds[i]->help != NULL)
66                         printf("  %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help);
67         fprintf(stderr, "\n");
68         return 0;
69 }
70
71 static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) {
72         if (cmd->debug)
73                 log_set_max_level(LOG_DEBUG);
74         log_debug("calling: %s", cmd->name);
75         return cmd->cmd(udev, argc, argv);
76 }
77
78 int main(int argc, char *argv[]) {
79         struct udev *udev;
80         static const struct option options[] = {
81                 { "debug", no_argument, NULL, 'd' },
82                 { "help", no_argument, NULL, 'h' },
83                 { "version", no_argument, NULL, 'V' },
84                 {}
85         };
86         const char *command;
87         unsigned int i;
88         int rc = 1, c;
89
90         udev = udev_new();
91         if (udev == NULL)
92                 goto out;
93
94         log_parse_environment();
95         log_open();
96         mac_selinux_init("/dev");
97
98         while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
99                 switch (c) {
100
101                 case 'd':
102                         log_set_max_level(LOG_DEBUG);
103                         break;
104
105                 case 'h':
106                         rc = adm_help(udev, argc, argv);
107                         goto out;
108
109                 case 'V':
110                         rc = adm_version(udev, argc, argv);
111                         goto out;
112
113                 default:
114                         goto out;
115                 }
116
117         command = argv[optind];
118
119         if (command != NULL)
120                 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
121                         if (streq(udevadm_cmds[i]->name, command)) {
122                                 argc -= optind;
123                                 argv += optind;
124                                 /* we need '0' here to reset the internal state */
125                                 optind = 0;
126                                 rc = run_command(udev, udevadm_cmds[i], argc, argv);
127                                 goto out;
128                         }
129
130         fprintf(stderr, "%s: missing or unknown command\n", program_invocation_short_name);
131         rc = 2;
132 out:
133         mac_selinux_finish();
134         udev_unref(udev);
135         log_close();
136         return rc;
137 }