-/*-*- 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;
- 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"
+ , 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,
+ };
- 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' },
+ {}
+ };
+
+ 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;
-static int event_handler(sd_event_source *s, int fd, uint32_t revents,
- void *userdata) {
- sd_event *event = userdata;
+ case 'i':
+ if (strv_extend(&arg_interfaces, 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();
umask(0022);
- if (argc != 1) {
- log_error("This program takes no arguments.");
- r = -EINVAL;
- goto out;
- }
+ r = parse_argv(argc, argv);
+ if (r <= 0)
+ return r;
- r = sd_network_monitor_new(NULL, &monitor);
- if (r < 0) {
- log_error("Could not create monitor: %s", strerror(-r));
- goto out;
- }
+ if (arg_quiet)
+ log_set_max_level(LOG_WARNING);
- r = sd_event_new(&event);
- if (r < 0) {
- log_error("Could not create event: %s", strerror(-r));
- goto out;
- }
+ assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
- fd = sd_network_monitor_get_fd(monitor);
- if (fd < 0) {
- log_error("Could not get monitor fd: %s", strerror(-r));
- goto out;
- }
-
- events = sd_network_monitor_get_events(monitor);
- if (events < 0) {
- log_error("Could not get monitor events: %s", strerror(-r));
- goto out;
- }
-
- 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 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...");
+finish:
+ sd_notify(false, "STATUS=All interfaces configured...");
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}