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