Remove powerpc, sparc fdim inlines (bug 22987).
[glibc.git] / support / support_enter_network_namespace.c
blob1d874df8854eca4cf757e3bb2ea71583fcea8cb4
1 /* Enter a network namespace.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <support/namespace.h>
21 #include <net/if.h>
22 #include <sched.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <support/check.h>
26 #include <support/xsocket.h>
27 #include <support/xunistd.h>
28 #include <sys/ioctl.h>
29 #include <unistd.h>
31 static bool in_uts_namespace;
33 bool
34 support_enter_network_namespace (void)
36 #ifdef CLONE_NEWUTS
37 if (unshare (CLONE_NEWUTS) == 0)
38 in_uts_namespace = true;
39 else
40 printf ("warning: unshare (CLONE_NEWUTS) failed: %m\n");
41 #endif
43 #ifdef CLONE_NEWNET
44 if (unshare (CLONE_NEWNET) == 0)
46 /* Bring up the loopback interface. */
47 int fd = xsocket (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
48 struct ifreq req;
49 strcpy (req.ifr_name, "lo");
50 TEST_VERIFY_EXIT (ioctl (fd, SIOCGIFFLAGS, &req) == 0);
51 bool already_up = req.ifr_flags & IFF_UP;
52 if (already_up)
53 /* This means that we likely have not achieved isolation from
54 the parent namespace. */
55 printf ("warning: loopback interface already exists"
56 " in new network namespace\n");
57 else
59 req.ifr_flags |= IFF_UP | IFF_RUNNING;
60 TEST_VERIFY_EXIT (ioctl (fd, SIOCSIFFLAGS, &req) == 0);
62 xclose (fd);
64 return !already_up;
66 #endif
67 printf ("warning: could not enter network namespace\n");
68 return false;
71 bool
72 support_in_uts_namespace (void)
74 return in_uts_namespace;