| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "creds-util.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "format-table.h" |
| 8 |
#include "help-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "options.h" |
| 11 |
#include "parse-argument.h" |
| 12 |
#include "parse-util.h" |
| 13 |
#include "static-destruct.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "syslog-util.h" |
| 16 |
#include "time-util.h" |
| 17 |
#include "udev-ctrl.h" |
| 18 |
#include "udev-varlink.h" |
| 19 |
#include "udevadm.h" |
| 20 |
#include "varlink-util.h" |
| 21 |
#include "virt.h" |
| 22 |
|
| 23 |
static char **arg_env = NULL; |
| 24 |
static usec_t arg_timeout = 60 * USEC_PER_SEC; |
| 25 |
static bool arg_ping = false; |
| 26 |
static bool arg_reload = false; |
| 27 |
static bool arg_exit = false; |
| 28 |
static int arg_max_children = -1; |
| 29 |
static int arg_log_level = -1; |
| 30 |
static int arg_start_exec_queue = -1; |
| 31 |
static int arg_trace = -1; |
| 32 |
static bool arg_revert = false; |
| 33 |
static bool arg_load_credentials = false; |
| 34 |
|
| 35 |
STATIC_DESTRUCTOR_REGISTER(arg_env, strv_freep); |
| 36 |
|
| 37 |
static bool arg_has_control_commands(void) { |
| 38 |
return |
| 39 |
arg_exit || |
| 40 |
arg_log_level >= 0 || |
| 41 |
arg_start_exec_queue >= 0 || |
| 42 |
arg_reload || |
| 43 |
!strv_isempty(arg_env) || |
| 44 |
arg_max_children >= 0 || |
| 45 |
arg_ping || |
| 46 |
arg_trace >= 0 || |
| 47 |
arg_revert; |
| 48 |
} |
| 49 |
|
| 50 |
static int help(void) { |
| 51 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 52 |
int r; |
| 53 |
|
| 54 |
r = option_parser_get_help_table_ns("udevadm-control", &options); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
help_cmdline("control OPTION"); |
| 59 |
help_abstract("Control the udev daemon."); |
| 60 |
help_section("Options"); |
| 61 |
r = table_print_or_warn(options); |
| 62 |
if (r < 0) |
| 63 |
return r; |
| 64 |
|
| 65 |
help_man_page_reference("udevadm", "8"); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
static int parse_argv(int argc, char *argv[]) { |
| 70 |
int r; |
| 71 |
|
| 72 |
assert(argc >= 0); |
| 73 |
assert(argv); |
| 74 |
|
| 75 |
OptionParser opts = { argc, argv, .namespace = "udevadm-control" }; |
| 76 |
|
| 77 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 78 |
switch (c) { |
| 79 |
|
| 80 |
OPTION_NAMESPACE("udevadm-control"): {} |
| 81 |
|
| 82 |
OPTION_COMMON_HELP: |
| 83 |
return help(); |
| 84 |
|
| 85 |
OPTION('V', "version", NULL, "Show package version"): |
| 86 |
return print_version(); |
| 87 |
|
| 88 |
OPTION('e', "exit", NULL, "Instruct the daemon to cleanup and exit"): |
| 89 |
arg_exit = true; |
| 90 |
break; |
| 91 |
|
| 92 |
OPTION_LONG("log-priority", "LEVEL", NULL): {} /* backward compat alias for --log-level */ |
| 93 |
OPTION('l', "log-level", "LEVEL", "Set the udev log level for the daemon"): |
| 94 |
arg_log_level = log_level_from_string(opts.arg); |
| 95 |
if (arg_log_level < 0) |
| 96 |
return log_error_errno(arg_log_level, "Failed to parse log level '%s': %m", opts.arg); |
| 97 |
break; |
| 98 |
|
| 99 |
OPTION('s', "stop-exec-queue", NULL, "Do not execute events, queue only"): |
| 100 |
arg_start_exec_queue = false; |
| 101 |
break; |
| 102 |
|
| 103 |
OPTION('S', "start-exec-queue", NULL, "Execute events, flush queue"): |
| 104 |
arg_start_exec_queue = true; |
| 105 |
break; |
| 106 |
|
| 107 |
OPTION_LONG("reload-rules", NULL, NULL): {} /* hidden alias for -R */ |
| 108 |
OPTION('R', "reload", NULL, "Reload rules and databases"): |
| 109 |
arg_reload = true; |
| 110 |
break; |
| 111 |
|
| 112 |
OPTION_LONG("env", "KEY=VALUE", NULL): {} /* hidden alias for -p */ |
| 113 |
OPTION('p', "property", "KEY=VALUE", "Set a global property for all events"): |
| 114 |
if (!strchr(opts.arg, '=')) |
| 115 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "expect <KEY>=<value> instead of '%s'", opts.arg); |
| 116 |
|
| 117 |
r = strv_extend(&arg_env, opts.arg); |
| 118 |
if (r < 0) |
| 119 |
return log_error_errno(r, "Failed to extend environment: %m"); |
| 120 |
|
| 121 |
break; |
| 122 |
|
| 123 |
OPTION('m', "children-max", "N", "Maximum number of children"): { |
| 124 |
unsigned i; |
| 125 |
r = safe_atou(opts.arg, &i); |
| 126 |
if (r < 0) |
| 127 |
return log_error_errno(r, "Failed to parse maximum number of children '%s': %m", opts.arg); |
| 128 |
arg_max_children = i; |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
OPTION_LONG("ping", NULL, "Wait for udev to respond to a ping message"): |
| 133 |
arg_ping = true; |
| 134 |
break; |
| 135 |
|
| 136 |
OPTION_LONG("trace", "BOOL", "Enable/disable trace logging"): |
| 137 |
r = parse_boolean_argument("--trace=", opts.arg, NULL); |
| 138 |
if (r < 0) |
| 139 |
return r; |
| 140 |
|
| 141 |
arg_trace = r; |
| 142 |
break; |
| 143 |
|
| 144 |
OPTION_LONG("revert", NULL, "Revert previously set configurations"): |
| 145 |
arg_revert = true; |
| 146 |
break; |
| 147 |
|
| 148 |
OPTION('t', "timeout", "SECONDS", "Maximum time to block for a reply"): |
| 149 |
r = parse_sec(opts.arg, &arg_timeout); |
| 150 |
if (r < 0) |
| 151 |
return log_error_errno(r, "Failed to parse timeout value '%s': %m", opts.arg); |
| 152 |
break; |
| 153 |
|
| 154 |
OPTION_LONG("load-credentials", NULL, "Load udev rules from credentials"): |
| 155 |
arg_load_credentials = true; |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
if (!arg_has_control_commands() && !arg_load_credentials) |
| 160 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 161 |
"No control command option is specified."); |
| 162 |
|
| 163 |
if (option_parser_get_n_args(&opts) > 0) |
| 164 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 165 |
"This subprogram takes no positional arguments."); |
| 166 |
|
| 167 |
return 1; |
| 168 |
} |
| 169 |
|
| 170 |
static int send_control_commands_via_ctrl(void) { |
| 171 |
_cleanup_(udev_ctrl_unrefp) UdevCtrl *uctrl = NULL; |
| 172 |
int r; |
| 173 |
|
| 174 |
r = udev_ctrl_new(&uctrl); |
| 175 |
if (r < 0) |
| 176 |
return log_error_errno(r, "Failed to initialize udev control: %m"); |
| 177 |
|
| 178 |
if (arg_exit) { |
| 179 |
r = udev_ctrl_send_exit(uctrl); |
| 180 |
if (r < 0) |
| 181 |
return log_error_errno(r, "Failed to send exit request: %m"); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
if (arg_log_level >= 0) { |
| 186 |
r = udev_ctrl_send_set_log_level(uctrl, arg_log_level); |
| 187 |
if (r < 0) |
| 188 |
return log_error_errno(r, "Failed to send request to set log level: %m"); |
| 189 |
} |
| 190 |
|
| 191 |
if (arg_start_exec_queue == false) { |
| 192 |
r = udev_ctrl_send_stop_exec_queue(uctrl); |
| 193 |
if (r < 0) |
| 194 |
return log_error_errno(r, "Failed to send request to stop exec queue: %m"); |
| 195 |
} |
| 196 |
|
| 197 |
if (arg_start_exec_queue == true) { |
| 198 |
r = udev_ctrl_send_start_exec_queue(uctrl); |
| 199 |
if (r < 0) |
| 200 |
return log_error_errno(r, "Failed to send request to start exec queue: %m"); |
| 201 |
} |
| 202 |
|
| 203 |
if (arg_reload) { |
| 204 |
r = udev_ctrl_send_reload(uctrl); |
| 205 |
if (r < 0) |
| 206 |
return log_error_errno(r, "Failed to send reload request: %m"); |
| 207 |
} |
| 208 |
|
| 209 |
STRV_FOREACH(env, arg_env) { |
| 210 |
r = udev_ctrl_send_set_env(uctrl, *env); |
| 211 |
if (r < 0) |
| 212 |
return log_error_errno(r, "Failed to send request to update environment: %m"); |
| 213 |
} |
| 214 |
|
| 215 |
if (arg_max_children >= 0) { |
| 216 |
r = udev_ctrl_send_set_children_max(uctrl, arg_max_children); |
| 217 |
if (r < 0) |
| 218 |
return log_error_errno(r, "Failed to send request to set number of children: %m"); |
| 219 |
} |
| 220 |
|
| 221 |
if (arg_ping) { |
| 222 |
r = udev_ctrl_send_ping(uctrl); |
| 223 |
if (r < 0) |
| 224 |
return log_error_errno(r, "Failed to send a ping message: %m"); |
| 225 |
} |
| 226 |
|
| 227 |
r = udev_ctrl_wait(uctrl, arg_timeout); |
| 228 |
if (r < 0) |
| 229 |
return log_error_errno(r, "Failed to wait for daemon to reply: %m"); |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
static int send_control_commands(void) { |
| 235 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL; |
| 236 |
int r; |
| 237 |
|
| 238 |
r = udev_varlink_connect(&link, arg_timeout); |
| 239 |
if (ERRNO_IS_NEG_DISCONNECT(r) || r == -ENOENT) { |
| 240 |
log_debug_errno(r, "Failed to connect to udev via varlink, falling back to use legacy control socket, ignoring: %m"); |
| 241 |
return send_control_commands_via_ctrl(); |
| 242 |
} |
| 243 |
if (r < 0) |
| 244 |
return log_error_errno(r, "Failed to connect to udev via varlink: %m"); |
| 245 |
|
| 246 |
if (arg_exit) |
| 247 |
return varlink_call_and_log(link, "io.systemd.Udev.Exit", /* parameters= */ NULL, /* reply= */ NULL); |
| 248 |
|
| 249 |
if (arg_revert) { |
| 250 |
r = varlink_call_and_log(link, "io.systemd.Udev.Revert", /* parameters= */ NULL, /* reply= */ NULL); |
| 251 |
if (r < 0) |
| 252 |
return r; |
| 253 |
} |
| 254 |
|
| 255 |
if (arg_log_level >= 0) { |
| 256 |
r = varlink_callbo_and_log(link, "io.systemd.service.SetLogLevel", /* reply= */ NULL, |
| 257 |
SD_JSON_BUILD_PAIR_INTEGER("level", arg_log_level)); |
| 258 |
if (r < 0) |
| 259 |
return r; |
| 260 |
} |
| 261 |
|
| 262 |
if (arg_start_exec_queue >= 0) { |
| 263 |
r = varlink_call_and_log(link, arg_start_exec_queue ? "io.systemd.Udev.StartExecQueue" : "io.systemd.Udev.StopExecQueue", |
| 264 |
/* parameters= */ NULL, /* reply= */ NULL); |
| 265 |
if (r < 0) |
| 266 |
return r; |
| 267 |
} |
| 268 |
|
| 269 |
if (arg_reload) { |
| 270 |
r = varlink_call_and_log(link, "io.systemd.service.Reload", /* parameters= */ NULL, /* reply= */ NULL); |
| 271 |
if (r < 0) |
| 272 |
return r; |
| 273 |
} |
| 274 |
|
| 275 |
if (!strv_isempty(arg_env)) { |
| 276 |
r = varlink_callbo_and_log(link, "io.systemd.Udev.SetEnvironment", /* reply= */ NULL, |
| 277 |
SD_JSON_BUILD_PAIR_STRV("assignments", arg_env)); |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
} |
| 281 |
|
| 282 |
if (arg_max_children >= 0) { |
| 283 |
r = varlink_callbo_and_log(link, "io.systemd.Udev.SetChildrenMax", /* reply= */ NULL, |
| 284 |
SD_JSON_BUILD_PAIR_UNSIGNED("number", arg_max_children)); |
| 285 |
if (r < 0) |
| 286 |
return r; |
| 287 |
} |
| 288 |
|
| 289 |
if (arg_ping) { |
| 290 |
r = varlink_call_and_log(link, "io.systemd.service.Ping", /* parameters= */ NULL, /* reply= */ NULL); |
| 291 |
if (r < 0) |
| 292 |
return r; |
| 293 |
} |
| 294 |
|
| 295 |
if (arg_trace >= 0) { |
| 296 |
r = varlink_callbo_and_log(link, "io.systemd.Udev.SetTrace", /* reply= */ NULL, |
| 297 |
SD_JSON_BUILD_PAIR_BOOLEAN("enable", arg_trace)); |
| 298 |
if (r < 0) |
| 299 |
return r; |
| 300 |
} |
| 301 |
|
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
int verb_control_main(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 306 |
int r; |
| 307 |
|
| 308 |
if (running_in_chroot() > 0) { |
| 309 |
log_info("Running in chroot, ignoring request."); |
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
r = parse_argv(argc, argv); |
| 314 |
if (r <= 0) |
| 315 |
return r; |
| 316 |
|
| 317 |
if (arg_load_credentials) { |
| 318 |
static const PickUpCredential table[] = { |
| 319 |
{ "udev.conf.", "/run/udev/udev.conf.d/", ".conf" }, |
| 320 |
{ "udev.rules.", "/run/udev/rules.d/", ".rules" }, |
| 321 |
}; |
| 322 |
r = pick_up_credentials(table, ELEMENTSOF(table)); |
| 323 |
if (r < 0) |
| 324 |
return r; |
| 325 |
} |
| 326 |
|
| 327 |
if (arg_has_control_commands()) { |
| 328 |
r = send_control_commands(); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
} |
| 332 |
|
| 333 |
return 0; |
| 334 |
} |