]> git.proxmox.com Git - systemd.git/blame - src/udev/udevadm-settle.c
New upstream version 242
[systemd.git] / src / udev / udevadm-settle.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: GPL-2.0+ */
663996b3 2/*
b012e921
MB
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
663996b3
MS
5 */
6
663996b3 7#include <errno.h>
663996b3 8#include <getopt.h>
e735f4d4 9#include <poll.h>
db2df898
MP
10#include <stddef.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
663996b3 15
6e866b33
MB
16#include "libudev-util.h"
17#include "time-util.h"
18#include "udevadm.h"
19#include "udev-ctrl.h"
60f067b4 20#include "util.h"
7c20daf6 21#include "virt.h"
60f067b4 22
6e866b33
MB
23static usec_t arg_timeout = 120 * USEC_PER_SEC;
24static const char *arg_exists = NULL;
25
26static int help(void) {
52ad194e 27 printf("%s settle [OPTIONS]\n\n"
e735f4d4
MP
28 "Wait for pending udev events.\n\n"
29 " -h --help Show this help\n"
52ad194e 30 " -V --version Show package version\n"
6e866b33 31 " -t --timeout=SEC Maximum time to wait for events\n"
e735f4d4
MP
32 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
33 , program_invocation_short_name);
6e866b33
MB
34
35 return 0;
60f067b4 36}
663996b3 37
6e866b33 38static int parse_argv(int argc, char *argv[]) {
663996b3 39 static const struct option options[] = {
60f067b4 40 { "timeout", required_argument, NULL, 't' },
663996b3 41 { "exit-if-exists", required_argument, NULL, 'E' },
52ad194e 42 { "version", no_argument, NULL, 'V' },
60f067b4 43 { "help", no_argument, NULL, 'h' },
e735f4d4
MP
44 { "seq-start", required_argument, NULL, 's' }, /* removed */
45 { "seq-end", required_argument, NULL, 'e' }, /* removed */
46 { "quiet", no_argument, NULL, 'q' }, /* removed */
663996b3
MS
47 {}
48 };
6e866b33
MB
49
50 int c, r;
60f067b4 51
52ad194e 52 while ((c = getopt_long(argc, argv, "t:E:Vhs:e:q", options, NULL)) >= 0) {
60f067b4 53 switch (c) {
6e866b33
MB
54 case 't':
55 r = parse_sec(optarg, &arg_timeout);
56 if (r < 0)
57 return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
663996b3
MS
58 break;
59 case 'E':
6e866b33 60 arg_exists = optarg;
663996b3 61 break;
52ad194e 62 case 'V':
6e866b33 63 return print_version();
663996b3 64 case 'h':
6e866b33 65 return help();
e735f4d4
MP
66 case 's':
67 case 'e':
68 case 'q':
6e866b33
MB
69 return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
70 "Option -%c no longer supported.",
71 c);
60f067b4 72 case '?':
6e866b33 73 return -EINVAL;
60f067b4 74 default:
6e866b33 75 assert_not_reached("Unknown option.");
663996b3
MS
76 }
77 }
78
6e866b33
MB
79 return 1;
80}
663996b3 81
6e866b33
MB
82int settle_main(int argc, char *argv[], void *userdata) {
83 _cleanup_(udev_queue_unrefp) struct udev_queue *queue = NULL;
84 struct pollfd pfd;
85 usec_t deadline;
86 int r;
87
88 r = parse_argv(argc, argv);
89 if (r <= 0)
90 return r;
91
7c20daf6
FS
92 if (running_in_chroot() > 0) {
93 log_info("Running in chroot, ignoring request.");
94 return 0;
95 }
96
6e866b33 97 deadline = now(CLOCK_MONOTONIC) + arg_timeout;
e3bff60a 98
663996b3
MS
99 /* guarantee that the udev daemon isn't pre-processing */
100 if (getuid() == 0) {
6e866b33
MB
101 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
102
bb4f798a
MB
103 if (udev_ctrl_new(&uctrl) >= 0) {
104 r = udev_ctrl_send_ping(uctrl);
6e866b33 105 if (r < 0) {
7c20daf6 106 log_debug_errno(r, "Failed to connect to udev daemon: %m");
6e866b33 107 return 0;
663996b3 108 }
bb4f798a
MB
109
110 r = udev_ctrl_wait(uctrl, MAX(5 * USEC_PER_SEC, arg_timeout));
111 if (r < 0)
112 return log_error_errno(r, "Failed to wait for daemon to reply: %m");
663996b3
MS
113 }
114 }
115
6e866b33
MB
116 queue = udev_queue_new(NULL);
117 if (!queue)
118 return log_error_errno(errno, "Failed to get udev queue: %m");
663996b3 119
6e866b33
MB
120 r = udev_queue_get_fd(queue);
121 if (r < 0) {
122 log_debug_errno(r, "Queue is empty, nothing to watch.");
123 return 0;
60f067b4 124 }
663996b3 125
6e866b33
MB
126 pfd = (struct pollfd) {
127 .events = POLLIN,
128 .fd = r,
129 };
130
60f067b4 131 for (;;) {
6e866b33
MB
132 if (arg_exists && access(arg_exists, F_OK) >= 0)
133 return 0;
663996b3 134
60f067b4 135 /* exit if queue is empty */
6e866b33
MB
136 if (udev_queue_get_queue_is_empty(queue))
137 return 0;
663996b3 138
e3bff60a 139 if (now(CLOCK_MONOTONIC) >= deadline)
6e866b33 140 return -ETIMEDOUT;
e3bff60a 141
6e866b33
MB
142 /* wake up when queue becomes empty */
143 if (poll(&pfd, 1, MSEC_PER_SEC) > 0 && pfd.revents & POLLIN) {
144 r = udev_queue_flush(queue);
145 if (r < 0)
146 return log_error_errno(r, "Failed to flush queue: %m");
147 }
663996b3 148 }
663996b3 149}