1 /* Enter a network namespace.
2 Copyright (C) 2016-2020 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 <https://www.gnu.org/licenses/>. */
19 #include <support/namespace.h>
25 #include <support/check.h>
26 #include <support/xsocket.h>
27 #include <support/xunistd.h>
28 #include <sys/ioctl.h>
31 static bool in_uts_namespace
;
34 support_enter_network_namespace (void)
37 if (unshare (CLONE_NEWUTS
) == 0)
38 in_uts_namespace
= true;
40 printf ("warning: unshare (CLONE_NEWUTS) failed: %m\n");
44 if (unshare (CLONE_NEWNET
) == 0)
46 /* Bring up the loopback interface. */
47 int fd
= xsocket (AF_UNIX
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
49 strcpy (req
.ifr_name
, "lo");
50 TEST_VERIFY_EXIT (ioctl (fd
, SIOCGIFFLAGS
, &req
) == 0);
51 bool already_up
= req
.ifr_flags
& IFF_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");
59 req
.ifr_flags
|= IFF_UP
| IFF_RUNNING
;
60 TEST_VERIFY_EXIT (ioctl (fd
, SIOCSIFFLAGS
, &req
) == 0);
67 printf ("warning: could not enter network namespace\n");
72 support_in_uts_namespace (void)
74 return in_uts_namespace
;