networkd-wait-online.c revision 473dfd7bd5a74b4724818ab8a764c072335fd510
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#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"
#include "build.h"
static bool arg_quiet = false;
static char **arg_interfaces = NULL;
static int 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",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
return help();
case 'q':
arg_quiet = true;
break;
case ARG_VERSION:
return 0;
case 'i':
return log_oom();
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
}
return 1;
}
static bool all_configured(Manager *m) {
char **ifname;
bool one_ready = false;
int r, n, i;
n = sd_network_get_ifindices(&indices);
if (n <= 0)
return false;
/* wait for networkd to be aware of all the links given on the commandline */
bool found = false;
int index;
if (r < 0) {
return false;
}
if (r < 0) {
return false;
}
if (r < 0) {
if (r != -ENODEV)
strerror(-r));
/* link does not yet exist */
return false;
}
if (r < 0) {
return false;
}
if (index <= 0) {
return false;
}
for (i = 0; i < n; i++) {
found = true;
break;
}
}
if (!found) {
/* link exists, but networkd is not yet aware of it */
return false;
}
}
/* wait for all links networkd manages to be in admin state 'configured'
and at least one link to gain a carrier */
for (i = 0; i < n; i++) {
if (sd_network_link_is_loopback(indices[i]))
/* ignore loopback devices */
continue;
/* not yet processed by udev, or managed by networkd, but not yet configured */
return false;
if (r >= 0 &&
/* we wait for at least one link to be ready,
regardless of who manages it */
one_ready = true;
}
return one_ready;
}
void *userdata) {
assert(m);
if (all_configured(m))
sd_event_exit(m->event, 0);
return 1;
}
void manager_free(Manager *m) {
if (!m)
return;
sd_event_unref(m->event);
sd_rtnl_unref(m->rtnl);
free(m);
}
umask(0022);
log_open();
if (r <= 0)
return r;
if (arg_quiet)
if (!m)
return log_oom();
r = sd_event_new(&m->event);
if (r < 0) {
goto out;
}
r = sd_rtnl_open(&m->rtnl, 0);
if (r < 0) {
goto out;
}
if (r < 0) {
goto out;
}
if (fd < 0) {
goto out;
}
if (events < 0) {
goto out;
}
m);
if (r < 0) {
goto out;
}
if (all_configured(m)) {
r = 0;
goto out;
}
sd_notify(false,
"READY=1\n"
"STATUS=Waiting for network connections...");
r = sd_event_loop(m->event);
if (r < 0) {
goto out;
}
out:
sd_notify(false,
"STATUS=All interfaces configured...");
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}