popen.c 4.1 KB

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