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