tst-cancel7.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@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 <fcntl.h>
  18. #include <pthread.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. const char *command;
  25. const char *pidfile;
  26. char pidfilename[] = "/tmp/tst-cancel7-XXXXXX";
  27. static void *
  28. tf (void *arg)
  29. {
  30. const char *args = " --direct --pidfile ";
  31. char *cmd = alloca (strlen (command) + strlen (args)
  32. + strlen (pidfilename) + 1);
  33. strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename);
  34. system (cmd);
  35. /* This call should never return. */
  36. return NULL;
  37. }
  38. static void
  39. sl (void)
  40. {
  41. FILE *f = fopen (pidfile, "w");
  42. if (f == NULL)
  43. exit (1);
  44. fprintf (f, "%lld\n", (long long) getpid ());
  45. fflush (f);
  46. struct flock fl =
  47. {
  48. .l_type = F_WRLCK,
  49. .l_start = 0,
  50. .l_whence = SEEK_SET,
  51. .l_len = 1
  52. };
  53. if (fcntl (fileno (f), F_SETLK, &fl) != 0)
  54. exit (1);
  55. sigset_t ss;
  56. sigfillset (&ss);
  57. sigsuspend (&ss);
  58. exit (0);
  59. }
  60. static void
  61. do_prepare (int argc, char *argv[])
  62. {
  63. if (command == NULL)
  64. command = argv[0];
  65. if (pidfile)
  66. sl ();
  67. int fd = mkstemp (pidfilename);
  68. if (fd == -1)
  69. {
  70. puts ("mkstemp failed");
  71. exit (1);
  72. }
  73. write (fd, " ", 1);
  74. close (fd);
  75. }
  76. static int
  77. do_test (void)
  78. {
  79. pthread_t th;
  80. if (pthread_create (&th, NULL, tf, NULL) != 0)
  81. {
  82. puts ("pthread_create failed");
  83. return 1;
  84. }
  85. do
  86. sleep (1);
  87. while (access (pidfilename, R_OK) != 0);
  88. if (pthread_cancel (th) != 0)
  89. {
  90. puts ("pthread_cancel failed");
  91. return 1;
  92. }
  93. void *r;
  94. if (pthread_join (th, &r) != 0)
  95. {
  96. puts ("pthread_join failed");
  97. return 1;
  98. }
  99. sleep (1);
  100. FILE *f = fopen (pidfilename, "r+");
  101. if (f == NULL)
  102. {
  103. puts ("no pidfile");
  104. return 1;
  105. }
  106. long long ll;
  107. if (fscanf (f, "%lld\n", &ll) != 1)
  108. {
  109. puts ("could not read pid");
  110. unlink (pidfilename);
  111. return 1;
  112. }
  113. struct flock fl =
  114. {
  115. .l_type = F_WRLCK,
  116. .l_start = 0,
  117. .l_whence = SEEK_SET,
  118. .l_len = 1
  119. };
  120. if (fcntl (fileno (f), F_GETLK, &fl) != 0)
  121. {
  122. puts ("F_GETLK failed");
  123. unlink (pidfilename);
  124. return 1;
  125. }
  126. if (fl.l_type != F_UNLCK)
  127. {
  128. printf ("child %lld still running\n", (long long) fl.l_pid);
  129. if (fl.l_pid == ll)
  130. kill (fl.l_pid, SIGKILL);
  131. unlink (pidfilename);
  132. return 1;
  133. }
  134. fclose (f);
  135. unlink (pidfilename);
  136. return r != PTHREAD_CANCELED;
  137. }
  138. #if 0 /* unused */
  139. static void
  140. do_cleanup (void)
  141. {
  142. FILE *f = fopen (pidfilename, "r+");
  143. long long ll;
  144. if (f != NULL && fscanf (f, "%lld\n", &ll) == 1)
  145. {
  146. struct flock fl =
  147. {
  148. .l_type = F_WRLCK,
  149. .l_start = 0,
  150. .l_whence = SEEK_SET,
  151. .l_len = 1
  152. };
  153. if (fcntl (fileno (f), F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK
  154. && fl.l_pid == ll)
  155. kill (fl.l_pid, SIGKILL);
  156. fclose (f);
  157. }
  158. unlink (pidfilename);
  159. }
  160. #endif
  161. #define OPT_COMMAND 10000
  162. #define OPT_PIDFILE 10001
  163. #define CMDLINE_OPTIONS \
  164. { "command", required_argument, NULL, OPT_COMMAND }, \
  165. { "pidfile", required_argument, NULL, OPT_PIDFILE },
  166. #define CMDLINE_PROCESS \
  167. case OPT_COMMAND: \
  168. command = optarg; \
  169. break; \
  170. case OPT_PIDFILE: \
  171. pidfile = optarg; \
  172. break;
  173. // #define CLEANUP_HANDLER do_cleanup ()
  174. #define PREPARE(argc, argv) do_prepare (argc, argv)
  175. #define TEST_FUNCTION do_test ()
  176. #define TIMEOUT 5
  177. #include "../test-skeleton.c"