]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-control.c
udevadm: add --wait-daemon option to 'trigger' command
[thirdparty/systemd.git] / src / udev / udevadm-control.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <errno.h>
15 #include <getopt.h>
16 #include <stddef.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include "parse-util.h"
23 #include "process-util.h"
24 #include "syslog-util.h"
25 #include "time-util.h"
26 #include "udevadm.h"
27 #include "udev-ctrl.h"
28 #include "util.h"
29 #include "virt.h"
30
31 static int help(void) {
32 printf("%s control OPTION\n\n"
33 "Control the udev daemon.\n\n"
34 " -h --help Show this help\n"
35 " -V --version Show package version\n"
36 " -e --exit Instruct the daemon to cleanup and exit\n"
37 " -l --log-priority=LEVEL Set the udev log level for the daemon\n"
38 " -s --stop-exec-queue Do not execute events, queue only\n"
39 " -S --start-exec-queue Execute events, flush queue\n"
40 " -R --reload Reload rules and databases\n"
41 " -p --property=KEY=VALUE Set a global property for all events\n"
42 " -m --children-max=N Maximum number of children\n"
43 " --ping Wait for udev to respond to a ping message\n"
44 " -t --timeout=SECONDS Maximum time to block for a reply\n"
45 , program_invocation_short_name);
46
47 return 0;
48 }
49
50 int control_main(int argc, char *argv[], void *userdata) {
51 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
52 usec_t timeout = 60 * USEC_PER_SEC;
53 int c, r;
54
55 enum {
56 ARG_PING = 0x100,
57 };
58
59 static const struct option options[] = {
60 { "exit", no_argument, NULL, 'e' },
61 { "log-priority", required_argument, NULL, 'l' },
62 { "stop-exec-queue", no_argument, NULL, 's' },
63 { "start-exec-queue", no_argument, NULL, 'S' },
64 { "reload", no_argument, NULL, 'R' },
65 { "reload-rules", no_argument, NULL, 'R' }, /* alias for -R */
66 { "property", required_argument, NULL, 'p' },
67 { "env", required_argument, NULL, 'p' }, /* alias for -p */
68 { "children-max", required_argument, NULL, 'm' },
69 { "ping", no_argument, NULL, ARG_PING },
70 { "timeout", required_argument, NULL, 't' },
71 { "version", no_argument, NULL, 'V' },
72 { "help", no_argument, NULL, 'h' },
73 {}
74 };
75
76 r = must_be_root();
77 if (r < 0)
78 return r;
79
80 if (running_in_chroot() > 0) {
81 log_info("Running in chroot, ignoring request.");
82 return 0;
83 }
84
85 if (argc <= 1)
86 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
87 "This command expects one or more options.");
88
89 uctrl = udev_ctrl_new();
90 if (!uctrl)
91 return log_oom();
92
93 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
94 switch (c) {
95 case 'e':
96 r = udev_ctrl_send_exit(uctrl, timeout);
97 if (r < 0)
98 return r;
99 break;
100 case 'l':
101 r = log_level_from_string(optarg);
102 if (r < 0)
103 return log_error_errno(r, "Failed to parse log priority '%s': %m", optarg);
104
105 r = udev_ctrl_send_set_log_level(uctrl, r, timeout);
106 if (r < 0)
107 return r;
108 break;
109 case 's':
110 r = udev_ctrl_send_stop_exec_queue(uctrl, timeout);
111 if (r < 0)
112 return r;
113 break;
114 case 'S':
115 r = udev_ctrl_send_start_exec_queue(uctrl, timeout);
116 if (r < 0)
117 return r;
118 break;
119 case 'R':
120 r = udev_ctrl_send_reload(uctrl, timeout);
121 if (r < 0)
122 return r;
123 break;
124 case 'p':
125 if (!strchr(optarg, '='))
126 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "expect <KEY>=<value> instead of '%s'", optarg);
127
128 r = udev_ctrl_send_set_env(uctrl, optarg, timeout);
129 if (r < 0)
130 return r;
131 break;
132 case 'm': {
133 unsigned i;
134
135 r = safe_atou(optarg, &i);
136 if (r < 0)
137 return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
138
139 r = udev_ctrl_send_set_children_max(uctrl, i, timeout);
140 if (r < 0)
141 return r;
142 break;
143 }
144 case ARG_PING:
145 r = udev_ctrl_send_ping(uctrl, timeout);
146 if (r < 0)
147 return log_error_errno(r, "Failed to connect to udev daemon: %m");
148 break;
149 case 't':
150 r = parse_sec(optarg, &timeout);
151 if (r < 0)
152 return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
153 break;
154 case 'V':
155 return print_version();
156 case 'h':
157 return help();
158 case '?':
159 return -EINVAL;
160 default:
161 assert_not_reached("Unknown option.");
162 }
163
164 if (optind < argc)
165 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
166 "Extraneous argument: %s", argv[optind]);
167
168 return 0;
169 }