udevadm-settle.c revision 0736455b1186c9515e0f093e1e686e684d225787
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2009 Canonical Ltd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * This program is free software: you can redistribute it and/or modify
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * it under the terms of the GNU General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * This program is distributed in the hope that it will be useful,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * GNU General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * You should have received a copy of the GNU General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdlib.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stddef.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <string.h>
96aad8d15a324d0e956a4e5653a11a67b209b41aLennart Poettering#include <stdio.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <unistd.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <errno.h>
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#include <getopt.h>
51323288fc628a5cac50914df915545d685b793eLennart Poettering#include <poll.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include "udev.h"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include "util.h"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void help(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering printf("%s settle OPTIONS\n\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering "Wait for pending udev events.\n\n"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering " -h --help Show this help\n"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering " --version Show package version\n"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering " -t --timeout=SECONDS Maximum time to wait for events\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " -E --exit-if-exists=FILE Stop waiting if file exists\n"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering , program_invocation_short_name);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic int adm_settle(struct udev *udev, int argc, char *argv[]) {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering static const struct option options[] = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering { "timeout", required_argument, NULL, 't' },
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering { "exit-if-exists", required_argument, NULL, 'E' },
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering { "help", no_argument, NULL, 'h' },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering { "seq-start", required_argument, NULL, 's' }, /* removed */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering { "seq-end", required_argument, NULL, 'e' }, /* removed */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering { "quiet", no_argument, NULL, 'q' }, /* removed */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering {}
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering };
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering usec_t deadline;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering const char *exists = NULL;
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering unsigned int timeout = 120;
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering struct pollfd pfd[1] = { {.fd = -1}, };
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering int c;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering struct udev_queue *queue;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering int rc = EXIT_FAILURE;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering while ((c = getopt_long(argc, argv, "t:E:hs:e:q", options, NULL)) >= 0) {
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering switch (c) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering case 't': {
019036a47fcd10fcf0286800d144c706f3773e2fLennart Poettering int r;
019036a47fcd10fcf0286800d144c706f3773e2fLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering r = safe_atou(optarg, &timeout);
3bbdc31df37a23b5134a115c01d15e7ff870b3ccLennart Poettering if (r < 0) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering fprintf(stderr, "Invalid timeout value '%s': %s\n",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering optarg, strerror(-r));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering exit(EXIT_FAILURE);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 'E':
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering exists = optarg;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 'h':
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering help();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return EXIT_SUCCESS;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering case 's':
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 'e':
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 'q':
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering log_info("Option -%c no longer supported.", c);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return EXIT_FAILURE;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering case '?':
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering return EXIT_FAILURE;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering default:
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering assert_not_reached("Unknown argument");
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (optind < argc) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return EXIT_FAILURE;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering }
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering deadline = now(CLOCK_MONOTONIC) + timeout * USEC_PER_SEC;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering /* guarantee that the udev daemon isn't pre-processing */
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (getuid() == 0) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering struct udev_ctrl *uctrl;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering uctrl = udev_ctrl_new(udev);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if (uctrl != NULL) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if (udev_ctrl_send_ping(uctrl, timeout) < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_debug("no connection to daemon");
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering udev_ctrl_unref(uctrl);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return EXIT_SUCCESS;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering }
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering udev_ctrl_unref(uctrl);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering queue = udev_queue_new(udev);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if (!queue) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering log_error("unable to get udev queue");
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return EXIT_FAILURE;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering pfd[0].events = POLLIN;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering pfd[0].fd = udev_queue_get_fd(queue);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if (pfd[0].fd < 0) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering log_debug("queue is empty, nothing to watch");
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering rc = EXIT_SUCCESS;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering goto out;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering }
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering for (;;) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if (exists && access(exists, F_OK) >= 0) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering rc = EXIT_SUCCESS;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering break;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering /* exit if queue is empty */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_queue_get_queue_is_empty(queue)) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering rc = EXIT_SUCCESS;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (timeout > 0 && now(CLOCK_MONOTONIC) >= deadline)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering break;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* wake up when queue is empty */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (poll(pfd, 1, MSEC_PER_SEC) > 0 && pfd[0].revents & POLLIN)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_queue_flush(queue);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringout:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_queue_unref(queue);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return rc;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringconst struct udevadm_cmd udevadm_settle = {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering .name = "settle",
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering .cmd = adm_settle,
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering .help = "Wait for pending udev events",
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering};
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering