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