/
t_clone.c
91 lines (71 loc) · 3.08 KB
/
t_clone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**********************************************************************\
* Copyright (C) Michael Kerrisk, 2010. *
* *
* This program is free software. You may use, modify, and redistribute *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 or (at *
* your option) any later version. This program is distributed without *
* any warranty. See the file COPYING for details. *
\**********************************************************************/
/* t_clone.c
Demonstrate the use of the Linux-specific clone() system call.
*/
#define _GNU_SOURCE
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sched.h>
#include "tlpi_hdr.h"
#ifndef CHILD_SIG
#define CHILD_SIG SIGUSR1 /* Signal to be generated on termination
of cloned child */
#endif
static int /* Startup function for cloned child */
childFunc(void *arg)
{
if (close(*((int *) arg)) == -1)
errExit("close");
return 0; /* Child terminates now */
}
int
main(int argc, char *argv[])
{
const int STACK_SIZE = 65536; /* Stack size for cloned child */
char *stack; /* Start of stack buffer */
char *stackTop; /* End of stack buffer */
int s, fd, flags;
fd = open("/dev/null", O_RDWR); /* Child will close this fd */
if (fd == -1)
errExit("open");
/* If argc > 1, child shares file descriptor table with parent */
flags = (argc > 1) ? CLONE_FILES : 0;
/* Allocate stack for child */
stack = malloc(STACK_SIZE);
if (stack == NULL)
errExit("malloc");
stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
/* Ignore CHILD_SIG, in case it is a signal whose default is to
terminate the process; but don't ignore SIGCHLD (which is ignored
by default), since that would prevent the creation of a zombie. */
if (CHILD_SIG != 0 && CHILD_SIG != SIGCHLD)
if (signal(CHILD_SIG, SIG_IGN) == SIG_ERR)
errExit("signal");
/* Create child; child commences execution in childFunc() */
if (clone(childFunc, stackTop, flags | CHILD_SIG, (void *) &fd) == -1)
errExit("clone");
/* Parent falls through to here. Wait for child; __WCLONE is
needed for child notifying with signal other than SIGCHLD. */
if (waitpid(-1, NULL, (CHILD_SIG != SIGCHLD) ? __WCLONE : 0) == -1)
errExit("waitpid");
printf("child has terminated\n");
/* Did close() of file descriptor in child affect parent? */
s = write(fd, "x", 1);
if (s == -1 && errno == EBADF)
printf("file descriptor %d has been closed\n", fd);
else if (s == -1)
printf("write() on file descriptor %d failed "
"unexpectedly (%s)\n", fd, strerror(errno));
else
printf("write() on file descriptor %d succeeded\n", fd);
exit(EXIT_SUCCESS);
}