Last active
October 18, 2016 04:28
-
-
Save tonybaloney/51ed0c1957a7d50257b52af708464d9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <sys/wait.h> | |
#include <sys/utsname.h> | |
#include <sched.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */ | |
int | |
main(int argc, char *argv[]) | |
{ | |
char *stack; /* Start of stack buffer */ | |
char *stackTop; /* End of stack buffer */ | |
pid_t pid; | |
struct utsname uts; | |
if (argc < 2) { | |
fprintf(stderr, "Usage: %s <child-hostname>\n", argv[0]); | |
exit(EXIT_SUCCESS); | |
} | |
/* Allocate stack for child */ | |
stack = malloc(STACK_SIZE); | |
if (stack == NULL) | |
errExit("malloc"); | |
stackTop = stack + STACK_SIZE; /* Assume stack grows downward */ | |
/* Create child that has its own UTS namespace; | |
child commences execution in childFunc() */ | |
pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); | |
if (pid == -1) | |
errExit("clone"); | |
printf("clone() returned %ld\n", (long) pid); | |
/* Parent falls through to here */ | |
sleep(1); /* Give child time to change its hostname */ | |
/* Display hostname in parent's UTS namespace. This will be | |
different from hostname in child's UTS namespace. */ | |
if (uname(&uts) == -1) | |
errExit("uname"); | |
printf("uts.nodename in parent: %s\n", uts.nodename); | |
if (waitpid(pid, NULL, 0) == -1) /* Wait for child */ | |
errExit("waitpid"); | |
printf("child has terminated\n"); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment