summaryrefslogtreecommitdiff
path: root/src/network/networkd-wait-online.c
blob: 900dc05125a800e9be402993205c4034d66f08c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2013 Tom Gundersen <teg@jklm.no>

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include "sd-event.h"
#include "sd-daemon.h"
#include "sd-network.h"

#include "util.h"

static bool all_configured(void) {
        _cleanup_free_ unsigned *indices = NULL;
        bool one_ready = false;
        int r, n, i;

        n = sd_network_get_ifindices(&indices);
        if (n <= 0)
                return false;

        for (i = 0; i < n; i++) {
                _cleanup_free_ char *state = NULL;

                r = sd_network_get_link_state(indices[i], &state);
                if (r < 0)
                        return false;

                if (streq(state, "configured"))
                        one_ready = true;

                if (!streq(state, "configured") && !streq(state, "unmanaged"))
                        return false;
        }

        if (one_ready)
                return true;

        return false;
}

static int event_handler(sd_event_source *s, int fd, uint32_t revents,
                         void *userdata) {
        sd_event *event = userdata;

        assert(event);

        if (all_configured())
                sd_event_exit(event, 0);

        return 1;
}

int main(int argc, char *argv[]) {
        sd_event *event;
        sd_event_source *event_source;
        sd_network_monitor *monitor;
        int r, fd, events;

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        umask(0022);

        if (argc != 1) {
                log_error("This program takes no arguments.");
                r = -EINVAL;
                goto out;
        }

        r = sd_network_monitor_new(NULL, &monitor);
        if (r < 0) {
                log_error("Could not create monitor: %s", strerror(-r));
                goto out;
        }

        r = sd_event_new(&event);
        if (r < 0) {
                log_error("Could not create event: %s", strerror(-r));
                goto out;
        }

        fd = sd_network_monitor_get_fd(monitor);
        if (fd < 0) {
                log_error("Could not get monitor fd: %s", strerror(-r));
                goto out;
        }

        events = sd_network_monitor_get_events(monitor);
        if (events < 0) {
                log_error("Could not get monitor events: %s", strerror(-r));
                goto out;
        }

        r = sd_event_add_io(event, &event_source, fd, events, &event_handler,
                            event);
        if (r < 0) {
                log_error("Could not add io event source: %s", strerror(-r));
                goto out;
        }

        if (all_configured()) {
                r = 0;
                goto out;
        }

        sd_notify(false,
                  "READY=1\n"
                  "STATUS=Waiting for network connecitons...");

        r = sd_event_loop(event);
        if (r < 0) {
                log_error("Event loop failed: %s", strerror(-r));
                goto out;
        }

out:
        sd_notify(false,
                  "STATUS=All interfaces configured...");

        sd_event_source_unref(event_source);
        sd_event_unref(event);
        sd_network_monitor_unref(monitor);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}