// Modified from https://lwn.net/Articles/533492/ // Copyright 2013, Michael Kerrisk // Licensed under GNU General Public License v2 or later #define _GNU_SOURCE #include #include #include #include #include #include #include /* A simple error-handling function: print an error message based on the value in 'errno' and terminate the calling process */ #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0) /* Start function for cloned child */ static int childFunc(void *arg) { char **argv = arg; // TODO: Mise en place pour le processus fils execvp(argv[0], &argv[0]); errExit("execvp"); } /* Space for child's stack */ #define STACK_SIZE (1024 * 1024) static char child_stack[STACK_SIZE]; int main(int argc, char *argv[]) { int flags = 0; pid_t child_pid; // TODO: mise en place du flag pour le PID namespace child_pid = clone(childFunc, child_stack + STACK_SIZE, flags | SIGCHLD, &argv[1]); if (child_pid == -1) errExit("clone"); printf("%s: PID of child created by clone() is %ld\n", argv[0], (long) child_pid); /* Parent falls through to here */ if (waitpid(child_pid, NULL, 0) == -1) /* Wait for child */ errExit("waitpid"); printf("%s: terminating\n", argv[0]); exit(EXIT_SUCCESS); }