]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
pivot_root.2: EXAMPLE: allocate stack using mmap() MAP_STACK rather than malloc()
authorMichael Kerrisk <mtk.manpages@gmail.com>
Fri, 8 Nov 2019 10:33:41 +0000 (11:33 +0100)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Thu, 14 Nov 2019 21:24:45 +0000 (22:24 +0100)
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
man2/pivot_root.2

index 93b2c029c6e836c057a21a8d50e6826a029fd1b5..e84417d1ca9b689e854d55b8e130d636fd4d2a10 100644 (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)