]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-settle.c
Merge pull request #7549 from poettering/ptyfwd-fixes
[thirdparty/systemd.git] / src / udev / udevadm-settle.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
7baada47 2/*
fc206fbe 3 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
bb38678e
SJR
4 * Copyright (C) 2009 Canonical Ltd.
5 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
7baada47 6 *
55e9959b
KS
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.
7baada47 11 *
55e9959b
KS
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/>.
7baada47
KS
19 */
20
7baada47 21#include <errno.h>
c2666405 22#include <getopt.h>
0a6f50c0 23#include <poll.h>
cf0fbc49
TA
24#include <stddef.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
7baada47 29
6bedfcbb 30#include "parse-util.h"
7baada47 31#include "udev.h"
5639df9a 32#include "udevadm-util.h"
4e93793d 33#include "util.h"
7baada47 34
7643ac9a 35static void help(void) {
5639df9a 36 printf("%s settle [OPTIONS]\n\n"
5ac0162c
LP
37 "Wait for pending udev events.\n\n"
38 " -h --help Show this help\n"
5639df9a 39 " -V --version Show package version\n"
5ac0162c
LP
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);
7643ac9a
ZJS
43}
44
9ec6e95b 45static int adm_settle(struct udev *udev, int argc, char *argv[]) {
912541b0 46 static const struct option options[] = {
7643ac9a 47 { "timeout", required_argument, NULL, 't' },
912541b0 48 { "exit-if-exists", required_argument, NULL, 'E' },
5639df9a 49 { "version", no_argument, NULL, 'V' },
7643ac9a 50 { "help", no_argument, NULL, 'h' },
2ac23519
LP
51 { "seq-start", required_argument, NULL, 's' }, /* removed */
52 { "seq-end", required_argument, NULL, 'e' }, /* removed */
53 { "quiet", no_argument, NULL, 'q' }, /* removed */
912541b0
KS
54 {}
55 };
0736455b 56 usec_t deadline;
912541b0
KS
57 const char *exists = NULL;
58 unsigned int timeout = 120;
b49d9b50 59 struct pollfd pfd[1] = { {.fd = -1}, };
14cb7336
KS
60 int c;
61 struct udev_queue *queue;
62 int rc = EXIT_FAILURE;
912541b0 63
5639df9a 64 while ((c = getopt_long(argc, argv, "t:E:Vhs:e:q", options, NULL)) >= 0) {
7643ac9a 65 switch (c) {
2ac23519 66
4e93793d
YZ
67 case 't': {
68 int r;
69
70 r = safe_atou(optarg, &timeout);
71 if (r < 0) {
e53fc357
LP
72 log_error_errno(r, "Invalid timeout value '%s': %m", optarg);
73 return EXIT_FAILURE;
74 }
912541b0 75 break;
7643ac9a 76 }
2ac23519 77
912541b0
KS
78 case 'E':
79 exists = optarg;
80 break;
2ac23519 81
5639df9a
YW
82 case 'V':
83 print_version();
84 return EXIT_SUCCESS;
85
912541b0 86 case 'h':
7643ac9a 87 help();
14cb7336 88 return EXIT_SUCCESS;
2ac23519
LP
89
90 case 's':
91 case 'e':
92 case 'q':
93 log_info("Option -%c no longer supported.", c);
94 return EXIT_FAILURE;
95
7643ac9a 96 case '?':
14cb7336 97 return EXIT_FAILURE;
2ac23519 98
7643ac9a 99 default:
6f285378 100 assert_not_reached("Unknown argument");
912541b0 101 }
9ea28c55 102 }
7643ac9a
ZJS
103
104 if (optind < argc) {
105 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
14cb7336 106 return EXIT_FAILURE;
912541b0
KS
107 }
108
0736455b
NS
109 deadline = now(CLOCK_MONOTONIC) + timeout * USEC_PER_SEC;
110
912541b0
KS
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) {
7375b3c4 117 if (udev_ctrl_send_ping(uctrl, MAX(5U, timeout)) < 0) {
9f6445e3 118 log_debug("no connection to daemon");
912541b0 119 udev_ctrl_unref(uctrl);
14cb7336 120 return EXIT_SUCCESS;
912541b0
KS
121 }
122 udev_ctrl_unref(uctrl);
123 }
124 }
125
14cb7336
KS
126 queue = udev_queue_new(udev);
127 if (!queue) {
128 log_error("unable to get udev queue");
129 return EXIT_FAILURE;
912541b0
KS
130 }
131
14cb7336
KS
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;
9ea28c55
KS
137 goto out;
138 }
912541b0 139
9ea28c55
KS
140 for (;;) {
141 if (exists && access(exists, F_OK) >= 0) {
912541b0
KS
142 rc = EXIT_SUCCESS;
143 break;
144 }
145
9ea28c55 146 /* exit if queue is empty */
14cb7336 147 if (udev_queue_get_queue_is_empty(queue)) {
9ea28c55
KS
148 rc = EXIT_SUCCESS;
149 break;
912541b0
KS
150 }
151
bf23b9f8 152 if (now(CLOCK_MONOTONIC) >= deadline)
0736455b
NS
153 break;
154
14cb7336 155 /* wake up when queue is empty */
8a7a0c19 156 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
14cb7336 157 udev_queue_flush(queue);
912541b0 158 }
9ea28c55 159
ff2c503d 160out:
14cb7336 161 udev_queue_unref(queue);
912541b0 162 return rc;
7baada47 163}
1985c76e
KS
164
165const struct udevadm_cmd udevadm_settle = {
912541b0
KS
166 .name = "settle",
167 .cmd = adm_settle,
5ac0162c 168 .help = "Wait for pending udev events",
1985c76e 169};