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