popen.c 4.1 KB

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