popen.c 4.2 KB

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