chiark / gitweb /
udev: support ENV{}=="" global property matches
[elogind.git] / src / network / networkd-wait-online.c
index 6a7c62feae544c1c191ebd0609d4aa7ee728be52..714343656b268d7019aaa96377f61a7348fc3b49 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 <netinet/ether.h>
-#include <linux/if.h>
+#include <getopt.h>
 
-#include "sd-event.h"
-#include "event-util.h"
-#include "sd-rtnl.h"
-#include "rtnl-util.h"
 #include "sd-daemon.h"
-#include "sd-network.h"
-#include "network-util.h"
-#include "network-internal.h"
+
 #include "networkd-wait-online.h"
 
-#include "conf-parser.h"
 #include "strv.h"
-#include "util.h"
-
-static bool all_configured(Manager *m) {
-        _cleanup_free_ unsigned *indices = NULL;
-        char **ifname;
-        bool one_ready = false;
-        int r, n, i;
-
-        n = sd_network_get_ifindices(&indices);
-        if (n <= 0)
-                return false;
-
-        STRV_FOREACH(ifname, m->expected_links) {
-                _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
-                bool found = false;
-                int index;
-
-                r = sd_rtnl_message_new_link(m->rtnl, &message, RTM_GETLINK, 0);
-                if (r < 0) {
-                        log_warning("colud not create GETLINK message: %s", strerror(-r));
-                        return false;
-                }
-
-                r = sd_rtnl_message_append_string(message, IFLA_IFNAME, *ifname);
-                if (r < 0) {
-                        log_warning("could not attach ifname to GETLINK message: %s", strerror(-r));
-                        return false;
-                }
-
-                r = sd_rtnl_call(m->rtnl, message, 0, &reply);
-                if (r < 0) {
-                        if (r != -ENODEV)
-                                log_warning("could not get link info for %s: %s", *ifname,
-                                            strerror(-r));
-
-                        /* link does not yet exist */
-                        return false;
-                }
-
-                r = sd_rtnl_message_link_get_ifindex(reply, &index);
-                if (r < 0) {
-                        log_warning("could not get ifindex: %s", strerror(-r));
-                        return false;
-                }
-
-                if (index <= 0) {
-                        log_warning("invalid ifindex %d for %s", index, *ifname);
-                        return false;
-                }
-
-                for (i = 0; i < n; i++) {
-                        if (indices[i] == (unsigned) index) {
-                                found = true;
-                                break;
-                        }
-                }
-
-                if (!found)
-                        /* link exists, but networkd is not yet aware of it */
-                        return false;
-        }
-
-        for (i = 0; i < n; i++) {
-                _cleanup_free_ char *state = NULL;
-
-                r = sd_network_get_link_state(indices[i], &state);
-                if (r == -EUNATCH) {
-                        _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
-                        unsigned flags;
-                        uint8_t operstate;
-
-                        r = sd_rtnl_message_new_link(m->rtnl, &message, RTM_GETLINK, indices[i]);
-                        if (r < 0) {
-                                log_warning("could not create GETLINK message: %s", strerror(-r));
-                                return false;
-                        }
-
-                        r = sd_rtnl_call(m->rtnl, message, 0, &reply);
-                        if (r < 0) {
-                                log_debug("could not get link %u: %s", indices[i], strerror(-r));
-                                continue;
-                        }
-
-                        r = sd_rtnl_message_link_get_flags(reply, &flags);
-                        if (r < 0) {
-                                log_warning("could not get link flags: %s", strerror(-r));
-                                return false;
-                        }
-
-                        r = sd_rtnl_message_read_u8(reply, IFLA_OPERSTATE, &operstate);
-                        if (r < 0) {
-                                log_debug("could not get link operational state: %s", strerror(-r));
-                                operstate = IF_OPER_UNKNOWN;
-                        }
-                        
-                        if (!(flags & IFF_LOOPBACK) &&
-                            link_has_carrier(flags, operstate)) {
-                                /* this link is not managed by us,
-                                   but something else may have
-                                   made it ready, so don't block */
-                                one_ready = true;
-                        }
-
-                        continue;
-                } else if (r < 0 || !streq(state, "configured"))
-                        return false;
-
-                /* we wait for at least one link to appear */
-                one_ready = true;
-        }
-
-        return one_ready;
+#include "build.h"
+
+static bool arg_quiet = false;
+static char **arg_interfaces = 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"
+               , program_invocation_short_name);
 }
 
