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 "time-util.h"
28 static int help(void) {
29 printf("%s control OPTION\n\n"
30 "Control the udev daemon.\n\n"
31 " -h --help Show this help\n"
32 " -V --version Show package version\n"
33 " -e --exit Instruct the daemon to cleanup and exit\n"
34 " -l --log-priority=LEVEL Set the udev log level for the daemon\n"
35 " -s --stop-exec-queue Do not execute events, queue only\n"
36 " -S --start-exec-queue Execute events, flush queue\n"
37 " -R --reload Reload rules and databases\n"
38 " -p --property=KEY=VALUE Set a global property for all events\n"
39 " -m --children-max=N Maximum number of children\n"
40 " -t --timeout=SECONDS Maximum time to block for a reply\n"
41 , program_invocation_short_name
);
46 int control_main(int argc
, char *argv
[], void *userdata
) {
47 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
51 static const struct option options
[] = {
52 { "exit", no_argument
, NULL
, 'e' },
53 { "log-priority", required_argument
, NULL
, 'l' },
54 { "stop-exec-queue", no_argument
, NULL
, 's' },
55 { "start-exec-queue", no_argument
, NULL
, 'S' },
56 { "reload", no_argument
, NULL
, 'R' },
57 { "reload-rules", no_argument
, NULL
, 'R' }, /* alias for -R */
58 { "property", required_argument
, NULL
, 'p' },
59 { "env", required_argument
, NULL
, 'p' }, /* alias for -p */
60 { "children-max", required_argument
, NULL
, 'm' },
61 { "timeout", required_argument
, NULL
, 't' },
62 { "version", no_argument
, NULL
, 'V' },
63 { "help", no_argument
, NULL
, 'h' },
72 log_error("Option missing");
74 uctrl
= udev_ctrl_new();
78 while ((c
= getopt_long(argc
, argv
, "el:sSRp:m:t:Vh", options
, NULL
)) >= 0)
81 r
= udev_ctrl_send_exit(uctrl
, timeout
);
88 i
= util_log_priority(optarg
);
90 return log_error_errno(i
, "invalid number '%s'", optarg
);
92 r
= udev_ctrl_send_set_log_level(uctrl
, i
, timeout
);
98 r
= udev_ctrl_send_stop_exec_queue(uctrl
, timeout
);
103 r
= udev_ctrl_send_start_exec_queue(uctrl
, timeout
);
108 r
= udev_ctrl_send_reload(uctrl
, timeout
);
113 if (!strchr(optarg
, '=')) {
114 log_error("expect <KEY>=<value> instead of '%s'", optarg
);
117 r
= udev_ctrl_send_set_env(uctrl
, optarg
, timeout
);
124 r
= safe_atou(optarg
, &i
);
126 return log_error_errno(r
, "Failed to parse maximum number of events '%s': %m", optarg
);
128 r
= udev_ctrl_send_set_children_max(uctrl
, i
, timeout
);
136 r
= parse_sec(optarg
, &s
);
138 return log_error_errno(r
, "Failed to parse timeout value '%s'.", optarg
);
140 if (DIV_ROUND_UP(s
, USEC_PER_SEC
) > INT_MAX
)
141 log_error("Timeout value is out of range, ignoring.");
143 timeout
= s
!= USEC_INFINITY
? (int) DIV_ROUND_UP(s
, USEC_PER_SEC
) : INT_MAX
;
147 return print_version();
153 assert_not_reached("Unknown option.");
157 log_error("Extraneous argument: %s", argv
[optind
]);
159 } else if (optind
== 1) {
160 log_error("Option missing");