]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/wait-online/wait-online.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / network / wait-online / wait-online.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4
5 #include "sd-daemon.h"
6
7 #include "manager.h"
8 #include "signal-util.h"
9 #include "strv.h"
10 #include "terminal-util.h"
11
12 static bool arg_quiet = false;
13 static usec_t arg_timeout = 120 * USEC_PER_SEC;
14 static char **arg_interfaces = NULL;
15 static char **arg_ignore = NULL;
16
17 static int help(void) {
18 _cleanup_free_ char *link = NULL;
19 int r;
20
21 r = terminal_urlify_man("systemd-networkd-wait-online.service", "8", &link);
22 if (r < 0)
23 return log_oom();
24
25 printf("%s [OPTIONS...]\n\n"
26 "Block until network is configured.\n\n"
27 " -h --help Show this help\n"
28 " --version Print version string\n"
29 " -q --quiet Do not show status information\n"
30 " -i --interface=INTERFACE Block until at least these interfaces have appeared\n"
31 " --ignore=INTERFACE Don't take these interfaces into account\n"
32 " --timeout=SECS Maximum time to wait for network connectivity\n"
33 "\nSee the %s for details.\n"
34 , program_invocation_short_name
35 , link
36 );
37
38 return 0;
39 }
40
41 static int parse_argv(int argc, char *argv[]) {
42
43 enum {
44 ARG_VERSION = 0x100,
45 ARG_IGNORE,
46 ARG_TIMEOUT,
47 };
48
49 static const struct option options[] = {
50 { "help", no_argument, NULL, 'h' },
51 { "version", no_argument, NULL, ARG_VERSION },
52 { "quiet", no_argument, NULL, 'q' },
53 { "interface", required_argument, NULL, 'i' },
54 { "ignore", required_argument, NULL, ARG_IGNORE },
55 { "timeout", required_argument, NULL, ARG_TIMEOUT },
56 {}
57 };
58
59 int c, r;
60
61 assert(argc >= 0);
62 assert(argv);
63
64 while ((c = getopt_long(argc, argv, "+hi:q", options, NULL)) >= 0)
65
66 switch (c) {
67
68 case 'h':
69 help();
70 return 0;
71
72 case 'q':
73 arg_quiet = true;
74 break;
75
76 case ARG_VERSION:
77 return version();
78
79 case 'i':
80 if (strv_extend(&arg_interfaces, optarg) < 0)
81 return log_oom();
82
83 break;
84
85 case ARG_IGNORE:
86 if (strv_extend(&arg_ignore, optarg) < 0)
87 return log_oom();
88
89 break;
90
91 case ARG_TIMEOUT:
92 r = parse_sec(optarg, &arg_timeout);
93 if (r < 0)
94 return r;
95
96 break;
97
98 case '?':
99 return -EINVAL;
100
101 default:
102 assert_not_reached("Unhandled option");
103 }
104
105 return 1;
106 }
107
108 int main(int argc, char *argv[]) {
109 _cleanup_(manager_freep) Manager *m = NULL;
110 int r;
111
112 log_set_target(LOG_TARGET_AUTO);
113 log_parse_environment();
114 log_open();
115
116 umask(0022);
117
118 r = parse_argv(argc, argv);
119 if (r <= 0)
120 return r;
121
122 if (arg_quiet)
123 log_set_max_level(LOG_WARNING);
124
125 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
126
127 r = manager_new(&m, arg_interfaces, arg_ignore, arg_timeout);
128 if (r < 0) {
129 log_error_errno(r, "Could not create manager: %m");
130 goto finish;
131 }
132
133 if (manager_all_configured(m)) {
134 r = 0;
135 goto finish;
136 }
137
138 sd_notify(false,
139 "READY=1\n"
140 "STATUS=Waiting for network connections...");
141
142 r = sd_event_loop(m->event);
143 if (r < 0) {
144 log_error_errno(r, "Event loop failed: %m");
145 goto finish;
146 }
147
148 finish:
149 strv_free(arg_interfaces);
150 strv_free(arg_ignore);
151
152 if (r >= 0) {
153 sd_notify(false, "STATUS=All interfaces configured...");
154
155 return EXIT_SUCCESS;
156 } else {
157 sd_notify(false, "STATUS=Failed waiting for network connectivity...");
158
159 return EXIT_FAILURE;
160 }
161 }