]> granicus.if.org Git - strace/blob - tests/attach-f-p.c
3bfc99686bd0ceff85fb53bd9dcf67237a303a8c
[strace] / tests / attach-f-p.c
1 /*
2  * This file is part of attach-f-p strace test.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <assert.h>
12 #include <errno.h>
13 #include <pthread.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/stat.h>
17 #include <asm/unistd.h>
18 #include <unistd.h>
19
20 #define N 3
21
22 typedef union {
23         void *ptr;
24         pid_t pid;
25 } retval_t;
26
27 static const char text_parent[] = "attach-f-p.test parent";
28 static const char *child[N] = {
29         "attach-f-p.test child 0",
30         "attach-f-p.test child 1",
31         "attach-f-p.test child 2"
32 };
33 typedef int pipefd[2];
34 static pipefd pipes[N];
35
36 static void *
37 thread(void *a)
38 {
39         unsigned int no = (long) a;
40         int i;
41
42         if (read(pipes[no][0], &i, sizeof(i)) != (int) sizeof(i))
43                 perror_msg_and_fail("read[%u]", no);
44         assert(chdir(child[no]) == -1);
45         retval_t retval = { .pid = syscall(__NR_gettid) };
46         return retval.ptr;
47 }
48
49 int
50 main(void)
51 {
52         pthread_t t[N];
53         unsigned int i;
54
55         if (write(1, "", 0) != 0)
56                 perror_msg_and_fail("write");
57
58         for (i = 0; i < N; ++i) {
59                 if (pipe(pipes[i]))
60                         perror_msg_and_fail("pipe");
61
62                 errno = pthread_create(&t[i], NULL, thread, (void *) (long) i);
63                 if (errno)
64                         perror_msg_and_fail("pthread_create");
65         }
66
67         if (write(1, "\n", 1) != 1)
68                 perror_msg_and_fail("write");
69
70         /* wait for the peer to write to stdout */
71         struct stat st;
72         for (;;) {
73                 if (fstat(1, &st))
74                         perror_msg_and_fail("fstat");
75                 if (st.st_size >= 103)
76                         break;
77         }
78
79         for (i = 0; i < N; ++i) {
80                 /* sleep a bit to let the tracer catch up */
81                 sleep(1);
82                 if (write(pipes[i][1], &i, sizeof(i)) != (int) sizeof(i))
83                         perror_msg_and_fail("write[%u]", i);
84                 retval_t retval;
85                 errno = pthread_join(t[i], &retval.ptr);
86                 if (errno)
87                         perror_msg_and_fail("pthread_join");
88                 errno = ENOENT;
89                 printf("%-5d chdir(\"%s\") = %s\n"
90                        "%-5d +++ exited with 0 +++\n",
91                        retval.pid, child[i], sprintrc(-1), retval.pid);
92         }
93
94         /* sleep a bit more to let the tracer catch up */
95         sleep(1);
96
97         pid_t pid = getpid();
98         assert(chdir(text_parent) == -1);
99
100         printf("%-5d chdir(\"%s\") = -1 ENOENT (%m)\n"
101                "%-5d +++ exited with 0 +++\n", pid, text_parent, pid);
102
103         return 0;
104 }