]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-settle.c
Merge pull request #10507 from cdown/cpu_acct
[thirdparty/systemd.git] / src / udev / udevadm-settle.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
7baada47 2/*
810adae9
LP
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
7baada47
KS
5 */
6
7baada47 7#include <errno.h>
c2666405 8#include <getopt.h>
0a6f50c0 9#include <poll.h>
cf0fbc49
TA
10#include <stddef.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
7baada47 15
7d68eb1b 16#include "libudev-private.h"
c7150902 17#include "time-util.h"
3d05193e 18#include "udevadm.h"
7d68eb1b
YW
19#include "udev-ctrl.h"
20#include "util.h"
c7150902
YW
21
22static usec_t arg_timeout = 120 * USEC_PER_SEC;
23static const char *arg_exists = NULL;
7baada47 24
c7150902 25static int help(void) {
5639df9a 26 printf("%s settle [OPTIONS]\n\n"
5ac0162c
LP
27 "Wait for pending udev events.\n\n"
28 " -h --help Show this help\n"
5639df9a 29 " -V --version Show package version\n"
c7150902 30 " -t --timeout=SEC Maximum time to wait for events\n"
5ac0162c
LP
31 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
32 , program_invocation_short_name);
c7150902
YW
33
34 return 0;
7643ac9a
ZJS
35}
36
c7150902 37static int parse_argv(int argc, char *argv[]) {
912541b0 38 static const struct option options[] = {
7643ac9a 39 { "timeout", required_argument, NULL, 't' },
912541b0 40 { "exit-if-exists", required_argument, NULL, 'E' },
5639df9a 41 { "version", no_argument, NULL, 'V' },
7643ac9a 42 { "help", no_argument, NULL, 'h' },
2ac23519
LP
43 { "seq-start", required_argument, NULL, 's' }, /* removed */
44 { "seq-end", required_argument, NULL, 'e' }, /* removed */
45 { "quiet", no_argument, NULL, 'q' }, /* removed */
912541b0
KS
46 {}
47 };
c7150902
YW
48
49 int c, r;
912541b0 50
5639df9a 51 while ((c = getopt_long(argc, argv, "t:E:Vhs:e:q", options, NULL)) >= 0) {
7643ac9a 52 switch (c) {
c7150902
YW
53 case 't':
54 r = parse_sec(optarg, &arg_timeout);
55 if (r < 0)
56 return log_error_errno(r, "Failed to parse timeout value '%s': %m", optarg);
912541b0 57 break;
912541b0 58 case 'E':
c7150902 59 arg_exists = optarg;
912541b0 60 break;
5639df9a 61 case 'V':
51b006e1 62 return print_version();
912541b0 63 case 'h':
c7150902 64 return help();
2ac23519
LP
65 case 's':
66 case 'e':
67 case 'q':
68 log_info("Option -%c no longer supported.", c);
c7150902 69 return -EINVAL;
7643ac9a 70 case '?':
c7150902 71 return -EINVAL;
7643ac9a 72 default:
c7150902 73 assert_not_reached("Unknown option.");
912541b0 74 }
9ea28c55 75 }
7643ac9a 76
c7150902
YW
77 return 1;
78}
79
80int settle_main(int argc, char *argv[], void *userdata) {
81 _cleanup_(udev_queue_unrefp) struct udev_queue *queue = NULL;
82 struct pollfd pfd;
83 usec_t deadline;
84 int r;
912541b0 85
c7150902
YW
86 r = parse_argv(argc, argv);
87 if (r <= 0)
88 return r;
89
90 deadline = now(CLOCK_MONOTONIC) + arg_timeout;
0736455b 91
912541b0
KS
92 /* guarantee that the udev daemon isn't pre-processing */
93 if (getuid() == 0) {
c7150902 94 _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
912541b0 95
2024ed61 96 uctrl = udev_ctrl_new();
c7150902
YW
97 if (uctrl) {
98 r = udev_ctrl_send_ping(uctrl, MAX(5U, arg_timeout / USEC_PER_SEC));
99 if (r < 0) {
100 log_debug_errno(r, "Failed to connect to udev daemon.");
101 return 0;
912541b0 102 }
912541b0
KS
103 }
104 }
105
2024ed61 106 queue = udev_queue_new(NULL);
c7150902
YW
107 if (!queue)
108 return log_error_errno(errno, "Failed to get udev queue: %m");
912541b0 109
c7150902
YW
110 r = udev_queue_get_fd(queue);
111 if (r < 0) {
112 log_debug_errno(r, "Queue is empty, nothing to watch.");
113 return 0;
9ea28c55 114 }
912541b0 115
c7150902
YW
116 pfd = (struct pollfd) {
117 .events = POLLIN,
118 .fd = r,
119 };
120
9ea28c55 121 for (;;) {
c7150902
YW
122 if (arg_exists && access(arg_exists, F_OK) >= 0)
123 return 0;
912541b0 124
9ea28c55 125 /* exit if queue is empty */
c7150902
YW
126 if (udev_queue_get_queue_is_empty(queue))
127 return 0;
912541b0 128
bf23b9f8 129 if (now(CLOCK_MONOTONIC) >= deadline)
c7150902 130 return -ETIMEDOUT;
0736455b 131
c7150902
YW
132 /* wake up when queue becomes empty */
133 if (poll(&pfd, 1, MSEC_PER_SEC) > 0 && pfd.revents & POLLIN) {
134 r = udev_queue_flush(queue);
135 if (r < 0)
136 return log_error_errno(r, "Failed to flush queue: %m");
137 }
912541b0 138 }
7baada47 139}