From 1b5473169260144854a0e63bae6f50199521c4e4 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Fri, 8 Nov 2019 11:33:41 +0100 Subject: [PATCH] pivot_root.2: EXAMPLE: allocate stack using mmap() MAP_STACK rather than malloc() Signed-off-by: Michael Kerrisk --- man2/pivot_root.2 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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) -- 2.39.2