.
[glibc/history.git] / nptl / tst-flock2.c
blobfdbffbb272cee80fb4733525b1b02597d86dc6df
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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. */
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>
30 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
31 static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
32 static int fd;
35 static void *
36 tf (void *arg)
38 struct flock fl =
40 .l_type = F_WRLCK,
41 .l_start = 0,
42 .l_whence = SEEK_SET,
43 .l_len = 10
45 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
47 puts ("fourth fcntl failed");
48 exit (1);
51 pthread_mutex_unlock (&lock);
53 pthread_mutex_lock (&lock2);
55 return NULL;
59 static int
60 do_test (void)
62 #if ! _POSIX_THREAD_PROCESS_SHARED
64 puts ("_POSIX_THREAD_PROCESS_SHARED not supported, test skipped");
65 return 0;
67 #else
69 char tmp[] = "/tmp/tst-flock2-XXXXXX";
71 fd = mkstemp (tmp);
72 if (fd == -1)
74 puts ("mkstemp failed");
75 return 1;
78 unlink (tmp);
80 int i;
81 for (i = 0; i < 20; ++i)
82 write (fd, "foobar xyzzy", 12);
84 pthread_barrier_t *b;
85 b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
86 MAP_SHARED, fd, 0);
87 if (b == MAP_FAILED)
89 puts ("mmap failed");
90 return 1;
93 pthread_barrierattr_t ba;
94 if (pthread_barrierattr_init (&ba) != 0)
96 puts ("barrierattr_init failed");
97 return 1;
100 if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
102 puts ("barrierattr_setpshared failed");
103 return 1;
106 if (pthread_barrier_init (b, &ba, 2) != 0)
108 puts ("barrier_init failed");
109 return 1;
112 if (pthread_barrierattr_destroy (&ba) != 0)
114 puts ("barrierattr_destroy failed");
115 return 1;
118 struct flock fl =
120 .l_type = F_WRLCK,
121 .l_start = 0,
122 .l_whence = SEEK_SET,
123 .l_len = 10
125 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
127 puts ("first fcntl failed");
128 return 1;
131 pid_t pid = fork ();
132 if (pid == -1)
134 puts ("fork failed");
135 return 1;
138 if (pid == 0)
140 /* Make sure the child does not stay around indefinitely. */
141 alarm (10);
143 /* Try to get the lock. */
144 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
146 puts ("child: second flock succeeded");
147 return 1;
151 pthread_barrier_wait (b);
153 if (pid != 0)
155 fl.l_type = F_UNLCK;
156 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
158 puts ("third fcntl failed");
159 return 1;
163 pthread_barrier_wait (b);
165 pthread_t th;
166 if (pid == 0)
168 if (pthread_mutex_lock (&lock) != 0)
170 puts ("1st locking of lock failed");
171 return 1;
174 if (pthread_mutex_lock (&lock2) != 0)
176 puts ("1st locking of lock2 failed");
177 return 1;
180 if (pthread_create (&th, NULL, tf, NULL) != 0)
182 puts ("pthread_create failed");
183 return 1;
186 if (pthread_mutex_lock (&lock) != 0)
188 puts ("2nd locking of lock failed");
189 return 1;
192 puts ("child locked file");
195 pthread_barrier_wait (b);
197 if (pid != 0)
199 fl.l_type = F_WRLCK;
200 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
202 puts ("fifth fcntl succeeded");
203 return 1;
206 puts ("file locked by child");
209 pthread_barrier_wait (b);
211 if (pid == 0)
213 if (pthread_mutex_unlock (&lock2) != 0)
215 puts ("unlock of lock2 failed");
216 return 1;
219 if (pthread_join (th, NULL) != 0)
221 puts ("join failed");
222 return 1;
225 puts ("child's thread terminated");
228 pthread_barrier_wait (b);
230 if (pid != 0)
232 fl.l_type = F_WRLCK;
233 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
235 puts ("fifth fcntl succeeded");
236 return 1;
239 puts ("file still locked");
242 pthread_barrier_wait (b);
244 if (pid == 0)
246 _exit (0);
249 int status;
250 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
252 puts ("waitpid failed");
253 return 1;
255 puts ("child terminated");
257 if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
259 puts ("sixth fcntl failed");
260 return 1;
263 return status;
264 #endif
267 #define TEST_FUNCTION do_test ()
268 #include "../test-skeleton.c"