popen.c 3.8 KB

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