tst-exec3.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* 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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <errno.h>
  18. #include <paths.h>
  19. #include <pthread.h>
  20. #include <signal.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <sys/wait.h>
  26. static void *
  27. tf (void *arg)
  28. {
  29. execl (_PATH_BSHELL, _PATH_BSHELL, "-c", "echo $$", NULL);
  30. puts ("execl failed");
  31. exit (1);
  32. }
  33. static int
  34. do_test (void)
  35. {
  36. int fd[2];
  37. if (pipe (fd) != 0)
  38. {
  39. puts ("pipe failed");
  40. exit (1);
  41. }
  42. /* Not interested in knowing when the pipe is closed. */
  43. if (sigignore (SIGPIPE) != 0)
  44. {
  45. puts ("sigignore failed");
  46. exit (1);
  47. }
  48. pid_t pid = fork ();
  49. if (pid == -1)
  50. {
  51. puts ("fork failed");
  52. exit (1);
  53. }
  54. if (pid == 0)
  55. {
  56. /* Use the fd for stdout. This is kind of ugly because it
  57. substitutes the fd of stdout but we know what we are doing
  58. here... */
  59. if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO)
  60. {
  61. puts ("dup2 failed");
  62. exit (1);
  63. }
  64. close (fd[0]);
  65. pthread_t th;
  66. if (pthread_create (&th, NULL, tf, NULL) != 0)
  67. {
  68. puts ("create failed");
  69. exit (1);
  70. }
  71. if (pthread_join (th, NULL) == 0)
  72. {
  73. puts ("join succeeded!?");
  74. exit (1);
  75. }
  76. puts ("join returned!?");
  77. exit (1);
  78. }
  79. close (fd[1]);
  80. char buf[200];
  81. ssize_t n;
  82. bool seen_pid = false;
  83. while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
  84. {
  85. /* We only expect to read the PID. */
  86. char *endp;
  87. long int rpid = strtol (buf, &endp, 10);
  88. if (*endp != '\n')
  89. {
  90. printf ("didn't parse whole line: \"%s\"\n", buf);
  91. exit (1);
  92. }
  93. if (endp == buf)
  94. {
  95. puts ("read empty line");
  96. exit (1);
  97. }
  98. if (rpid != pid)
  99. {
  100. printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
  101. exit (1);
  102. }
  103. if (seen_pid)
  104. {
  105. puts ("found more than one PID line");
  106. exit (1);
  107. }
  108. seen_pid = true;
  109. }
  110. close (fd[0]);
  111. int status;
  112. int err = waitpid (pid, &status, 0);
  113. if (err != pid)
  114. {
  115. puts ("waitpid failed");
  116. exit (1);
  117. }
  118. if (!seen_pid)
  119. {
  120. puts ("didn't get PID");
  121. exit (1);
  122. }
  123. return 0;
  124. }
  125. #define TEST_FUNCTION do_test ()
  126. #include "../test-skeleton.c"