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