networkd-wait-online.c revision 7de12ae764e73730df0658f9fb04bcf42add48e2
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen/***
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen This file is part of systemd.
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen Copyright 2013 Tom Gundersen <teg@jklm.no>
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen systemd is free software; you can redistribute it and/or modify it
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen under the terms of the GNU Lesser General Public License as published by
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen (at your option) any later version.
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen systemd is distributed in the hope that it will be useful, but
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen Lesser General Public License for more details.
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen You should have received a copy of the GNU Lesser General Public License
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen***/
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen#include <getopt.h>
3a67e927e3be7efb8edf314a31aa4f8f5cba4f53Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen#include "sd-daemon.h"
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
3a67e927e3be7efb8edf314a31aa4f8f5cba4f53Tom Gundersen#include "networkd-wait-online.h"
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
3a67e927e3be7efb8edf314a31aa4f8f5cba4f53Tom Gundersen#include "strv.h"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen#include "build.h"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersenstatic bool arg_quiet = false;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersenstatic char **arg_interfaces = NULL;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersenstatic int help(void) {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen printf("%s [OPTIONS...]\n\n"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen "Block until network is configured.\n\n"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen " -h --help Show this help\n"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen " --version Print version string\n"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen " -q --quiet Do not show status information\n"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen " -i --interface=INTERFACE Block until at least these interfaces have appeared\n",
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen program_invocation_short_name);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen return 0;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen}
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersenstatic int parse_argv(int argc, char *argv[]) {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen enum {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen ARG_VERSION = 0x100,
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen };
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen static const struct option options[] = {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen { "help", no_argument, NULL, 'h' },
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen { "version", no_argument, NULL, ARG_VERSION },
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen { "quiet", no_argument, NULL, 'q' },
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen { "interface", required_argument, NULL, 'i' },
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen {}
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen };
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen int c;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen assert(argc >= 0);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen assert(argv);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen while ((c = getopt_long(argc, argv, "+hq", options, NULL)) >= 0) {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen switch (c) {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen case 'h':
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen return help();
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen case 'q':
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen arg_quiet = true;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen break;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen case ARG_VERSION:
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen puts(PACKAGE_STRING);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen puts(SYSTEMD_FEATURES);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen return 0;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen case 'i':
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen if (strv_extend(&arg_interfaces, optarg) < 0)
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen return log_oom();
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen break;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen case '?':
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen return -EINVAL;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen default:
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen assert_not_reached("Unhandled option");
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen }
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen }
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen return 1;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen}
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersenint main(int argc, char *argv[]) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen _cleanup_(manager_freep) Manager *m = NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen int r;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen log_set_target(LOG_TARGET_AUTO);
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen log_parse_environment();
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen log_open();
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen umask(0022);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen r = parse_argv(argc, argv);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen if (r <= 0)
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen return r;
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen if (arg_quiet)
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen log_set_max_level(LOG_WARNING);
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
3a67e927e3be7efb8edf314a31aa4f8f5cba4f53Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = manager_new(&m, arg_interfaces);
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen if (r < 0) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen log_error("Could not create manager: %s", strerror(-r));
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen goto finish;
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen }
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (manager_all_configured(m)) {
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen r = 0;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen goto finish;
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen }
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen sd_notify(false,
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen "READY=1\n"
b6b8adbff4b1a67a2fffc2c225f1b083d9e4a69eTom Gundersen "STATUS=Waiting for network connections...");
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
3a67e927e3be7efb8edf314a31aa4f8f5cba4f53Tom Gundersen r = sd_event_loop(m->event);
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen if (r < 0) {
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen log_error("Event loop failed: %s", strerror(-r));
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen goto finish;
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen }
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenfinish:
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen sd_notify(false, "STATUS=All interfaces configured...");
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen}