]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm.c
systemd-hwdb: introduce new tool
[thirdparty/systemd.git] / src / udev / udevadm.c
CommitLineData
601185b4 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
225cb03b 2/*
1298001e 3 * Copyright (C) 2007-2012 Kay Sievers <kay@vrfy.org>
225cb03b 4 *
55e9959b
KS
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.
225cb03b 9 *
55e9959b
KS
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/>.
225cb03b
KS
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
9ec6e95b 29static int adm_version(struct udev *udev, int argc, char *argv[]) {
912541b0
KS
30 printf("%s\n", VERSION);
31 return 0;
225cb03b 32}
baa30fbc 33
1985c76e 34static const struct udevadm_cmd udevadm_version = {
912541b0
KS
35 .name = "version",
36 .cmd = adm_version,
1985c76e
KS
37};
38
39static int adm_help(struct udev *udev, int argc, char *argv[]);
baa30fbc 40
1985c76e 41static const struct udevadm_cmd udevadm_help = {
912541b0
KS
42 .name = "help",
43 .cmd = adm_help,
1985c76e
KS
44};
45
46static const struct udevadm_cmd *udevadm_cmds[] = {
912541b0
KS
47 &udevadm_info,
48 &udevadm_trigger,
49 &udevadm_settle,
50 &udevadm_control,
51 &udevadm_monitor,
796b06c2 52 &udevadm_hwdb,
912541b0
KS
53 &udevadm_test,
54 &udevadm_test_builtin,
55 &udevadm_version,
56 &udevadm_help,
1985c76e 57};
225cb03b 58
9ec6e95b 59static int adm_help(struct udev *udev, int argc, char *argv[]) {
912541b0
KS
60 unsigned int i;
61
62 fprintf(stderr, "Usage: udevadm [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n");
8fef0ff2 63 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
912541b0
KS
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;
225cb03b
KS
68}
69
9ec6e95b 70static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) {
baa30fbc
KS
71 if (cmd->debug)
72 log_set_max_level(LOG_DEBUG);
9f6445e3 73 log_debug("calling: %s", cmd->name);
912541b0 74 return cmd->cmd(udev, argc, argv);
7d563a17
KS
75}
76
9ec6e95b 77int main(int argc, char *argv[]) {
912541b0
KS
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;
601185b4 87 int rc = 1, c;
912541b0
KS
88
89 udev = udev_new();
90 if (udev == NULL)
91 goto out;
92
baa30fbc 93 log_parse_environment();
4b261568 94 log_open();
cc56fafe 95 mac_selinux_init("/dev");
912541b0 96
601185b4
ZJS
97 while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
98 switch (c) {
912541b0 99
912541b0 100 case 'd':
baa30fbc 101 log_set_max_level(LOG_DEBUG);
912541b0 102 break;
601185b4 103
912541b0
KS
104 case 'h':
105 rc = adm_help(udev, argc, argv);
106 goto out;
601185b4 107
912541b0
KS
108 case 'V':
109 rc = adm_version(udev, argc, argv);
110 goto out;
601185b4 111
912541b0
KS
112 default:
113 goto out;
114 }
601185b4 115
912541b0
KS
116 command = argv[optind];
117
912541b0 118 if (command != NULL)
601185b4 119 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
090be865 120 if (streq(udevadm_cmds[i]->name, command)) {
912541b0
KS
121 argc -= optind;
122 argv += optind;
e5f2783e
KS
123 /* we need '0' here to reset the internal state */
124 optind = 0;
912541b0
KS
125 rc = run_command(udev, udevadm_cmds[i], argc, argv);
126 goto out;
127 }
912541b0 128
92e63a51 129 fprintf(stderr, "%s: missing or unknown command\n", program_invocation_short_name);
912541b0 130 rc = 2;
225cb03b 131out:
cc56fafe 132 mac_selinux_finish();
912541b0 133 udev_unref(udev);
baa30fbc 134 log_close();
912541b0 135 return rc;
225cb03b 136}