The child process does not start with syscall(SYS_clone3, ...) + CLONE_VM

Hello, everyone.
It is necessary that the calling process and the child process are in the same memory space.
Therefore, I use the CLONE_VM flag.
But the child process does not start.
It looks like something is wrong with the allocation of memory for the stack.
Could you please explain the reason?
#define _DEFAULT_SOURCE /* syscall() */
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64 /* getrlimit() */
#include <sched.h> /* CLONE_* constants */
#include <linux/sched.h> /* struct clone_args */
#include <sys/syscall.h> /* SYS_* constants */
#include <unistd.h>
#include <stdint.h> /* uintptr_t */
#include <stdio.h>
#include <stdlib.h>
#include <err.h> /* err() */
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <signal.h>
void spawn(void)
{
struct rlimit rlim;
struct clone_args cl_args;
__u64 stack_size;
__u64 *stackBot;
__u64 *stackTop;
pid_t pid;
memset(&cl_args, 0, sizeof(cl_args));
if (getrlimit(RLIMIT_STACK, &rlim) == -1) {
err(EXIT_FAILURE, "getrlimit");
}
stack_size = rlim.rlim_cur;
stackBot = (__u64 *)
mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN |
MAP_STACK, -1, 0);
if (stackBot == MAP_FAILED) {
err(EXIT_FAILURE, "mmap");
}
stackTop = stackBot + (stack_size / sizeof(__u64));
cl_args.flags = CLONE_FILES | CLONE_IO | CLONE_VM;
cl_args.exit_signal = SIGCHLD;
cl_args.stack = (__u64) (uintptr_t) stackTop;
cl_args.stack_size = stack_size;
pid = syscall(SYS_clone3, &cl_args, sizeof(cl_args));
switch(pid) {
case -1:
munmap(stackBot, stack_size);
err(EXIT_FAILURE, "syscall");
case 0: /* Child */
printf("Hello from child!\n");
munmap(stackBot, stack_size);
break;
default: /* Parent */
printf("Hello from parent!\n");
break;
}
}
int
main(int argc, char *argv[])
{
spawn();
printf("Before last while\n");
while(1) {
}
return EXIT_SUCCESS;
}
Popular #Linux topics
Other popular topics

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New

Why, if your answer is yes?
New

I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me.
I think the Planck and the Preonic (o...
New

“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin
Even when you’re ultimately right about what the future ho...
New

Think Again 50% Off Sale »
The theme of this sale is new perspectives on familiar topics.
Enter coupon code ThinkAgain2021 at checkout t...
New

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
New

Was just curious to see if any were around, found this one:
I got 51/100:
Not sure if it was meant to buy I am sure at times the b...
New

If you want a quick and easy way to block any website on your Mac using Little Snitch simply…
File > New Rule:
And select Deny, O...
New

Will Swifties’ war on AI fakes spark a deepfake porn reckoning?
New

This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New
Latest in #Linux
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /flutter
- /preact
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm