chiark / gitweb /
sd-network: expose both admin and operational state directly
[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/inotify.h>
33 #include <sys/poll.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include "udev.h"
38 #include "udev-util.h"
39 #include "util.h"
40
41 static void help(void) {
42         printf("Usage: udevadm settle OPTIONS\n"
43                "  -t,--timeout=<seconds>     maximum time to wait for events\n"
44                "  -E,--exit-if-exists=<file> stop waiting if file exists\n"
45                "  -h,--help\n\n");
46 }
47
48 static int adm_settle(struct udev *udev, int argc, char *argv[])
49 {
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 rc = EXIT_FAILURE, c;
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                         exit(EXIT_SUCCESS);
83                 case '?':
84                         exit(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                 exit(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                                 rc = EXIT_SUCCESS;
105                                 goto out;
106                         }
107                         udev_ctrl_unref(uctrl);
108                 }
109         }
110
111         pfd[0].events = POLLIN;
112         pfd[0].fd = inotify_init1(IN_CLOEXEC);
113         if (pfd[0].fd < 0) {
114                 log_error("inotify_init failed: %m");
115                 goto out;
116         }
117
118         if (inotify_add_watch(pfd[0].fd, "/run/udev/queue" , IN_DELETE) < 0) {
119                 log_debug("watching /run/udev failed");
120                 goto out;
121         }
122
123         for (;;) {
124                 if (exists && access(exists, F_OK) >= 0) {
125                         rc = EXIT_SUCCESS;
126                         break;
127                 }
128
129                 /* exit if queue is empty */
130                 if (access("/run/udev/queue", F_OK) < 0) {
131                         rc = EXIT_SUCCESS;
132                         break;
133                 }
134
135                 /* wake up when "queue" file is deleted */
136                 if (poll(pfd, 1, 100) > 0 && pfd[0].revents & POLLIN) {
137                         char buf[sizeof(struct inotify_event) + PATH_MAX];
138
139                         read(pfd[0].fd, buf, sizeof(buf));
140                 }
141         }
142
143 out:
144         if (pfd[0].fd >= 0)
145                 close(pfd[0].fd);
146         return rc;
147 }
148
149 const struct udevadm_cmd udevadm_settle = {
150         .name = "settle",
151         .cmd = adm_settle,
152         .help = "wait for pending udev events",
153 };