OSDN Git Service

Merge remote branch 'origin/master' into nptl_merge
[uclinux-h8/uclibc-ng.git] / test / nptl / tst-flock2.c
1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/file.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
28
29
30 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
31 static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
32 static int fd;
33
34
35 static void *
36 tf (void *arg)
37 {
38   struct flock fl =
39     {
40       .l_type = F_WRLCK,
41       .l_start = 0,
42       .l_whence = SEEK_SET,
43       .l_len = 10
44     };
45   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
46     {
47       puts ("fourth fcntl failed");
48       exit (1);
49     }
50
51   pthread_mutex_unlock (&lock);
52
53   pthread_mutex_lock (&lock2);
54
55   return NULL;
56 }
57
58
59 static int
60 do_test (void)
61 {
62   char tmp[] = "/tmp/tst-flock2-XXXXXX";
63
64   fd = mkstemp (tmp);
65   if (fd == -1)
66     {
67       puts ("mkstemp failed");
68       return 1;
69     }
70
71   unlink (tmp);
72
73   int i;
74   for (i = 0; i < 20; ++i)
75     write (fd, "foobar xyzzy", 12);
76
77   pthread_barrier_t *b;
78   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
79             MAP_SHARED, fd, 0);
80   if (b == MAP_FAILED)
81     {
82       puts ("mmap failed");
83       return 1;
84     }
85
86   pthread_barrierattr_t ba;
87   if (pthread_barrierattr_init (&ba) != 0)
88     {
89       puts ("barrierattr_init failed");
90       return 1;
91     }
92
93   if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
94     {
95       puts ("barrierattr_setpshared failed");
96       return 1;
97     }
98
99   if (pthread_barrier_init (b, &ba, 2) != 0)
100     {
101       puts ("barrier_init failed");
102       return 1;
103     }
104
105   if (pthread_barrierattr_destroy (&ba) != 0)
106     {
107       puts ("barrierattr_destroy failed");
108       return 1;
109     }
110
111   struct flock fl =
112     {
113       .l_type = F_WRLCK,
114       .l_start = 0,
115       .l_whence = SEEK_SET,
116       .l_len = 10
117     };
118   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
119     {
120       puts ("first fcntl failed");
121       return 1;
122     }
123
124   pid_t pid = fork ();
125   if (pid == -1)
126     {
127       puts ("fork failed");
128       return 1;
129     }
130
131   if (pid == 0)
132     {
133       /* Make sure the child does not stay around indefinitely.  */
134       alarm (10);
135
136       /* Try to get the lock.  */
137       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
138         {
139           puts ("child:  second flock succeeded");
140           return 1;
141         }
142     }
143
144   pthread_barrier_wait (b);
145
146   if (pid != 0)
147     {
148       fl.l_type = F_UNLCK;
149       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
150         {
151           puts ("third fcntl failed");
152           return 1;
153         }
154     }
155
156   pthread_barrier_wait (b);
157
158   pthread_t th;
159   if (pid == 0)
160     {
161       if (pthread_mutex_lock (&lock) != 0)
162         {
163           puts ("1st locking of lock failed");
164           return 1;
165         }
166
167       if (pthread_mutex_lock (&lock2) != 0)
168         {
169           puts ("1st locking of lock2 failed");
170           return 1;
171         }
172
173       if (pthread_create (&th, NULL, tf, NULL) != 0)
174         {
175           puts ("pthread_create failed");
176           return 1;
177         }
178
179       if (pthread_mutex_lock (&lock) != 0)
180         {
181           puts ("2nd locking of lock failed");
182           return 1;
183         }
184
185       puts ("child locked file");
186     }
187
188   pthread_barrier_wait (b);
189
190   if (pid != 0)
191     {
192       fl.l_type = F_WRLCK;
193       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
194         {
195           puts ("fifth fcntl succeeded");
196           return 1;
197         }
198
199       puts ("file locked by child");
200     }
201
202   pthread_barrier_wait (b);
203
204   if (pid == 0)
205     {
206       if (pthread_mutex_unlock (&lock2) != 0)
207         {
208           puts ("unlock of lock2 failed");
209           return 1;
210         }
211
212       if (pthread_join (th, NULL) != 0)
213         {
214           puts ("join failed");
215           return 1;
216         }
217
218       puts ("child's thread terminated");
219     }
220
221   pthread_barrier_wait (b);
222
223   if (pid != 0)
224     {
225       fl.l_type = F_WRLCK;
226       if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
227         {
228           puts ("fifth fcntl succeeded");
229           return 1;
230         }
231
232       puts ("file still locked");
233     }
234
235   pthread_barrier_wait (b);
236
237   if (pid == 0)
238     {
239       _exit (0);
240     }
241
242   int status;
243   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
244     {
245       puts ("waitpid failed");
246       return 1;
247     }
248   puts ("child terminated");
249
250   if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
251     {
252       puts ("sixth fcntl failed");
253       return 1;
254     }
255
256   return status;
257 }
258
259 #define TEST_FUNCTION do_test ()
260 #include "../test-skeleton.c"