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 "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
help(void)31 static int help(void) {
32 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-level=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 return 0;
48 }
49
control_main(int argc,char * argv[],void * userdata)50 int control_main(int argc, char *argv[], void *userdata) {
51 _cleanup_(udev_ctrl_unrefp) UdevCtrl *uctrl = NULL;
52 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-level", required_argument, NULL, 'l' },
62 { "log-priority", required_argument, NULL, 'l' }, /* for backward compatibility */
63 { "stop-exec-queue", no_argument, NULL, 's' },
64 { "start-exec-queue", no_argument, NULL, 'S' },
65 { "reload", no_argument, NULL, 'R' },
66 { "reload-rules", no_argument, NULL, 'R' }, /* alias for -R */
67 { "property", required_argument, NULL, 'p' },
68 { "env", required_argument, NULL, 'p' }, /* alias for -p */
69 { "children-max", required_argument, NULL, 'm' },
70 { "ping", no_argument, NULL, ARG_PING },
71 { "timeout", required_argument, NULL, 't' },
72 { "version", no_argument, NULL, 'V' },
73 { "help", no_argument, NULL, 'h' },
74 {}
75 };
76
77 if (running_in_chroot() > 0) {
78 log_info("Running in chroot, ignoring request.");
79 return 0;
80 }
81
82 if (argc <= 1)
83 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
84 "This command expects one or more options.");
85
86 r = udev_ctrl_new(&uctrl);
87 if (r < 0)
88 return log_error_errno(r, "Failed to initialize udev control: %m");
89
90 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
91 switch (c) {
92 case 'e':
93 r = udev_ctrl_send_exit(uctrl);
94 if (r == -ENOANO)
95 log_warning("Cannot specify --exit after --exit, ignoring.");
96 else if (r < 0)
97 return log_error_errno(r, "Failed to send exit request: %m");
98 break;
99 case 'l':
100 r = log_level_from_string(optarg);
101 if (r < 0)
102 return log_error_errno(r, "Failed to parse log level '%s': %m", optarg);
103
104 r = udev_ctrl_send_set_log_level(uctrl, r);
105 if (r == -ENOANO)
106 log_warning("Cannot specify --log-level after --exit, ignoring.");
107 else if (r < 0)
108 return log_error_errno(r, "Failed to send request to set log level: %m");
109 break;
110 case 's':
111 r = udev_ctrl_send_stop_exec_queue(uctrl);
112 if (r == -ENOANO)
113 log_warning("Cannot specify --stop-exec-queue after --exit, ignoring.");
114 else if (r < 0)
115 return log_error_errno(r, "Failed to send request to stop exec queue: %m");
116 break;
117 case 'S':
118 r = udev_ctrl_send_start_exec_queue(uctrl);
119 if (r == -ENOANO)
120 log_warning("Cannot specify --start-exec-queue after --exit, ignoring.");
121 else if (r < 0)
122 return log_error_errno(r, "Failed to send request to start exec queue: %m");
123 break;
124 case 'R':
125 r = udev_ctrl_send_reload(uctrl);
126 if (r == -ENOANO)
127 log_warning("Cannot specify --reload after --exit, ignoring.");
128 else if (r < 0)
129 return log_error_errno(r, "Failed to send reload request: %m");
130 break;
131 case 'p':
132 if (!strchr(optarg, '='))
133 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "expect <KEY>=<value> instead of '%s'", optarg);
134
135 r = udev_ctrl_send_set_env(uctrl, optarg);
136 if (r == -ENOANO)
137 log_warning("Cannot specify --property after --exit, ignoring.");
138 else if (r < 0)
139 return log_error_errno(r, "Failed to send request to update environment: %m");
140 break;
141 case 'm': {
142 unsigned i;
143
144 r = safe_atou(optarg, &i);
145 if (r < 0)
146 return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
147
148 r = udev_ctrl_send_set_children_max(uctrl, i);
149 if (r == -ENOANO)
150 log_warning("Cannot specify --children-max after --exit, ignoring.");
151 else if (r < 0)
152 return log_error_errno(r, "Failed to send request to set number of children: %m");
153 break;
154 }
155 case ARG_PING:
156 r = udev_ctrl_send_ping(uctrl);
157 if (r == -ENOANO)
158 log_error("Cannot specify --ping after --exit, ignoring.");
159 else if (r < 0)
160 return log_error_errno(r, "Failed to send a ping message: %m");
161 break;
162 case 't':
163 r = parse_sec(optarg, &timeout);
164 if (r < 0)
165 return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
166 break;
167 case 'V':
168 return print_version();
169 case 'h':
170 return help();
171 case '?':
172 return -EINVAL;
173 default:
174 assert_not_reached();
175 }
176
177 if (optind < argc)
178 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
179 "Extraneous argument: %s", argv[optind]);
180
181 r = udev_ctrl_wait(uctrl, timeout);
182 if (r < 0)
183 return log_error_errno(r, "Failed to wait for daemon to reply: %m");
184
185 return 0;
186 }
187