]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-settle.c
Merge pull request #2056 from evverx/expose-soft-limits-on-the-bus
[thirdparty/systemd.git] / src / udev / udevadm-settle.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
5 * Copyright (C) 2009 Canonical Ltd.
6 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <errno.h>
23 #include <getopt.h>
24 #include <poll.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "parse-util.h"
32 #include "udev.h"
33 #include "util.h"
34
35 static void help(void) {
36 printf("%s settle OPTIONS\n\n"
37 "Wait for pending udev events.\n\n"
38 " -h --help Show this help\n"
39 " --version Show package version\n"
40 " -t --timeout=SECONDS Maximum time to wait for events\n"
41 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
42 , program_invocation_short_name);
43 }
44
45 static int adm_settle(struct udev *udev, int argc, char *argv[]) {
46 static const struct option options[] = {
47 { "timeout", required_argument, NULL, 't' },
48 { "exit-if-exists", required_argument, NULL, 'E' },
49 { "help", no_argument, NULL, 'h' },
50 { "seq-start", required_argument, NULL, 's' }, /* removed */
51 { "seq-end", required_argument, NULL, 'e' }, /* removed */
52 { "quiet", no_argument, NULL, 'q' }, /* removed */
53 {}
54 };
55 usec_t deadline;
56 const char *exists = NULL;
57 unsigned int timeout = 120;
58 struct pollfd pfd[1] = { {.fd = -1}, };
59 int c;
60 struct udev_queue *queue;
61 int rc = EXIT_FAILURE;
62
63 while ((c = getopt_long(argc, argv, "t:E:hs:e:q", options, NULL)) >= 0) {
64 switch (c) {
65
66 case 't': {
67 int r;
68
69 r = safe_atou(optarg, &timeout);
70 if (r < 0) {
71 log_error_errno(r, "Invalid timeout value '%s': %m", optarg);
72 return EXIT_FAILURE;
73 }
74 break;
75 }
76
77 case 'E':
78 exists = optarg;
79 break;
80
81 case 'h':
82 help();
83 return EXIT_SUCCESS;
84
85 case 's':
86 case 'e':
87 case 'q':
88 log_info("Option -%c no longer supported.", c);
89 return EXIT_FAILURE;
90
91 case '?':
92 return EXIT_FAILURE;
93
94 default:
95 assert_not_reached("Unknown argument");
96 }
97 }
98
99 if (optind < argc) {
100 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
101 return EXIT_FAILURE;
102 }
103
104 deadline = now(CLOCK_MONOTONIC) + timeout * USEC_PER_SEC;
105
106 /* guarantee that the udev daemon isn't pre-processing */
107 if (getuid() == 0) {
108 struct udev_ctrl *uctrl;
109
110 uctrl = udev_ctrl_new(udev);
111 if (uctrl != NULL) {
112 if (udev_ctrl_send_ping(uctrl, MAX(5U, timeout)) < 0) {
113 log_debug("no connection to daemon");
114 udev_ctrl_unref(uctrl);
115 return EXIT_SUCCESS;
116 }
117 udev_ctrl_unref(uctrl);
118 }
119 }
120
121 queue = udev_queue_new(udev);
122 if (!queue) {
123 log_error("unable to get udev queue");
124 return EXIT_FAILURE;
125 }
126
127 pfd[0].events = POLLIN;
128 pfd[0].fd = udev_queue_get_fd(queue);
129 if (pfd[0].fd < 0) {
130 log_debug("queue is empty, nothing to watch");
131 rc = EXIT_SUCCESS;
132 goto out;
133 }
134
135 for (;;) {
136 if (exists && access(exists, F_OK) >= 0) {
137 rc = EXIT_SUCCESS;
138 break;
139 }
140
141 /* exit if queue is empty */
142 if (udev_queue_get_queue_is_empty(queue)) {
143 rc = EXIT_SUCCESS;
144 break;
145 }
146
147 if (now(CLOCK_MONOTONIC) >= deadline)
148 break;
149
150 /* wake up when queue is empty */
151 if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
152 udev_queue_flush(queue);
153 }
154
155 out:
156 udev_queue_unref(queue);
157 return rc;
158 }
159
160 const struct udevadm_cmd udevadm_settle = {
161 .name = "settle",
162 .cmd = adm_settle,
163 .help = "Wait for pending udev events",
164 };