]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/loopback-setup.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / core / loopback-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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
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 <net/if.h>
23 #include <stdlib.h>
24
25 #include "sd-netlink.h"
26
27 #include "loopback-setup.h"
28 #include "missing.h"
29 #include "netlink-util.h"
30
31 static int start_loopback(sd_netlink *rtnl) {
32 _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL;
33 int r;
34
35 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
36 if (r < 0)
37 return r;
38
39 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
40 if (r < 0)
41 return r;
42
43 r = sd_netlink_call(rtnl, req, 0, NULL);
44 if (r < 0)
45 return r;
46
47 return 0;
48 }
49
50 static bool check_loopback(sd_netlink *rtnl) {
51 _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
52 unsigned flags;
53 int r;
54
55 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
56 if (r < 0)
57 return false;
58
59 r = sd_netlink_call(rtnl, req, 0, &reply);
60 if (r < 0)
61 return false;
62
63 r = sd_rtnl_message_link_get_flags(reply, &flags);
64 if (r < 0)
65 return false;
66
67 return flags & IFF_UP;
68 }
69
70 int loopback_setup(void) {
71 _cleanup_netlink_unref_ sd_netlink *rtnl = NULL;
72 int r;
73
74 r = sd_netlink_open(&rtnl);
75 if (r < 0)
76 return r;
77
78 r = start_loopback(rtnl);
79 if (r < 0) {
80
81 /* If we lack the permissions to configure the
82 * loopback device, but we find it to be already
83 * configured, let's exit cleanly, in order to
84 * supported unprivileged containers. */
85 if (r == -EPERM && check_loopback(rtnl))
86 return 0;
87
88 return log_warning_errno(r, "Failed to configure loopback device: %m");
89 }
90
91 return 0;
92 }