X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-wait-online.c;h=d588935e9353ebc67d45d5a906012d94ea879169;hp=900dc05125a800e9be402993205c4034d66f08c8;hb=7de12ae764e73730df0658f9fb04bcf42add48e2;hpb=020d59000f86b3d98be763eaee6a2671f0427e46 diff --git a/src/network/networkd-wait-online.c b/src/network/networkd-wait-online.c index 900dc0512..d588935e9 100644 --- a/src/network/networkd-wait-online.c +++ b/src/network/networkd-wait-online.c @@ -1,4 +1,3 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -19,58 +18,86 @@ along with systemd; If not, see . ***/ -#include "sd-event.h" +#include + #include "sd-daemon.h" -#include "sd-network.h" -#include "util.h" +#include "networkd-wait-online.h" -static bool all_configured(void) { - _cleanup_free_ unsigned *indices = NULL; - bool one_ready = false; - int r, n, i; +#include "strv.h" +#include "build.h" - n = sd_network_get_ifindices(&indices); - if (n <= 0) - return false; +static bool arg_quiet = false; +static char **arg_interfaces = NULL; - for (i = 0; i < n; i++) { - _cleanup_free_ char *state = NULL; +static int help(void) { - r = sd_network_get_link_state(indices[i], &state); - if (r < 0) - return false; + printf("%s [OPTIONS...]\n\n" + "Block until network is configured.\n\n" + " -h --help Show this help\n" + " --version Print version string\n" + " -q --quiet Do not show status information\n" + " -i --interface=INTERFACE Block until at least these interfaces have appeared\n", + program_invocation_short_name); - if (streq(state, "configured")) - one_ready = true; + return 0; +} - if (!streq(state, "configured") && !streq(state, "unmanaged")) - return false; - } +static int parse_argv(int argc, char *argv[]) { - if (one_ready) - return true; + enum { + ARG_VERSION = 0x100, + }; - return false; -} + static const struct option options[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, ARG_VERSION }, + { "quiet", no_argument, NULL, 'q' }, + { "interface", required_argument, NULL, 'i' }, + {} + }; + + int c; + + assert(argc >= 0); + assert(argv); + + while ((c = getopt_long(argc, argv, "+hq", options, NULL)) >= 0) { + + switch (c) { + + case 'h': + return help(); + + case 'q': + arg_quiet = true; + break; + + case ARG_VERSION: + puts(PACKAGE_STRING); + puts(SYSTEMD_FEATURES); + return 0; + + case 'i': + if (strv_extend(&arg_interfaces, optarg) < 0) + return log_oom(); -static int event_handler(sd_event_source *s, int fd, uint32_t revents, - void *userdata) { - sd_event *event = userdata; + break; - assert(event); + case '?': + return -EINVAL; - if (all_configured()) - sd_event_exit(event, 0); + default: + assert_not_reached("Unhandled option"); + } + } return 1; } int main(int argc, char *argv[]) { - sd_event *event; - sd_event_source *event_source; - sd_network_monitor *monitor; - int r, fd, events; + _cleanup_(manager_freep) Manager *m = NULL; + int r; log_set_target(LOG_TARGET_AUTO); log_parse_environment(); @@ -78,65 +105,38 @@ int main(int argc, char *argv[]) { umask(0022); - if (argc != 1) { - log_error("This program takes no arguments."); - r = -EINVAL; - goto out; - } - - r = sd_network_monitor_new(NULL, &monitor); - if (r < 0) { - log_error("Could not create monitor: %s", strerror(-r)); - goto out; - } - - r = sd_event_new(&event); - if (r < 0) { - log_error("Could not create event: %s", strerror(-r)); - goto out; - } + r = parse_argv(argc, argv); + if (r <= 0) + return r; - fd = sd_network_monitor_get_fd(monitor); - if (fd < 0) { - log_error("Could not get monitor fd: %s", strerror(-r)); - goto out; - } + if (arg_quiet) + log_set_max_level(LOG_WARNING); - events = sd_network_monitor_get_events(monitor); - if (events < 0) { - log_error("Could not get monitor events: %s", strerror(-r)); - goto out; - } + assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); - r = sd_event_add_io(event, &event_source, fd, events, &event_handler, - event); + r = manager_new(&m, arg_interfaces); if (r < 0) { - log_error("Could not add io event source: %s", strerror(-r)); - goto out; + log_error("Could not create manager: %s", strerror(-r)); + goto finish; } - if (all_configured()) { + if (manager_all_configured(m)) { r = 0; - goto out; + goto finish; } sd_notify(false, "READY=1\n" - "STATUS=Waiting for network connecitons..."); + "STATUS=Waiting for network connections..."); - r = sd_event_loop(event); + r = sd_event_loop(m->event); if (r < 0) { log_error("Event loop failed: %s", strerror(-r)); - goto out; + goto finish; } -out: - sd_notify(false, - "STATUS=All interfaces configured..."); - - sd_event_source_unref(event_source); - sd_event_unref(event); - sd_network_monitor_unref(monitor); +finish: + sd_notify(false, "STATUS=All interfaces configured..."); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }