]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-settle.c
treewide: use log_*_errno whenever %m is in the format string
[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 <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <getopt.h>
29 #include <signal.h>
30 #include <time.h>
31 #include <sys/poll.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include "udev.h"
36 #include "udev-util.h"
37 #include "util.h"
38
39 static void help(void) {
40 printf("Usage: udevadm settle OPTIONS\n"
41 " -t,--timeout=<seconds> maximum time to wait for events\n"
42 " -E,--exit-if-exists=<file> stop waiting if file exists\n"
43 " -h,--help\n\n");
44 }
45
46 static int adm_settle(struct udev *udev, int argc, char *argv[]) {
47 static const struct option options[] = {
48 { "seq-start", required_argument, NULL, '\0' }, /* removed */
49 { "seq-end", required_argument, NULL, '\0' }, /* removed */
50 { "timeout", required_argument, NULL, 't' },
51 { "exit-if-exists", required_argument, NULL, 'E' },
52 { "quiet", no_argument, NULL, 'q' }, /* removed */
53 { "help", no_argument, NULL, 'h' },
54 {}
55 };
56 const char *exists = NULL;
57 unsigned int timeout = 120;
58 struct pollfd pfd[1] = { {.fd = -1}, };
59 int c;
60 struct udev_queue *queue;
61 int rc = EXIT_FAILURE;
62
63 while ((c = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL)) >= 0) {
64 switch (c) {
65 case 't': {
66 int r;
67
68 r = safe_atou(optarg, &timeout);
69 if (r < 0) {
70 fprintf(stderr, "Invalid timeout value '%s': %s\n",
71 optarg, strerror(-r));
72 exit(EXIT_FAILURE);
73 };
74 break;
75 }
76 case 'E':
77 exists = optarg;
78 break;
79 case 'h':
80 help();
81 return EXIT_SUCCESS;
82 case '?':
83 return EXIT_FAILURE;
84 default:
85 assert_not_reached("Unknown argument");
86 }
87 }
88
89 if (optind < argc) {
90 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
91 return EXIT_FAILURE;
92 }
93
94 /* guarantee that the udev daemon isn't pre-processing */
95 if (getuid() == 0) {
96 struct udev_ctrl *uctrl;
97
98 uctrl = udev_ctrl_new(udev);
99 if (uctrl != NULL) {
100 if (udev_ctrl_send_ping(uctrl, timeout) < 0) {
101 log_debug("no connection to daemon");
102 udev_ctrl_unref(uctrl);
103 return EXIT_SUCCESS;
104 }
105 udev_ctrl_unref(uctrl);
106 }
107 }
108
109 queue = udev_queue_new(udev);
110 if (!queue) {
111 log_error("unable to get udev queue");
112 return EXIT_FAILURE;
113 }
114
115 pfd[0].events = POLLIN;
116 pfd[0].fd = udev_queue_get_fd(queue);
117 if (pfd[0].fd < 0) {
118 log_debug("queue is empty, nothing to watch");
119 rc = EXIT_SUCCESS;
120 goto out;
121 }
122
123 for (;;) {
124 if (exists && access(exists, F_OK) >= 0) {
125 rc = EXIT_SUCCESS;
126 break;
127 }
128
129 /* exit if queue is empty */
130 if (udev_queue_get_queue_is_empty(queue)) {
131 rc = EXIT_SUCCESS;
132 break;
133 }
134
135 /* wake up when queue is empty */
136 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
137 udev_queue_flush(queue);
138 }
139
140 out:
141 udev_queue_unref(queue);
142 return rc;
143 }
144
145 const struct udevadm_cmd udevadm_settle = {
146 .name = "settle",
147 .cmd = adm_settle,
148 .help = "wait for pending udev events",
149 };