popen.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #warning is pclose correct wrt the new mutex semantics?
  105. int pclose(FILE *stream)
  106. {
  107. struct popen_list_item *p;
  108. int stat;
  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, &stat, 0) >= 0) {
  140. return stat;
  141. }
  142. if (errno != EINTR) {
  143. break;
  144. }
  145. } while (1);
  146. }
  147. return -1;
  148. }