]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm.c
Merge pull request #9832 from yuwata/fix-9831
[thirdparty/systemd.git] / src / udev / udevadm.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stddef.h>
6 #include <stdio.h>
7
8 #include "selinux-util.h"
9 #include "string-util.h"
10 #include "terminal-util.h"
11 #include "udev-util.h"
12 #include "udev.h"
13
14 static int adm_version(struct udev *udev, int argc, char *argv[]) {
15 printf("%s\n", PACKAGE_VERSION);
16 return 0;
17 }
18
19 static const struct udevadm_cmd udevadm_version = {
20 .name = "version",
21 .cmd = adm_version,
22 };
23
24 static int adm_help(struct udev *udev, int argc, char *argv[]);
25
26 static const struct udevadm_cmd udevadm_help = {
27 .name = "help",
28 .cmd = adm_help,
29 };
30
31 static const struct udevadm_cmd *udevadm_cmds[] = {
32 &udevadm_info,
33 &udevadm_trigger,
34 &udevadm_settle,
35 &udevadm_control,
36 &udevadm_monitor,
37 &udevadm_hwdb,
38 &udevadm_test,
39 &udevadm_test_builtin,
40 &udevadm_version,
41 &udevadm_help,
42 };
43
44 static int adm_help(struct udev *udev, int argc, char *argv[]) {
45 _cleanup_free_ char *link = NULL;
46 size_t i;
47 int r;
48
49 r = terminal_urlify_man("udevadm", "8", &link);
50 if (r < 0)
51 return log_oom();
52
53 printf("%s [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n\n"
54 "Send control commands or test the device manager.\n\n"
55 "Commands:\n"
56 , program_invocation_short_name);
57
58 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
59 if (udevadm_cmds[i]->help != NULL)
60 printf(" %-12s %s\n", udevadm_cmds[i]->name, udevadm_cmds[i]->help);
61
62 printf("\nSee the %s for details.\n", link);
63 return 0;
64 }
65
66 static int run_command(struct udev *udev, const struct udevadm_cmd *cmd, int argc, char *argv[]) {
67 if (cmd->debug)
68 log_set_max_level(LOG_DEBUG);
69 log_debug("calling: %s", cmd->name);
70 return cmd->cmd(udev, argc, argv);
71 }
72
73 int main(int argc, char *argv[]) {
74 struct udev *udev;
75 static const struct option options[] = {
76 { "debug", no_argument, NULL, 'd' },
77 { "help", no_argument, NULL, 'h' },
78 { "version", no_argument, NULL, 'V' },
79 {}
80 };
81 const char *command;
82 unsigned int i;
83 int rc = 1, c;
84
85 udev_parse_config();
86 log_parse_environment();
87 log_open();
88
89 mac_selinux_init();
90
91 udev = udev_new();
92 if (udev == NULL)
93 goto out;
94
95 while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
96 switch (c) {
97
98 case 'd':
99 log_set_max_level(LOG_DEBUG);
100 break;
101
102 case 'h':
103 rc = adm_help(udev, argc, argv);
104 goto out;
105
106 case 'V':
107 rc = adm_version(udev, argc, argv);
108 goto out;
109
110 default:
111 goto out;
112 }
113
114 command = argv[optind];
115
116 if (command != NULL)
117 for (i = 0; i < ELEMENTSOF(udevadm_cmds); i++)
118 if (streq(udevadm_cmds[i]->name, command)) {
119 argc -= optind;
120 argv += optind;
121 /* we need '0' here to reset the internal state */
122 optind = 0;
123 rc = run_command(udev, udevadm_cmds[i], argc, argv);
124 goto out;
125 }
126
127 fprintf(stderr, "%s: missing or unknown command\n", program_invocation_short_name);
128 rc = 2;
129 out:
130 mac_selinux_finish();
131 udev_unref(udev);
132 log_close();
133 return rc;
134 }