]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-settle.c
Merge pull request #1527 from keszybz/lz4
[thirdparty/systemd.git] / src / udev / udevadm-settle.c
1 /*
2 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
5 *
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.
10 *
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/>.
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 <getopt.h>
27 #include <poll.h>
28
29 #include "udev.h"
30 #include "util.h"
31
32 static void help(void) {
33 printf("%s settle OPTIONS\n\n"
34 "Wait for pending udev events.\n\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " -t --timeout=SECONDS Maximum time to wait for events\n"
38 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
39 , program_invocation_short_name);
40 }
41
42 static int adm_settle(struct udev *udev, int argc, char *argv[]) {
43 static const struct option options[] = {
44 { "timeout", required_argument, NULL, 't' },
45 { "exit-if-exists", required_argument, NULL, 'E' },
46 { "help", no_argument, NULL, 'h' },
47 { "seq-start", required_argument, NULL, 's' }, /* removed */
48 { "seq-end", required_argument, NULL, 'e' }, /* removed */
49 { "quiet", no_argument, NULL, 'q' }, /* removed */
50 {}
51 };
52 usec_t deadline;
53 const char *exists = NULL;
54 unsigned int timeout = 120;
55 struct pollfd pfd[1] = { {.fd = -1}, };
56 int c;
57 struct udev_queue *queue;
58 int rc = EXIT_FAILURE;
59
60 while ((c = getopt_long(argc, argv, "t:E:hs:e:q", options, NULL)) >= 0) {
61 switch (c) {
62
63 case 't': {
64 int r;
65
66 r = safe_atou(optarg, &timeout);
67 if (r < 0) {
68 log_error_errno(r, "Invalid timeout value '%s': %m", optarg);
69 return EXIT_FAILURE;
70 }
71 break;
72 }
73
74 case 'E':
75 exists = optarg;
76 break;
77
78 case 'h':
79 help();
80 return EXIT_SUCCESS;
81
82 case 's':
83 case 'e':
84 case 'q':
85 log_info("Option -%c no longer supported.", c);
86 return EXIT_FAILURE;
87
88 case '?':
89 return EXIT_FAILURE;
90
91 default:
92 assert_not_reached("Unknown argument");
93 }
94 }
95
96 if (optind < argc) {
97 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
98 return EXIT_FAILURE;
99 }
100
101 deadline = now(CLOCK_MONOTONIC) + timeout * USEC_PER_SEC;
102
103 /* guarantee that the udev daemon isn't pre-processing */
104 if (getuid() == 0) {
105 struct udev_ctrl *uctrl;
106
107 uctrl = udev_ctrl_new(udev);
108 if (uctrl != NULL) {
109 if (udev_ctrl_send_ping(uctrl, MAX(5U, timeout)) < 0) {
110 log_debug("no connection to daemon");
111 udev_ctrl_unref(uctrl);
112 return EXIT_SUCCESS;
113 }
114 udev_ctrl_unref(uctrl);
115 }
116 }
117
118 queue = udev_queue_new(udev);
119 if (!queue) {
120 log_error("unable to get udev queue");
121 return EXIT_FAILURE;
122 }
123
124 pfd[0].events = POLLIN;
125 pfd[0].fd = udev_queue_get_fd(queue);
126 if (pfd[0].fd < 0) {
127 log_debug("queue is empty, nothing to watch");
128 rc = EXIT_SUCCESS;
129 goto out;
130 }
131
132 for (;;) {
133 if (exists && access(exists, F_OK) >= 0) {
134 rc = EXIT_SUCCESS;
135 break;
136 }
137
138 /* exit if queue is empty */
139 if (udev_queue_get_queue_is_empty(queue)) {
140 rc = EXIT_SUCCESS;
141 break;
142 }
143
144 if (now(CLOCK_MONOTONIC) >= deadline)
145 break;
146
147 /* wake up when queue is empty */
148 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
149 udev_queue_flush(queue);
150 }
151
152 out:
153 udev_queue_unref(queue);
154 return rc;
155 }
156
157 const struct udevadm_cmd udevadm_settle = {
158 .name = "settle",
159 .cmd = adm_settle,
160 .help = "Wait for pending udev events",
161 };