]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/wait-online/wait-online.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / network / wait-online / wait-online.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
22 #include <getopt.h>
23
24 #include "sd-daemon.h"
25
26 #include "manager.h"
27 #include "signal-util.h"
28 #include "strv.h"
29
30 static bool arg_quiet = false;
31 static usec_t arg_timeout = 120 * USEC_PER_SEC;
32 static char **arg_interfaces = NULL;
33 static char **arg_ignore = NULL;
34
35 static void help(void) {
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"
41 " -i --interface=INTERFACE Block until at least these interfaces have appeared\n"
42 " --ignore=INTERFACE Don't take these interfaces into account\n"
43 " --timeout=SECS Maximum time to wait for network connectivity\n"
44 , program_invocation_short_name);
45 }
46
47 static int parse_argv(int argc, char *argv[]) {
48
49 enum {
50 ARG_VERSION = 0x100,
51 ARG_IGNORE,
52 ARG_TIMEOUT,
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' },
60 { "ignore", required_argument, NULL, ARG_IGNORE },
61 { "timeout", required_argument, NULL, ARG_TIMEOUT },
62 {}
63 };
64
65 int c, r;
66
67 assert(argc >= 0);
68 assert(argv);
69
70 while ((c = getopt_long(argc, argv, "+hi:q", options, NULL)) >= 0)
71
72 switch (c) {
73
74 case 'h':
75 help();
76 return 0;
77
78 case 'q':
79 arg_quiet = true;
80 break;
81
82 case ARG_VERSION:
83 return version();
84
85 case 'i':
86 if (strv_extend(&arg_interfaces, optarg) < 0)
87 return log_oom();
88
89 break;
90
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
104 case '?':
105 return -EINVAL;
106
107 default:
108 assert_not_reached("Unhandled option");
109 }
110
111 return 1;
112 }
113
114 int main(int argc, char *argv[]) {
115 _cleanup_(manager_freep) Manager *m = NULL;
116 int r;
117
118 log_set_target(LOG_TARGET_AUTO);
119 log_parse_environment();
120 log_open();
121
122 umask(0022);
123
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
131 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
132
133 r = manager_new(&m, arg_interfaces, arg_ignore, arg_timeout);
134 if (r < 0) {
135 log_error_errno(r, "Could not create manager: %m");
136 goto finish;
137 }
138
139 if (manager_all_configured(m)) {
140 r = 0;
141 goto finish;
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) {
150 log_error_errno(r, "Event loop failed: %m");
151 goto finish;
152 }
153
154 finish:
155 strv_free(arg_interfaces);
156 strv_free(arg_ignore);
157
158 if (r >= 0) {
159 sd_notify(false, "STATUS=All interfaces configured...");
160
161 return EXIT_SUCCESS;
162 } else {
163 sd_notify(false, "STATUS=Failed waiting for network connectivity...");
164
165 return EXIT_FAILURE;
166 }
167 }