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