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