github.com/Cloud-Foundations/Dominator@v0.3.4/c/run-in-mntns.c (about)

     1  #define _GNU_SOURCE
     2  #include <stdio.h>
     3  #include <stdlib.h>
     4  #include <sys/types.h>
     5  #include <sys/stat.h>
     6  #include <sys/time.h>
     7  #include <sys/resource.h>
     8  #include <fcntl.h>
     9  #include <sched.h>
    10  #include <sys/syscall.h>
    11  
    12  int main (int argc, char **argv) 
    13  {
    14      int pid, fd;
    15      char filename[256];
    16  
    17      if (argc < 3) 
    18      {
    19  	fprintf (stderr, "Usage: run-in-mntns pid command...\n");
    20  	exit (1);
    21      }
    22      setpriority(PRIO_PROCESS, 0, 0);
    23      pid = atoi(argv[1]);
    24      snprintf(filename, sizeof(filename), "/proc/%d/ns/mnt", pid);
    25      fd = open(filename, O_RDONLY);
    26      if (fd < 0) 
    27      {
    28  	perror (filename);
    29  	exit (1);
    30      }
    31      if (syscall(SYS_setns, fd, CLONE_NEWNS) < 0) 
    32      {
    33  	perror ("Error setting namespace");
    34  	exit (1);
    35      }
    36      close (fd);
    37      execvp (argv[2], argv + 2);
    38      perror (argv[2]);
    39      exit (1);
    40  }