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