]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-settle.c
Merge pull request #7547 from hvenev/sysctl-no-net-default
[thirdparty/systemd.git] / src / udev / udevadm-settle.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
4 * Copyright (C) 2009 Canonical Ltd.
5 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <errno.h>
22 #include <getopt.h>
23 #include <poll.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "parse-util.h"
31 #include "udev.h"
32 #include "udevadm-util.h"
33 #include "util.h"
34
35 static void help(void) {
36 printf("%s settle [OPTIONS]\n\n"
37 "Wait for pending udev events.\n\n"
38 " -h --help Show this help\n"
39 " -V --version Show package version\n"
40 " -t --timeout=SECONDS Maximum time to wait for events\n"
41 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
42 , program_invocation_short_name);
43 }
44
45 static int adm_settle(struct udev *udev, int argc, char *argv[]) {
46 static const struct option options[] = {
47 { "timeout", required_argument, NULL, 't' },
48 { "exit-if-exists", required_argument, NULL, 'E' },
49 { "version", no_argument, NULL, 'V' },
50 { "help", no_argument, NULL, 'h' },
51 { "seq-start", required_argument, NULL, 's' }, /* removed */
52 { "seq-end", required_argument, NULL, 'e' }, /* removed */
53 { "quiet", no_argument, NULL, 'q' }, /* removed */
54 {}
55 };
56 usec_t deadline;
57 const char *exists = NULL;
58 unsigned int timeout = 120;
59 struct pollfd pfd[1] = { {.fd = -1}, };
60 int c;
61 struct udev_queue *queue;
62 int rc = EXIT_FAILURE;
63
64 while ((c = getopt_long(argc, argv, "t:E:Vhs:e:q", options, NULL)) >= 0) {
65 switch (c) {
66
67 case 't': {
68 int r;
69
70 r = safe_atou(optarg, &timeout);
71 if (r < 0) {
72 log_error_errno(r, "Invalid timeout value '%s': %m", optarg);
73 return EXIT_FAILURE;
74 }
75 break;
76 }
77
78 case 'E':
79 exists = optarg;
80 break;
81
82 case 'V':
83 print_version();
84 return EXIT_SUCCESS;
85
86 case 'h':
87 help();
88 return EXIT_SUCCESS;
89
90 case 's':
91 case 'e':
92 case 'q':
93 log_info("Option -%c no longer supported.", c);
94 return EXIT_FAILURE;
95
96 case '?':
97 return EXIT_FAILURE;
98
99 default:
100 assert_not_reached("Unknown argument");
101 }
102 }
103
104 if (optind < argc) {
105 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
106 return EXIT_FAILURE;
107 }
108
109 deadline = now(CLOCK_MONOTONIC) + timeout * USEC_PER_SEC;
110
111 /* guarantee that the udev daemon isn't pre-processing */
112 if (getuid() == 0) {
113 struct udev_ctrl *uctrl;
114
115 uctrl = udev_ctrl_new(udev);
116 if (uctrl != NULL) {
117 if (udev_ctrl_send_ping(uctrl, MAX(5U, timeout)) < 0) {
118 log_debug("no connection to daemon");
119 udev_ctrl_unref(uctrl);
120 return EXIT_SUCCESS;
121 }
122 udev_ctrl_unref(uctrl);
123 }
124 }
125
126 queue = udev_queue_new(udev);
127 if (!queue) {
128 log_error("unable to get udev queue");
129 return EXIT_FAILURE;
130 }
131
132 pfd[0].events = POLLIN;
133 pfd[0].fd = udev_queue_get_fd(queue);
134 if (pfd[0].fd < 0) {
135 log_debug("queue is empty, nothing to watch");
136 rc = EXIT_SUCCESS;
137 goto out;
138 }
139
140 for (;;) {
141 if (exists && access(exists, F_OK) >= 0) {
142 rc = EXIT_SUCCESS;
143 break;
144 }
145
146 /* exit if queue is empty */
147 if (udev_queue_get_queue_is_empty(queue)) {
148 rc = EXIT_SUCCESS;
149 break;
150 }
151
152 if (now(CLOCK_MONOTONIC) >= deadline)
153 break;
154
155 /* wake up when queue is empty */
156 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
157 udev_queue_flush(queue);
158 }
159
160 out:
161 udev_queue_unref(queue);
162 return rc;
163 }
164
165 const struct udevadm_cmd udevadm_settle = {
166 .name = "settle",
167 .cmd = adm_settle,
168 .help = "Wait for pending udev events",
169 };