]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-control.c
Merge pull request #10059 from yuwata/env-exec-directory
[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 "parse-util.h"
23 #include "process-util.h"
24 #include "time-util.h"
25 #include "udev.h"
26 #include "udevadm.h"
27
28 static int help(void) {
29 printf("%s control OPTION\n\n"
30 "Control the udev daemon.\n\n"
31 " -h --help Show this help\n"
32 " -V --version Show package version\n"
33 " -e --exit Instruct the daemon to cleanup and exit\n"
34 " -l --log-priority=LEVEL Set the udev log level for the daemon\n"
35 " -s --stop-exec-queue Do not execute events, queue only\n"
36 " -S --start-exec-queue Execute events, flush queue\n"
37 " -R --reload Reload rules and databases\n"
38 " -p --property=KEY=VALUE Set a global property for all events\n"
39 " -m --children-max=N Maximum number of children\n"
40 " -t --timeout=SECONDS Maximum time to block for a reply\n"
41 , program_invocation_short_name);
42
43 return 0;
44 }
45
46 int control_main(int argc, char *argv[], void *userdata) {
47 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
48 int timeout = 60;
49 int c, r;
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 r = must_be_root();
68 if (r < 0)
69 return r;
70
71 if (argc <= 1)
72 log_error("Option missing");
73
74 uctrl = udev_ctrl_new();
75 if (!uctrl)
76 return -ENOMEM;
77
78 while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
79 switch (c) {
80 case 'e':
81 r = udev_ctrl_send_exit(uctrl, timeout);
82 if (r < 0)
83 return r;
84 break;
85 case 'l': {
86 int i;
87
88 i = util_log_priority(optarg);
89 if (i < 0)
90 return log_error_errno(i, "invalid number '%s'", optarg);
91
92 r = udev_ctrl_send_set_log_level(uctrl, i, timeout);
93 if (r < 0)
94 return r;
95 break;
96 }
97 case 's':
98 r = udev_ctrl_send_stop_exec_queue(uctrl, timeout);
99 if (r < 0)
100 return r;
101 break;
102 case 'S':
103 r = udev_ctrl_send_start_exec_queue(uctrl, timeout);
104 if (r < 0)
105 return r;
106 break;
107 case 'R':
108 r = udev_ctrl_send_reload(uctrl, timeout);
109 if (r < 0)
110 return r;
111 break;
112 case 'p':
113 if (!strchr(optarg, '=')) {
114 log_error("expect <KEY>=<value> instead of '%s'", optarg);
115 return -EINVAL;
116 }
117 r = udev_ctrl_send_set_env(uctrl, optarg, timeout);
118 if (r < 0)
119 return r;
120 break;
121 case 'm': {
122 unsigned i;
123
124 r = safe_atou(optarg, &i);
125 if (r < 0)
126 return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
127
128 r = udev_ctrl_send_set_children_max(uctrl, i, timeout);
129 if (r < 0)
130 return r;
131 break;
132 }
133 case 't': {
134 usec_t s;
135
136 r = parse_sec(optarg, &s);
137 if (r < 0)
138 return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg);
139
140 if (DIV_ROUND_UP(s, USEC_PER_SEC) > INT_MAX)
141 log_error("Timeout value is out of range, ignoring.");
142 else
143 timeout = s != USEC_INFINITY ? (int) DIV_ROUND_UP(s, USEC_PER_SEC) : INT_MAX;
144 break;
145 }
146 case 'V':
147 return print_version();
148 case 'h':
149 return help();
150 case '?':
151 return -EINVAL;
152 default:
153 assert_not_reached("Unknown option.");
154 }
155
156 if (optind < argc) {
157 log_error("Extraneous argument: %s", argv[optind]);
158 return -EINVAL;
159 } else if (optind == 1) {
160 log_error("Option missing");
161 return -EINVAL;
162 }
163
164 return 0;
165 }