blob: 73d3c7437ac711694d5df368aac2bd88850ecd10 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* $OpenBSD: popen.c,v 1.17 2005/08/08 08:05:34 espie Exp $ */
2/*
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software written by Ken Arnold and
7 * published in UNIX Review, Vol. 6, No. 8.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/param.h>
35#include <sys/wait.h>
36
37#include <signal.h>
38#include <errno.h>
39#include <unistd.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <paths.h>
44
45#include "defines.h"
46
47static struct pid {
48 struct pid *next;
49 FILE *fp;
50 pid_t pid;
51} *pidlist;
52
53FILE *
54__popen(const char *program, const char *type)
55{
56 struct pid * volatile cur;
57 FILE *iop;
58 int pdes[2];
59 pid_t pid;
60
61 if ((*type != 'r' && *type != 'w') || type[1] != '\0') {
62 errno = EINVAL;
63 return (NULL);
64 }
65
66 if ((cur = malloc(sizeof(struct pid))) == NULL)
67 return (NULL);
68
69 if (pipe(pdes) < 0) {
70 free(cur);
71 return (NULL);
72 }
73
74 switch (pid = vfork()) {
75 case -1: /* Error. */
76 (void)close(pdes[0]);
77 (void)close(pdes[1]);
78 free(cur);
79 return (NULL);
80 /* NOTREACHED */
81 case 0: /* Child. */
82 {
83 struct pid *pcur;
84 /*
85 * because vfork() instead of fork(), must leak FILE *,
86 * but luckily we are terminally headed for an execl()
87 */
88 for (pcur = pidlist; pcur; pcur = pcur->next)
89 close(fileno(pcur->fp));
90
91 if (*type == 'r') {
92 int tpdes1 = pdes[1];
93
94 (void) close(pdes[0]);
95 /*
96 * We must NOT modify pdes, due to the
97 * semantics of vfork.
98 */
99 if (tpdes1 != STDOUT_FILENO) {
100 (void)dup2(tpdes1, STDOUT_FILENO);
101 (void)close(tpdes1);
102 tpdes1 = STDOUT_FILENO;
103 }
104 } else {
105 (void)close(pdes[1]);
106 if (pdes[0] != STDIN_FILENO) {
107 (void)dup2(pdes[0], STDIN_FILENO);
108 (void)close(pdes[0]);
109 }
110 }
111 execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL);
112 _exit(127);
113 /* NOTREACHED */
114 }
115 }
116
117 /* Parent; assume fdopen can't fail. */
118 if (*type == 'r') {
119 iop = fdopen(pdes[0], type);
120 (void)close(pdes[1]);
121 } else {
122 iop = fdopen(pdes[1], type);
123 (void)close(pdes[0]);
124 }
125
126 /* Link into list of file descriptors. */
127 cur->fp = iop;
128 cur->pid = pid;
129 cur->next = pidlist;
130 pidlist = cur;
131
132 return (iop);
133}
134
135/*
136 * pclose --
137 * Pclose returns -1 if stream is not associated with a `popened' command,
138 * if already `pclosed', or waitpid returns an error.
139 */
140int
141__pclose(FILE *iop)
142{
143 struct pid *cur, *last;
144 int pstat;
145 pid_t pid;
146
147 /* Find the appropriate file pointer. */
148 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
149 if (cur->fp == iop)
150 break;
151
152 if (cur == NULL)
153 return (-1);
154
155 (void)fclose(iop);
156
157 do {
158 pid = waitpid(cur->pid, &pstat, 0);
159 } while (pid == -1 && errno == EINTR);
160
161 /* Remove the entry from the linked list. */
162 if (last == NULL)
163 pidlist = cur->next;
164 else
165 last->next = cur->next;
166 free(cur);
167
168 return (pid == -1 ? -1 : pstat);
169}