tst-flock2.c 5.1 KB

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