popen.c 4.2 KB

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