• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
help(void)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 
adm_settle(struct udev * udev,int argc,char * argv[])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         usec_t deadline;
60         const char *exists = NULL;
61         unsigned int timeout = 120;
62         struct pollfd pfd[1] = { {.fd = -1}, };
63         int c;
64         struct udev_queue *queue;
65         int rc = EXIT_FAILURE;
66 
67         while ((c = getopt_long(argc, argv, "t:E:hs:e:q", options, NULL)) >= 0) {
68                 switch (c) {
69 
70                 case 't': {
71                         int r;
72 
73                         r = safe_atou(optarg, &timeout);
74                         if (r < 0) {
75                                 fprintf(stderr, "Invalid timeout value '%s': %s\n",
76                                         optarg, strerror(-r));
77                                 exit(EXIT_FAILURE);
78                         };
79                         break;
80                 }
81 
82                 case 'E':
83                         exists = optarg;
84                         break;
85 
86                 case 'h':
87                         help();
88                         return EXIT_SUCCESS;
89 
90                 case 's':
91                 case 'e':
92                 case 'q':
93                         log_info("Option -%c no longer supported.", c);
94                         return EXIT_FAILURE;
95 
96                 case '?':
97                         return EXIT_FAILURE;
98 
99                 default:
100                         assert_not_reached("Unknown argument");
101                 }
102         }
103 
104         if (optind < argc) {
105                 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
106                 return EXIT_FAILURE;
107         }
108 
109         deadline = now(CLOCK_MONOTONIC) + timeout * USEC_PER_SEC;
110 
111         /* guarantee that the udev daemon isn't pre-processing */
112         if (getuid() == 0) {
113                 struct udev_ctrl *uctrl;
114 
115                 uctrl = udev_ctrl_new(udev);
116                 if (uctrl != NULL) {
117                         if (udev_ctrl_send_ping(uctrl, MAX(5U, timeout)) < 0) {
118                                 log_debug("no connection to daemon");
119                                 udev_ctrl_unref(uctrl);
120                                 return EXIT_SUCCESS;
121                         }
122                         udev_ctrl_unref(uctrl);
123                 }
124         }
125 
126         queue = udev_queue_new(udev);
127         if (!queue) {
128                 log_error("unable to get udev queue");
129                 return EXIT_FAILURE;
130         }
131 
132         pfd[0].events = POLLIN;
133         pfd[0].fd = udev_queue_get_fd(queue);
134         if (pfd[0].fd < 0) {
135                 log_debug("queue is empty, nothing to watch");
136                 rc = EXIT_SUCCESS;
137                 goto out;
138         }
139 
140         for (;;) {
141                 if (exists && access(exists, F_OK) >= 0) {
142                         rc = EXIT_SUCCESS;
143                         break;
144                 }
145 
146                 /* exit if queue is empty */
147                 if (udev_queue_get_queue_is_empty(queue)) {
148                         rc = EXIT_SUCCESS;
149                         break;
150                 }
151 
152                 if (now(CLOCK_MONOTONIC) >= deadline)
153                         break;
154 
155                 /* wake up when queue is empty */
156                 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
157                         udev_queue_flush(queue);
158         }
159 
160 out:
161         udev_queue_unref(queue);
162         return rc;
163 }
164 
165 const struct udevadm_cmd udevadm_settle = {
166         .name = "settle",
167         .cmd = adm_settle,
168         .help = "Wait for pending udev events",
169 };
170