0m3

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;
}
/c

Most Liked

0m3

0m3

Hello, everyone.

The solution is here The child process does not start with syscall(SYS_clone3, …) + CLONE_VM.

BR, Denis

Popular Linux topics Top

New
First poster: bot
A new Arch Linux blog post/announcement has been posted! Get the full details here: Arch Linux - News: Arch Linux mailing list id changes
New
First poster: bot
A new Arch Linux blog post/announcement has been posted! Get the full details here: Arch Linux - News: Manual pages indexing service
New
First poster: bot
A new openSUSE blog post/announcement has been posted! Get the full details here: PostgreSQL, GNOME, Rubygems Update in Tumbleweed - op...
New
First poster: bot
A new openSUSE blog post/announcement has been posted! Get the full details here: openSUSE Project Selected for Google Summer of Code M...
New
First poster: bot
A new CentOS blog post/announcement has been posted! Get the full details here: CentOS Community Newsletter, April 2021 (#2104) – Blog....
New
New
First poster: bot
A new Rocky Linux blog post/announcement has been posted! Get the full details here: Community Update - December 2021 - Q4 | Rocky Linux
New
First poster: bot
A new NixOS blog post/announcement has been posted! Get the full details here: Blog → Announcements | Nix & NixOS
New
First poster: bot
A new Rocky Linux blog post/announcement has been posted! Get the full details here: FIPS Validation Update - June 2022 | Rocky Linux
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
AstonJ
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
First poster: bot
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
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New