// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: Apache-2.023/*4* This is a very simple tool, used by the testing system to clone/exec into5* the jailer.6* All it does is7* - clone() into a new PID namespace, then8* - have the child process exec() into the binary received via command line,9* and10* - have the parent process print the child PID to stdout.11*12* Usage: ./newpid_cloner <binary_to_execute> <arg1> <arg2> ...13* Example: ./newpid_cloner /bin/firecracker --api-sock /var/run/fire.sock14*15*/1617#define _GNU_SOURCE1819#include <sched.h>20#include <stdio.h>21#include <unistd.h>22#include <errno.h>23#include <sys/mman.h>242526#define CHILD_STACK_SIZE 4096272829int child_main(void *arg) {30char **argv = (char**)arg;31execv(argv[0], argv);32}3334int main(int argc, char *const argv[]) {3536char child_stack[CHILD_STACK_SIZE];37int child_pid = child_pid = clone(38child_main,39(char*)child_stack + CHILD_STACK_SIZE,40CLONE_NEWPID,41((char **)argv) + 142);4344printf("%d", child_pid);45return (child_pid != -1) ? 0 : errno;46}474849