popen.c 4.0 KB

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