2020-11-09 05:25:50 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2005-06-05 04:41:09 +02:00
|
|
|
/*
|
2008-09-10 02:40:42 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2005-06-05 04:41:09 +02:00
|
|
|
*
|
2008-09-10 02:40:42 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2005-06-05 04:41:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stddef.h>
|
2005-06-05 04:41:09 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-08-24 14:55:19 +02:00
|
|
|
#include "parse-util.h"
|
2018-01-11 00:39:12 +01:00
|
|
|
#include "process-util.h"
|
2018-10-29 08:50:39 +01:00
|
|
|
#include "syslog-util.h"
|
2016-10-11 09:08:04 +02:00
|
|
|
#include "time-util.h"
|
2018-08-21 04:17:01 +02:00
|
|
|
#include "udevadm.h"
|
2018-08-29 09:03:45 +02:00
|
|
|
#include "udev-ctrl.h"
|
|
|
|
#include "util.h"
|
2019-01-07 06:30:55 +01:00
|
|
|
#include "virt.h"
|
2008-09-08 17:59:00 +02:00
|
|
|
|
2018-08-24 14:55:19 +02:00
|
|
|
static int help(void) {
|
2017-12-05 15:30:10 +01:00
|
|
|
printf("%s control OPTION\n\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
"Control the udev daemon.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
2017-12-05 15:30:10 +01:00
|
|
|
" -V --version Show package version\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
" -e --exit Instruct the daemon to cleanup and exit\n"
|
2020-09-21 11:22:12 +02:00
|
|
|
" -l --log-level=LEVEL Set the udev log level for the daemon\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
" -s --stop-exec-queue Do not execute events, queue only\n"
|
|
|
|
" -S --start-exec-queue Execute events, flush queue\n"
|
|
|
|
" -R --reload Reload rules and databases\n"
|
|
|
|
" -p --property=KEY=VALUE Set a global property for all events\n"
|
|
|
|
" -m --children-max=N Maximum number of children\n"
|
2019-01-12 23:44:38 +01:00
|
|
|
" --ping Wait for udev to respond to a ping message\n"
|
2017-12-03 18:36:09 +01:00
|
|
|
" -t --timeout=SECONDS Maximum time to block for a reply\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
, program_invocation_short_name);
|
2018-08-24 14:55:19 +02:00
|
|
|
|
|
|
|
return 0;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
2005-06-05 04:41:09 +02:00
|
|
|
|
2018-08-21 04:17:01 +02:00
|
|
|
int control_main(int argc, char *argv[], void *userdata) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
|
2019-01-08 04:02:08 +01:00
|
|
|
usec_t timeout = 60 * USEC_PER_SEC;
|
2018-08-24 14:55:19 +02:00
|
|
|
int c, r;
|
2008-09-03 21:56:47 +02:00
|
|
|
|
2019-01-12 23:44:38 +01:00
|
|
|
enum {
|
|
|
|
ARG_PING = 0x100,
|
|
|
|
};
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
static const struct option options[] = {
|
2019-01-12 23:44:38 +01:00
|
|
|
{ "exit", no_argument, NULL, 'e' },
|
2020-09-21 11:22:12 +02:00
|
|
|
{ "log-level", required_argument, NULL, 'l' },
|
|
|
|
{ "log-priority", required_argument, NULL, 'l' }, /* for backward compatibility */
|
2019-01-12 23:44:38 +01:00
|
|
|
{ "stop-exec-queue", no_argument, NULL, 's' },
|
|
|
|
{ "start-exec-queue", no_argument, NULL, 'S' },
|
|
|
|
{ "reload", no_argument, NULL, 'R' },
|
|
|
|
{ "reload-rules", no_argument, NULL, 'R' }, /* alias for -R */
|
|
|
|
{ "property", required_argument, NULL, 'p' },
|
|
|
|
{ "env", required_argument, NULL, 'p' }, /* alias for -p */
|
|
|
|
{ "children-max", required_argument, NULL, 'm' },
|
|
|
|
{ "ping", no_argument, NULL, ARG_PING },
|
|
|
|
{ "timeout", required_argument, NULL, 't' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{}
|
|
|
|
};
|
2005-06-05 04:41:09 +02:00
|
|
|
|
2019-01-07 06:30:55 +01:00
|
|
|
if (running_in_chroot() > 0) {
|
|
|
|
log_info("Running in chroot, ignoring request.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-24 14:55:19 +02:00
|
|
|
if (argc <= 1)
|
2019-01-12 00:31:53 +01:00
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"This command expects one or more options.");
|
2008-09-03 21:56:47 +02:00
|
|
|
|
2019-01-24 07:00:06 +01:00
|
|
|
r = udev_ctrl_new(&uctrl);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to initialize udev control: %m");
|
2008-09-03 21:56:47 +02:00
|
|
|
|
2017-12-05 15:30:10 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
|
2013-12-18 03:48:14 +01:00
|
|
|
switch (c) {
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'e':
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_exit(uctrl);
|
|
|
|
if (r == -ENOANO)
|
|
|
|
log_warning("Cannot specify --exit after --exit, ignoring.");
|
|
|
|
else if (r < 0)
|
2019-01-18 16:39:58 +01:00
|
|
|
return log_error_errno(r, "Failed to send exit request: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
2018-10-29 08:50:39 +01:00
|
|
|
case 'l':
|
|
|
|
r = log_level_from_string(optarg);
|
|
|
|
if (r < 0)
|
2020-09-21 11:22:12 +02:00
|
|
|
return log_error_errno(r, "Failed to parse log level '%s': %m", optarg);
|
2018-08-24 14:55:19 +02:00
|
|
|
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_set_log_level(uctrl, r);
|
|
|
|
if (r == -ENOANO)
|
2020-09-21 11:22:12 +02:00
|
|
|
log_warning("Cannot specify --log-level after --exit, ignoring.");
|
2019-01-25 16:33:08 +01:00
|
|
|
else if (r < 0)
|
2019-01-18 16:39:58 +01:00
|
|
|
return log_error_errno(r, "Failed to send request to set log level: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 's':
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_stop_exec_queue(uctrl);
|
|
|
|
if (r == -ENOANO)
|
|
|
|
log_warning("Cannot specify --stop-exec-queue after --exit, ignoring.");
|
|
|
|
else if (r < 0)
|
2019-01-18 16:39:58 +01:00
|
|
|
return log_error_errno(r, "Failed to send request to stop exec queue: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'S':
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_start_exec_queue(uctrl);
|
|
|
|
if (r == -ENOANO)
|
|
|
|
log_warning("Cannot specify --start-exec-queue after --exit, ignoring.");
|
|
|
|
else if (r < 0)
|
2019-01-18 16:39:58 +01:00
|
|
|
return log_error_errno(r, "Failed to send request to start exec queue: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'R':
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_reload(uctrl);
|
|
|
|
if (r == -ENOANO)
|
|
|
|
log_warning("Cannot specify --reload after --exit, ignoring.");
|
|
|
|
else if (r < 0)
|
2019-01-18 16:39:58 +01:00
|
|
|
return log_error_errno(r, "Failed to send reload request: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'p':
|
2019-01-12 00:33:09 +01:00
|
|
|
if (!strchr(optarg, '='))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "expect <KEY>=<value> instead of '%s'", optarg);
|
|
|
|
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_set_env(uctrl, optarg);
|
|
|
|
if (r == -ENOANO)
|
|
|
|
log_warning("Cannot specify --property after --exit, ignoring.");
|
|
|
|
else if (r < 0)
|
2019-01-18 16:39:58 +01:00
|
|
|
return log_error_errno(r, "Failed to send request to update environment: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'm': {
|
2018-08-24 14:55:19 +02:00
|
|
|
unsigned i;
|
2011-04-13 01:17:09 +02:00
|
|
|
|
2018-08-24 14:55:19 +02:00
|
|
|
r = safe_atou(optarg, &i);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
|
|
|
|
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_set_children_max(uctrl, i);
|
|
|
|
if (r == -ENOANO)
|
|
|
|
log_warning("Cannot specify --children-max after --exit, ignoring.");
|
|
|
|
else if (r < 0)
|
2019-01-18 16:39:58 +01:00
|
|
|
return log_error_errno(r, "Failed to send request to set number of children: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-01-12 23:44:38 +01:00
|
|
|
case ARG_PING:
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_send_ping(uctrl);
|
|
|
|
if (r == -ENOANO)
|
|
|
|
log_error("Cannot specify --ping after --exit, ignoring.");
|
|
|
|
else if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to send a ping message: %m");
|
2019-01-12 23:44:38 +01:00
|
|
|
break;
|
2019-01-08 04:02:08 +01:00
|
|
|
case 't':
|
|
|
|
r = parse_sec(optarg, &timeout);
|
2016-10-11 09:08:04 +02:00
|
|
|
if (r < 0)
|
2019-01-08 04:02:08 +01:00
|
|
|
return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
2017-12-05 15:30:10 +01:00
|
|
|
case 'V':
|
2018-09-21 10:45:26 +02:00
|
|
|
return print_version();
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'h':
|
2018-08-24 14:55:19 +02:00
|
|
|
return help();
|
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown option.");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2011-10-09 22:52:03 +02:00
|
|
|
|
2019-01-12 00:31:53 +01:00
|
|
|
if (optind < argc)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Extraneous argument: %s", argv[optind]);
|
2018-08-24 14:55:19 +02:00
|
|
|
|
2019-01-25 16:33:08 +01:00
|
|
|
r = udev_ctrl_wait(uctrl, timeout);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to wait for daemon to reply: %m");
|
|
|
|
|
2018-08-24 14:55:19 +02:00
|
|
|
return 0;
|
2005-06-05 04:41:09 +02:00
|
|
|
}
|