popen.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. *
  6. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  7. */
  8. /* Jan 1, 2004
  9. *
  10. * Rewrite popen for SUSv3 compliance.
  11. * Added a list of popen()'d to store pids and use waitpid() in pclose().
  12. * Loop on waitpid() failure due to EINTR as required.
  13. * Close parent's popen()'d FILEs in the {v}fork()'d child.
  14. * Fix failure exit code for failed execve().
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <sys/wait.h>
  21. libc_hidden_proto(close)
  22. libc_hidden_proto(_exit)
  23. libc_hidden_proto(waitpid)
  24. libc_hidden_proto(execl)
  25. libc_hidden_proto(dup2)
  26. libc_hidden_proto(fdopen)
  27. libc_hidden_proto(pipe)
  28. libc_hidden_proto(vfork)
  29. libc_hidden_proto(fclose)
  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. libc_hidden_proto(fork)
  37. #endif
  38. #ifdef __UCLIBC_HAS_THREADS__
  39. # include <pthread.h>
  40. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  41. #endif
  42. #define LOCK __pthread_mutex_lock(&mylock)
  43. #define UNLOCK __pthread_mutex_unlock(&mylock)
  44. #ifndef VFORK_LOCK
  45. # define VFORK_LOCK LOCK
  46. # define VFORK_UNLOCK UNLOCK
  47. #endif
  48. struct popen_list_item {
  49. struct popen_list_item *next;
  50. FILE *f;
  51. pid_t pid;
  52. };
  53. static struct popen_list_item *popen_list /* = NULL (bss initialized) */;
  54. FILE *popen(const char *command, const char *modes)
  55. {
  56. FILE *fp;
  57. struct popen_list_item *pi;
  58. struct popen_list_item *po;
  59. int pipe_fd[2];
  60. int parent_fd;
  61. int child_fd;
  62. int child_writing; /* Doubles as the desired child fildes. */
  63. pid_t pid;
  64. child_writing = 0; /* Assume child is writing. */
  65. if (modes[0] != 'w') { /* Parent not writing... */
  66. ++child_writing; /* so child must be writing. */
  67. if (modes[0] != 'r') { /* Oops! Parent not reading either! */
  68. __set_errno(EINVAL);
  69. goto RET_NULL;
  70. }
  71. }
  72. if (!(pi = malloc(sizeof(struct popen_list_item)))) {
  73. goto RET_NULL;
  74. }
  75. if (pipe(pipe_fd)) {
  76. goto FREE_PI;
  77. }
  78. child_fd = pipe_fd[child_writing];
  79. parent_fd = pipe_fd[1-child_writing];
  80. if (!(fp = fdopen(parent_fd, modes))) {
  81. close(parent_fd);
  82. close(child_fd);
  83. goto FREE_PI;
  84. }
  85. VFORK_LOCK;
  86. if ((pid = vfork()) == 0) { /* Child of vfork... */
  87. close(parent_fd);
  88. if (child_fd != child_writing) {
  89. dup2(child_fd, child_writing);
  90. close(child_fd);
  91. }
  92. /* SUSv3 requires that any previously popen()'d streams in the
  93. * parent shall be closed in the child. */
  94. for (po = popen_list ; po ; po = po->next) {
  95. close(po->f->__filedes);
  96. }
  97. execl("/bin/sh", "sh", "-c", command, (char *)0);
  98. /* SUSv3 mandates an exit code of 127 for the child if the
  99. * command interpreter can not be invoked. */
  100. _exit(127);
  101. }
  102. VFORK_UNLOCK;
  103. /* We need to close the child filedes whether vfork failed or
  104. * it succeeded and we're in the parent. */
  105. close(child_fd);
  106. if (pid > 0) { /* Parent of vfork... */
  107. pi->pid = pid;
  108. pi->f = fp;
  109. LOCK;
  110. pi->next = popen_list;
  111. popen_list = pi;
  112. UNLOCK;
  113. return fp;
  114. }
  115. /* If we get here, vfork failed. */
  116. fclose(fp); /* Will close parent_fd. */
  117. FREE_PI:
  118. free(pi);
  119. RET_NULL:
  120. return NULL;
  121. }
  122. int pclose(FILE *stream)
  123. {
  124. struct popen_list_item *p;
  125. int stat;
  126. pid_t pid;
  127. /* First, find the list entry corresponding to stream and remove it
  128. * from the list. Set p to the list item (NULL if not found). */
  129. LOCK;
  130. if ((p = popen_list) != NULL) {
  131. if (p->f == stream) {
  132. popen_list = p->next;
  133. } else {
  134. struct popen_list_item *t;
  135. do {
  136. t = p;
  137. if (!(p = t->next)) {
  138. __set_errno(EINVAL); /* Not required by SUSv3. */
  139. break;
  140. }
  141. if (p->f == stream) {
  142. t->next = p->next;
  143. break;
  144. }
  145. } while (1);
  146. }
  147. }
  148. UNLOCK;
  149. if (p) {
  150. pid = p->pid; /* Save the pid we need */
  151. free(p); /* and free the list item. */
  152. fclose(stream); /* The SUSv3 example code ignores the return. */
  153. /* SUSv3 specificly requires that pclose not return before the child
  154. * terminates, in order to disallow pclose from returning on EINTR. */
  155. do {
  156. if (waitpid(pid, &stat, 0) >= 0) {
  157. return stat;
  158. }
  159. if (errno != EINTR) {
  160. break;
  161. }
  162. } while (1);
  163. }
  164. return -1;
  165. }