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