]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-control.c
Merge pull request #10176 from yuwata/udev-cleanup-7
[thirdparty/systemd.git] / src / udev / udevadm-control.c
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 "libudev-private.h"
23 #include "parse-util.h"
24 #include "process-util.h"
25 #include "time-util.h"
26 #include "udevadm.h"
27 #include "udev-ctrl.h"
28 #include "util.h"
29
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);
44
45 return 0;
46 }
47
48 int control_main(int argc, char *argv[], void *userdata) {
49 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
50 int timeout = 60;
51 int c, r;
52
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' },
66 {}
67 };
68
69 r = must_be_root();
70 if (r < 0)
71 return r;
72
73 if (argc <= 1)
74 log_error("Option missing");
75
76 uctrl = udev_ctrl_new();
77 if (!uctrl)
78 return -ENOMEM;
79
80 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
81 switch (c) {
82 case 'e':
83 r = udev_ctrl_send_exit(uctrl, timeout);
84 if (r < 0)
85 return r;
86 break;
87 case 'l': {
88 int i;
89
90 i = util_log_priority(optarg);
91 if (i < 0)
92 return log_error_errno(i, "invalid number '%s'", optarg);
93
94 r = udev_ctrl_send_set_log_level(uctrl, i, timeout);
95 if (r < 0)
96 return r;
97 break;
98 }
99 case 's':
100 r = udev_ctrl_send_stop_exec_queue(uctrl, timeout);
101 if (r < 0)
102 return r;
103 break;
104 case 'S':
105 r = udev_ctrl_send_start_exec_queue(uctrl, timeout);
106 if (r < 0)
107 return r;
108 break;
109 case 'R':
110 r = udev_ctrl_send_reload(uctrl, timeout);
111 if (r < 0)
112 return r;
113 break;
114 case 'p':
115 if (!strchr(optarg, '=')) {
116 log_error("expect <KEY>=<value> instead of '%s'", optarg);
117 return -EINVAL;
118 }
119 r = udev_ctrl_send_set_env(uctrl, optarg, timeout);
120 if (r < 0)
121 return r;
122 break;
123 case 'm': {
124 unsigned i;
125
126 r = safe_atou(optarg, &i);
127 if (r < 0)
128 return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
129
130 r = udev_ctrl_send_set_children_max(uctrl, i, timeout);
131 if (r < 0)
132 return r;
133 break;
134 }
135 case 't': {
136 usec_t s;
137
138 r = parse_sec(optarg, &s);
139 if (r < 0)
140 return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg);
141
142 if (DIV_ROUND_UP(s, USEC_PER_SEC) > INT_MAX)
143 log_error("Timeout value is out of range, ignoring.");
144 else
145 timeout = s != USEC_INFINITY ? (int) DIV_ROUND_UP(s, USEC_PER_SEC) : INT_MAX;
146 break;
147 }
148 case 'V':
149 return print_version();
150 case 'h':
151 return help();
152 case '?':
153 return -EINVAL;
154 default:
155 assert_not_reached("Unknown option.");
156 }
157
158 if (optind < argc) {
159 log_error("Extraneous argument: %s", argv[optind]);
160 return -EINVAL;
161 } else if (optind == 1) {
162 log_error("Option missing");
163 return -EINVAL;
164 }
165
166 return 0;
167 }