1 | /* Copyright (C) 2003-2024 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <pthread.h> |
19 | #include <stdio.h> |
20 | #include <stdlib.h> |
21 | #include <string.h> |
22 | #include <unistd.h> |
23 | |
24 | |
25 | static char *p; |
26 | |
27 | static pthread_barrier_t b; |
28 | #define BT \ |
29 | e = pthread_barrier_wait (&b); \ |
30 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) \ |
31 | { \ |
32 | puts ("barrier_wait failed"); \ |
33 | exit (1); \ |
34 | } |
35 | |
36 | |
37 | static void * |
38 | tf (void *a) |
39 | { |
40 | int e; |
41 | |
42 | BT; |
43 | |
44 | char *p2 = getcwd (NULL, size: 0); |
45 | if (p2 == NULL) |
46 | { |
47 | puts (s: "2nd getcwd failed" ); |
48 | exit (1); |
49 | } |
50 | |
51 | if (strcmp (p, p2) != 0) |
52 | { |
53 | printf (format: "initial cwd mismatch: \"%s\" vs \"%s\"\n" , p, p2); |
54 | exit (1); |
55 | } |
56 | |
57 | free (ptr: p); |
58 | free (ptr: p2); |
59 | |
60 | if (chdir (path: ".." ) != 0) |
61 | { |
62 | puts (s: "chdir failed" ); |
63 | exit (1); |
64 | } |
65 | |
66 | p = getcwd (NULL, size: 0); |
67 | if (p == NULL) |
68 | { |
69 | puts (s: "getcwd failed" ); |
70 | exit (1); |
71 | } |
72 | |
73 | return a; |
74 | } |
75 | |
76 | |
77 | int |
78 | do_test (void) |
79 | { |
80 | if (pthread_barrier_init (barrier: &b, NULL, count: 2) != 0) |
81 | { |
82 | puts (s: "barrier_init failed" ); |
83 | exit (1); |
84 | } |
85 | |
86 | pthread_t th; |
87 | if (pthread_create (newthread: &th, NULL, start_routine: tf, NULL) != 0) |
88 | { |
89 | puts (s: "create failed" ); |
90 | exit (1); |
91 | } |
92 | |
93 | p = getcwd (NULL, size: 0); |
94 | if (p == NULL) |
95 | { |
96 | puts (s: "getcwd failed" ); |
97 | exit (1); |
98 | } |
99 | |
100 | int e; |
101 | BT; |
102 | |
103 | if (pthread_join (th: th, NULL) != 0) |
104 | { |
105 | puts (s: "join failed" ); |
106 | exit (1); |
107 | } |
108 | |
109 | char *p2 = getcwd (NULL, size: 0); |
110 | if (p2 == NULL) |
111 | { |
112 | puts (s: "2nd getcwd failed" ); |
113 | exit (1); |
114 | } |
115 | |
116 | if (strcmp (p, p2) != 0) |
117 | { |
118 | printf (format: "cwd after chdir mismatch: \"%s\" vs \"%s\"\n" , p, p2); |
119 | exit (1); |
120 | } |
121 | |
122 | free (ptr: p); |
123 | free (ptr: p2); |
124 | |
125 | return 0; |
126 | } |
127 | |
128 | |
129 | #define TEST_FUNCTION do_test () |
130 | #include "../test-skeleton.c" |
131 | |