pivot_root.2: EXAMPLE: allocate stack using mmap() MAP_STACK rather than malloc()

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2019-11-08 11:33:41 +01:00
parent 99c3a00027
commit 1b54731692
1 changed files with 5 additions and 3 deletions

View File

@ -325,6 +325,7 @@ hello world
#include <sys/mount.h>
#include <sys/stat.h>
#include <limits.h>
#include <sys/mman.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
} while (0)
@ -392,9 +393,10 @@ main(int argc, char *argv[])
{
/* Create a child process in a new mount namespace */
char *stack = malloc(STACK_SIZE);
if (stack == NULL)
errExit("malloc");
char *stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, \-1, 0);
if (stack == MAP_FAILED)
errExit("mmap");
if (clone(child, stack + STACK_SIZE,
CLONE_NEWNS | SIGCHLD, &argv[1]) == \-1)