Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0+ */
2 : /*
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.
7 : *
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.
12 : */
13 :
14 : #include <errno.h>
15 : #include <getopt.h>
16 : #include <stddef.h>
17 : #include <stdio.h>
18 : #include <stdlib.h>
19 : #include <string.h>
20 : #include <unistd.h>
21 :
22 : #include "parse-util.h"
23 : #include "process-util.h"
24 : #include "syslog-util.h"
25 : #include "time-util.h"
26 : #include "udevadm.h"
27 : #include "udev-ctrl.h"
28 : #include "util.h"
29 : #include "virt.h"
30 :
31 0 : static int help(void) {
32 0 : printf("%s control OPTION\n\n"
33 : "Control the udev daemon.\n\n"
34 : " -h --help Show this help\n"
35 : " -V --version Show package version\n"
36 : " -e --exit Instruct the daemon to cleanup and exit\n"
37 : " -l --log-priority=LEVEL Set the udev log level for the daemon\n"
38 : " -s --stop-exec-queue Do not execute events, queue only\n"
39 : " -S --start-exec-queue Execute events, flush queue\n"
40 : " -R --reload Reload rules and databases\n"
41 : " -p --property=KEY=VALUE Set a global property for all events\n"
42 : " -m --children-max=N Maximum number of children\n"
43 : " --ping Wait for udev to respond to a ping message\n"
44 : " -t --timeout=SECONDS Maximum time to block for a reply\n"
45 : , program_invocation_short_name);
46 :
47 0 : return 0;
48 : }
49 :
50 0 : int control_main(int argc, char *argv[], void *userdata) {
51 0 : _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
52 0 : usec_t timeout = 60 * USEC_PER_SEC;
53 : int c, r;
54 :
55 : enum {
56 : ARG_PING = 0x100,
57 : };
58 :
59 : static const struct option options[] = {
60 : { "exit", no_argument, NULL, 'e' },
61 : { "log-priority", required_argument, NULL, 'l' },
62 : { "stop-exec-queue", no_argument, NULL, 's' },
63 : { "start-exec-queue", no_argument, NULL, 'S' },
64 : { "reload", no_argument, NULL, 'R' },
65 : { "reload-rules", no_argument, NULL, 'R' }, /* alias for -R */
66 : { "property", required_argument, NULL, 'p' },
67 : { "env", required_argument, NULL, 'p' }, /* alias for -p */
68 : { "children-max", required_argument, NULL, 'm' },
69 : { "ping", no_argument, NULL, ARG_PING },
70 : { "timeout", required_argument, NULL, 't' },
71 : { "version", no_argument, NULL, 'V' },
72 : { "help", no_argument, NULL, 'h' },
73 : {}
74 : };
75 :
76 0 : if (running_in_chroot() > 0) {
77 0 : log_info("Running in chroot, ignoring request.");
78 0 : return 0;
79 : }
80 :
81 0 : if (argc <= 1)
82 0 : return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
83 : "This command expects one or more options.");
84 :
85 0 : r = udev_ctrl_new(&uctrl);
86 0 : if (r < 0)
87 0 : return log_error_errno(r, "Failed to initialize udev control: %m");
88 :
89 0 : while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
90 0 : switch (c) {
91 0 : case 'e':
92 0 : r = udev_ctrl_send_exit(uctrl);
93 0 : if (r == -ENOANO)
94 0 : log_warning("Cannot specify --exit after --exit, ignoring.");
95 0 : else if (r < 0)
96 0 : return log_error_errno(r, "Failed to send exit request: %m");
97 0 : break;
98 0 : case 'l':
99 0 : r = log_level_from_string(optarg);
100 0 : if (r < 0)
101 0 : return log_error_errno(r, "Failed to parse log priority '%s': %m", optarg);
102 :
103 0 : r = udev_ctrl_send_set_log_level(uctrl, r);
104 0 : if (r == -ENOANO)
105 0 : log_warning("Cannot specify --log-priority after --exit, ignoring.");
106 0 : else if (r < 0)
107 0 : return log_error_errno(r, "Failed to send request to set log level: %m");
108 0 : break;
109 0 : case 's':
110 0 : r = udev_ctrl_send_stop_exec_queue(uctrl);
111 0 : if (r == -ENOANO)
112 0 : log_warning("Cannot specify --stop-exec-queue after --exit, ignoring.");
113 0 : else if (r < 0)
114 0 : return log_error_errno(r, "Failed to send request to stop exec queue: %m");
115 0 : break;
116 0 : case 'S':
117 0 : r = udev_ctrl_send_start_exec_queue(uctrl);
118 0 : if (r == -ENOANO)
119 0 : log_warning("Cannot specify --start-exec-queue after --exit, ignoring.");
120 0 : else if (r < 0)
121 0 : return log_error_errno(r, "Failed to send request to start exec queue: %m");
122 0 : break;
123 0 : case 'R':
124 0 : r = udev_ctrl_send_reload(uctrl);
125 0 : if (r == -ENOANO)
126 0 : log_warning("Cannot specify --reload after --exit, ignoring.");
127 0 : else if (r < 0)
128 0 : return log_error_errno(r, "Failed to send reload request: %m");
129 0 : break;
130 0 : case 'p':
131 0 : if (!strchr(optarg, '='))
132 0 : return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "expect <KEY>=<value> instead of '%s'", optarg);
133 :
134 0 : r = udev_ctrl_send_set_env(uctrl, optarg);
135 0 : if (r == -ENOANO)
136 0 : log_warning("Cannot specify --property after --exit, ignoring.");
137 0 : else if (r < 0)
138 0 : return log_error_errno(r, "Failed to send request to update environment: %m");
139 0 : break;
140 0 : case 'm': {
141 : unsigned i;
142 :
143 0 : r = safe_atou(optarg, &i);
144 0 : if (r < 0)
145 0 : return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
146 :
147 0 : r = udev_ctrl_send_set_children_max(uctrl, i);
148 0 : if (r == -ENOANO)
149 0 : log_warning("Cannot specify --children-max after --exit, ignoring.");
150 0 : else if (r < 0)
151 0 : return log_error_errno(r, "Failed to send request to set number of children: %m");
152 0 : break;
153 : }
154 0 : case ARG_PING:
155 0 : r = udev_ctrl_send_ping(uctrl);
156 0 : if (r == -ENOANO)
157 0 : log_error("Cannot specify --ping after --exit, ignoring.");
158 0 : else if (r < 0)
159 0 : return log_error_errno(r, "Failed to send a ping message: %m");
160 0 : break;
161 0 : case 't':
162 0 : r = parse_sec(optarg, &timeout);
163 0 : if (r < 0)
164 0 : return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
165 0 : break;
166 0 : case 'V':
167 0 : return print_version();
168 0 : case 'h':
169 0 : return help();
170 0 : case '?':
171 0 : return -EINVAL;
172 0 : default:
173 0 : assert_not_reached("Unknown option.");
174 : }
175 :
176 0 : if (optind < argc)
177 0 : return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
178 : "Extraneous argument: %s", argv[optind]);
179 :
180 0 : r = udev_ctrl_wait(uctrl, timeout);
181 0 : if (r < 0)
182 0 : return log_error_errno(r, "Failed to wait for daemon to reply: %m");
183 :
184 0 : return 0;
185 : }
|