]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/iopopen.c
Update.
[thirdparty/glibc.git] / libio / iopopen.c
1 /* Copyright (C) 1993, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
3 Written by Per Bothner <bothner@cygnus.com>.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or (at
8 your option) any later version.
9
10 This library is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA.
19
20 As a special exception, if you link this library with files
21 compiled with a GNU compiler to produce an executable, this does
22 not cause the resulting executable to be covered by the GNU General
23 Public License. This exception does not however invalidate any
24 other reasons why the executable file might be covered by the GNU
25 General Public License. */
26
27 #ifndef _POSIX_SOURCE
28 # define _POSIX_SOURCE
29 #endif
30 #include "libioP.h"
31 #if _IO_HAVE_SYS_WAIT
32 #include <signal.h>
33 #include <unistd.h>
34 #ifdef __STDC__
35 #include <stdlib.h>
36 #endif
37 #ifdef _LIBC
38 # include <unistd.h>
39 # include <shlib-compat.h>
40 #endif
41 #include <sys/types.h>
42 #include <sys/wait.h>
43
44 #ifndef _IO_fork
45 #ifdef _LIBC
46 #define _IO_fork __vfork
47 #else
48 #define _IO_fork vfork /* defined in libiberty, if needed */
49 #endif
50 extern _IO_pid_t _IO_fork __P ((void));
51 #endif
52
53 #endif /* _IO_HAVE_SYS_WAIT */
54
55 #ifndef _IO_pipe
56 #ifdef _LIBC
57 #define _IO_pipe __pipe
58 #else
59 #define _IO_pipe pipe
60 #endif
61 extern int _IO_pipe __P ((int des[2]));
62 #endif
63
64 #ifndef _IO_dup2
65 #ifdef _LIBC
66 #define _IO_dup2 __dup2
67 #else
68 #define _IO_dup2 dup2
69 #endif
70 extern int _IO_dup2 __P ((int fd, int fd2));
71 #endif
72
73 #ifndef _IO_waitpid
74 #ifdef _LIBC
75 #define _IO_waitpid __waitpid
76 #else
77 #define _IO_waitpid waitpid
78 #endif
79 #endif
80
81 #ifndef _IO_execl
82 #define _IO_execl execl
83 #endif
84 #ifndef _IO__exit
85 #define _IO__exit _exit
86 #endif
87
88 #ifndef _IO_close
89 #ifdef _LIBC
90 #define _IO_close __close
91 #else
92 #define _IO_close close
93 #endif
94 #endif
95
96 struct _IO_proc_file
97 {
98 struct _IO_FILE_plus file;
99 /* Following fields must match those in class procbuf (procbuf.h) */
100 _IO_pid_t pid;
101 struct _IO_proc_file *next;
102 };
103 typedef struct _IO_proc_file _IO_proc_file;
104
105 static struct _IO_jump_t _IO_wproc_jumps;
106
107 static struct _IO_proc_file *proc_file_chain;
108
109 _IO_FILE *
110 _IO_new_proc_open (fp, command, mode)
111 _IO_FILE *fp;
112 const char *command;
113 const char *mode;
114 {
115 #if _IO_HAVE_SYS_WAIT
116 volatile int read_or_write;
117 volatile int parent_end, child_end;
118 int pipe_fds[2];
119 _IO_pid_t child_pid;
120 if (_IO_file_is_open (fp))
121 return NULL;
122 if (_IO_pipe (pipe_fds) < 0)
123 return NULL;
124 if (mode[0] == 'r' && mode[1] == '\0')
125 {
126 parent_end = pipe_fds[0];
127 child_end = pipe_fds[1];
128 read_or_write = _IO_NO_WRITES;
129 }
130 else if (mode[0] == 'w' && mode[1] == '\0')
131 {
132 parent_end = pipe_fds[1];
133 child_end = pipe_fds[0];
134 read_or_write = _IO_NO_READS;
135 }
136 else
137 {
138 __set_errno (EINVAL);
139 return NULL;
140 }
141 ((_IO_proc_file *) fp)->pid = child_pid = _IO_fork ();
142 if (child_pid == 0)
143 {
144 int child_std_end = mode[0] == 'r' ? 1 : 0;
145 struct _IO_proc_file *p;
146
147 _IO_close (parent_end);
148 if (child_end != child_std_end)
149 {
150 _IO_dup2 (child_end, child_std_end);
151 _IO_close (child_end);
152 }
153 /* POSIX.2: "popen() shall ensure that any streams from previous
154 popen() calls that remain open in the parent process are closed
155 in the new child process." */
156 for (p = proc_file_chain; p; p = p->next)
157 _IO_close (_IO_fileno ((_IO_FILE *) p));
158
159 _IO_execl ("/bin/sh", "sh", "-c", command, (char *) 0);
160 _IO__exit (127);
161 }
162 _IO_close (child_end);
163 if (child_pid < 0)
164 {
165 _IO_close (parent_end);
166 return NULL;
167 }
168 _IO_fileno (fp) = parent_end;
169
170 /* Link into proc_file_chain. */
171 ((_IO_proc_file *) fp)->next = proc_file_chain;
172 proc_file_chain = (_IO_proc_file *) fp;
173
174 _IO_mask_flags (fp, read_or_write, _IO_NO_READS|_IO_NO_WRITES);
175 return fp;
176 #else /* !_IO_HAVE_SYS_WAIT */
177 return NULL;
178 #endif
179 }
180
181 _IO_FILE *
182 _IO_new_popen (command, mode)
183 const char *command;
184 const char *mode;
185 {
186 struct locked_FILE
187 {
188 struct _IO_proc_file fpx;
189 #ifdef _IO_MTSAFE_IO
190 _IO_lock_t lock;
191 #endif
192 struct _IO_wide_data wd;
193 } *new_f;
194 _IO_FILE *fp;
195
196 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
197 if (new_f == NULL)
198 return NULL;
199 #ifdef _IO_MTSAFE_IO
200 new_f->fpx.file.file._lock = &new_f->lock;
201 #endif
202 fp = &new_f->fpx.file.file;
203 _IO_no_init (fp, 0, 0, &new_f->wd, &_IO_wproc_jumps);
204 _IO_JUMPS (fp) = &_IO_proc_jumps;
205 _IO_new_file_init (fp);
206 #if !_IO_UNIFIED_JUMPTABLES
207 new_f->fpx.file.vtable = NULL;
208 #endif
209 if (_IO_new_proc_open (fp, command, mode) != NULL)
210 return fp;
211 _IO_un_link (fp);
212 free (new_f);
213 return NULL;
214 }
215
216 int
217 _IO_new_proc_close (fp)
218 _IO_FILE *fp;
219 {
220 /* This is not name-space clean. FIXME! */
221 #if _IO_HAVE_SYS_WAIT
222 int wstatus;
223 _IO_proc_file **ptr = &proc_file_chain;
224 _IO_pid_t wait_pid;
225 int status = -1;
226
227 /* Unlink from proc_file_chain. */
228 for ( ; *ptr != NULL; ptr = &(*ptr)->next)
229 {
230 if (*ptr == (_IO_proc_file *) fp)
231 {
232 *ptr = (*ptr)->next;
233 status = 0;
234 break;
235 }
236 }
237
238 if (status < 0 || _IO_close (_IO_fileno(fp)) < 0)
239 return -1;
240 /* POSIX.2 Rationale: "Some historical implementations either block
241 or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waiting
242 for the child process to terminate. Since this behavior is not
243 described in POSIX.2, such implementations are not conforming." */
244 do
245 {
246 wait_pid = _IO_waitpid (((_IO_proc_file *) fp)->pid, &wstatus, 0);
247 }
248 while (wait_pid == -1 && errno == EINTR);
249 if (wait_pid == -1)
250 return -1;
251 return wstatus;
252 #else /* !_IO_HAVE_SYS_WAIT */
253 return -1;
254 #endif
255 }
256
257 struct _IO_jump_t _IO_proc_jumps = {
258 JUMP_INIT_DUMMY,
259 JUMP_INIT(finish, _IO_new_file_finish),
260 JUMP_INIT(overflow, _IO_new_file_overflow),
261 JUMP_INIT(underflow, _IO_new_file_underflow),
262 JUMP_INIT(uflow, _IO_default_uflow),
263 JUMP_INIT(pbackfail, _IO_default_pbackfail),
264 JUMP_INIT(xsputn, _IO_new_file_xsputn),
265 JUMP_INIT(xsgetn, _IO_default_xsgetn),
266 JUMP_INIT(seekoff, _IO_new_file_seekoff),
267 JUMP_INIT(seekpos, _IO_default_seekpos),
268 JUMP_INIT(setbuf, _IO_new_file_setbuf),
269 JUMP_INIT(sync, _IO_new_file_sync),
270 JUMP_INIT(doallocate, _IO_file_doallocate),
271 JUMP_INIT(read, _IO_file_read),
272 JUMP_INIT(write, _IO_new_file_write),
273 JUMP_INIT(seek, _IO_file_seek),
274 JUMP_INIT(close, _IO_new_proc_close),
275 JUMP_INIT(stat, _IO_file_stat),
276 JUMP_INIT(showmanyc, _IO_default_showmanyc),
277 JUMP_INIT(imbue, _IO_default_imbue)
278 };
279
280 static struct _IO_jump_t _IO_wproc_jumps = {
281 JUMP_INIT_DUMMY,
282 JUMP_INIT(finish, _IO_new_file_finish),
283 JUMP_INIT(overflow, _IO_new_file_overflow),
284 JUMP_INIT(underflow, _IO_new_file_underflow),
285 JUMP_INIT(uflow, _IO_default_uflow),
286 JUMP_INIT(pbackfail, _IO_default_pbackfail),
287 JUMP_INIT(xsputn, _IO_new_file_xsputn),
288 JUMP_INIT(xsgetn, _IO_default_xsgetn),
289 JUMP_INIT(seekoff, _IO_new_file_seekoff),
290 JUMP_INIT(seekpos, _IO_default_seekpos),
291 JUMP_INIT(setbuf, _IO_new_file_setbuf),
292 JUMP_INIT(sync, _IO_new_file_sync),
293 JUMP_INIT(doallocate, _IO_file_doallocate),
294 JUMP_INIT(read, _IO_file_read),
295 JUMP_INIT(write, _IO_new_file_write),
296 JUMP_INIT(seek, _IO_file_seek),
297 JUMP_INIT(close, _IO_new_proc_close),
298 JUMP_INIT(stat, _IO_file_stat),
299 JUMP_INIT(showmanyc, _IO_default_showmanyc),
300 JUMP_INIT(imbue, _IO_default_imbue)
301 };
302
303 strong_alias (_IO_new_popen, __new_popen)
304 versioned_symbol (libc, _IO_new_popen, _IO_popen, GLIBC_2_1);
305 versioned_symbol (libc, __new_popen, popen, GLIBC_2_1);
306 versioned_symbol (libc, _IO_new_proc_open, _IO_proc_open, GLIBC_2_1);
307 versioned_symbol (libc, _IO_new_proc_close, _IO_proc_close, GLIBC_2_1);