File: dtrace_clone.c

package info (click to toggle)
systemtap 5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,556 kB
  • sloc: cpp: 81,117; ansic: 54,933; xml: 49,795; exp: 43,595; sh: 11,526; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (142 lines) | stat: -rw-r--r-- 3,315 bytes parent folder | download | duplicates (9)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Systemtap test case
 * Copyright (C) 2010, Red Hat Inc.
 *                                                          
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sched.h>
#include <errno.h>
#include "dtrace_clone_probes.h"

#if !defined(CLONE_NEWPID)
#define CLONE_NEWPID 0
#endif

#define CLONE_FLAGS (CLONE_NEWPID|CLONE_FILES|CLONE_FS|CLONE_VM|SIGCHLD)

#if !defined(USE_SEMAPHORES)
#undef TEST_CHILD2_ENABLED
#define TEST_CHILD2_ENABLED() (1)
#undef TEST_CHILD1_ENABLED
#define TEST_CHILD1_ENABLED() (1)
#undef TEST_CHILD2_PID_ENABLED
#define TEST_CHILD2_PID_ENABLED() (1)
#undef TEST_MAIN_ENABLED
#define TEST_MAIN_ENABLED() (1)
#undef TEST_CHILD1_PID_ENABLED
#define TEST_CHILD1_PID_ENABLED() (1)
#undef TEST_MAIN2_ENABLED
#define TEST_MAIN2_ENABLED() (1)
#endif

void *stack_base2 = NULL;
void *stack_top2 = NULL;

static int uprobes_ns_child2(void *argv __attribute__((__unused__)))
{
    if (TEST_CHILD2_ENABLED()) {
	TEST_CHILD2(getpid());
    }
    _exit(0);
}

static int uprobes_ns_child1(void *argv __attribute__((__unused__)))
{
    int pid;
    int rc = 0;

    /* Create the second clone. */
    if (TEST_CHILD1_ENABLED()) {
	TEST_CHILD1(getpid());
    }

    pid = clone(uprobes_ns_child2, stack_top2, CLONE_FLAGS, NULL);
    if (TEST_CHILD2_PID_ENABLED()) {
	TEST_CHILD2_PID(pid);
    }
    if (pid < 0) {
	/* Error. */
	char *msg2 = "clone 2 failed\n";
	write(2, msg2, strlen(msg2));
	rc = errno;
    }
    else {
	int status;

	waitpid(pid, &status, 0);
	rc = WEXITSTATUS(status);
    }
    _exit(rc);
}

int
main(int argc, char **argv)
{
    long pagesize = sysconf(_SC_PAGESIZE);
    void *stack_base1, *stack_top1;
    int pid;
    int rc = 0;

    /* Allocate both stacks here. */
    stack_base1 = calloc(pagesize * 4, 1);
    if (stack_base1 == NULL) {
	perror("calloc 1 failed:");
	return -1;
    }
    stack_base2 = calloc(pagesize * 4, 1);
    if (stack_base2 == NULL) {
	perror("calloc 2 failed:");
	return -1;
    }

    /*
     * Get top of stacks.  According to the clone() manpage:
     *   Stacks grow downwards on all processors that run Linux
     *   (except the HP PA processors), so child_stack usually points
     *   to the topmost address of the memory space set up for the
     *   child stack. 
     */
    stack_top1 = stack_base1 + (pagesize * 4);
    stack_top2 = stack_base2 + (pagesize * 4);

    /* Create the first clone (which will create the 2nd). */
    if (TEST_MAIN_ENABLED()) {
	TEST_MAIN(getpid());
    }
    pid = clone(uprobes_ns_child1, stack_top1, CLONE_FLAGS, NULL);
    if (TEST_CHILD1_PID_ENABLED()) {
	TEST_CHILD1_PID(pid);
    }
    if (pid < 0) {
	/* error */
	perror("clone 1 failed:");
	rc = -1;
    }
    else {
	int status;

	/* Wait on the first clone to finish. */
	waitpid(pid, &status, 0);
	rc = WEXITSTATUS(status);
    }
    /* Cleanup */
    free(stack_base1);
    free(stack_base2);
    if (TEST_MAIN2_ENABLED()) {
	TEST_MAIN2();
    }

    return rc;
}