Debian Codegraph @

src/network/wait-online/wait-online.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4
5 #include "sd-event.h"
6
7 #include "alloc-util.h"
8 #include "build.h"
9 #include "daemon-util.h"
10 #include "format-table.h"
11 #include "hashmap.h"
12 #include "help-util.h"
13 #include "log.h"
14 #include "main-func.h"
15 #include "options.h"
16 #include "parse-argument.h"
17 #include "socket-util.h"
18 #include "strv.h"
19 #include "time-util.h"
20 #include "wait-online-manager.h"
21
22 static bool arg_quiet = false;
23 static usec_t arg_timeout = 120 * USEC_PER_SEC;
24 static Hashmap *arg_interfaces = NULL;
25 static char **arg_ignore = NULL;
26 static LinkOperationalStateRange arg_required_operstate = LINK_OPERSTATE_RANGE_INVALID;
27 static AddressFamily arg_required_family = ADDRESS_FAMILY_NO;
28 static bool arg_any = false;
29 static bool arg_requires_dns = false;
30
31 STATIC_DESTRUCTOR_REGISTER(arg_interfaces, hashmap_freep);
32 STATIC_DESTRUCTOR_REGISTER(arg_ignore, strv_freep);
33
34 static int help(void) {
35 _cleanup_(table_unrefp) Table *options = NULL;
36 int r;
37
38 r = option_parser_get_help_table(&options);
39 if (r < 0)
40 return r;
41
42 help_cmdline("[OPTIONS...]");
43 help_abstract("Block until network is configured.");
44
45 help_section("Options");
46 r = table_print_or_warn(options);
47 if (r < 0)
48 return r;
49
50 help_man_page_reference("systemd-networkd-wait-online.service", "8");
51 return 0;
52 }
53
54 static int parse_interface_with_operstate_range(const char *str) {
55 _cleanup_free_ char *ifname = NULL;
56 _cleanup_free_ LinkOperationalStateRange *range = NULL;
57 const char *p;
58 int r;
59
60 assert(str);
61
62 range = new(LinkOperationalStateRange, 1);
63 if (!range)
64 return log_oom();
65
66 p = strchr(str, ':');
67 if (p) {
68 r = parse_operational_state_range(p + 1, range);
69 if (r < 0)
70 return log_error_errno(r, "Invalid operational state range: %s", p + 1);
71
72 ifname = strndup(str, p - str);
73 } else {
74 *range = LINK_OPERSTATE_RANGE_INVALID;
75 ifname = strdup(str);
76 }
77 if (!ifname)
78 return log_oom();
79
80 if (!ifname_valid(ifname))
81 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
82 "Invalid interface name: %s", ifname);
83
84 r = hashmap_ensure_put(&arg_interfaces, &string_hash_ops_free_free, ifname, range);
85 if (r == -ENOMEM)
86 return log_oom();
87 if (r < 0)
88 return log_error_errno(r, "Failed to store interface name: %m");
89 if (r == 0)
90 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
91 "Interface name %s is already specified.", ifname);
92
93 TAKE_PTR(ifname);
94 TAKE_PTR(range);
95 return 0;
96 }
97
98 static int parse_argv(int argc, char *argv[]) {
99 int r;
100
101 assert(argc >= 0);
102 assert(argv);
103
104 OptionParser opts = { argc, argv };
105
106 FOREACH_OPTION_OR_RETURN(c, &opts)
107 switch (c) {
108
109 OPTION_COMMON_HELP:
110 return help();
111
112 OPTION_COMMON_VERSION:
113 return version();
114
115 OPTION('q', "quiet", NULL, "Do not show status information"):
116 arg_quiet = true;
117 break;
118
119 OPTION('i', "interface", "IFNAME[:MIN[:MAX]]",
120 "Block until at least these interfaces have appeared, "
121 "in the operational state between MIN and MAX"):
122 r = parse_interface_with_operstate_range(opts.arg);
123 if (r < 0)
124 return r;
125 break;
126
127 OPTION_LONG("ignore", "IFNAME", "Don't take these interfaces into account"):
128 if (strv_extend(&arg_ignore, opts.arg) < 0)
129 return log_oom();
130 break;
131
132 OPTION('o', "operational-state", "MIN[:MAX]",
133 "Require operational state between MIN and MAX"):
134 r = parse_operational_state_range(opts.arg, &arg_required_operstate);
135 if (r < 0)
136 return log_error_errno(r, "Invalid operational state range '%s'", opts.arg);
137 break;
138
139 OPTION('4', "ipv4", NULL, "Require at least one IPv4 address"):
140 arg_required_family |= ADDRESS_FAMILY_IPV4;
141 break;
142
143 OPTION('6', "ipv6", NULL, "Require at least one IPv6 address"):
144 arg_required_family |= ADDRESS_FAMILY_IPV6;
145 break;
146
147 OPTION_LONG("any", NULL, "Wait until at least one of the interfaces is online"):
148 arg_any = true;
149 break;
150
151 OPTION_LONG("timeout", "SECS", "Maximum time to wait for network connectivity"):
152 r = parse_sec(opts.arg, &arg_timeout);
153 if (r < 0)
154 return r;
155 break;
156
157 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "dns", "BOOL",
158 "Require at least one DNS server to be accessible"):
159 r = parse_boolean_argument("--dns", opts.arg, &arg_requires_dns);
160 if (r < 0)
161 return r;
162 break;
163 }
164
165 return 1;
166 }
167
168 static int run(int argc, char *argv[]) {
169 _cleanup_(manager_freep) Manager *m = NULL;
170 _unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
171 int r;
172
173 log_setup();
174
175 umask(0022);
176
177 r = parse_argv(argc, argv);
178 if (r <= 0)
179 return r;
180
181 if (arg_quiet)
182 log_set_max_level(LOG_ERR);
183
184 r = manager_new(&m,
185 arg_interfaces,
186 arg_ignore,
187 arg_required_operstate,
188 arg_required_family,
189 arg_any,
190 arg_timeout,
191 arg_requires_dns);
192 if (r < 0)
193 return log_error_errno(r, "Could not create manager: %m");
194
195 if (manager_configured(m))
196 goto success;
197
198 notify_message = notify_start("READY=1\n"
199 "STATUS=Waiting for network connections...",
200 "STATUS=Failed to wait for network connectivity...");
201
202 r = sd_event_loop(m->event);
203 if (r == -ETIMEDOUT)
204 return log_error_errno(r, "Timeout occurred while waiting for network connectivity.");
205 if (r < 0)
206 return log_error_errno(r, "Event loop failed: %m");
207
208 success:
209 notify_message = "STATUS=All interfaces configured...";
210
211 return 0;
212 }
213
214 DEFINE_MAIN_FUNCTION(run);