tst-exec1.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Simple exec test, only a thread in the parent.
  2. Copyright (C) 2002-2019 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 <spawn.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. pthread_t th = (pthread_t) arg;
  30. if (pthread_join (th, NULL) == 0)
  31. {
  32. puts ("thread in parent joined!?");
  33. exit (1);
  34. }
  35. puts ("join in thread in parent returned!?");
  36. exit (1);
  37. }
  38. static int
  39. do_test (void)
  40. {
  41. int fd[2];
  42. if (pipe (fd) != 0)
  43. {
  44. puts ("pipe failed");
  45. exit (1);
  46. }
  47. /* Not interested in knowing when the pipe is closed. */
  48. if (sigignore (SIGPIPE) != 0)
  49. {
  50. puts ("sigignore failed");
  51. exit (1);
  52. }
  53. posix_spawn_file_actions_t a;
  54. if (posix_spawn_file_actions_init (&a) != 0)
  55. {
  56. puts ("spawn_file_actions_init failed");
  57. exit (1);
  58. }
  59. if (posix_spawn_file_actions_adddup2 (&a, fd[1], STDOUT_FILENO) != 0)
  60. {
  61. puts ("spawn_file_actions_adddup2 failed");
  62. exit (1);
  63. }
  64. if (posix_spawn_file_actions_addclose (&a, fd[0]) != 0)
  65. {
  66. puts ("spawn_file_actions_addclose");
  67. exit (1);
  68. }
  69. pthread_t th;
  70. if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
  71. {
  72. puts ("create failed");
  73. exit (1);
  74. }
  75. pid_t pid;
  76. char *argv[] = { (char *) _PATH_BSHELL, (char *) "-c", (char *) "echo $$",
  77. NULL };
  78. if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, argv, NULL) != 0)
  79. {
  80. puts ("spawn failed");
  81. exit (1);
  82. }
  83. close (fd[1]);
  84. char buf[200];
  85. ssize_t n;
  86. bool seen_pid = false;
  87. while (TEMP_FAILURE_RETRY ((n = read (fd[0], buf, sizeof (buf)))) > 0)
  88. {
  89. /* We only expect to read the PID. */
  90. char *endp;
  91. long int rpid = strtol (buf, &endp, 10);
  92. if (*endp != '\n')
  93. {
  94. printf ("didn't parse whole line: \"%s\"\n", buf);
  95. exit (1);
  96. }
  97. if (endp == buf)
  98. {
  99. puts ("read empty line");
  100. exit (1);
  101. }
  102. if (rpid != pid)
  103. {
  104. printf ("found \"%s\", expected PID %ld\n", buf, (long int) pid);
  105. exit (1);
  106. }
  107. if (seen_pid)
  108. {
  109. puts ("found more than one PID line");
  110. exit (1);
  111. }
  112. seen_pid = true;
  113. }
  114. close (fd[0]);
  115. int status;
  116. int err = waitpid (pid, &status, 0);
  117. if (err != pid)
  118. {
  119. puts ("waitpid failed");
  120. exit (1);
  121. }
  122. if (!seen_pid)
  123. {
  124. puts ("didn't get PID");
  125. exit (1);
  126. }
  127. puts ("read correct PID");
  128. return 0;
  129. }
  130. #define TEST_FUNCTION do_test ()
  131. #include "../test-skeleton.c"