1 /* SPDX-License-Identifier: GPL-2.0+ */
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 "syslog-util.h"
25 #include "time-util.h"
27 #include "udev-ctrl.h"
30 static int help(void) {
31 printf("%s control OPTION\n\n"
32 "Control the udev daemon.\n\n"
33 " -h --help Show this help\n"
34 " -V --version Show package version\n"
35 " -e --exit Instruct the daemon to cleanup and exit\n"
36 " -l --log-priority=LEVEL Set the udev log level for the daemon\n"
37 " -s --stop-exec-queue Do not execute events, queue only\n"
38 " -S --start-exec-queue Execute events, flush queue\n"
39 " -R --reload Reload rules and databases\n"
40 " -p --property=KEY=VALUE Set a global property for all events\n"
41 " -m --children-max=N Maximum number of children\n"
42 " -t --timeout=SECONDS Maximum time to block for a reply\n"
43 , program_invocation_short_name
);
48 int control_main(int argc
, char *argv
[], void *userdata
) {
49 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
53 static const struct option options
[] = {
54 { "exit", no_argument
, NULL
, 'e' },
55 { "log-priority", required_argument
, NULL
, 'l' },
56 { "stop-exec-queue", no_argument
, NULL
, 's' },
57 { "start-exec-queue", no_argument
, NULL
, 'S' },
58 { "reload", no_argument
, NULL
, 'R' },
59 { "reload-rules", no_argument
, NULL
, 'R' }, /* alias for -R */
60 { "property", required_argument
, NULL
, 'p' },
61 { "env", required_argument
, NULL
, 'p' }, /* alias for -p */
62 { "children-max", required_argument
, NULL
, 'm' },
63 { "timeout", required_argument
, NULL
, 't' },
64 { "version", no_argument
, NULL
, 'V' },
65 { "help", no_argument
, NULL
, 'h' },
74 log_error("Option missing");
76 uctrl
= udev_ctrl_new();
80 while ((c
= getopt_long(argc
, argv
, "el:sSRp:m:t:Vh", options
, NULL
)) >= 0)
83 r
= udev_ctrl_send_exit(uctrl
, timeout
);
88 r
= log_level_from_string(optarg
);
90 return log_error_errno(r
, "Failed to parse log priority '%s': %m", optarg
);
92 r
= udev_ctrl_send_set_log_level(uctrl
, r
, timeout
);
97 r
= udev_ctrl_send_stop_exec_queue(uctrl
, timeout
);
102 r
= udev_ctrl_send_start_exec_queue(uctrl
, timeout
);
107 r
= udev_ctrl_send_reload(uctrl
, timeout
);
112 if (!strchr(optarg
, '=')) {
113 log_error("expect <KEY>=<value> instead of '%s'", optarg
);
116 r
= udev_ctrl_send_set_env(uctrl
, optarg
, timeout
);
123 r
= safe_atou(optarg
, &i
);
125 return log_error_errno(r
, "Failed to parse maximum number of events '%s': %m", optarg
);
127 r
= udev_ctrl_send_set_children_max(uctrl
, i
, timeout
);
135 r
= parse_sec(optarg
, &s
);
137 return log_error_errno(r
, "Failed to parse timeout value '%s'.", optarg
);
139 if (DIV_ROUND_UP(s
, USEC_PER_SEC
) > INT_MAX
)
140 log_error("Timeout value is out of range, ignoring.");
142 timeout
= s
!= USEC_INFINITY
? (int) DIV_ROUND_UP(s
, USEC_PER_SEC
) : INT_MAX
;
146 return print_version();
152 assert_not_reached("Unknown option.");
156 log_error("Extraneous argument: %s", argv
[optind
]);
158 } else if (optind
== 1) {
159 log_error("Option missing");