]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-control.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 "syslog-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 r = log_level_from_string(optarg);
89 if (r < 0)
90 return log_error_errno(r, "Failed to parse log priority '%s': %m", optarg);
91
92 r = udev_ctrl_send_set_log_level(uctrl, r, timeout);
93 if (r < 0)
94 return r;
95 break;
96 case 's':
97 r = udev_ctrl_send_stop_exec_queue(uctrl, timeout);
98 if (r < 0)
99 return r;
100 break;
101 case 'S':
102 r = udev_ctrl_send_start_exec_queue(uctrl, timeout);
103 if (r < 0)
104 return r;
105 break;
106 case 'R':
107 r = udev_ctrl_send_reload(uctrl, timeout);
108 if (r < 0)
109 return r;
110 break;
111 case 'p':
112 if (!strchr(optarg, '=')) {
113 log_error("expect <KEY>=<value> instead of '%s'", optarg);
114 return -EINVAL;
115 }
116 r = udev_ctrl_send_set_env(uctrl, optarg, timeout);
117 if (r < 0)
118 return r;
119 break;
120 case 'm': {
121 unsigned i;
122
123 r = safe_atou(optarg, &i);
124 if (r < 0)
125 return log_error_errno(r, "Failed to parse maximum number of events '%s': %m", optarg);
126
127 r = udev_ctrl_send_set_children_max(uctrl, i, timeout);
128 if (r < 0)
129 return r;
130 break;
131 }
132 case 't': {
133 usec_t s;
134
135 r = parse_sec(optarg, &s);
136 if (r < 0)
137 return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg);
138
139 if (DIV_ROUND_UP(s, USEC_PER_SEC) > INT_MAX)
140 log_error("Timeout value is out of range, ignoring.");
141 else
142 timeout = s != USEC_INFINITY ? (int) DIV_ROUND_UP(s, USEC_PER_SEC) : INT_MAX;
143 break;
144 }
145 case 'V':
146 return print_version();
147 case 'h':
148 return help();
149 case '?':
150 return -EINVAL;
151 default:
152 assert_not_reached("Unknown option.");
153 }
154
155 if (optind < argc) {
156 log_error("Extraneous argument: %s", argv[optind]);
157 return -EINVAL;
158 } else if (optind == 1) {
159 log_error("Option missing");
160 return -EINVAL;
161 }
162
163 return 0;
164 }