blob: 4c21688c111565e06c80eb976b18830b98d3462a [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:
Denys Vlasenkocb419c52012-03-17 01:24:25 +010013 * 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:
Denys Vlasenkocb419c52012-03-17 01:24:25 +010018 * strace -oLOG -ff ./threaded_execve
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010019 *
20 * # Should not be confused by non-traced execve-ing thread
21 * # replacing traced leader:
Denys Vlasenkocb419c52012-03-17 01:24:25 +010022 * strace -oLOG ./threaded_execve
23 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010024 * 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:
Denys Vlasenkocb419c52012-03-17 01:24:25 +010030 * strace -oLOG -f ./threaded_execve <NUM>
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010031 *
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
James Hogan554e6f32013-05-01 13:16:50 +010057#elif defined(__metag__)
58#define clone2(func, stack_base, size, flags, arg...) \
59 clone(func, stack_base, flags, arg)
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010060#else
61#define clone2(func, stack_base, size, flags, arg...) \
62 clone(func, (stack_base) + (size), flags, arg)
63#endif
64/* Direct calls to syscalls, avoiding libc wrappers */
65#define syscall_tgkill(pid, tid, sig) syscall(__NR_tgkill, (pid), (tid), (sig))
66#define syscall_getpid() syscall(__NR_getpid)
67#define syscall_gettid() syscall(__NR_gettid)
68#define syscall_exit(v) syscall(__NR_exit, (v));
69
70static char my_name[PATH_MAX];
Denys Vlasenkocb419c52012-03-17 01:24:25 +010071static int leader_final_action;
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010072
73static int
74thread1(void *unused)
75{
76 write(1, "1", 1);
77 for(;;) pause();
78 return 0;
79}
80
81static int
82thread2(void *unused)
83{
Denys Vlasenkocb419c52012-03-17 01:24:25 +010084 char buf[64];
85 sprintf(buf, "%d", leader_final_action);
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010086 write(1, "2", 1);
87 usleep(20*1000);
88 /* This fails with ENOENT if leader has exited by now! :) */
Denys Vlasenkocb419c52012-03-17 01:24:25 +010089 execl("/proc/self/exe", "exe", "exe", buf, NULL);
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010090 /* So fall back to resolved name */
Denys Vlasenkocb419c52012-03-17 01:24:25 +010091 execl(my_name, "exe", "exe", buf, NULL);
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010092 for(;;) pause();
93 return 0;
94}
95
96static void
Denys Vlasenkocb419c52012-03-17 01:24:25 +010097thread_leader(void)
Denys Vlasenkoc76ae432012-01-27 15:37:13 +010098{
99 /* malloc gives sufficiently aligned buffer.
100 * long buf[] does not! (on ia64).
101 */
102 int cnt = NUM_THREADS;
103 while (--cnt >= 0) {
104 /* As seen in pthread_create(): */
105 clone2(thread1, malloc(16 * 1024), 16 * 1024, 0
106 | CLONE_VM
107 | CLONE_FS
108 | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM
109 | 0 /* no signal to send on death */
110 , NULL);
111 usleep(20*1000);
112 }
113 clone2(thread2, malloc(16 * 1024), 16 * 1024, 0
114 | CLONE_VM
115 | CLONE_FS
116 | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM
117 | 0 /* no signal to send on death */
118 , NULL);
119
Denys Vlasenkocb419c52012-03-17 01:24:25 +0100120 /* Various states leader can be while other thread execve's: */
121 switch (leader_final_action % 3) {
122 case 0: syscall_exit(42); /* leader is dead */
123 case 1: for(;;) pause(); /* leader is in syscall */
124 default: for(;;) continue; /* leader is in userspace */
125 }
Denys Vlasenkoc76ae432012-01-27 15:37:13 +0100126}
127
128int
129main(int argc, char **argv)
130{
Denys Vlasenkoc76ae432012-01-27 15:37:13 +0100131 if (readlink("/proc/self/exe", my_name, sizeof(my_name)-1) <= 0)
132 return 1;
133
134 setbuf(stdout, NULL);
135
Denys Vlasenkocb419c52012-03-17 01:24:25 +0100136 if (argv[1] && strcmp(argv[1], "exe") == 0) {
137 leader_final_action = atoi(argv[2]) + 1;
138 thread_leader();
139 }
Denys Vlasenkoc76ae432012-01-27 15:37:13 +0100140
141 printf("%d: thread leader\n", getpid());
142
143 alarm(argv[1] ? atoi(argv[1]) : 1);
Denys Vlasenkocb419c52012-03-17 01:24:25 +0100144 thread_leader();
Denys Vlasenkoc76ae432012-01-27 15:37:13 +0100145
146 return 0;
147}