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