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