tst-exec2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Thread with running thread calls exec.
  2. Copyright (C) 2002 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <paths.h>
  18. #include <pthread.h>
  19. #include <signal.h>
  20. #include <stdbool.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. #include "../test-skeleton.h"
  26. #ifdef __ARCH_USE_MMU__
  27. static void *
  28. tf (void *arg)
  29. {
  30. pthread_t th = (pthread_t) arg;
  31. if (pthread_join (th, NULL) == 0)
  32. {
  33. puts ("thread in parent joined!?");
  34. exit (1);
  35. }
  36. puts ("join in thread in parent returned!?");
  37. exit (1);
  38. }
  39. static int
  40. do_test (void)
  41. {
  42. int fd[2];
  43. if (pipe (fd) != 0)
  44. {
  45. puts ("pipe failed");
  46. exit (1);
  47. }
  48. /* Not interested in knowing when the pipe is closed. */
  49. if (sigignore (SIGPIPE) != 0)
  50. {
  51. puts ("sigignore failed");
  52. exit (1);
  53. }
  54. pid_t pid = fork ();
  55. if (pid == -1)
  56. {
  57. puts ("fork failed");
  58. exit (1);
  59. }
  60. if (pid == 0)
  61. {
  62. /* Use the fd for stdout. This is kind of ugly because it
  63. substitutes the fd of stdout but we know what we are doing
  64. here... */
  65. if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO)
  66. {
  67. puts ("dup2 failed");
  68. exit (1);
  69. }
  70. close (fd[0]);
  71. pthread_t th;
  72. if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
  73. {
  74. puts ("create failed");
  75. exit (1);
  76. }
  77. execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL);
  78. puts ("execl failed");
  79. exit (1);
  80. }
  81. close (fd[1]);
  82. char buf[200];
  83. ssize_t n;
  84. bool seen_pid = false;
  85. while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
  86. {
  87. /* We only expect to read the PID. */
  88. char *endp;
  89. long int rpid = strtol (buf, &endp, 10);
  90. if (*endp != '\n')
  91. {
  92. printf ("didn't parse whole line: \"%s\"\n", buf);
  93. exit (1);
  94. }
  95. if (endp == buf)
  96. {
  97. puts ("read empty line");
  98. exit (1);
  99. }
  100. if (rpid != pid)
  101. {
  102. printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
  103. exit (1);
  104. }
  105. if (seen_pid)
  106. {
  107. puts ("found more than one PID line");
  108. exit (1);
  109. }
  110. seen_pid = true;
  111. }
  112. close (fd[0]);
  113. int status;
  114. int err = waitpid (pid, &status, 0);
  115. if (err != pid)
  116. {
  117. puts ("waitpid failed");
  118. exit (1);
  119. }
  120. if (!seen_pid)
  121. {
  122. puts ("didn't get PID");
  123. exit (1);
  124. }
  125. return 0;
  126. }
  127. #define TEST_FUNCTION do_test ()
  128. #include "../test-skeleton.c"
  129. #else
  130. int main(void)
  131. {
  132. printf("Skipping test on non-mmu host!\n");
  133. return 23;
  134. }
  135. #endif