]>
Commit | Line | Data |
---|---|---|
7baada47 | 1 | /* |
fc206fbe | 2 | * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org> |
bb38678e SJR |
3 | * Copyright (C) 2009 Canonical Ltd. |
4 | * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com> | |
7baada47 | 5 | * |
55e9959b KS |
6 | * This program is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 2 of the License, or | |
9 | * (at your option) any later version. | |
7baada47 | 10 | * |
55e9959b KS |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
7baada47 KS |
18 | */ |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <stddef.h> | |
22 | #include <string.h> | |
23 | #include <stdio.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
26 | #include <dirent.h> | |
27 | #include <fcntl.h> | |
28 | #include <syslog.h> | |
c2666405 | 29 | #include <getopt.h> |
c2c24d4d | 30 | #include <signal.h> |
959e8b5d | 31 | #include <time.h> |
a3eca08b | 32 | #include <sys/poll.h> |
7baada47 KS |
33 | #include <sys/stat.h> |
34 | #include <sys/types.h> | |
35 | ||
36 | #include "udev.h" | |
44433ebd | 37 | #include "udev-util.h" |
4e93793d | 38 | #include "util.h" |
7baada47 | 39 | |
7643ac9a ZJS |
40 | static void help(void) { |
41 | printf("Usage: udevadm settle OPTIONS\n" | |
42 | " -t,--timeout=<seconds> maximum time to wait for events\n" | |
7643ac9a | 43 | " -E,--exit-if-exists=<file> stop waiting if file exists\n" |
7643ac9a ZJS |
44 | " -h,--help\n\n"); |
45 | } | |
46 | ||
1985c76e | 47 | static int adm_settle(struct udev *udev, int argc, char *argv[]) |
7baada47 | 48 | { |
912541b0 | 49 | static const struct option options[] = { |
9ea28c55 KS |
50 | { "seq-start", required_argument, NULL, '\0' }, /* removed */ |
51 | { "seq-end", required_argument, NULL, '\0' }, /* removed */ | |
7643ac9a | 52 | { "timeout", required_argument, NULL, 't' }, |
912541b0 | 53 | { "exit-if-exists", required_argument, NULL, 'E' }, |
9ea28c55 | 54 | { "quiet", no_argument, NULL, 'q' }, /* removed */ |
7643ac9a | 55 | { "help", no_argument, NULL, 'h' }, |
912541b0 KS |
56 | {} |
57 | }; | |
912541b0 KS |
58 | const char *exists = NULL; |
59 | unsigned int timeout = 120; | |
b49d9b50 | 60 | struct pollfd pfd[1] = { {.fd = -1}, }; |
14cb7336 KS |
61 | int c; |
62 | struct udev_queue *queue; | |
63 | int rc = EXIT_FAILURE; | |
912541b0 | 64 | |
9ea28c55 | 65 | while ((c = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL)) >= 0) { |
7643ac9a | 66 | switch (c) { |
4e93793d YZ |
67 | case 't': { |
68 | int r; | |
69 | ||
70 | r = safe_atou(optarg, &timeout); | |
71 | if (r < 0) { | |
72 | fprintf(stderr, "Invalid timeout value '%s': %s\n", | |
73 | optarg, strerror(-r)); | |
74 | exit(EXIT_FAILURE); | |
75 | }; | |
912541b0 | 76 | break; |
7643ac9a | 77 | } |
912541b0 KS |
78 | case 'E': |
79 | exists = optarg; | |
80 | break; | |
81 | case 'h': | |
7643ac9a | 82 | help(); |
14cb7336 | 83 | return EXIT_SUCCESS; |
7643ac9a | 84 | case '?': |
14cb7336 | 85 | return EXIT_FAILURE; |
7643ac9a | 86 | default: |
6f285378 | 87 | assert_not_reached("Unknown argument"); |
912541b0 | 88 | } |
9ea28c55 | 89 | } |
7643ac9a ZJS |
90 | |
91 | if (optind < argc) { | |
92 | fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]); | |
14cb7336 | 93 | return EXIT_FAILURE; |
912541b0 KS |
94 | } |
95 | ||
912541b0 KS |
96 | /* guarantee that the udev daemon isn't pre-processing */ |
97 | if (getuid() == 0) { | |
98 | struct udev_ctrl *uctrl; | |
99 | ||
100 | uctrl = udev_ctrl_new(udev); | |
101 | if (uctrl != NULL) { | |
102 | if (udev_ctrl_send_ping(uctrl, timeout) < 0) { | |
9f6445e3 | 103 | log_debug("no connection to daemon"); |
912541b0 | 104 | udev_ctrl_unref(uctrl); |
14cb7336 | 105 | return EXIT_SUCCESS; |
912541b0 KS |
106 | } |
107 | udev_ctrl_unref(uctrl); | |
108 | } | |
109 | } | |
110 | ||
14cb7336 KS |
111 | queue = udev_queue_new(udev); |
112 | if (!queue) { | |
113 | log_error("unable to get udev queue"); | |
114 | return EXIT_FAILURE; | |
912541b0 KS |
115 | } |
116 | ||
14cb7336 KS |
117 | pfd[0].events = POLLIN; |
118 | pfd[0].fd = udev_queue_get_fd(queue); | |
119 | if (pfd[0].fd < 0) { | |
120 | log_debug("queue is empty, nothing to watch"); | |
121 | rc = EXIT_SUCCESS; | |
9ea28c55 KS |
122 | goto out; |
123 | } | |
912541b0 | 124 | |
9ea28c55 KS |
125 | for (;;) { |
126 | if (exists && access(exists, F_OK) >= 0) { | |
912541b0 KS |
127 | rc = EXIT_SUCCESS; |
128 | break; | |
129 | } | |
130 | ||
9ea28c55 | 131 | /* exit if queue is empty */ |
14cb7336 | 132 | if (udev_queue_get_queue_is_empty(queue)) { |
9ea28c55 KS |
133 | rc = EXIT_SUCCESS; |
134 | break; | |
912541b0 KS |
135 | } |
136 | ||
14cb7336 KS |
137 | /* wake up when queue is empty */ |
138 | if (poll(pfd, 1, 100) > 0 && pfd[0].revents & POLLIN) | |
139 | udev_queue_flush(queue); | |
912541b0 | 140 | } |
9ea28c55 | 141 | |
ff2c503d | 142 | out: |
14cb7336 | 143 | udev_queue_unref(queue); |
912541b0 | 144 | return rc; |
7baada47 | 145 | } |
1985c76e KS |
146 | |
147 | const struct udevadm_cmd udevadm_settle = { | |
912541b0 KS |
148 | .name = "settle", |
149 | .cmd = adm_settle, | |
9ea28c55 | 150 | .help = "wait for pending udev events", |
1985c76e | 151 | }; |