1 /* Source: xio-namespaces.c */
2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* This file contains Linux namespace related code */
7 #include "xiosysincludes.h"
10 #include "xio-namespaces.h"
14 const struct optdesc opt_set_netns
= { "netns", NULL
, OPT_SET_NETNS
, GROUP_PROCESS
, PH_INIT
, TYPE_STRING
, OFUNC_SET_NAMESPACE
, 0, 0, 0 };
17 /* Set the given namespace. Requires root or the appropriate CAP_*-
18 Returns 0 on success, or -1 on error. */
19 int xio_set_namespace(
23 char nspath
[PATH_MAX
];
27 if (!xioparms
.experimental
) {
28 Error1("option \"%s\" requires use of --experimental", nstype
);
31 snprintf(nspath
, sizeof(nspath
)-1, "/run/%s/%s", nstype
, nsname
);
32 Info1("switching to net namespace \"%s\"", nsname
);
33 nsfd
= Open(nspath
, O_RDONLY
|O_CLOEXEC
, 000);
35 Error2("open(%s, O_RDONLY|O_CLOEXEC): %s", nspath
, strerror(errno
));
38 rc
= Setns(nsfd
, CLONE_NEWNET
);
40 Error2("setns(%d, CLONE_NEWNET): %s", nsfd
, strerror(errno
));
47 int xio_apply_namespace(
52 char old_nspath
[PATH_MAX
];
55 if (retropt_string(opts
, OPT_SET_NETNS
, &netns_name
) < 0)
58 /* Get path describing current namespace */
59 snprintf(old_nspath
, sizeof(old_nspath
)-1, "/proc/"F_pid
"/ns/net",
62 /* Get a file descriptor to current ns for later reset */
63 old_netfd
= Open(old_nspath
, O_RDONLY
|O_CLOEXEC
, 000);
65 Error2("open(%s, O_RDONLY|O_CLOEXEC): %s",
66 old_nspath
, strerror(errno
));
71 /* 0 means not netns option, oops */
72 Error1("%s(): INTERNAL", __func__
);
77 rc
= xio_set_namespace("netns", netns_name
);
87 /* Sets the given namespace to that of process 1, this is assumed to be the
89 Returns 0 on success, or -1 on error. */
90 int xio_reset_namespace(
95 rc
= Setns(saved_netfd
, CLONE_NEWNET
);
97 Error2("xio_reset_namespace(%d): %s", saved_netfd
, strerror(errno
));
105 #endif /* WITH_NAMESPACES */