From: Michael Kerrisk Date: Fri, 8 Nov 2019 10:33:41 +0000 (+0100) Subject: pivot_root.2: EXAMPLE: allocate stack using mmap() MAP_STACK rather than malloc() X-Git-Tag: man-pages-5.04~14 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1b5473169260144854a0e63bae6f50199521c4e4;p=thirdparty%2Fman-pages.git pivot_root.2: EXAMPLE: allocate stack using mmap() MAP_STACK rather than malloc() Signed-off-by: Michael Kerrisk --- diff --git a/man2/pivot_root.2 b/man2/pivot_root.2 index 93b2c029c6..e84417d1ca 100644 --- a/man2/pivot_root.2 +++ b/man2/pivot_root.2 @@ -325,6 +325,7 @@ hello world #include #include #include +#include #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)