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