blob: 9cf3dae2ddec64c58b8ff42aa1d4a4150f14aaa6 [file] [log] [blame]
Denys Vlasenkoc76ae432012-01-27 15:37:13 +01001/*
2 * Create NUM_THREADS threads which print "1" and sleep in pause().
3 * Then create another thread which prints "2", and re-execs the program.
4 * The leader then either sleeps in pause(), or exits if $LEADER_EXIT is set.
5 * This triggers "execve'ed thread replaces thread leader" case.
6 *
7 * gcc -Wall -Os -o threaded_execve threaded_execve.c
8 *
9 * Try running it under strace like this:
10 *
11 * # Should not be confused by traced execve-ing thread
12 * # replacing traced leader:
13 * [LEADER_EXIT=1] strace -oLOG -f ./threaded_execve
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010014 *
15 * # Same, but different output mode. Output after execve
16 * # should go into leader's LOG.<pid> file, not into execve'ed
17 * # thread's log file:
18 * [LEADER_EXIT=1] strace -oLOG -ff ./threaded_execve
19 *
20 * # Should not be confused by non-traced execve-ing thread
21 * # replacing traced leader:
22 * [LEADER_EXIT=1] strace -oLOG ./threaded_execve
23 * ^^^^^^^^^^^^^^^^^^^^^
24 * In Linux 3.2, non-traced execve-ing thread does not
25 * become traced after execve, even though it has pid == leader's pid
Denys Vlasenko000b6012012-01-28 01:25:03 +010026 * after execve. And yet, strace's waitpid doesn't return ECHILD.
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010027 *
28 * # Run for NUM seconds, not just one second.
29 * # Watch top to check for memory leaks in strace:
30 * [LEADER_EXIT=1] strace -oLOG -f ./threaded_execve <NUM>
31 *
32 */
33#define NUM_THREADS 1
34
35#define _GNU_SOURCE 1
36#include <assert.h>
37#include <limits.h>
38#include <stddef.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include <errno.h>
43#include <stdio.h>
44#include <sched.h>
45#include <signal.h>
46#include <dirent.h>
47#include <fcntl.h>
48#include <sys/types.h>
49#include <sys/wait.h>
50#include <sys/syscall.h>
51
52/* Define clone2 for all arches */
53#ifdef __ia64__
54extern int __clone2(int (*fn) (void *), void *child_stack_base,
55 size_t stack_size, int flags, void *arg, ...);
56#define clone2 __clone2
57#else
58#define clone2(func, stack_base, size, flags, arg...) \
59 clone(func, (stack_base) + (size), flags, arg)
60#endif
61/* Direct calls to syscalls, avoiding libc wrappers */
62#define syscall_tgkill(pid, tid, sig) syscall(__NR_tgkill, (pid), (tid), (sig))
63#define syscall_getpid() syscall(__NR_getpid)
64#define syscall_gettid() syscall(__NR_gettid)
65#define syscall_exit(v) syscall(__NR_exit, (v));
66
67static char my_name[PATH_MAX];
68
69static int
70thread1(void *unused)
71{
72 write(1, "1", 1);
73 for(;;) pause();
74 return 0;
75}
76
77static int
78thread2(void *unused)
79{
80 write(1, "2", 1);
81 usleep(20*1000);
82 /* This fails with ENOENT if leader has exited by now! :) */
83 execl("/proc/self/exe", "exe", "exe", NULL);
84 /* So fall back to resolved name */
85 execl(my_name, "exe", "exe", NULL);
86 for(;;) pause();
87 return 0;
88}
89
90static void
91thread_leader(int die)
92{
93 /* malloc gives sufficiently aligned buffer.
94 * long buf[] does not! (on ia64).
95 */
96 int cnt = NUM_THREADS;
97 while (--cnt >= 0) {
98 /* As seen in pthread_create(): */
99 clone2(thread1, malloc(16 * 1024), 16 * 1024, 0
100 | CLONE_VM
101 | CLONE_FS
102 | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM
103 | 0 /* no signal to send on death */
104 , NULL);
105 usleep(20*1000);
106 }
107 clone2(thread2, malloc(16 * 1024), 16 * 1024, 0
108 | CLONE_VM
109 | CLONE_FS
110 | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM
111 | 0 /* no signal to send on death */
112 , NULL);
113
114 if (die) syscall_exit(42);
115 for(;;) pause();
116}
117
118int
119main(int argc, char **argv)
120{
121 int die = getenv("LEADER_EXIT") != NULL;
122
123 if (readlink("/proc/self/exe", my_name, sizeof(my_name)-1) <= 0)
124 return 1;
125
126 setbuf(stdout, NULL);
127
128 if (argv[1] && strcmp(argv[1], "exe") == 0)
129 thread_leader(die);
130
131 printf("%d: thread leader\n", getpid());
132
133 alarm(argv[1] ? atoi(argv[1]) : 1);
134 thread_leader(die);
135
136 return 0;
137}