]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-control.c
05df3e81986a9cd038b4c61b2b88ea63eb62e7c5
[thirdparty/systemd.git] / src / udev / udevadm-control.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (C) 2005-2011 Kay Sievers <kay@vrfy.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <errno.h>
17 #include <getopt.h>
18 #include <stddef.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include "process-util.h"
25 #include "time-util.h"
26 #include "udev-util.h"
27 #include "udev.h"
28 #include "udevadm-util.h"
29
30 static void print_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
46 static int adm_control(struct udev *udev, int argc, char *argv[]) {
47 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
48 int timeout = 60;
49 int rc = 1, c;
50
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' },
64 {}
65 };
66
67 if (must_be_root() < 0)
68 return 1;
69
70 uctrl = udev_ctrl_new(udev);
71 if (uctrl == NULL)
72 return 2;
73
74 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
75 switch (c) {
76 case 'e':
77 if (udev_ctrl_send_exit(uctrl, timeout) < 0)
78 rc = 2;
79 else
80 rc = 0;
81 break;
82 case 'l': {
83 int i;
84
85 i = util_log_priority(optarg);
86 if (i < 0) {
87 log_error("invalid number '%s'", optarg);
88 return rc;
89 }
90 if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg), timeout) < 0)
91 rc = 2;
92 else
93 rc = 0;
94 break;
95 }
96 case 's':
97 if (udev_ctrl_send_stop_exec_queue(uctrl, timeout) < 0)
98 rc = 2;
99 else
100 rc = 0;
101 break;
102 case 'S':
103 if (udev_ctrl_send_start_exec_queue(uctrl, timeout) < 0)
104 rc = 2;
105 else
106 rc = 0;
107 break;
108 case 'R':
109 if (udev_ctrl_send_reload(uctrl, timeout) < 0)
110 rc = 2;
111 else
112 rc = 0;
113 break;
114 case 'p':
115 if (strchr(optarg, '=') == NULL) {
116 log_error("expect <KEY>=<value> instead of '%s'", optarg);
117 return rc;
118 }
119 if (udev_ctrl_send_set_env(uctrl, optarg, timeout) < 0)
120 rc = 2;
121 else
122 rc = 0;
123 break;
124 case 'm': {
125 char *endp;
126 int i;
127
128 i = strtoul(optarg, &endp, 0);
129 if (endp[0] != '\0' || i < 1) {
130 log_error("invalid number '%s'", optarg);
131 return rc;
132 }
133 if (udev_ctrl_send_set_children_max(uctrl, i, timeout) < 0)
134 rc = 2;
135 else
136 rc = 0;
137 break;
138 }
139 case 't': {
140 int r, seconds;
141 usec_t s;
142
143 r = parse_sec(optarg, &s);
144 if (r < 0)
145 return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg);
146
147 if (DIV_ROUND_UP(s, USEC_PER_SEC) > INT_MAX)
148 log_error("Timeout value is out of range.");
149 else {
150 seconds = s != USEC_INFINITY ? (int) DIV_ROUND_UP(s, USEC_PER_SEC) : INT_MAX;
151 timeout = seconds;
152 rc = 0;
153 }
154 break;
155 }
156 case 'V':
157 print_version();
158 rc = 0;
159 break;
160 case 'h':
161 print_help();
162 rc = 0;
163 break;
164 }
165
166 if (optind < argc)
167 log_error("Extraneous argument: %s", argv[optind]);
168 else if (optind == 1)
169 log_error("Option missing");
170 return rc;
171 }
172
173 const struct udevadm_cmd udevadm_control = {
174 .name = "control",
175 .cmd = adm_control,
176 .help = "Control the udev daemon",
177 };