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