popen.c 4.4 KB

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