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