networkd-wait-online.c revision 79b1f37d95f08fe7620b071eec51d2810ed077c9
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;
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersenstatic char **arg_ignore = NULL;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmekstatic void help(void) {
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"
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek " -i --interface=INTERFACE Block until at least these interfaces have appeared\n"
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen " --ignore=INTERFACE Don't take these interfaces into account\n"
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek , program_invocation_short_name);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen}
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersenstatic int parse_argv(int argc, char *argv[]) {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen enum {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen ARG_VERSION = 0x100,
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen ARG_IGNORE,
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' },
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen { "ignore", required_argument, NULL, ARG_IGNORE },
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen {}
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen };
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen int c;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen assert(argc >= 0);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen assert(argv);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
32dfe42c66085c55916e5306a9a07d42d3958b6bŁukasz Stelmach while ((c = getopt_long(argc, argv, "+hiq", options, NULL)) >= 0)
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen switch (c) {
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen case 'h':
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek help();
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek return 0;
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
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen case ARG_IGNORE:
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen if (strv_extend(&arg_ignore, optarg) < 0)
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen return log_oom();
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen break;
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom 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 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
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen r = manager_new(&m, arg_interfaces, arg_ignore);
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Could not create manager: %m");
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) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Event loop failed: %m");
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen goto finish;
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen }
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenfinish:
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen sd_notify(false, "STATUS=All interfaces configured...");
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen strv_free(arg_interfaces);
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen strv_free(arg_ignore);
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen}