]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-settle.c
udev: use usec_t for timeout in udev_ctrl_send_*()
[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 <string.h>
14 #include <unistd.h>
15
16 #include "libudev-util.h"
17 #include "time-util.h"
18 #include "udevadm.h"
19 #include "udev-ctrl.h"
20 #include "util.h"
21 #include "virt.h"
22
23 static usec_t arg_timeout = 120 * USEC_PER_SEC;
24 static const char *arg_exists = NULL;
25
26 static int help(void) {
27 printf("%s settle [OPTIONS]\n\n"
28 "Wait for pending udev events.\n\n"
29 " -h --help Show this help\n"
30 " -V --version Show package version\n"
31 " -t --timeout=SEC Maximum time to wait for events\n"
32 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
33 , program_invocation_short_name);
34
35 return 0;
36 }
37
38 static int parse_argv(int argc, char *argv[]) {
39 static const struct option options[] = {
40 { "timeout", required_argument, NULL, 't' },
41 { "exit-if-exists", required_argument, NULL, 'E' },
42 { "version", no_argument, NULL, 'V' },
43 { "help", no_argument, NULL, 'h' },
44 { "seq-start", required_argument, NULL, 's' }, /* removed */
45 { "seq-end", required_argument, NULL, 'e' }, /* removed */
46 { "quiet", no_argument, NULL, 'q' }, /* removed */
47 {}
48 };
49
50 int c, r;
51
52 while ((c = getopt_long(argc, argv, "t:E:Vhs:e:q", options, NULL)) >= 0) {
53 switch (c) {
54 case 't':
55 r = parse_sec(optarg, &arg_timeout);
56 if (r < 0)
57 return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
58 break;
59 case 'E':
60 arg_exists = optarg;
61 break;
62 case 'V':
63 return print_version();
64 case 'h':
65 return help();
66 case 's':
67 case 'e':
68 case 'q':
69 return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
70 "Option -%c no longer supported.",
71 c);
72 case '?':
73 return -EINVAL;
74 default:
75 assert_not_reached("Unknown option.");
76 }
77 }
78
79 return 1;
80 }
81
82 int settle_main(int argc, char *argv[], void *userdata) {
83 _cleanup_(udev_queue_unrefp) struct udev_queue *queue = NULL;
84 struct pollfd pfd;
85 usec_t deadline;
86 int r;
87
88 r = parse_argv(argc, argv);
89 if (r <= 0)
90 return r;
91
92 if (running_in_chroot() > 0) {
93 log_info("Running in chroot, ignoring request.");
94 return 0;
95 }
96
97 deadline = now(CLOCK_MONOTONIC) + arg_timeout;
98
99 /* guarantee that the udev daemon isn't pre-processing */
100 if (getuid() == 0) {
101 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
102
103 uctrl = udev_ctrl_new();
104 if (uctrl) {
105 r = udev_ctrl_send_ping(uctrl, MAX(5 * USEC_PER_SEC, arg_timeout));
106 if (r < 0) {
107 log_debug_errno(r, "Failed to connect to udev daemon: %m");
108 return 0;
109 }
110 }
111 }
112
113 queue = udev_queue_new(NULL);
114 if (!queue)
115 return log_error_errno(errno, "Failed to get udev queue: %m");
116
117 r = udev_queue_get_fd(queue);
118 if (r < 0) {
119 log_debug_errno(r, "Queue is empty, nothing to watch.");
120 return 0;
121 }
122
123 pfd = (struct pollfd) {
124 .events = POLLIN,
125 .fd = r,
126 };
127
128 for (;;) {
129 if (arg_exists && access(arg_exists, F_OK) >= 0)
130 return 0;
131
132 /* exit if queue is empty */
133 if (udev_queue_get_queue_is_empty(queue))
134 return 0;
135
136 if (now(CLOCK_MONOTONIC) >= deadline)
137 return -ETIMEDOUT;
138
139 /* wake up when queue becomes empty */
140 if (poll(&pfd, 1, MSEC_PER_SEC) > 0 && pfd.revents & POLLIN) {
141 r = udev_queue_flush(queue);
142 if (r < 0)
143 return log_error_errno(r, "Failed to flush queue: %m");
144 }
145 }
146 }