popen.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 2004 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This 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. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* Jan 1, 2004
  18. *
  19. * Rewrite popen for SUSv3 compliance.
  20. * Added a list of popen()'d to store pids and use waitpid() in pclose().
  21. * Loop on waitpid() failure due to EINTR as required.
  22. * Close parent's popen()'d FILEs in the {v}fork()'d child.
  23. * Fix failure exit code for failed execve().
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <sys/wait.h>
  30. /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
  31. #include <sys/syscall.h>
  32. #if ! defined __NR_vfork
  33. # define vfork fork
  34. # define VFORK_LOCK ((void) 0)
  35. # define VFORK_UNLOCK ((void) 0)
  36. #endif
  37. #ifdef __UCLIBC_HAS_THREADS__
  38. #include <pthread.h>
  39. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  40. # define LOCK __pthread_mutex_lock(&mylock)
  41. # define UNLOCK __pthread_mutex_unlock(&mylock);
  42. #else
  43. # define LOCK ((void) 0)
  44. # define UNLOCK ((void) 0)
  45. #endif
  46. #ifndef VFORK_LOCK
  47. # define VFORK_LOCK LOCK
  48. # define VFORK_UNLOCK UNLOCK
  49. #endif
  50. /* Temporarily support old stdio code. */
  51. #ifndef __MASK_READING
  52. #define __filedes filedes
  53. #endif
  54. struct popen_list_item {
  55. struct popen_list_item *next;
  56. FILE *f;
  57. pid_t pid;
  58. };
  59. static struct popen_list_item *popen_list /* = NULL (bss initialized) */;
  60. FILE *popen(const char *command, const char *modes)
  61. {
  62. FILE *fp;
  63. struct popen_list_item *pi;
  64. struct popen_list_item *po;
  65. int pipe_fd[2];
  66. int parent_fd;
  67. int child_fd;
  68. int child_writing; /* Doubles as the desired child fildes. */
  69. pid_t pid;
  70. child_writing = 0; /* Assume child is writing. */
  71. if (modes[0] != 'w') { /* Parent not writing... */
  72. ++child_writing; /* so child must be writing. */
  73. if (modes[0] != 'r') { /* Oops! Parent not reading either! */
  74. __set_errno(EINVAL);
  75. goto RET_NULL;
  76. }
  77. }
  78. if (!(pi = malloc(sizeof(struct popen_list_item)))) {
  79. goto RET_NULL;
  80. }
  81. if (pipe(pipe_fd)) {
  82. goto FREE_PI;
  83. }
  84. child_fd = pipe_fd[child_writing];
  85. parent_fd = pipe_fd[1-child_writing];
  86. if (!(fp = fdopen(parent_fd, modes))) {
  87. close(parent_fd);
  88. close(child_fd);
  89. goto FREE_PI;
  90. }
  91. VFORK_LOCK;
  92. if ((pid = vfork()) == 0) { /* Child of vfork... */
  93. close(parent_fd);
  94. if (child_fd != child_writing) {
  95. dup2(child_fd, child_writing);
  96. close(child_fd);
  97. }
  98. /* SUSv3 requires that any previously popen()'d streams in the
  99. * parent shall be closed in the child. */
  100. for (po = popen_list ; po ; po = po->next) {
  101. close(po->f->__filedes);
  102. }
  103. execl("/bin/sh", "sh", "-c", command, (char *)0);
  104. /* SUSv3 mandates an exit code of 127 for the child if the
  105. * command interpreter can not be invoked. */
  106. _exit(127);
  107. }
  108. VFORK_UNLOCK;
  109. /* We need to close the child filedes whether vfork failed or
  110. * it succeeded and we're in the parent. */
  111. close(child_fd);
  112. if (pid > 0) { /* Parent of vfork... */
  113. pi->pid = pid;
  114. pi->f = fp;
  115. LOCK;
  116. pi->next = popen_list;
  117. popen_list = pi;
  118. UNLOCK;
  119. return fp;
  120. }
  121. /* If we get here, vfork failed. */
  122. fclose(fp); /* Will close parent_fd. */
  123. FREE_PI:
  124. free(pi);
  125. RET_NULL:
  126. return NULL;
  127. }
  128. int pclose(FILE *stream)
  129. {
  130. struct popen_list_item *p;
  131. int stat;
  132. pid_t pid;
  133. /* First, find the list entry corresponding to stream and remove it
  134. * from the list. Set p to the list item (NULL if not found). */
  135. LOCK;
  136. if ((p = popen_list) != NULL) {
  137. if (p->f == stream) {
  138. popen_list = p->next;
  139. } else {
  140. struct popen_list_item *t;
  141. do {
  142. t = p;
  143. if (!(p = t->next)) {
  144. __set_errno(EINVAL); /* Not required by SUSv3. */
  145. break;
  146. }
  147. if (p->f == stream) {
  148. t->next = p->next;
  149. break;
  150. }
  151. } while (1);
  152. }
  153. }
  154. UNLOCK;
  155. if (p) {
  156. pid = p->pid; /* Save the pid we need */
  157. free(p); /* and free the list item. */
  158. fclose(stream); /* The SUSv3 example code ignores the return. */
  159. /* SUSv3 specificly requires that pclose not return before the child
  160. * terminates, in order to disallow pclose from returning on EINTR. */
  161. do {
  162. if (waitpid(pid, &stat, 0) >= 0) {
  163. return stat;
  164. }
  165. if (errno != EINTR) {
  166. break;
  167. }
  168. } while (1);
  169. }
  170. return -1;
  171. }