]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-settle.c
Merge pull request #1909 from keszybz/filetriggers-v2
[thirdparty/systemd.git] / src / udev / udevadm-settle.c
1 /*
2 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
5 *
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.
10 *
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/>.
18 */
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <poll.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "parse-util.h"
30 #include "udev.h"
31 #include "util.h"
32
33 static void help(void) {
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);
41 }
42
43 static int adm_settle(struct udev *udev, int argc, char *argv[]) {
44 static const struct option options[] = {
45 { "timeout", required_argument, NULL, 't' },
46 { "exit-if-exists", required_argument, NULL, 'E' },
47 { "help", no_argument, NULL, 'h' },
48 { "seq-start", required_argument, NULL, 's' }, /* removed */
49 { "seq-end", required_argument, NULL, 'e' }, /* removed */
50 { "quiet", no_argument, NULL, 'q' }, /* removed */
51 {}
52 };
53 usec_t deadline;
54 const char *exists = NULL;
55 unsigned int timeout = 120;
56 struct pollfd pfd[1] = { {.fd = -1}, };
57 int c;
58 struct udev_queue *queue;
59 int rc = EXIT_FAILURE;
60
61 while ((c = getopt_long(argc, argv, "t:E:hs:e:q", options, NULL)) >= 0) {
62 switch (c) {
63
64 case 't': {
65 int r;
66
67 r = safe_atou(optarg, &timeout);
68 if (r < 0) {
69 log_error_errno(r, "Invalid timeout value '%s': %m", optarg);
70 return EXIT_FAILURE;
71 }
72 break;
73 }
74
75 case 'E':
76 exists = optarg;
77 break;
78
79 case 'h':
80 help();
81 return EXIT_SUCCESS;
82
83 case 's':
84 case 'e':
85 case 'q':
86 log_info("Option -%c no longer supported.", c);
87 return EXIT_FAILURE;
88
89 case '?':
90 return EXIT_FAILURE;
91
92 default:
93 assert_not_reached("Unknown argument");
94 }
95 }
96
97 if (optind < argc) {
98 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
99 return EXIT_FAILURE;
100 }
101
102 deadline = now(CLOCK_MONOTONIC) + timeout * USEC_PER_SEC;
103
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) {
110 if (udev_ctrl_send_ping(uctrl, MAX(5U, timeout)) < 0) {
111 log_debug("no connection to daemon");
112 udev_ctrl_unref(uctrl);
113 return EXIT_SUCCESS;
114 }
115 udev_ctrl_unref(uctrl);
116 }
117 }
118
119 queue = udev_queue_new(udev);
120 if (!queue) {
121 log_error("unable to get udev queue");
122 return EXIT_FAILURE;
123 }
124
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;
130 goto out;
131 }
132
133 for (;;) {
134 if (exists && access(exists, F_OK) >= 0) {
135 rc = EXIT_SUCCESS;
136 break;
137 }
138
139 /* exit if queue is empty */
140 if (udev_queue_get_queue_is_empty(queue)) {
141 rc = EXIT_SUCCESS;
142 break;
143 }
144
145 if (now(CLOCK_MONOTONIC) >= deadline)
146 break;
147
148 /* wake up when queue is empty */
149 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
150 udev_queue_flush(queue);
151 }
152
153 out:
154 udev_queue_unref(queue);
155 return rc;
156 }
157
158 const struct udevadm_cmd udevadm_settle = {
159 .name = "settle",
160 .cmd = adm_settle,
161 .help = "Wait for pending udev events",
162 };