-static int monitor_event_handler(sd_event_source *s, int fd, uint32_t revents,
-                         void *userdata) {
-        Manager *m = userdata;
+static int parse_argv(int argc, char *argv[]) {
 
-        assert(m);
-        assert(m->event);
+        enum {
+                ARG_VERSION = 0x100,
+        };
 
-        if (all_configured(m))
-                sd_event_exit(m->event, 0);
+        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'         },
+                {}
+        };
 
-        return 1;
-}
+        int c;
 
-static int newlink_event_handler(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
-        Manager *m = userdata;
+        assert(argc >= 0);
+        assert(argv);
 
-        assert(m);
-        assert(m->event);
+        while ((c = getopt_long(argc, argv, "+hiq", options, NULL)) >= 0)
 
-        if (all_configured(m))
-                sd_event_exit(m->event, 0);
+                switch (c) {
 
-        return 1;
-}
+                case 'h':
+                        help();
+                        return 0;
 
-static int parse_config_file(Manager *m) {
-        static const char fn[] = "/etc/systemd/networkd-wait-online.conf";
-        _cleanup_fclose_ FILE *f = NULL;
-        int r;
+                case 'q':
+                        arg_quiet = true;
+                        break;
 
-        f = fopen(fn, "re");
-        if (!f) {
-                if (errno == ENOENT)
+                case ARG_VERSION:
+                        puts(PACKAGE_STRING);
+                        puts(SYSTEMD_FEATURES);
                         return 0;
 
-                log_warning("Failed to open configuration file %s: %m", fn);
-                return -errno;
-        }
-
-        r = config_parse(NULL, fn, f, "WaitOnline\0", config_item_perf_lookup,
-                         (void*) wait_online_gperf_lookup, false, false, m);
-        if (r < 0)
-                log_warning("Failed to parse configuration file: %s", strerror(-r));
+                case 'i':
+                        if (strv_extend(&arg_interfaces, optarg) < 0)
+                                return log_oom();
 
-        return r;
-}
+                        break;
 
-void manager_free(Manager *m) {
-        if (!m)
-                return;
+                case '?':
+                        return -EINVAL;
 
-        sd_event_unref(m->event);
-        sd_rtnl_unref(m->rtnl);
-        strv_free(m->expected_links);
+                default:
+                        assert_not_reached("Unhandled option");
+                }
 
-        free(m);
+        return 1;
 }
 
 int main(int argc, char *argv[]) {
-        _cleanup_manager_free_ Manager *m = 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();
@@ -218,70 +102,24 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        if (argc != 1) {
-                log_error("This program takes no arguments.");
-                r = -EINVAL;
-                goto out;
-        }
-
-        m = new0(Manager, 1);
-        if (!m)
-                return log_oom();
-
-        r = parse_config_file(m);
-        if (r < 0)
-                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(&m->event);
-        if (r < 0) {
-                log_error("Could not create event: %s", strerror(-r));
-                goto out;
-        }
-
-        fd = sd_network_monitor_get_fd(monitor);
-        if (fd < 0) {
-                log_error("Could not get monitor fd: %s", strerror(-r));
-                goto out;
-        }
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                return r;
 
-        events = sd_network_monitor_get_events(monitor);
-        if (events < 0) {
-                log_error("Could not get monitor events: %s", strerror(-r));
-                goto out;
-        }
+        if (arg_quiet)
+                log_set_max_level(LOG_WARNING);
 
-        r = sd_event_add_io(m->event, &event_source, fd, events, &monitor_event_handler,
-                            m);
-        if (r < 0) {
-                log_error("Could not add io event source: %s", strerror(-r));
-                goto out;
-        }
+        assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
 
-        r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK);
+        r = manager_new(&m, arg_interfaces);
         if (r < 0) {
-                log_error("Could not create rtnl: %s", strerror(-r));
-                goto out;
+                log_error("Could not create manager: %s", strerror(-r));
+                goto finish;
         }
 
-        r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
-        if (r < 0) {
-                log_error("Could not attach event to rtnl: %s", strerror(-r));
-                return r;
-        }
-
-        r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, newlink_event_handler, m);
-        if (r < 0)
-                return r;
-
-        if (all_configured(m)) {
+        if (manager_all_configured(m)) {
                 r = 0;
-                goto out;
+                goto finish;
         }
 
         sd_notify(false,
@@ -291,12 +129,11 @@ int main(int argc, char *argv[]) {
         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...");
+finish:
+        sd_notify(false, "STATUS=All interfaces configured...");
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }