1 /* Copyright (C) 2002-2022 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 <errno.h>
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/file.h>
24 #include <sys/mman.h>
25 #include <sys/wait.h>
26 
27 
28 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
29 static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
30 static int fd;
31 
32 
33 static void *
tf(void * arg)34 tf (void *arg)
35 {
36   struct flock fl =
37     {
38       .l_type = F_WRLCK,
39       .l_start = 0,
40       .l_whence = SEEK_SET,
41       .l_len = 10
42     };
43   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
44     {
45       puts ("fourth fcntl failed");
46       exit (1);
47     }
48 
49   pthread_mutex_unlock (&lock);
50 
51   pthread_mutex_lock (&lock2);
52 
53   return NULL;
54 }
55 
56 
57 static int
do_test(void)58 do_test (void)
59 {
60   char tmp[] = "/tmp/tst-flock2-XXXXXX";
61 
62   fd = mkstemp (tmp);
63   if (fd == -1)
64     {
65       puts ("mkstemp failed");
66       return 1;
67     }
68 
69   unlink (tmp);
70 
71   int i;
72   for (i = 0; i < 20; ++i)
73     write (fd, "foobar xyzzy", 12);
74 
75   pthread_barrier_t *b;
76   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
77 	    MAP_SHARED, fd, 0);
78   if (b == MAP_FAILED)
79     {
80       puts ("mmap failed");
81       return 1;
82     }
83 
84   pthread_barrierattr_t ba;
85   if (pthread_barrierattr_init (&ba) != 0)
86     {
87       puts ("barrierattr_init failed");
88       return 1;
89     }
90 
91   if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
92     {
93       puts ("barrierattr_setpshared failed");
94       return 1;
95     }
96 
97   if (pthread_barrier_init (b, &ba, 2) != 0)
98     {
99       puts ("barrier_init failed");
100       return 1;
101     }
102 
103   if (pthread_barrierattr_destroy (&ba) != 0)
104     {
105       puts ("barrierattr_destroy failed");
106       return 1;
107     }
108 
109   struct flock fl =
110     {
111       .l_type = F_WRLCK,
112       .l_start = 0,
113       .l_whence = SEEK_SET,
114       .l_len = 10
115     };
116   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
117     {
118       puts ("first fcntl failed");
119       return 1;
120     }
121 
122   pid_t pid = fork ();
123   if (pid == -1)
124     {
125       puts ("fork failed");
126       return 1;
127     }
128 
129   if (pid == 0)
130     {
131       /* Make sure the child does not stay around indefinitely.  */
132       alarm (10);
133 
134       /* Try to get the lock.  */
135       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
136 	{
137 	  puts ("child:  second flock succeeded");
138 	  return 1;
139 	}
140     }
141 
142   pthread_barrier_wait (b);
143 
144   if (pid != 0)
145     {
146       fl.l_type = F_UNLCK;
147       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
148 	{
149 	  puts ("third fcntl failed");
150 	  return 1;
151 	}
152     }
153 
154   pthread_barrier_wait (b);
155 
156   pthread_t th;
157   if (pid == 0)
158     {
159       if (pthread_mutex_lock (&lock) != 0)
160 	{
161 	  puts ("1st locking of lock failed");
162 	  return 1;
163 	}
164 
165       if (pthread_mutex_lock (&lock2) != 0)
166 	{
167 	  puts ("1st locking of lock2 failed");
168 	  return 1;
169 	}
170 
171       if (pthread_create (&th, NULL, tf, NULL) != 0)
172 	{
173 	  puts ("pthread_create failed");
174 	  return 1;
175 	}
176 
177       if (pthread_mutex_lock (&lock) != 0)
178 	{
179 	  puts ("2nd locking of lock failed");
180 	  return 1;
181 	}
182 
183       puts ("child locked file");
184     }
185 
186   pthread_barrier_wait (b);
187 
188   if (pid != 0)
189     {
190       fl.l_type = F_WRLCK;
191       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
192 	{
193 	  puts ("fifth fcntl succeeded");
194 	  return 1;
195 	}
196 
197       puts ("file locked by child");
198     }
199 
200   pthread_barrier_wait (b);
201 
202   if (pid == 0)
203     {
204       if (pthread_mutex_unlock (&lock2) != 0)
205 	{
206 	  puts ("unlock of lock2 failed");
207 	  return 1;
208 	}
209 
210       if (pthread_join (th, NULL) != 0)
211 	{
212 	  puts ("join failed");
213 	  return 1;
214 	}
215 
216       puts ("child's thread terminated");
217     }
218 
219   pthread_barrier_wait (b);
220 
221   if (pid != 0)
222     {
223       fl.l_type = F_WRLCK;
224       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
225 	{
226 	  puts ("fifth fcntl succeeded");
227 	  return 1;
228 	}
229 
230       puts ("file still locked");
231     }
232 
233   pthread_barrier_wait (b);
234 
235   if (pid == 0)
236     {
237       _exit (0);
238     }
239 
240   int status;
241   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
242     {
243       puts ("waitpid failed");
244       return 1;
245     }
246   puts ("child terminated");
247 
248   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
249     {
250       puts ("sixth fcntl failed");
251       return 1;
252     }
253 
254   return status;
255 }
256 
257 #define TEST_FUNCTION do_test ()
258 #include "../test-skeleton.c"
259