popen.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. struct popen_list_item {
  51. struct popen_list_item *next;
  52. FILE *f;
  53. pid_t pid;
  54. };
  55. static struct popen_list_item *popen_list /* = NULL (bss initialized) */;
  56. FILE *popen(const char *command, const char *modes)
  57. {
  58. FILE *fp;
  59. struct popen_list_item *pi;
  60. struct popen_list_item *po;
  61. int pipe_fd[2];
  62. int parent_fd;
  63. int child_fd;
  64. int child_writing; /* Doubles as the desired child fildes. */
  65. pid_t pid;
  66. child_writing = 0; /* Assume child is writing. */
  67. if (modes[0] != 'w') { /* Parent not writing... */
  68. ++child_writing; /* so child must be writing. */
  69. if (modes[0] != 'r') { /* Oops! Parent not reading either! */
  70. __set_errno(EINVAL);
  71. goto RET_NULL;
  72. }
  73. }
  74. if (!(pi = malloc(sizeof(struct popen_list_item)))) {
  75. goto RET_NULL;
  76. }
  77. if (pipe(pipe_fd)) {
  78. goto FREE_PI;
  79. }
  80. child_fd = pipe_fd[child_writing];
  81. parent_fd = pipe_fd[1-child_writing];
  82. if (!(fp = fdopen(parent_fd, modes))) {
  83. close(parent_fd);
  84. close(child_fd);
  85. goto FREE_PI;
  86. }
  87. VFORK_LOCK;
  88. if ((pid = vfork()) == 0) { /* Child of vfork... */
  89. close(parent_fd);
  90. if (child_fd != child_writing) {
  91. dup2(child_fd, child_writing);
  92. close(child_fd);
  93. }
  94. /* SUSv3 requires that any previously popen()'d streams in the
  95. * parent shall be closed in the child. */
  96. for (po = popen_list ; po ; po = po->next) {
  97. close(po->f->__filedes);
  98. }
  99. execl("/bin/sh", "sh", "-c", command, (char *)0);
  100. /* SUSv3 mandates an exit code of 127 for the child if the
  101. * command interpreter can not be invoked. */
  102. _exit(127);
  103. }
  104. VFORK_UNLOCK;
  105. /* We need to close the child filedes whether vfork failed or
  106. * it succeeded and we're in the parent. */
  107. close(child_fd);
  108. if (pid > 0) { /* Parent of vfork... */
  109. pi->pid = pid;
  110. pi->f = fp;
  111. LOCK;
  112. pi->next = popen_list;
  113. popen_list = pi;
  114. UNLOCK;
  115. return fp;
  116. }
  117. /* If we get here, vfork failed. */
  118. fclose(fp); /* Will close parent_fd. */
  119. FREE_PI:
  120. free(pi);
  121. RET_NULL:
  122. return NULL;
  123. }
  124. int pclose(FILE *stream)
  125. {
  126. struct popen_list_item *p;
  127. int stat;
  128. pid_t pid;
  129. /* First, find the list entry corresponding to stream and remove it
  130. * from the list. Set p to the list item (NULL if not found). */
  131. LOCK;
  132. if ((p = popen_list) != NULL) {
  133. if (p->f == stream) {
  134. popen_list = p->next;
  135. } else {
  136. struct popen_list_item *t;
  137. do {
  138. t = p;
  139. if (!(p = t->next)) {
  140. __set_errno(EINVAL); /* Not required by SUSv3. */
  141. break;
  142. }
  143. if (p->f == stream) {
  144. t->next = p->next;
  145. break;
  146. }
  147. } while (1);
  148. }
  149. }
  150. UNLOCK;
  151. if (p) {
  152. pid = p->pid; /* Save the pid we need */
  153. free(p); /* and free the list item. */
  154. fclose(stream); /* The SUSv3 example code ignores the return. */
  155. /* SUSv3 specificly requires that pclose not return before the child
  156. * terminates, in order to disallow pclose from returning on EINTR. */
  157. do {
  158. if (waitpid(pid, &stat, 0) >= 0) {
  159. return stat;
  160. }
  161. if (errno != EINTR) {
  162. break;
  163. }
  164. } while (1);
  165. }
  166. return -1;
  167. }