popen.c 4.0 KB

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