chiark / gitweb /
sd-network: turn states 'unknown' and 'unmanaged' into errnos
[elogind.git] / src / network / networkd-wait-online.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Tom Gundersen <teg@jklm.no>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "sd-event.h"
23 #include "sd-daemon.h"
24 #include "sd-network.h"
25
26 #include "util.h"
27
28 static bool all_configured(void) {
29         _cleanup_free_ unsigned *indices = NULL;
30         bool one_ready = false;
31         int r, n, i;
32
33         n = sd_network_get_ifindices(&indices);
34         if (n <= 0)
35                 return false;
36
37         for (i = 0; i < n; i++) {
38                 _cleanup_free_ char *state = NULL;
39
40                 r = sd_network_get_link_state(indices[i], &state);
41                 if (r == -EUNATCH)
42                         continue;
43                 if (r < 0 || !streq(state, "configured"))
44                         return false;
45
46                 one_ready = true;
47         }
48
49         return one_ready;
50 }
51
52 static int event_handler(sd_event_source *s, int fd, uint32_t revents,
53                          void *userdata) {
54         sd_event *event = userdata;
55
56         assert(event);
57
58         if (all_configured())
59                 sd_event_exit(event, 0);
60
61         return 1;
62 }
63
64 int main(int argc, char *argv[]) {
65         sd_event *event;
66         sd_event_source *event_source;
67         sd_network_monitor *monitor;
68         int r, fd, events;
69
70         log_set_target(LOG_TARGET_AUTO);
71         log_parse_environment();
72         log_open();
73
74         umask(0022);
75
76         if (argc != 1) {
77                 log_error("This program takes no arguments.");
78                 r = -EINVAL;
79                 goto out;
80         }
81
82         r = sd_network_monitor_new(NULL, &monitor);
83         if (r < 0) {
84                 log_error("Could not create monitor: %s", strerror(-r));
85                 goto out;
86         }
87
88         r = sd_event_new(&event);
89         if (r < 0) {
90                 log_error("Could not create event: %s", strerror(-r));
91                 goto out;
92         }
93
94         fd = sd_network_monitor_get_fd(monitor);
95         if (fd < 0) {
96                 log_error("Could not get monitor fd: %s", strerror(-r));
97                 goto out;
98         }
99
100         events = sd_network_monitor_get_events(monitor);
101         if (events < 0) {
102                 log_error("Could not get monitor events: %s", strerror(-r));
103                 goto out;
104         }
105
106         r = sd_event_add_io(event, &event_source, fd, events, &event_handler,
107                             event);
108         if (r < 0) {
109                 log_error("Could not add io event source: %s", strerror(-r));
110                 goto out;
111         }
112
113         if (all_configured()) {
114                 r = 0;
115                 goto out;
116         }
117
118         sd_notify(false,
119                   "READY=1\n"
120                   "STATUS=Waiting for network connecitons...");
121
122         r = sd_event_loop(event);
123         if (r < 0) {
124                 log_error("Event loop failed: %s", strerror(-r));
125                 goto out;
126         }
127
128 out:
129         sd_notify(false,
130                   "STATUS=All interfaces configured...");
131
132         sd_event_source_unref(event_source);
133         sd_event_unref(event);
134         sd_network_monitor_unref(monitor);
135
136         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
137 }