chiark / gitweb /
networkd: store ifindex as int
[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 int help(void) {
34
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                program_invocation_short_name);
42
43         return 0;
44 }
45
46 static int parse_argv(int argc, char *argv[]) {
47
48         enum {
49                 ARG_VERSION = 0x100,
50         };
51
52         static const struct option options[] = {
53                 { "help",            no_argument,       NULL, 'h'         },
54                 { "version",         no_argument,       NULL, ARG_VERSION },
55                 { "quiet",           no_argument,       NULL, 'q'         },
56                 { "interface",       required_argument, NULL, 'i'         },
57                 {}
58         };
59
60         int c;
61
62         assert(argc >= 0);
63         assert(argv);
64
65         while ((c = getopt_long(argc, argv, "+hq", options, NULL)) >= 0) {
66
67                 switch (c) {
68
69                 case 'h':
70                         return help();
71
72                 case 'q':
73                         arg_quiet = true;
74                         break;
75
76                 case ARG_VERSION:
77                         puts(PACKAGE_STRING);
78                         puts(SYSTEMD_FEATURES);
79                         return 0;
80
81                 case 'i':
82                         if (strv_extend(&arg_interfaces, optarg) < 0)
83                                 return log_oom();
84
85                         break;
86
87                 case '?':
88                         return -EINVAL;
89
90                 default:
91                         assert_not_reached("Unhandled option");
92                 }
93         }
94
95         return 1;
96 }
97
98 int main(int argc, char *argv[]) {
99         _cleanup_(manager_freep) Manager *m = NULL;
100         int r;
101
102         log_set_target(LOG_TARGET_AUTO);
103         log_parse_environment();
104         log_open();
105
106         umask(0022);
107
108         r = parse_argv(argc, argv);
109         if (r <= 0)
110                 return r;
111
112         if (arg_quiet)
113                 log_set_max_level(LOG_WARNING);
114
115         assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
116
117         r = manager_new(&m, arg_interfaces);
118         if (r < 0) {
119                 log_error("Could not create manager: %s", strerror(-r));
120                 goto finish;
121         }
122
123         if (manager_all_configured(m)) {
124                 r = 0;
125                 goto finish;
126         }
127
128         sd_notify(false,
129                   "READY=1\n"
130                   "STATUS=Waiting for network connections...");
131
132         r = sd_event_loop(m->event);
133         if (r < 0) {
134                 log_error("Event loop failed: %s", strerror(-r));
135                 goto finish;
136         }
137
138 finish:
139         sd_notify(false, "STATUS=All interfaces configured...");
140
141         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
142 }