Ishan Khare
Ishan Khare

Reputation: 1767

linux clone() returning -1 as child_pid

I have the following program:

#define _GNU_SOURCE
#include<sched.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>

static char child_stack[2048];

static int child_fn() {
    printf("Pid: %ld\n", (long) getpid());
    return 0;
}

int main() {
    pid_t child_pid = clone(child_fn, child_stack+1024, CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, NULL);
    printf("clone()= %ld\n", (long) child_pid);

    waitpid(child_pid, NULL, 0);
    return 0;
}

I get the following output

# ./clone                                                                                                       
clone()= -1

Can anyone help me understand why is it so?

Note: I'm running this under a docker image with ubuntu:latest. Also notice that the prompt is # instead of $, so does it have anything to do with the user who's running this process, probably root in this case?

Upvotes: 0

Views: 331

Answers (1)

Ishan Khare
Ishan Khare

Reputation: 1767

As mentioned in the comments I used strerror(3) to find why I was getting the error. The error was Operation not permitted. I searched a bit about it and found the solution here - clone: operation not permitted

So to fix it, all I had to do was to run my docker container with --privileged flag

Upvotes: 1

Related Questions