]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/loopback-setup.c
service: improve readability, by reducing line-breaks
[thirdparty/systemd.git] / src / core / loopback-setup.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
af5bc85d
LP
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
af5bc85d
LP
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
5430f7f2 16 Lesser General Public License for more details.
af5bc85d 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
af5bc85d
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
af5bc85d 22#include <net/if.h>
af5bc85d 23#include <stdlib.h>
af5bc85d 24
9ca903cc 25#include "sd-rtnl.h"
81eca919 26#include "rtnl-util.h"
9ca903cc
LP
27#include "missing.h"
28#include "loopback-setup.h"
5a723174 29
f3fc4815 30static int start_loopback(sd_rtnl *rtnl) {
cf6a8911 31 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
81eca919 32 int r;
af5bc85d 33
f3fc4815 34 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
fc25d7f8
TG
35 if (r < 0)
36 return r;
37
5d4795f3 38 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
81eca919
TG
39 if (r < 0)
40 return r;
af5bc85d 41
f3fc4815 42 r = sd_rtnl_call(rtnl, req, 0, NULL);
81eca919
TG
43 if (r < 0)
44 return r;
af5bc85d
LP
45
46 return 0;
47}
48
e95e909d
TG
49static bool check_loopback(sd_rtnl *rtnl) {
50 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
51 unsigned flags;
e62d8c39 52 int r;
e95e909d
TG
53
54 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
55 if (r < 0)
2f0af4e1 56 return false;
e95e909d
TG
57
58 r = sd_rtnl_call(rtnl, req, 0, &reply);
59 if (r < 0)
2f0af4e1 60 return false;
e95e909d
TG
61
62 r = sd_rtnl_message_link_get_flags(reply, &flags);
63 if (r < 0)
2f0af4e1 64 return false;
e95e909d
TG
65
66 return flags & IFF_UP;
2c3ff76e
LP
67}
68
af5bc85d 69int loopback_setup(void) {
cf6a8911 70 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
f3fc4815 71 int r;
0a0dc69b 72
151b9b96 73 r = sd_rtnl_open(&rtnl, 0);
2c3ff76e 74 if (r < 0)
81eca919 75 return r;
af5bc85d 76
f3fc4815 77 r = start_loopback(rtnl);
8f084002
LP
78 if (r < 0) {
79
80 /* If we lack the permissions to configure the
81 * loopback device, but we find it to be already
82 * configured, let's exit cleanly, in order to
83 * supported unprivileged containers. */
84 if (r == -EPERM && check_loopback(rtnl))
85 return 0;
af5bc85d 86
8f084002
LP
87 return log_warning_errno(r, "Failed to configure loopback device: %m");
88 }
2c3ff76e 89
e62d8c39 90 return 0;
af5bc85d 91}