tst-flock2.c 4.9 KB

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