1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/*
* newpid: launch a subprocess in a new PID namespace
* Copyright (C) 2013, 2014 Christoph Berg <myon@debian.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
/* squeeze's and lucid's libc do not expose these: */
#ifndef MS_REC
#define MS_REC 16384
#endif
#ifndef MS_SLAVE
#define MS_SLAVE (1<<19)
#endif
int
run (void *argv_void)
{
char *const *argv = argv_void;
char *argv_sh[] = { NULL, NULL };
pid_t child;
pid_t pid;
if (mount("none", "/proc", NULL, MS_SLAVE|MS_REC, NULL) != 0) {
perror ("remount proc private");
exit (1);
}
if (mount ("proc", "/proc", "proc", 0, NULL) != 0) {
perror ("mount proc");
exit (1);
}
if (argv[0] == NULL) {
char *shell = getenv ("SHELL");
if (shell)
argv_sh[0] = shell;
else
argv_sh[0] = "/bin/sh";
argv = argv_sh;
}
if ((child = fork ()) == 0) {
if (execvp (argv[0], argv) < 0) {
perror ("execvp");
exit (1);
}
/* NOT REACHED */
}
if (child < 0) {
perror ("fork");
exit (1);
}
int status;
while ((pid = wait (&status)) != child) {
if (pid < 0 && errno != EINTR) {
perror ("waitpid");
exit (1);
}
/* ignore SIGCHLD for other children and retry */
// printf ("Reaped child %d with status %d\n", pid, status);
}
if (WIFEXITED (status))
return WEXITSTATUS (status);
if (WIFSIGNALED (status))
return 128 + WTERMSIG (status);
return -1;
}
int
main (int argc, char *argv[], char *envp[])
{
char cstack[2048];
int child;
int status;
if ((child = clone (run,
cstack + 1024, /* middle of array so we don't care which way the stack grows */
CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, /* new pid & mount namespace, send SIGCHLD on termination */
argv + 1) /* skip argv[0] */
) < 0) {
perror ("clone");
exit (1);
}
if (waitpid (child, &status, 0) < 0) {
perror ("waitpid");
}
if (WIFEXITED (status))
return WEXITSTATUS (status);
if (WIFSIGNALED (status))
return 128 + WTERMSIG (status);
return -1;
}
|