chiark / gitweb /
cgroup: Handle error when destroying cgroup
[elogind.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 <sys/socket.h>
23 #include <net/if.h>
24 #include <asm/types.h>
25 #include <netinet/in.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 #include "util.h"
31 #include "macro.h"
32 #include "loopback-setup.h"
33 #include "socket-util.h"
34 #include "sd-rtnl.h"
35 #include "rtnl-util.h"
36
37 /* this is hardcoded in the kernel, so don't look it up */
38 #define LOOPBACK_IFINDEX 1
39
40 static int start_loopback(sd_rtnl *rtnl) {
41         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
42         int r;
43
44         r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
45         if (r < 0)
46                 return r;
47
48         r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
49         if (r < 0)
50                 return r;
51
52         r = sd_rtnl_call(rtnl, req, 0, NULL);
53         if (r < 0)
54                 return r;
55
56         return 0;
57 }
58
59 static int check_loopback(void) {
60         int r;
61         _cleanup_close_ int fd = -1;
62         union {
63                 struct sockaddr sa;
64                 struct sockaddr_in in;
65         } sa = {
66                 .in.sin_family = AF_INET,
67                 .in.sin_addr.s_addr = INADDR_LOOPBACK,
68         };
69
70         /* If we failed to set up the loop back device, check whether
71          * it might already be set up */
72
73         fd = socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
74         if (fd < 0)
75                 return -errno;
76
77         if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
78                 r = 1;
79         else
80                 r = errno == EADDRNOTAVAIL ? 0 : -errno;
81
82         return r;
83 }
84
85 int loopback_setup(void) {
86         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
87         int r;
88
89         r = sd_rtnl_open(&rtnl, 0);
90         if (r < 0)
91                 return r;
92
93         r = start_loopback(rtnl);
94         if (r == -EPERM) {
95                 if (check_loopback() < 0)
96                         return log_warning_errno(EPERM, "Failed to configure loopback device: %m");
97         } else if (r < 0)
98                 return log_warning_errno(r, "Failed to configure loopback device: %m");
99
100
101         return 0;
102 }