#define _BSD_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/unistd.h>
#include <sched.h>

#ifndef CLONE_NEWUTS
#define CLONE_NEWUTS 0x04000000
#endif

int do_clone_task(void)
{
	execl("/bin/sh", "/bin/sh", NULL);
}

int main(int argc, char *argv[])
{
	int pid;
	int ret;
	int status;
	int fd1, fd2;

	if (argc < 2)
		return 1;
	pid = atoi(argv[1]);

	fd1 = open("/tmp/helloworld", O_RDWR|O_CREAT);
	if (fd1 == -1)
		printf("Error opening /tmp/helloworld\n");
	if (fd1 != 5) {
		dup2(fd1, 5);
		close(fd1);
	}
	write(5, "hi\n", 4);

	ret = syscall(327, SIGCHLD, pid, NULL, NULL);

	if  (ret == 0) {
		return do_clone_task();
	} else if (ret < 0) {
		perror("sys_hijack");
	} else {
		printf("waiting on cloned process %d\n", ret);
		ret = waitpid(ret, &status, __WALL);
		printf("cloned process exited with %d (waitpid ret %d)\n",
				status, ret);
	}

	return ret;
}

