]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-settle.c
rules: fix tests for removable state
[thirdparty/systemd.git] / src / udev / udevadm-settle.c
CommitLineData
7baada47 1/*
fc206fbe 2 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
bb38678e
SJR
3 * Copyright (C) 2009 Canonical Ltd.
4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
7baada47 5 *
55e9959b
KS
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.
7baada47 10 *
55e9959b
KS
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/>.
7baada47
KS
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>
c2666405 26#include <getopt.h>
0a6f50c0 27#include <poll.h>
7baada47
KS
28
29#include "udev.h"
4e93793d 30#include "util.h"
7baada47 31
7643ac9a 32static void help(void) {
5ac0162c
LP
33 printf("%s settle OPTIONS\n\n"
34 "Wait for pending udev events.\n\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " -t --timeout=SECONDS Maximum time to wait for events\n"
38 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
39 , program_invocation_short_name);
7643ac9a
ZJS
40}
41
9ec6e95b 42static int adm_settle(struct udev *udev, int argc, char *argv[]) {
912541b0 43 static const struct option options[] = {
7643ac9a 44 { "timeout", required_argument, NULL, 't' },
912541b0 45 { "exit-if-exists", required_argument, NULL, 'E' },
7643ac9a 46 { "help", no_argument, NULL, 'h' },
2ac23519
LP
47 { "seq-start", required_argument, NULL, 's' }, /* removed */
48 { "seq-end", required_argument, NULL, 'e' }, /* removed */
49 { "quiet", no_argument, NULL, 'q' }, /* removed */
912541b0
KS
50 {}
51 };
912541b0
KS
52 const char *exists = NULL;
53 unsigned int timeout = 120;
b49d9b50 54 struct pollfd pfd[1] = { {.fd = -1}, };
14cb7336
KS
55 int c;
56 struct udev_queue *queue;
57 int rc = EXIT_FAILURE;
912541b0 58
2ac23519 59 while ((c = getopt_long(argc, argv, "t:E:hs:e:q", options, NULL)) >= 0) {
7643ac9a 60 switch (c) {
2ac23519 61
4e93793d
YZ
62 case 't': {
63 int r;
64
65 r = safe_atou(optarg, &timeout);
66 if (r < 0) {
67 fprintf(stderr, "Invalid timeout value '%s': %s\n",
68 optarg, strerror(-r));
69 exit(EXIT_FAILURE);
70 };
912541b0 71 break;
7643ac9a 72 }
2ac23519 73
912541b0
KS
74 case 'E':
75 exists = optarg;
76 break;
2ac23519 77
912541b0 78 case 'h':
7643ac9a 79 help();
14cb7336 80 return EXIT_SUCCESS;
2ac23519
LP
81
82 case 's':
83 case 'e':
84 case 'q':
85 log_info("Option -%c no longer supported.", c);
86 return EXIT_FAILURE;
87
7643ac9a 88 case '?':
14cb7336 89 return EXIT_FAILURE;
2ac23519 90
7643ac9a 91 default:
6f285378 92 assert_not_reached("Unknown argument");
912541b0 93 }
9ea28c55 94 }
7643ac9a
ZJS
95
96 if (optind < argc) {
97 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
14cb7336 98 return EXIT_FAILURE;
912541b0
KS
99 }
100
912541b0
KS
101 /* guarantee that the udev daemon isn't pre-processing */
102 if (getuid() == 0) {
103 struct udev_ctrl *uctrl;
104
105 uctrl = udev_ctrl_new(udev);
106 if (uctrl != NULL) {
107 if (udev_ctrl_send_ping(uctrl, timeout) < 0) {
9f6445e3 108 log_debug("no connection to daemon");
912541b0 109 udev_ctrl_unref(uctrl);
14cb7336 110 return EXIT_SUCCESS;
912541b0
KS
111 }
112 udev_ctrl_unref(uctrl);
113 }
114 }
115
14cb7336
KS
116 queue = udev_queue_new(udev);
117 if (!queue) {
118 log_error("unable to get udev queue");
119 return EXIT_FAILURE;
912541b0
KS
120 }
121
14cb7336
KS
122 pfd[0].events = POLLIN;
123 pfd[0].fd = udev_queue_get_fd(queue);
124 if (pfd[0].fd < 0) {
125 log_debug("queue is empty, nothing to watch");
126 rc = EXIT_SUCCESS;
9ea28c55
KS
127 goto out;
128 }
912541b0 129
9ea28c55
KS
130 for (;;) {
131 if (exists && access(exists, F_OK) >= 0) {
912541b0
KS
132 rc = EXIT_SUCCESS;
133 break;
134 }
135
9ea28c55 136 /* exit if queue is empty */
14cb7336 137 if (udev_queue_get_queue_is_empty(queue)) {
9ea28c55
KS
138 rc = EXIT_SUCCESS;
139 break;
912541b0
KS
140 }
141
14cb7336 142 /* wake up when queue is empty */
8a7a0c19 143 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
14cb7336 144 udev_queue_flush(queue);
912541b0 145 }
9ea28c55 146
ff2c503d 147out:
14cb7336 148 udev_queue_unref(queue);
912541b0 149 return rc;
7baada47 150}
1985c76e
KS
151
152const struct udevadm_cmd udevadm_settle = {
912541b0
KS
153 .name = "settle",
154 .cmd = adm_settle,
5ac0162c 155 .help = "Wait for pending udev events",
1985c76e 156};