chiark / gitweb /
networkd-wait-online: allow specific devices to be ignored
[elogind.git] / src / network / networkd-wait-online.c
index e62859e1cf9c8b229204a2e853329173026f7c0e..826ab929cc5bb5af17d9ab4e9d505461e436f58d 100644 (file)
@@ -1,4 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include "sd-event.h"
-#include "event-util.h"
+#include <getopt.h>
+
 #include "sd-daemon.h"
-#include "sd-network.h"
-#include "network-util.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;
+static char **arg_ignore = NULL;
 
-        for (i = 0; i < n; i++) {
-                _cleanup_free_ char *state = NULL;
+static void help(void) {
+        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"
+               "     --ignore=INTERFACE     Don't take these interfaces into account\n"
+               , program_invocation_short_name);
+}
 
-                r = sd_network_get_link_state(indices[i], &state);
-                if (r == -EUNATCH)
-                        continue;
-                if (r < 0 || !streq(state, "configured"))
-                        return false;
+static int parse_argv(int argc, char *argv[]) {
 
-                one_ready = true;
-        }
+        enum {
+                ARG_VERSION = 0x100,
+                ARG_IGNORE,
+        };
 
-        return one_ready;
-}
+        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'         },
+                { "ignore",          required_argument, NULL, ARG_IGNORE  },
+                {}
+        };
+
+        int c;
+
+        assert(argc >= 0);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "+hiq", options, NULL)) >= 0)
+
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                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();
+
+                        break;
 
-static int event_handler(sd_event_source *s, int fd, uint32_t revents,
-                         void *userdata) {
-        sd_event *event = userdata;
+                case ARG_IGNORE:
+                        if (strv_extend(&arg_ignore, optarg) < 0)
+                                return log_oom();
 
-        assert(event);
+                        break;
 
-        if (all_configured())
-                sd_event_exit(event, 0);
+                case '?':
+                        return -EINVAL;
+
+                default:
+                        assert_not_reached("Unhandled option");
+                }
 
         return 1;
 }
 
 int main(int argc, char *argv[]) {
-        _cleanup_event_unref_ sd_event *event = NULL;
-        _cleanup_event_source_unref_ sd_event_source *event_source = NULL;
-        _cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL;
-        int r, fd, events;
+        _cleanup_(manager_freep) Manager *m = NULL;
+        int r;
 
         log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
@@ -75,61 +112,41 @@ 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, arg_ignore);
         if (r < 0) {
-                log_error("Could not add io event source: %s", strerror(-r));
-                goto out;
+                log_error_errno(r, "Could not create manager: %m");
+                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 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;
+                log_error_errno(r, "Event loop failed: %m");
+                goto finish;
         }
 
-out:
-        sd_notify(false,
-                  "STATUS=All interfaces configured...");
+finish:
+        sd_notify(false, "STATUS=All interfaces configured...");
+
+        strv_free(arg_interfaces);
+        strv_free(arg_ignore);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }