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