popen.c 4.0 KB

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