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