]> git.proxmox.com Git - systemd.git/blame - src/network/networkd-wait-online.c
Imported Upstream version 227
[systemd.git] / src / network / networkd-wait-online.c
CommitLineData
60f067b4
JS
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
60f067b4
JS
21#include <getopt.h>
22
60f067b4 23#include "sd-daemon.h"
6300502b 24
86f210e9 25#include "networkd-wait-online.h"
6300502b
MP
26#include "signal-util.h"
27#include "strv.h"
60f067b4
JS
28
29static bool arg_quiet = false;
e735f4d4 30static usec_t arg_timeout = 120 * USEC_PER_SEC;
60f067b4 31static char **arg_interfaces = NULL;
e735f4d4 32static char **arg_ignore = NULL;
60f067b4 33
5eef597e 34static void help(void) {
60f067b4
JS
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"
5eef597e 40 " -i --interface=INTERFACE Block until at least these interfaces have appeared\n"
e735f4d4
MP
41 " --ignore=INTERFACE Don't take these interfaces into account\n"
42 " --timeout=SECS Maximum time to wait for network connectivity\n"
5eef597e 43 , program_invocation_short_name);
60f067b4
JS
44}
45
46static int parse_argv(int argc, char *argv[]) {
47
48 enum {
49 ARG_VERSION = 0x100,
e735f4d4
MP
50 ARG_IGNORE,
51 ARG_TIMEOUT,
60f067b4
JS
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' },
e735f4d4
MP
59 { "ignore", required_argument, NULL, ARG_IGNORE },
60 { "timeout", required_argument, NULL, ARG_TIMEOUT },
60f067b4
JS
61 {}
62 };
63
e735f4d4 64 int c, r;
60f067b4
JS
65
66 assert(argc >= 0);
67 assert(argv);
68
7035cd9e 69 while ((c = getopt_long(argc, argv, "+hi:q", options, NULL)) >= 0)
60f067b4
JS
70
71 switch (c) {
72
73 case 'h':
5eef597e
MP
74 help();
75 return 0;
60f067b4
JS
76
77 case 'q':
78 arg_quiet = true;
79 break;
80
81 case ARG_VERSION:
6300502b 82 return version();
60f067b4
JS
83
84 case 'i':
85 if (strv_extend(&arg_interfaces, optarg) < 0)
86 return log_oom();
87
88 break;
89
e735f4d4
MP
90 case ARG_IGNORE:
91 if (strv_extend(&arg_ignore, optarg) < 0)
92 return log_oom();
93
94 break;
95
96 case ARG_TIMEOUT:
97 r = parse_sec(optarg, &arg_timeout);
98 if (r < 0)
99 return r;
100
101 break;
102
60f067b4
JS
103 case '?':
104 return -EINVAL;
105
106 default:
107 assert_not_reached("Unhandled option");
108 }
60f067b4
JS
109
110 return 1;
111}
112
60f067b4 113int main(int argc, char *argv[]) {
5eef597e
MP
114 _cleanup_(manager_freep) Manager *m = NULL;
115 int r;
60f067b4 116
5eef597e 117 log_set_target(LOG_TARGET_AUTO);
60f067b4
JS
118 log_parse_environment();
119 log_open();
120
5eef597e
MP
121 umask(0022);
122
60f067b4
JS
123 r = parse_argv(argc, argv);
124 if (r <= 0)
125 return r;
126
127 if (arg_quiet)
128 log_set_max_level(LOG_WARNING);
129
86f210e9 130 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
60f067b4 131
e735f4d4 132 r = manager_new(&m, arg_interfaces, arg_ignore, arg_timeout);
60f067b4 133 if (r < 0) {
f47781d8 134 log_error_errno(r, "Could not create manager: %m");
5eef597e 135 goto finish;
60f067b4
JS
136 }
137
5eef597e 138 if (manager_all_configured(m)) {
60f067b4 139 r = 0;
5eef597e 140 goto finish;
60f067b4
JS
141 }
142
143 sd_notify(false,
144 "READY=1\n"
145 "STATUS=Waiting for network connections...");
146
147 r = sd_event_loop(m->event);
148 if (r < 0) {
f47781d8 149 log_error_errno(r, "Event loop failed: %m");
5eef597e 150 goto finish;
60f067b4
JS
151 }
152
5eef597e 153finish:
e735f4d4
MP
154 strv_free(arg_interfaces);
155 strv_free(arg_ignore);
156
157 if (r >= 0) {
158 sd_notify(false, "STATUS=All interfaces configured...");
60f067b4 159
e735f4d4
MP
160 return EXIT_SUCCESS;
161 } else {
162 sd_notify(false, "STATUS=Failed waiting for network connectivity...");
163
164 return EXIT_FAILURE;
165 }
60f067b4 166}