1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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.
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.
22 #include "parse-util.h"
23 #include "process-util.h"
24 #include "static-destruct.h"
26 #include "syslog-util.h"
27 #include "time-util.h"
29 #include "udev-ctrl.h"
32 static char **arg_env
= NULL
;
33 static usec_t arg_timeout
= 60 * USEC_PER_SEC
;
34 static bool arg_ping
= false;
35 static bool arg_reload
= false;
36 static bool arg_exit
= false;
37 static int arg_max_children
= -1;
38 static int arg_log_level
= -1;
39 static int arg_start_exec_queue
= -1;
41 STATIC_DESTRUCTOR_REGISTER(arg_env
, strv_freep
);
43 static int help(void) {
44 printf("%s control OPTION\n\n"
45 "Control the udev daemon.\n\n"
46 " -h --help Show this help\n"
47 " -V --version Show package version\n"
48 " -e --exit Instruct the daemon to cleanup and exit\n"
49 " -l --log-level=LEVEL Set the udev log level for the daemon\n"
50 " -s --stop-exec-queue Do not execute events, queue only\n"
51 " -S --start-exec-queue Execute events, flush queue\n"
52 " -R --reload Reload rules and databases\n"
53 " -p --property=KEY=VALUE Set a global property for all events\n"
54 " -m --children-max=N Maximum number of children\n"
55 " --ping Wait for udev to respond to a ping message\n"
56 " -t --timeout=SECONDS Maximum time to block for a reply\n",
57 program_invocation_short_name
);
62 static int parse_argv(int argc
, char *argv
[]) {
67 static const struct option options
[] = {
68 { "exit", no_argument
, NULL
, 'e' },
69 { "log-level", required_argument
, NULL
, 'l' },
70 { "log-priority", required_argument
, NULL
, 'l' }, /* for backward compatibility */
71 { "stop-exec-queue", no_argument
, NULL
, 's' },
72 { "start-exec-queue", no_argument
, NULL
, 'S' },
73 { "reload", no_argument
, NULL
, 'R' },
74 { "reload-rules", no_argument
, NULL
, 'R' }, /* alias for -R */
75 { "property", required_argument
, NULL
, 'p' },
76 { "env", required_argument
, NULL
, 'p' }, /* alias for -p */
77 { "children-max", required_argument
, NULL
, 'm' },
78 { "ping", no_argument
, NULL
, ARG_PING
},
79 { "timeout", required_argument
, NULL
, 't' },
80 { "version", no_argument
, NULL
, 'V' },
81 { "help", no_argument
, NULL
, 'h' },
91 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
92 "This command expects one or more options.");
94 while ((c
= getopt_long(argc
, argv
, "el:sSRp:m:t:Vh", options
, NULL
)) >= 0)
102 arg_log_level
= log_level_from_string(optarg
);
103 if (arg_log_level
< 0)
104 return log_error_errno(arg_log_level
, "Failed to parse log level '%s': %m", optarg
);
108 arg_start_exec_queue
= false;
112 arg_start_exec_queue
= true;
120 if (!strchr(optarg
, '='))
121 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "expect <KEY>=<value> instead of '%s'", optarg
);
123 r
= strv_extend(&arg_env
, optarg
);
125 return log_error_errno(r
, "Failed to extend environment: %m");
131 r
= safe_atou(optarg
, &i
);
133 return log_error_errno(r
, "Failed to parse maximum number of children '%s': %m", optarg
);
134 arg_max_children
= i
;
143 r
= parse_sec(optarg
, &arg_timeout
);
145 return log_error_errno(r
, "Failed to parse timeout value '%s': %m", optarg
);
149 return print_version();
158 assert_not_reached();
162 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
163 "Extraneous argument: %s", argv
[optind
]);
168 int control_main(int argc
, char *argv
[], void *userdata
) {
169 _cleanup_(udev_ctrl_unrefp
) UdevCtrl
*uctrl
= NULL
;
172 if (running_in_chroot() > 0) {
173 log_info("Running in chroot, ignoring request.");
177 r
= parse_argv(argc
, argv
);
181 r
= udev_ctrl_new(&uctrl
);
183 return log_error_errno(r
, "Failed to initialize udev control: %m");
186 r
= udev_ctrl_send_exit(uctrl
);
188 return log_error_errno(r
, "Failed to send exit request: %m");
192 if (arg_log_level
>= 0) {
193 r
= udev_ctrl_send_set_log_level(uctrl
, arg_log_level
);
195 return log_error_errno(r
, "Failed to send request to set log level: %m");
198 if (arg_start_exec_queue
== false) {
199 r
= udev_ctrl_send_stop_exec_queue(uctrl
);
201 return log_error_errno(r
, "Failed to send request to stop exec queue: %m");
204 if (arg_start_exec_queue
== true) {
205 r
= udev_ctrl_send_start_exec_queue(uctrl
);
207 return log_error_errno(r
, "Failed to send request to start exec queue: %m");
211 r
= udev_ctrl_send_reload(uctrl
);
213 return log_error_errno(r
, "Failed to send reload request: %m");
216 STRV_FOREACH(env
, arg_env
) {
217 r
= udev_ctrl_send_set_env(uctrl
, *env
);
219 return log_error_errno(r
, "Failed to send request to update environment: %m");
222 if (arg_max_children
>= 0) {
223 r
= udev_ctrl_send_set_children_max(uctrl
, arg_max_children
);
225 return log_error_errno(r
, "Failed to send request to set number of children: %m");
229 r
= udev_ctrl_send_ping(uctrl
);
231 return log_error_errno(r
, "Failed to send a ping message: %m");
234 r
= udev_ctrl_wait(uctrl
, arg_timeout
);
236 return log_error_errno(r
, "Failed to wait for daemon to reply: %m");