tst-flock2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. 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. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/file.h>
  22. #include <sys/mman.h>
  23. #include <sys/wait.h>
  24. #include "../test-skeleton.h"
  25. static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  26. static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
  27. static int fd;
  28. static void *
  29. tf (void *arg)
  30. {
  31. struct flock fl =
  32. {
  33. .l_type = F_WRLCK,
  34. .l_start = 0,
  35. .l_whence = SEEK_SET,
  36. .l_len = 10
  37. };
  38. if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
  39. {
  40. puts ("fourth fcntl failed");
  41. exit (1);
  42. }
  43. pthread_mutex_unlock (&lock);
  44. pthread_mutex_lock (&lock2);
  45. return NULL;
  46. }
  47. static int
  48. do_test (void)
  49. {
  50. char tmp[] = "/tmp/tst-flock2-XXXXXX";
  51. fd = mkstemp (tmp);
  52. if (fd == -1)
  53. {
  54. puts ("mkstemp failed");
  55. return 1;
  56. }
  57. unlink (tmp);
  58. int i;
  59. for (i = 0; i < 20; ++i)
  60. write (fd, "foobar xyzzy", 12);
  61. pthread_barrier_t *b;
  62. b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
  63. MAP_SHARED, fd, 0);
  64. if (b == MAP_FAILED)
  65. {
  66. puts ("mmap failed");
  67. return 1;
  68. }
  69. pthread_barrierattr_t ba;
  70. if (pthread_barrierattr_init (&ba) != 0)
  71. {
  72. puts ("barrierattr_init failed");
  73. return 1;
  74. }
  75. if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
  76. {
  77. puts ("barrierattr_setpshared failed");
  78. return 1;
  79. }
  80. if (pthread_barrier_init (b, &ba, 2) != 0)
  81. {
  82. puts ("barrier_init failed");
  83. return 1;
  84. }
  85. if (pthread_barrierattr_destroy (&ba) != 0)
  86. {
  87. puts ("barrierattr_destroy failed");
  88. return 1;
  89. }
  90. struct flock fl =
  91. {
  92. .l_type = F_WRLCK,
  93. .l_start = 0,
  94. .l_whence = SEEK_SET,
  95. .l_len = 10
  96. };
  97. if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
  98. {
  99. puts ("first fcntl failed");
  100. return 1;
  101. }
  102. pid_t pid = fork ();
  103. if (pid == -1)
  104. {
  105. puts ("fork failed");
  106. return 1;
  107. }
  108. if (pid == 0)
  109. {
  110. /* Make sure the child does not stay around indefinitely. */
  111. alarm (10);
  112. /* Try to get the lock. */
  113. if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
  114. {
  115. puts ("child: second flock succeeded");
  116. return 1;
  117. }
  118. }
  119. pthread_barrier_wait (b);
  120. if (pid != 0)
  121. {
  122. fl.l_type = F_UNLCK;
  123. if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
  124. {
  125. puts ("third fcntl failed");
  126. return 1;
  127. }
  128. }
  129. pthread_barrier_wait (b);
  130. pthread_t th;
  131. if (pid == 0)
  132. {
  133. if (pthread_mutex_lock (&lock) != 0)
  134. {
  135. puts ("1st locking of lock failed");
  136. return 1;
  137. }
  138. if (pthread_mutex_lock (&lock2) != 0)
  139. {
  140. puts ("1st locking of lock2 failed");
  141. return 1;
  142. }
  143. if (pthread_create (&th, NULL, tf, NULL) != 0)
  144. {
  145. puts ("pthread_create failed");
  146. return 1;
  147. }
  148. if (pthread_mutex_lock (&lock) != 0)
  149. {
  150. puts ("2nd locking of lock failed");
  151. return 1;
  152. }
  153. puts ("child locked file");
  154. }
  155. pthread_barrier_wait (b);
  156. if (pid != 0)
  157. {
  158. fl.l_type = F_WRLCK;
  159. if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
  160. {
  161. puts ("fifth fcntl succeeded");
  162. return 1;
  163. }
  164. puts ("file locked by child");
  165. }
  166. pthread_barrier_wait (b);
  167. if (pid == 0)
  168. {
  169. if (pthread_mutex_unlock (&lock2) != 0)
  170. {
  171. puts ("unlock of lock2 failed");
  172. return 1;
  173. }
  174. if (pthread_join (th, NULL) != 0)
  175. {
  176. puts ("join failed");
  177. return 1;
  178. }
  179. puts ("child's thread terminated");
  180. }
  181. pthread_barrier_wait (b);
  182. if (pid != 0)
  183. {
  184. fl.l_type = F_WRLCK;
  185. if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0)
  186. {
  187. puts ("fifth fcntl succeeded");
  188. return 1;
  189. }
  190. puts ("file still locked");
  191. }
  192. pthread_barrier_wait (b);
  193. if (pid == 0)
  194. {
  195. _exit (0);
  196. }
  197. int status;
  198. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  199. {
  200. puts ("waitpid failed");
  201. return 1;
  202. }
  203. puts ("child terminated");
  204. if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0)
  205. {
  206. puts ("sixth fcntl failed");
  207. return 1;
  208. }
  209. return status;
  210. }
  211. #define TEST_FUNCTION do_test ()
  212. #include "../test-skeleton.c"