
0m3
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;
}
Most Liked

0m3
Hello, everyone.
The solution is here The child process does not start with syscall(SYS_clone3, …) + CLONE_VM.
BR, Denis
1
Popular Linux topics

Hey everyone,
I am about to receive a work laptop: the Lenovo T490 (could be changed to T490s, unclear as of yet).
I am after the perfe...
New

I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New

I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New

It is a bit late for RockyLinux to ship their official release, they are still in RC1.
I am ready using AlmaLinux 8.4.
New

Hello, everyone.
It is necessary that the calling process and the child process are in the same memory space.
Therefore, I use the CLON...
New
Other popular topics

Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face:
Perhaps if there’s enough peop...
New

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

Not sure if following fits exactly this thread, or if we should have a hobby thread…
For many years I’m designing and building model air...
New

Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New

Saw this on TikTok of all places! :lol:
Anyone heard of them before?
Lite:
New

Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

Author Spotlight
Dmitry Zinoviev
@aqsaqal
Today we’re putting our spotlight on Dmitry Zinoviev, author of Data Science Essentials in ...
New

The overengineered Solution to my Pigeon Problem.
TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New

Author Spotlight:
Karl Stolley
@karlstolley
Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New

Author Spotlight:
Peter Ullrich
@PJUllrich
Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /java
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /sublime-text
- /textmate
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /django
- /spring
- /manjaro
- /diversity
- /nodejs
- /lua
- /julia
- /slackware
- /c
- /markdown