]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-wait-online.c
Unify parse_argv style
[thirdparty/systemd.git] / src / network / networkd-wait-online.c
CommitLineData
020d5900
TG
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
cef8b073 21#include <getopt.h>
3a67e927 22
020d5900 23#include "sd-daemon.h"
7de12ae7 24
3a67e927 25#include "networkd-wait-online.h"
020d5900 26
3a67e927 27#include "strv.h"
cef8b073
TG
28#include "build.h"
29
30static bool arg_quiet = false;
31static char **arg_interfaces = NULL;
32
601185b4 33static void help(void) {
cef8b073
TG
34 printf("%s [OPTIONS...]\n\n"
35 "Block until network is configured.\n\n"
36 " -h --help Show this help\n"
37 " --version Print version string\n"
38 " -q --quiet Do not show status information\n"
601185b4
ZJS
39 " -i --interface=INTERFACE Block until at least these interfaces have appeared\n"
40 , program_invocation_short_name);
cef8b073
TG
41}
42
43static int parse_argv(int argc, char *argv[]) {
44
45 enum {
46 ARG_VERSION = 0x100,
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 {}
55 };
56
57 int c;
58
59 assert(argc >= 0);
60 assert(argv);
61
601185b4 62 while ((c = getopt_long(argc, argv, "+hq", options, NULL)) >= 0)
cef8b073
TG
63
64 switch (c) {
65
66 case 'h':
601185b4
ZJS
67 help();
68 return 0;
cef8b073
TG
69
70 case 'q':
71 arg_quiet = true;
72 break;
73
74 case ARG_VERSION:
75 puts(PACKAGE_STRING);
76 puts(SYSTEMD_FEATURES);
77 return 0;
78
79 case 'i':
80 if (strv_extend(&arg_interfaces, optarg) < 0)
81 return log_oom();
82
83 break;
84
85 case '?':
86 return -EINVAL;
87
88 default:
89 assert_not_reached("Unhandled option");
90 }
cef8b073
TG
91
92 return 1;
93}
020d5900 94
020d5900 95int main(int argc, char *argv[]) {
7de12ae7
TG
96 _cleanup_(manager_freep) Manager *m = NULL;
97 int r;
cef8b073 98
7de12ae7 99 log_set_target(LOG_TARGET_AUTO);
020d5900
TG
100 log_parse_environment();
101 log_open();
102
7de12ae7
TG
103 umask(0022);
104
cef8b073
TG
105 r = parse_argv(argc, argv);
106 if (r <= 0)
107 return r;
020d5900 108
cef8b073
TG
109 if (arg_quiet)
110 log_set_max_level(LOG_WARNING);
020d5900 111
7de12ae7 112 assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
3a67e927 113
7de12ae7 114 r = manager_new(&m, arg_interfaces);
020d5900 115 if (r < 0) {
7de12ae7
TG
116 log_error("Could not create manager: %s", strerror(-r));
117 goto finish;
020d5900
TG
118 }
119
7de12ae7 120 if (manager_all_configured(m)) {
020d5900 121 r = 0;
7de12ae7 122 goto finish;
020d5900
TG
123 }
124
125 sd_notify(false,
126 "READY=1\n"
b6b8adbf 127 "STATUS=Waiting for network connections...");
020d5900 128
3a67e927 129 r = sd_event_loop(m->event);
020d5900
TG
130 if (r < 0) {
131 log_error("Event loop failed: %s", strerror(-r));
7de12ae7 132 goto finish;
020d5900
TG
133 }
134
7de12ae7
TG
135finish:
136 sd_notify(false, "STATUS=All interfaces configured...");
020d5900 137
020d5900
TG
138 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
139}