]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
7baada47 | 2 | /* |
810adae9 LP |
3 | * Copyright © 2009 Canonical Ltd. |
4 | * Copyright © 2009 Scott James Remnant <scott@netsplit.com> | |
7baada47 KS |
5 | */ |
6 | ||
7baada47 | 7 | #include <errno.h> |
c2666405 | 8 | #include <getopt.h> |
0a6f50c0 | 9 | #include <poll.h> |
cf0fbc49 TA |
10 | #include <stddef.h> |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
cf0fbc49 | 13 | #include <unistd.h> |
7baada47 | 14 | |
16627327 ZJS |
15 | #include "sd-bus.h" |
16 | #include "sd-login.h" | |
77ee1783 | 17 | #include "sd-messages.h" |
16627327 | 18 | |
77ee1783 | 19 | #include "bus-util.h" |
0f2d351f | 20 | #include "io-util.h" |
5ea78a39 | 21 | #include "libudev-util.h" |
16627327 ZJS |
22 | #include "string-util.h" |
23 | #include "strv.h" | |
c7150902 | 24 | #include "time-util.h" |
7d68eb1b | 25 | #include "udev-ctrl.h" |
16627327 ZJS |
26 | #include "udevadm.h" |
27 | #include "unit-def.h" | |
7d68eb1b | 28 | #include "util.h" |
c494b739 | 29 | #include "virt.h" |
c7150902 YW |
30 | |
31 | static usec_t arg_timeout = 120 * USEC_PER_SEC; | |
32 | static const char *arg_exists = NULL; | |
7baada47 | 33 | |
c7150902 | 34 | static int help(void) { |
5639df9a | 35 | printf("%s settle [OPTIONS]\n\n" |
5ac0162c LP |
36 | "Wait for pending udev events.\n\n" |
37 | " -h --help Show this help\n" | |
5639df9a | 38 | " -V --version Show package version\n" |
c7150902 | 39 | " -t --timeout=SEC Maximum time to wait for events\n" |
5ac0162c LP |
40 | " -E --exit-if-exists=FILE Stop waiting if file exists\n" |
41 | , program_invocation_short_name); | |
c7150902 YW |
42 | |
43 | return 0; | |
7643ac9a ZJS |
44 | } |
45 | ||
c7150902 | 46 | static int parse_argv(int argc, char *argv[]) { |
912541b0 | 47 | static const struct option options[] = { |
7643ac9a | 48 | { "timeout", required_argument, NULL, 't' }, |
912541b0 | 49 | { "exit-if-exists", required_argument, NULL, 'E' }, |
5639df9a | 50 | { "version", no_argument, NULL, 'V' }, |
7643ac9a | 51 | { "help", no_argument, NULL, 'h' }, |
2ac23519 LP |
52 | { "seq-start", required_argument, NULL, 's' }, /* removed */ |
53 | { "seq-end", required_argument, NULL, 'e' }, /* removed */ | |
54 | { "quiet", no_argument, NULL, 'q' }, /* removed */ | |
912541b0 KS |
55 | {} |
56 | }; | |
c7150902 YW |
57 | |
58 | int c, r; | |
912541b0 | 59 | |
5639df9a | 60 | while ((c = getopt_long(argc, argv, "t:E:Vhs:e:q", options, NULL)) >= 0) { |
7643ac9a | 61 | switch (c) { |
c7150902 YW |
62 | case 't': |
63 | r = parse_sec(optarg, &arg_timeout); | |
64 | if (r < 0) | |
65 | return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg); | |
912541b0 | 66 | break; |
912541b0 | 67 | case 'E': |
c7150902 | 68 | arg_exists = optarg; |
912541b0 | 69 | break; |
5639df9a | 70 | case 'V': |
51b006e1 | 71 | return print_version(); |
912541b0 | 72 | case 'h': |
c7150902 | 73 | return help(); |
2ac23519 LP |
74 | case 's': |
75 | case 'e': | |
76 | case 'q': | |
baaa35ad ZJS |
77 | return log_info_errno(SYNTHETIC_ERRNO(EINVAL), |
78 | "Option -%c no longer supported.", | |
79 | c); | |
7643ac9a | 80 | case '?': |
c7150902 | 81 | return -EINVAL; |
7643ac9a | 82 | default: |
c7150902 | 83 | assert_not_reached("Unknown option."); |
912541b0 | 84 | } |
9ea28c55 | 85 | } |
7643ac9a | 86 | |
c7150902 YW |
87 | return 1; |
88 | } | |
89 | ||
16627327 ZJS |
90 | static int emit_deprecation_warning(void) { |
91 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
77ee1783 LP |
92 | _cleanup_strv_free_ char **a = NULL; |
93 | _cleanup_free_ char *unit = NULL; | |
16627327 ZJS |
94 | int r; |
95 | ||
96 | r = sd_pid_get_unit(0, &unit); | |
77ee1783 LP |
97 | if (r < 0) { |
98 | log_debug_errno(r, "Failed to determine unit we run in, ignoring: %m"); | |
16627327 | 99 | return 0; |
77ee1783 | 100 | } |
16627327 | 101 | |
77ee1783 LP |
102 | if (!streq(unit, "systemd-udev-settle.service")) |
103 | return 0; | |
16627327 | 104 | |
77ee1783 | 105 | r = bus_connect_system_systemd(&bus); |
16627327 | 106 | if (r < 0) |
77ee1783 LP |
107 | log_debug_errno(r, "Failed to open connection to systemd, skipping dependency queries: %m"); |
108 | else { | |
109 | _cleanup_strv_free_ char **b = NULL; | |
110 | _cleanup_free_ char *unit_path = NULL; | |
111 | ||
112 | unit_path = unit_dbus_path_from_name("systemd-udev-settle.service"); | |
113 | if (!unit_path) | |
114 | return -ENOMEM; | |
115 | ||
116 | (void) sd_bus_get_property_strv( | |
117 | bus, | |
118 | "org.freedesktop.systemd1", | |
119 | unit_path, | |
120 | "org.freedesktop.systemd1.Unit", | |
121 | "WantedBy", | |
122 | NULL, | |
123 | &a); | |
124 | ||
125 | (void) sd_bus_get_property_strv( | |
126 | bus, | |
127 | "org.freedesktop.systemd1", | |
128 | unit_path, | |
129 | "org.freedesktop.systemd1.Unit", | |
130 | "RequiredBy", | |
131 | NULL, | |
132 | &b); | |
133 | ||
134 | r = strv_extend_strv(&a, b, true); | |
135 | if (r < 0) | |
136 | return r; | |
137 | } | |
16627327 | 138 | |
77ee1783 LP |
139 | if (strv_isempty(a)) |
140 | /* Print a simple message if we cannot determine the dependencies */ | |
141 | log_notice("systemd-udev-settle.service is deprecated."); | |
142 | else { | |
143 | /* Print a longer, structured message if we can acquire the dependencies (this should be the | |
144 | * common case). This is hooked up with a catalog entry and everything. */ | |
16627327 ZJS |
145 | _cleanup_free_ char *t = NULL; |
146 | ||
147 | t = strv_join(a, ", "); | |
148 | if (!t) | |
149 | return -ENOMEM; | |
150 | ||
77ee1783 LP |
151 | log_struct(LOG_NOTICE, |
152 | "MESSAGE=systemd-udev-settle.service is deprecated. Please fix %s not to pull it in.", t, | |
153 | "OFFENDING_UNITS=%s", t, | |
154 | "MESSAGE_ID=" SD_MESSAGE_SYSTEMD_UDEV_SETTLE_DEPRECATED_STR); | |
16627327 ZJS |
155 | } |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
c7150902 YW |
160 | int settle_main(int argc, char *argv[], void *userdata) { |
161 | _cleanup_(udev_queue_unrefp) struct udev_queue *queue = NULL; | |
c7150902 | 162 | usec_t deadline; |
0f2d351f | 163 | int r, fd; |
912541b0 | 164 | |
c7150902 YW |
165 | r = parse_argv(argc, argv); |
166 | if (r <= 0) | |
167 | return r; | |
168 | ||
c494b739 YW |
169 | if (running_in_chroot() > 0) { |
170 | log_info("Running in chroot, ignoring request."); | |
171 | return 0; | |
172 | } | |
173 | ||
c7150902 | 174 | deadline = now(CLOCK_MONOTONIC) + arg_timeout; |
0736455b | 175 | |
912541b0 KS |
176 | /* guarantee that the udev daemon isn't pre-processing */ |
177 | if (getuid() == 0) { | |
c7150902 | 178 | _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL; |
912541b0 | 179 | |
100bc5bf | 180 | if (udev_ctrl_new(&uctrl) >= 0) { |
78467aeb | 181 | r = udev_ctrl_send_ping(uctrl); |
c7150902 | 182 | if (r < 0) { |
3797776e | 183 | log_debug_errno(r, "Failed to connect to udev daemon: %m"); |
c7150902 | 184 | return 0; |
912541b0 | 185 | } |
78467aeb YW |
186 | |
187 | r = udev_ctrl_wait(uctrl, MAX(5 * USEC_PER_SEC, arg_timeout)); | |
188 | if (r < 0) | |
189 | return log_error_errno(r, "Failed to wait for daemon to reply: %m"); | |
912541b0 KS |
190 | } |
191 | } | |
192 | ||
2024ed61 | 193 | queue = udev_queue_new(NULL); |
c7150902 YW |
194 | if (!queue) |
195 | return log_error_errno(errno, "Failed to get udev queue: %m"); | |
912541b0 | 196 | |
0f2d351f LP |
197 | fd = udev_queue_get_fd(queue); |
198 | if (fd < 0) { | |
199 | log_debug_errno(fd, "Queue is empty, nothing to watch: %m"); | |
c7150902 | 200 | return 0; |
9ea28c55 | 201 | } |
912541b0 | 202 | |
16627327 ZJS |
203 | (void) emit_deprecation_warning(); |
204 | ||
9ea28c55 | 205 | for (;;) { |
c7150902 YW |
206 | if (arg_exists && access(arg_exists, F_OK) >= 0) |
207 | return 0; | |
912541b0 | 208 | |
9ea28c55 | 209 | /* exit if queue is empty */ |
c7150902 YW |
210 | if (udev_queue_get_queue_is_empty(queue)) |
211 | return 0; | |
912541b0 | 212 | |
bf23b9f8 | 213 | if (now(CLOCK_MONOTONIC) >= deadline) |
c7150902 | 214 | return -ETIMEDOUT; |
0736455b | 215 | |
c7150902 | 216 | /* wake up when queue becomes empty */ |
0f2d351f | 217 | r = fd_wait_for_event(fd, POLLIN, MSEC_PER_SEC); |
dad28bff | 218 | if (r < 0) |
0f2d351f LP |
219 | return r; |
220 | if (r & POLLIN) { | |
c7150902 YW |
221 | r = udev_queue_flush(queue); |
222 | if (r < 0) | |
223 | return log_error_errno(r, "Failed to flush queue: %m"); | |
224 | } | |
912541b0 | 225 | } |
7baada47 | 226 | } |