]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-control.c
udevadm: make sure we don't reset max children on each invocation
[thirdparty/systemd.git] / src / udev / udevadm-control.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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 "static-destruct.h"
25 #include "strv.h"
26 #include "syslog-util.h"
27 #include "time-util.h"
28 #include "udevadm.h"
29 #include "udev-ctrl.h"
30 #include "virt.h"
31
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;
40
41 STATIC_DESTRUCTOR_REGISTER(arg_env, strv_freep);
42
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);
58
59 return 0;
60 }
61
62 static int parse_argv(int argc, char *argv[]) {
63 enum {
64 ARG_PING = 0x100,
65 };
66
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' },
82 {}
83 };
84
85 int c, r;
86
87 assert(argc >= 0);
88 assert(argv);
89
90 if (argc <= 1)
91 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
92 "This command expects one or more options.");
93
94 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
95 switch (c) {
96
97 case 'e':
98 arg_exit = true;
99 break;
100
101 case 'l':
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);
105 break;
106
107 case 's':
108 arg_start_exec_queue = false;
109 break;
110
111 case 'S':
112 arg_start_exec_queue = true;
113 break;
114
115 case 'R':
116 arg_reload = true;
117 break;
118
119 case 'p':
120 if (!strchr(optarg, '='))
121 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "expect <KEY>=<value> instead of '%s'", optarg);
122
123 r = strv_extend(&arg_env, optarg);
124 if (r < 0)
125 return log_error_errno(r, "Failed to extend environment: %m");
126
127 break;
128
129 case 'm': {
130 unsigned i;
131 r = safe_atou(optarg, &i);
132 if (r < 0)
133 return log_error_errno(r, "Failed to parse maximum number of children '%s': %m", optarg);
134 arg_max_children = i;
135 break;
136 }
137
138 case ARG_PING:
139 arg_ping = true;
140 break;
141
142 case 't':
143 r = parse_sec(optarg, &arg_timeout);
144 if (r < 0)
145 return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
146 break;
147
148 case 'V':
149 return print_version();
150
151 case 'h':
152 return help();
153
154 case '?':
155 return -EINVAL;
156
157 default:
158 assert_not_reached();
159 }
160
161 if (optind < argc)
162 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
163 "Extraneous argument: %s", argv[optind]);
164
165 return 1;
166 }
167
168 int control_main(int argc, char *argv[], void *userdata) {
169 _cleanup_(udev_ctrl_unrefp) UdevCtrl *uctrl = NULL;
170 int r;
171
172 if (running_in_chroot() > 0) {
173 log_info("Running in chroot, ignoring request.");
174 return 0;
175 }
176
177 r = parse_argv(argc, argv);
178 if (r <= 0)
179 return r;
180
181 r = udev_ctrl_new(&uctrl);
182 if (r < 0)
183 return log_error_errno(r, "Failed to initialize udev control: %m");
184
185 if (arg_exit) {
186 r = udev_ctrl_send_exit(uctrl);
187 if (r < 0)
188 return log_error_errno(r, "Failed to send exit request: %m");
189 return 0;
190 }
191
192 if (arg_log_level >= 0) {
193 r = udev_ctrl_send_set_log_level(uctrl, r);
194 if (r < 0)
195 return log_error_errno(r, "Failed to send request to set log level: %m");
196 }
197
198 if (arg_start_exec_queue == false) {
199 r = udev_ctrl_send_stop_exec_queue(uctrl);
200 if (r < 0)
201 return log_error_errno(r, "Failed to send request to stop exec queue: %m");
202 }
203
204 if (arg_start_exec_queue == true) {
205 r = udev_ctrl_send_start_exec_queue(uctrl);
206 if (r < 0)
207 return log_error_errno(r, "Failed to send request to start exec queue: %m");
208 }
209
210 if (arg_reload) {
211 r = udev_ctrl_send_reload(uctrl);
212 if (r < 0)
213 return log_error_errno(r, "Failed to send reload request: %m");
214 }
215
216 STRV_FOREACH(env, arg_env) {
217 r = udev_ctrl_send_set_env(uctrl, *env);
218 if (r < 0)
219 return log_error_errno(r, "Failed to send request to update environment: %m");
220 }
221
222 if (arg_max_children >= 0) {
223 r = udev_ctrl_send_set_children_max(uctrl, arg_max_children);
224 if (r < 0)
225 return log_error_errno(r, "Failed to send request to set number of children: %m");
226 }
227
228 if (arg_ping) {
229 r = udev_ctrl_send_ping(uctrl);
230 if (r < 0)
231 return log_error_errno(r, "Failed to send a ping message: %m");
232 }
233
234 r = udev_ctrl_wait(uctrl, arg_timeout);
235 if (r < 0)
236 return log_error_errno(r, "Failed to wait for daemon to reply: %m");
237
238 return 0;
239 }