popen.c 3.9 KB

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