| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 |  | 
|  | 9 | /* Jan 1, 2004 | 
|  | 10 | * | 
|  | 11 | * Rewrite popen for SUSv3 compliance. | 
|  | 12 | *   Added a list of popen()'d to store pids and use waitpid() in pclose(). | 
|  | 13 | *   Loop on waitpid() failure due to EINTR as required. | 
|  | 14 | *   Close parent's popen()'d FILEs in the {v}fork()'d child. | 
|  | 15 | *   Fix failure exit code for failed execve(). | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 | #include <stdio.h> | 
|  | 19 | #include <stdlib.h> | 
|  | 20 | #include <errno.h> | 
|  | 21 | #include <unistd.h> | 
|  | 22 | #include <sys/wait.h> | 
|  | 23 | #include <bits/uClibc_mutex.h> | 
|  | 24 |  | 
|  | 25 | #ifdef __UCLIBC_MJN3_ONLY__ | 
|  | 26 | #warning "hmm... susv3 says Pipe streams are byte-oriented." | 
|  | 27 | #endif /* __UCLIBC_MJN3_ONLY__ */ | 
|  | 28 |  | 
|  | 29 |  | 
|  | 30 | /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */ | 
|  | 31 | #include <sys/syscall.h> | 
|  | 32 | #if ! defined __NR_vfork | 
|  | 33 | # define vfork fork | 
|  | 34 | # define VFORK_LOCK		((void) 0) | 
|  | 35 | # define VFORK_UNLOCK		((void) 0) | 
|  | 36 | #endif | 
|  | 37 |  | 
|  | 38 | #ifndef VFORK_LOCK | 
|  | 39 | __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); | 
|  | 40 | # define VFORK_LOCK		__UCLIBC_MUTEX_LOCK(mylock) | 
|  | 41 | # define VFORK_UNLOCK		__UCLIBC_MUTEX_UNLOCK(mylock) | 
|  | 42 | #endif | 
|  | 43 |  | 
|  | 44 | struct popen_list_item { | 
|  | 45 | struct popen_list_item *next; | 
|  | 46 | FILE *f; | 
|  | 47 | pid_t pid; | 
|  | 48 | }; | 
|  | 49 |  | 
|  | 50 | static struct popen_list_item *popen_list /* = NULL (bss initialized) */; | 
|  | 51 |  | 
|  | 52 | FILE *popen(const char *command, const char *modes) | 
|  | 53 | { | 
|  | 54 | FILE *fp; | 
|  | 55 | struct popen_list_item *pi; | 
|  | 56 | struct popen_list_item *po; | 
|  | 57 | int pipe_fd[2]; | 
|  | 58 | int parent_fd; | 
|  | 59 | int child_fd; | 
|  | 60 | int child_writing;			/* Doubles as the desired child fildes. */ | 
|  | 61 | pid_t pid; | 
|  | 62 |  | 
|  | 63 | child_writing = 0;			/* Assume child is writing. */ | 
|  | 64 | if (modes[0] != 'w') {		/* Parent not writing... */ | 
|  | 65 | ++child_writing;		/* so child must be writing. */ | 
|  | 66 | if (modes[0] != 'r') {	/* Oops!  Parent not reading either! */ | 
|  | 67 | __set_errno(EINVAL); | 
|  | 68 | goto RET_NULL; | 
|  | 69 | } | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | if (!(pi = malloc(sizeof(struct popen_list_item)))) { | 
|  | 73 | goto RET_NULL; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | if (pipe(pipe_fd)) { | 
|  | 77 | goto FREE_PI; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | child_fd = pipe_fd[child_writing]; | 
|  | 81 | parent_fd = pipe_fd[1-child_writing]; | 
|  | 82 |  | 
|  | 83 | if (!(fp = fdopen(parent_fd, modes))) { | 
|  | 84 | close(parent_fd); | 
|  | 85 | close(child_fd); | 
|  | 86 | goto FREE_PI; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | VFORK_LOCK; | 
|  | 90 | if ((pid = vfork()) == 0) {	/* Child of vfork... */ | 
|  | 91 | close(parent_fd); | 
|  | 92 | if (child_fd != child_writing) { | 
|  | 93 | dup2(child_fd, child_writing); | 
|  | 94 | close(child_fd); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | /* SUSv3 requires that any previously popen()'d streams in the | 
|  | 98 | * parent shall be closed in the child. */ | 
|  | 99 | for (po = popen_list ; po ; po = po->next) { | 
|  | 100 | close(po->f->__filedes); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | execl("/bin/sh", "sh", "-c", command, (char *)0); | 
|  | 104 |  | 
|  | 105 | /* SUSv3 mandates an exit code of 127 for the child if the | 
|  | 106 | * command interpreter can not be invoked. */ | 
|  | 107 | _exit(127); | 
|  | 108 | } | 
|  | 109 | VFORK_UNLOCK; | 
|  | 110 |  | 
|  | 111 | /* We need to close the child filedes whether vfork failed or | 
|  | 112 | * it succeeded and we're in the parent. */ | 
|  | 113 | close(child_fd); | 
|  | 114 |  | 
|  | 115 | if (pid > 0) {				/* Parent of vfork... */ | 
|  | 116 | pi->pid = pid; | 
|  | 117 | pi->f = fp; | 
|  | 118 | VFORK_LOCK; | 
|  | 119 | pi->next = popen_list; | 
|  | 120 | popen_list = pi; | 
|  | 121 | VFORK_UNLOCK; | 
|  | 122 |  | 
|  | 123 | return fp; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | /* If we get here, vfork failed. */ | 
|  | 127 | fclose(fp);					/* Will close parent_fd. */ | 
|  | 128 |  | 
|  | 129 | FREE_PI: | 
|  | 130 | free(pi); | 
|  | 131 |  | 
|  | 132 | RET_NULL: | 
|  | 133 | return NULL; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | #warning is pclose correct wrt the new mutex semantics? | 
|  | 137 |  | 
|  | 138 | int pclose(FILE *stream) | 
|  | 139 | { | 
|  | 140 | struct popen_list_item *p; | 
|  | 141 | int stat; | 
|  | 142 | pid_t pid; | 
|  | 143 |  | 
|  | 144 | /* First, find the list entry corresponding to stream and remove it | 
|  | 145 | * from the list.  Set p to the list item (NULL if not found). */ | 
|  | 146 | VFORK_LOCK; | 
|  | 147 | if ((p = popen_list) != NULL) { | 
|  | 148 | if (p->f == stream) { | 
|  | 149 | popen_list = p->next; | 
|  | 150 | } else { | 
|  | 151 | struct popen_list_item *t; | 
|  | 152 | do { | 
|  | 153 | t = p; | 
|  | 154 | if (!(p = t->next)) { | 
|  | 155 | __set_errno(EINVAL); /* Not required by SUSv3. */ | 
|  | 156 | break; | 
|  | 157 | } | 
|  | 158 | if (p->f == stream) { | 
|  | 159 | t->next = p->next; | 
|  | 160 | break; | 
|  | 161 | } | 
|  | 162 | } while (1); | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 | VFORK_UNLOCK; | 
|  | 166 |  | 
|  | 167 | if (p) { | 
|  | 168 | pid = p->pid;			/* Save the pid we need */ | 
|  | 169 | free(p);				/* and free the list item. */ | 
|  | 170 |  | 
|  | 171 | fclose(stream);	/* The SUSv3 example code ignores the return. */ | 
|  | 172 |  | 
|  | 173 | /* SUSv3 specificly requires that pclose not return before the child | 
|  | 174 | * terminates, in order to disallow pclose from returning on EINTR. */ | 
|  | 175 | do { | 
|  | 176 | if (waitpid(pid, &stat, 0) >= 0) { | 
|  | 177 | return stat; | 
|  | 178 | } | 
|  | 179 | if (errno != EINTR) { | 
|  | 180 | break; | 
|  | 181 | } | 
|  | 182 | } while (1); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | return -1; | 
|  | 186 | } |