]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-wait-online.c
Merge pull request #208 from poettering/btrfs-rec-snapshot
[thirdparty/systemd.git] / src / network / networkd-wait-online.c
CommitLineData
020d5900
TG
1
2/***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
cef8b073 21#include <getopt.h>
3a67e927 22
020d5900 23#include "sd-daemon.h"
3a67e927 24#include "strv.h"
cef8b073 25#include "build.h"
24882e06
LP
26#include "signal-util.h"
27#include "networkd-wait-online.h"
cef8b073
TG
28
29static bool arg_quiet = false;
e56cdb7a 30static usec_t arg_timeout = 120 * USEC_PER_SEC;
cef8b073 31static char **arg_interfaces = NULL;
79b1f37d 32static char **arg_ignore = NULL;
cef8b073 33
601185b4 34static void help(void) {
cef8b073
TG
35 printf("%s [OPTIONS...]\n\n"
36 "Block until network is configured.\n\n"
37 " -h --help Show this help\n"
38 " --version Print version string\n"
39 " -q --quiet Do not show status information\n"
601185b4 40 " -i --interface=INTERFACE Block until at least these interfaces have appeared\n"
79b1f37d 41 " --ignore=INTERFACE Don't take these interfaces into account\n"
e56cdb7a 42 " --timeout=SECS Maximum time to wait for network connectivity\n"
601185b4 43 , program_invocation_short_name);
cef8b073
TG
44}
45
46static int parse_argv(int argc, char *argv[]) {
47
48 enum {
49 ARG_VERSION = 0x100,
79b1f37d 50 ARG_IGNORE,
e56cdb7a 51 ARG_TIMEOUT,
cef8b073
TG
52 };
53
54 static const struct option options[] = {
55 { "help", no_argument, NULL, 'h' },
56 { "version", no_argument, NULL, ARG_VERSION },
57 { "quiet", no_argument, NULL, 'q' },
58 { "interface", required_argument, NULL, 'i' },
79b1f37d 59 { "ignore", required_argument, NULL, ARG_IGNORE },
e56cdb7a 60 { "timeout", required_argument, NULL, ARG_TIMEOUT },
cef8b073
TG
61 {}
62 };
63
e56cdb7a 64 int c, r;
cef8b073
TG
65
66 assert(argc >= 0);
67 assert(argv);
68
32dfe42c 69 while ((c = getopt_long(argc, argv, "+hiq", options, NULL)) >= 0)
cef8b073
TG
70
71 switch (c) {
72
73 case 'h':
601185b4
ZJS
74 help();
75 return 0;
cef8b073
TG
76
77 case 'q':
78 arg_quiet = true;
79 break;
80
81 case ARG_VERSION:
82 puts(PACKAGE_STRING);
83 puts(SYSTEMD_FEATURES);
84 return 0;
85
86 case 'i':
87 if (strv_extend(&arg_interfaces, optarg) < 0)
88 return log_oom();
89
90 break;
91
79b1f37d
TG
92 case ARG_IGNORE:
93 if (strv_extend(&arg_ignore, optarg) < 0)
94 return log_oom();
95
96 break;
97
e56cdb7a
TG
98 case ARG_TIMEOUT:
99 r = parse_sec(optarg, &arg_timeout);
100 if (r < 0)
101 return r;
102
103 break;
104
cef8b073
TG
105 case '?':
106 return -EINVAL;
107
108 default:
109 assert_not_reached("Unhandled option");
110 }
cef8b073
TG
111
112 return 1;
113}
020d5900 114
020d5900 115int main(int argc, char *argv[]) {
7de12ae7
TG
116 _cleanup_(manager_freep) Manager *m = NULL;
117 int r;
cef8b073 118
7de12ae7 119 log_set_target(LOG_TARGET_AUTO);
020d5900
TG
120 log_parse_environment();
121 log_open();
122
7de12ae7
TG
123 umask(0022);
124
cef8b073
TG
125 r = parse_argv(argc, argv);
126 if (r <= 0)
127 return r;
020d5900 128
cef8b073
TG
129 if (arg_quiet)
130 log_set_max_level(LOG_WARNING);
020d5900 131
7de12ae7 132 assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
3a67e927 133
e56cdb7a 134 r = manager_new(&m, arg_interfaces, arg_ignore, arg_timeout);
020d5900 135 if (r < 0) {
da927ba9 136 log_error_errno(r, "Could not create manager: %m");
7de12ae7 137 goto finish;
020d5900
TG
138 }
139
7de12ae7 140 if (manager_all_configured(m)) {
020d5900 141 r = 0;
7de12ae7 142 goto finish;
020d5900
TG
143 }
144
145 sd_notify(false,
146 "READY=1\n"
b6b8adbf 147 "STATUS=Waiting for network connections...");
020d5900 148
3a67e927 149 r = sd_event_loop(m->event);
020d5900 150 if (r < 0) {
da927ba9 151 log_error_errno(r, "Event loop failed: %m");
7de12ae7 152 goto finish;
020d5900
TG
153 }
154
7de12ae7 155finish:
79b1f37d
TG
156 strv_free(arg_interfaces);
157 strv_free(arg_ignore);
158
e56cdb7a
TG
159 if (r >= 0) {
160 sd_notify(false, "STATUS=All interfaces configured...");
161
162 return EXIT_SUCCESS;
163 } else {
164 sd_notify(false, "STATUS=Failed waiting for network connectivity...");
165
166 return EXIT_FAILURE;
167 }
020d5900 168}