setns


SYNOPSIS
       #define _GNU_SOURCE             /* See feature_test_macros(7) */
       #include <sched.h>

       int setns(int fd, int nstype);

DESCRIPTION
       Given a file descriptor referring to a namespace, reassociate the call-
       ing thread with that namespace.

       The fd argument is a file descriptor referring to one of the  namespace
       entries  in a /proc/[pid]/ns/ directory; see proc(5) for further infor-
       mation on /proc/[pid]/ns/.  The calling  thread  will  be  reassociated
       with the corresponding namespace, subject to any constraints imposed by
       the nstype argument.

       The nstype argument specifies  which  type  of  namespace  the  calling
       thread  may  be  reassociated  with.  This argument can have one of the
       following values:

       0      Allow any type of namespace to be joined.

       CLONE_NEWIPC
              fd must refer to an IPC namespace.

       CLONE_NEWNET
              fd must refer to a network namespace.

       CLONE_NEWUTS
              fd must refer to a UTS namespace.

       Specifying nstype as 0 suffices if the caller knows (or does not  care)
       what  type  of  namespace  is  referred to by fd.  Specifying a nonzero
       value for nstype is useful if the caller does not  know  what  type  of
       namespace  is  referred to by fd and wants to ensure that the namespace
       is of a particular type.  (The caller might not know the  type  of  the
       namespace  referred  to  by  fd  if  the  file descriptor was opened by
       another process and, for example, passed  to  the  caller  via  a  UNIX
       domain socket.)

RETURN VALUE
       On success, setns() returns 0.  On failure, -1 is returned and errno is
       set to indicate the error.

ERRORS
       EBADF  fd is not a valid file descriptor.

       EINVAL fd refers to a namespace whose type does not match  that  speci-
              fied  in  nstype, or there is problem with reassociating the the
              thread with the specified namespace.

       ENOMEM Cannot allocate sufficient memory to change the specified names-
              pace.

       Not  all of the attributes that can be shared when a new thread is cre-
       ated using clone(2) can be changed using setns().

EXAMPLE
       The program below takes two or  more  arguments.   The  first  argument
       specifies   the   pathname   of   a   namespace  file  in  an  existing
       /proc/[pid]/ns/ directory.  The remaining arguments specify  a  command
       and  its  arguments.   The program opens the namespace file, joins that
       namespace using setns(), and executes the specified command inside that
       namespace.

       The  following shell session demonstrates the use of this program (com-
       piled as a binary named ns_exec) in conjunction with  the  CLONE_NEWUTS
       example  program  in  the clone(2) man page (complied as a binary named
       newuts).

       We begin by executing the example program  in  clone(2)  in  the  back-
       ground.  That program creates a child in a separate UTS namespace.  The
       child changes the hostname in its namespace, and  then  both  processes
       display  the hostnames in their UTS namespaces, so that we can see that
       they are different.

           $ su                   # Need privilege for namespace operations
           Password:
           # ./newuts bizarro &
           [1] 3549
           clone() returned 3550
           uts.nodename in child:  bizarro
           uts.nodename in parent: antero
           # uname -n             # Verify hostname in the shell
           antero

       We then run the program shown below,  using  it  to  execute  a  shell.
       Inside  that  shell,  we verify that the hostname is the one set by the
       child created by the first program:

           # ./ns_exec /proc/3550/ns/uts /bin/bash
           # uname -n             # Executed in shell started by ns_exec
           bizarro

   Program source
       #define _GNU_SOURCE
       #include <fcntl.h>
       #include <sched.h>
       #include <unistd.h>
       #include <stdlib.h>
       #include <stdio.h>

       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
                               } while (0)

       int
       main(int argc, char *argv[])
       {
               errExit("setns");

           execvp(argv[2], &argv[2]);      /* Execute a command in namespace */
           errExit("execvp");
       }

SEE ALSO
       clone(2), fork(2), vfork(2), proc(5), unix(7)

COLOPHON
       This page is part of release 3.54 of the Linux  man-pages  project.   A
       description  of  the project, and information about reporting bugs, can
       be found at http://www.kernel.org/doc/man-pages/.



Linux                             2013-01-01                          SETNS(2)
Man Pages Copyright Respective Owners. Site Copyright (C) 1994 - 2018 Hurricane Electric. All Rights Reserved.