xref: /aosp_15_r20/external/strace/tests-m32/attach-f-p.c (revision cf84ac9a129d8ea9952db616b4e9b904c4bdde56)
1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker  * This file is part of attach-f-p strace test.
3*cf84ac9aSAndroid Build Coastguard Worker  *
4*cf84ac9aSAndroid Build Coastguard Worker  * Copyright (c) 2016 Dmitry V. Levin <[email protected]>
5*cf84ac9aSAndroid Build Coastguard Worker  * All rights reserved.
6*cf84ac9aSAndroid Build Coastguard Worker  *
7*cf84ac9aSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
8*cf84ac9aSAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
9*cf84ac9aSAndroid Build Coastguard Worker  * are met:
10*cf84ac9aSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
11*cf84ac9aSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
12*cf84ac9aSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
13*cf84ac9aSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in the
14*cf84ac9aSAndroid Build Coastguard Worker  *    documentation and/or other materials provided with the distribution.
15*cf84ac9aSAndroid Build Coastguard Worker  * 3. The name of the author may not be used to endorse or promote products
16*cf84ac9aSAndroid Build Coastguard Worker  *    derived from this software without specific prior written permission.
17*cf84ac9aSAndroid Build Coastguard Worker  *
18*cf84ac9aSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*cf84ac9aSAndroid Build Coastguard Worker  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*cf84ac9aSAndroid Build Coastguard Worker  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*cf84ac9aSAndroid Build Coastguard Worker  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*cf84ac9aSAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*cf84ac9aSAndroid Build Coastguard Worker  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*cf84ac9aSAndroid Build Coastguard Worker  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*cf84ac9aSAndroid Build Coastguard Worker  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*cf84ac9aSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*cf84ac9aSAndroid Build Coastguard Worker  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*cf84ac9aSAndroid Build Coastguard Worker  */
29*cf84ac9aSAndroid Build Coastguard Worker 
30*cf84ac9aSAndroid Build Coastguard Worker #include "tests.h"
31*cf84ac9aSAndroid Build Coastguard Worker #include <assert.h>
32*cf84ac9aSAndroid Build Coastguard Worker #include <errno.h>
33*cf84ac9aSAndroid Build Coastguard Worker #include <pthread.h>
34*cf84ac9aSAndroid Build Coastguard Worker #include <stdio.h>
35*cf84ac9aSAndroid Build Coastguard Worker #include <stdlib.h>
36*cf84ac9aSAndroid Build Coastguard Worker #include <sys/stat.h>
37*cf84ac9aSAndroid Build Coastguard Worker #include <asm/unistd.h>
38*cf84ac9aSAndroid Build Coastguard Worker #include <unistd.h>
39*cf84ac9aSAndroid Build Coastguard Worker 
40*cf84ac9aSAndroid Build Coastguard Worker #define N 3
41*cf84ac9aSAndroid Build Coastguard Worker 
42*cf84ac9aSAndroid Build Coastguard Worker typedef union {
43*cf84ac9aSAndroid Build Coastguard Worker 	void *ptr;
44*cf84ac9aSAndroid Build Coastguard Worker 	pid_t pid;
45*cf84ac9aSAndroid Build Coastguard Worker } retval_t;
46*cf84ac9aSAndroid Build Coastguard Worker 
47*cf84ac9aSAndroid Build Coastguard Worker static const char text_parent[] = "attach-f-p.test parent";
48*cf84ac9aSAndroid Build Coastguard Worker static const char *child[N] = {
49*cf84ac9aSAndroid Build Coastguard Worker 	"attach-f-p.test child 0",
50*cf84ac9aSAndroid Build Coastguard Worker 	"attach-f-p.test child 1",
51*cf84ac9aSAndroid Build Coastguard Worker 	"attach-f-p.test child 2"
52*cf84ac9aSAndroid Build Coastguard Worker };
53*cf84ac9aSAndroid Build Coastguard Worker typedef int pipefd[2];
54*cf84ac9aSAndroid Build Coastguard Worker static pipefd pipes[N];
55*cf84ac9aSAndroid Build Coastguard Worker 
56*cf84ac9aSAndroid Build Coastguard Worker static void *
thread(void * a)57*cf84ac9aSAndroid Build Coastguard Worker thread(void *a)
58*cf84ac9aSAndroid Build Coastguard Worker {
59*cf84ac9aSAndroid Build Coastguard Worker 	unsigned int no = (long) a;
60*cf84ac9aSAndroid Build Coastguard Worker 	int i;
61*cf84ac9aSAndroid Build Coastguard Worker 
62*cf84ac9aSAndroid Build Coastguard Worker 	if (read(pipes[no][0], &i, sizeof(i)) != (int) sizeof(i))
63*cf84ac9aSAndroid Build Coastguard Worker 		perror_msg_and_fail("read[%u]", no);
64*cf84ac9aSAndroid Build Coastguard Worker 	assert(chdir(child[no]) == -1);
65*cf84ac9aSAndroid Build Coastguard Worker 	retval_t retval = { .pid = syscall(__NR_gettid) };
66*cf84ac9aSAndroid Build Coastguard Worker 	return retval.ptr;
67*cf84ac9aSAndroid Build Coastguard Worker }
68*cf84ac9aSAndroid Build Coastguard Worker 
69*cf84ac9aSAndroid Build Coastguard Worker int
main(void)70*cf84ac9aSAndroid Build Coastguard Worker main(void)
71*cf84ac9aSAndroid Build Coastguard Worker {
72*cf84ac9aSAndroid Build Coastguard Worker 	pthread_t t[N];
73*cf84ac9aSAndroid Build Coastguard Worker 	unsigned int i;
74*cf84ac9aSAndroid Build Coastguard Worker 
75*cf84ac9aSAndroid Build Coastguard Worker 	if (write(1, "", 0) != 0)
76*cf84ac9aSAndroid Build Coastguard Worker 		perror_msg_and_fail("write");
77*cf84ac9aSAndroid Build Coastguard Worker 
78*cf84ac9aSAndroid Build Coastguard Worker 	for (i = 0; i < N; ++i) {
79*cf84ac9aSAndroid Build Coastguard Worker 		if (pipe(pipes[i]))
80*cf84ac9aSAndroid Build Coastguard Worker 			perror_msg_and_fail("pipe");
81*cf84ac9aSAndroid Build Coastguard Worker 
82*cf84ac9aSAndroid Build Coastguard Worker 		errno = pthread_create(&t[i], NULL, thread, (void *) (long) i);
83*cf84ac9aSAndroid Build Coastguard Worker 		if (errno)
84*cf84ac9aSAndroid Build Coastguard Worker 			perror_msg_and_fail("pthread_create");
85*cf84ac9aSAndroid Build Coastguard Worker 	}
86*cf84ac9aSAndroid Build Coastguard Worker 
87*cf84ac9aSAndroid Build Coastguard Worker 	if (write(1, "\n", 1) != 1)
88*cf84ac9aSAndroid Build Coastguard Worker 		perror_msg_and_fail("write");
89*cf84ac9aSAndroid Build Coastguard Worker 
90*cf84ac9aSAndroid Build Coastguard Worker 	/* wait for the peer to write to stdout */
91*cf84ac9aSAndroid Build Coastguard Worker 	struct stat st;
92*cf84ac9aSAndroid Build Coastguard Worker 	for (;;) {
93*cf84ac9aSAndroid Build Coastguard Worker 		if (fstat(1, &st))
94*cf84ac9aSAndroid Build Coastguard Worker 			perror_msg_and_fail("fstat");
95*cf84ac9aSAndroid Build Coastguard Worker 		if (st.st_size >= 103)
96*cf84ac9aSAndroid Build Coastguard Worker 			break;
97*cf84ac9aSAndroid Build Coastguard Worker 	}
98*cf84ac9aSAndroid Build Coastguard Worker 
99*cf84ac9aSAndroid Build Coastguard Worker 	for (i = 0; i < N; ++i) {
100*cf84ac9aSAndroid Build Coastguard Worker 		/* sleep a bit to let the tracer catch up */
101*cf84ac9aSAndroid Build Coastguard Worker 		sleep(1);
102*cf84ac9aSAndroid Build Coastguard Worker 		if (write(pipes[i][1], &i, sizeof(i)) != (int) sizeof(i))
103*cf84ac9aSAndroid Build Coastguard Worker 			perror_msg_and_fail("write[%u]", i);
104*cf84ac9aSAndroid Build Coastguard Worker 		retval_t retval;
105*cf84ac9aSAndroid Build Coastguard Worker 		errno = pthread_join(t[i], &retval.ptr);
106*cf84ac9aSAndroid Build Coastguard Worker 		if (errno)
107*cf84ac9aSAndroid Build Coastguard Worker 			perror_msg_and_fail("pthread_join");
108*cf84ac9aSAndroid Build Coastguard Worker 		errno = ENOENT;
109*cf84ac9aSAndroid Build Coastguard Worker 		printf("%-5d chdir(\"%s\") = %s\n"
110*cf84ac9aSAndroid Build Coastguard Worker 		       "%-5d +++ exited with 0 +++\n",
111*cf84ac9aSAndroid Build Coastguard Worker 		       retval.pid, child[i], sprintrc(-1), retval.pid);
112*cf84ac9aSAndroid Build Coastguard Worker 	}
113*cf84ac9aSAndroid Build Coastguard Worker 
114*cf84ac9aSAndroid Build Coastguard Worker 	/* sleep a bit more to let the tracer catch up */
115*cf84ac9aSAndroid Build Coastguard Worker 	sleep(1);
116*cf84ac9aSAndroid Build Coastguard Worker 
117*cf84ac9aSAndroid Build Coastguard Worker 	pid_t pid = getpid();
118*cf84ac9aSAndroid Build Coastguard Worker 	assert(chdir(text_parent) == -1);
119*cf84ac9aSAndroid Build Coastguard Worker 
120*cf84ac9aSAndroid Build Coastguard Worker 	printf("%-5d chdir(\"%s\") = -1 ENOENT (%m)\n"
121*cf84ac9aSAndroid Build Coastguard Worker 	       "%-5d +++ exited with 0 +++\n", pid, text_parent, pid);
122*cf84ac9aSAndroid Build Coastguard Worker 
123*cf84ac9aSAndroid Build Coastguard Worker 	return 0;
124*cf84ac9aSAndroid Build Coastguard Worker }
125