chiark / gitweb /
sd-network: rename state INITALIZING to PENDING and expose as any other state
[elogind.git] / src / network / networkd-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 "networkd-wait-online.h"
26
27 #include "strv.h"
28 #include "build.h"
29
30 static bool arg_quiet = false;
31 static char **arg_interfaces = NULL;
32
33 static void help(void) {
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"
39                "  -i --interface=INTERFACE  Block until at least these interfaces have appeared\n"
40                , program_invocation_short_name);
41 }
42
43 static 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
62         while ((c = getopt_long(argc, argv, "+hq", options, NULL)) >= 0)
63
64                 switch (c) {
65
66                 case 'h':
67                         help();
68                         return 0;
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                 }
91
92         return 1;
93 }
94
95 int main(int argc, char *argv[]) {
96         _cleanup_(manager_freep) Manager *m = NULL;
97         int r;
98
99         log_set_target(LOG_TARGET_AUTO);
100         log_parse_environment();
101         log_open();
102
103         umask(0022);
104
105         r = parse_argv(argc, argv);
106         if (r <= 0)
107                 return r;
108
109         if (arg_quiet)
110                 log_set_max_level(LOG_WARNING);
111
112         assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
113
114         r = manager_new(&m, arg_interfaces);
115         if (r < 0) {
116                 log_error("Could not create manager: %s", strerror(-r));
117                 goto finish;
118         }
119
120         if (manager_all_configured(m)) {
121                 r = 0;
122                 goto finish;
123         }
124
125         sd_notify(false,
126                   "READY=1\n"
127                   "STATUS=Waiting for network connections...");
128
129         r = sd_event_loop(m->event);
130         if (r < 0) {
131                 log_error("Event loop failed: %s", strerror(-r));
132                 goto finish;
133         }
134
135 finish:
136         sd_notify(false, "STATUS=All interfaces configured...");
137
138         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
139 }