chiark / gitweb /
networkd: add networkd-wait-online
[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 < 0)
42                         return false;
43
44                 if (streq(state, "configured"))
45                         one_ready = true;
46
47                 if (!streq(state, "configured") && !streq(state, "unmanaged"))
48                         return false;
49         }
50
51         if (one_ready)
52                 return true;
53
54         return false;
55 }
56
57 static int event_handler(sd_event_source *s, int fd, uint32_t revents,
58                          void *userdata) {
59         sd_event *event = userdata;
60
61         assert(event);
62
63         if (all_configured())
64                 sd_event_exit(event, 0);
65
66         return 1;
67 }
68
69 int main(int argc, char *argv[]) {
70         sd_event *event;
71         sd_event_source *event_source;
72         sd_network_monitor *monitor;
73         int r, fd, events;
74
75         log_set_target(LOG_TARGET_AUTO);
76         log_parse_environment();
77         log_open();
78
79         umask(0022);
80
81         if (argc != 1) {
82                 log_error("This program takes no arguments.");
83                 r = -EINVAL;
84                 goto out;
85         }
86
87         r = sd_network_monitor_new(NULL, &monitor);
88         if (r < 0) {
89                 log_error("Could not create monitor: %s", strerror(-r));
90                 goto out;
91         }
92
93         r = sd_event_new(&event);
94         if (r < 0) {
95                 log_error("Could not create event: %s", strerror(-r));
96                 goto out;
97         }
98
99         fd = sd_network_monitor_get_fd(monitor);
100         if (fd < 0) {
101                 log_error("Could not get monitor fd: %s", strerror(-r));
102                 goto out;
103         }
104
105         events = sd_network_monitor_get_events(monitor);
106         if (events < 0) {
107                 log_error("Could not get monitor events: %s", strerror(-r));
108                 goto out;
109         }
110
111         r = sd_event_add_io(event, &event_source, fd, events, &event_handler,
112                             event);
113         if (r < 0) {
114                 log_error("Could not add io event source: %s", strerror(-r));
115                 goto out;
116         }
117
118         if (all_configured()) {
119                 r = 0;
120                 goto out;
121         }
122
123         sd_notify(false,
124                   "READY=1\n"
125                   "STATUS=Waiting for network connecitons...");
126
127         r = sd_event_loop(event);
128         if (r < 0) {
129                 log_error("Event loop failed: %s", strerror(-r));
130                 goto out;
131         }
132
133 out:
134         sd_notify(false,
135                   "STATUS=All interfaces configured...");
136
137         sd_event_source_unref(event_source);
138         sd_event_unref(event);
139         sd_network_monitor_unref(monitor);
140
141         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
142 }