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"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "networkd-wait-online.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "signal-util.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "strv.h"
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersenstatic bool arg_quiet = false;
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersenstatic usec_t arg_timeout = 120 * USEC_PER_SEC;
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"
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen " --timeout=SECS Maximum time to wait for network connectivity\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,
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen ARG_TIMEOUT,
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 },
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen { "timeout", required_argument, NULL, ARG_TIMEOUT },
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen {}
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen };
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen int c, r;
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen assert(argc >= 0);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen assert(argv);
cef8b0735831b2bfeaf21d8a245d8024c3de816aTom Gundersen
baee30afce50b611724e6c6a4ca61a4469b11d9dMartin Pitt while ((c = getopt_long(argc, argv, "+hi:q", 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:
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering return version();
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
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen case ARG_TIMEOUT:
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen r = parse_sec(optarg, &arg_timeout);
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen if (r < 0)
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen return r;
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen break;
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom 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
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
3a67e927e3be7efb8edf314a31aa4f8f5cba4f53Tom Gundersen
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen r = manager_new(&m, arg_interfaces, arg_ignore, arg_timeout);
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:
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen strv_free(arg_interfaces);
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen strv_free(arg_ignore);
79b1f37d95f08fe7620b071eec51d2810ed077c9Tom Gundersen
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen if (r >= 0) {
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen sd_notify(false, "STATUS=All interfaces configured...");
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen return EXIT_SUCCESS;
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen } else {
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen sd_notify(false, "STATUS=Failed waiting for network connectivity...");
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen return EXIT_FAILURE;
e56cdb7ae2657d62c9a8b6d3427382b209dff8e7Tom Gundersen }
020d59000f86b3d98be763eaee6a2671f0427e46Tom Gundersen}