1 // This is part of the iostream library, providing input/output for C++.
2 // Copyright (C) 1992 Per Bothner.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Library General Public
6 // License as published by the Free Software Foundation; either
7 // version 2 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Library General Public License for more details.
13 //
14 // You should have received a copy of the GNU Library General Public
15 // License along with this library; if not, write to the Free
16 // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 #ifdef notdef
19 #define _POSIX_SOURCE
20 #endif
21 #include "ioprivate.h"
22 #include "procbuf.h"
23 #include <signal.h>
24 #include <unistd.h>
25 #include <sys/wait.h>
26
27
28 #ifndef FORK
29 #define FORK vfork
30 #endif
31
procbuf(const char * command,int mode)32 procbuf::procbuf(const char *command, int mode) : filebuf()
33 {
34 open(command, mode);
35 }
36
open(const char * command,int mode)37 procbuf *procbuf::open(const char *command, int mode)
38 {
39 int read_or_write;
40 if (is_open())
41 return NULL;
42 int pipe_fds[2];
43 int parent_end, child_end;
44 if (::pipe(pipe_fds) < 0)
45 return NULL;
46 if (mode == ios::in) {
47 parent_end = pipe_fds[0];
48 child_end = pipe_fds[1];
49 read_or_write = _S_NO_WRITES;
50 }
51 else {
52 parent_end = pipe_fds[1];
53 child_end = pipe_fds[0];
54 read_or_write = _S_NO_READS;
55 }
56 _pid = FORK();
57 if (_pid == 0) {
58 ::close(parent_end);
59 int child_std_end = mode == ios::in ? 1 : 0;
60 if (child_end != child_std_end) {
61 ::dup2(child_end, child_std_end);
62 ::close(child_end);
63 }
64 ::execl("/bin/sh", "sh", "-c", command, NULL);
65 ::_exit(127);
66 }
67 ::close(child_end);
68 if (_pid < 0) {
69 ::close(parent_end);
70 return NULL;
71 }
72 _fb._fileno = parent_end;
73 xsetflags(read_or_write, _S_NO_READS|_S_NO_WRITES);
74 return this;
75 }
76
77 /* #define USE_SIGMASK */
78
sys_close()79 int procbuf::sys_close()
80 {
81 _G_pid_t wait_pid;
82 int status = filebuf::sys_close();
83 if (status < 0)
84 return status;
85 int wstatus;
86 #if defined(SIG_BLOCK) && defined(SIG_SETMASK)
87 sigset_t set, oset;
88 sigemptyset (&set);
89 sigaddset (&set, SIGINT);
90 sigaddset (&set, SIGQUIT);
91 sigaddset (&set, SIGHUP);
92 sigprocmask (SIG_BLOCK, &set, &oset);
93 #else
94 #ifdef USE_SIGMASK
95 int mask = sigblock(sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGHUP));
96 #else
97 typedef void (*void_func)(int);
98 void_func intsave = (void_func)signal(SIGINT, SIG_IGN);
99 void_func quitsave = (void_func)signal(SIGQUIT, SIG_IGN);
100 void_func hupsave = (void_func)signal(SIGHUP, SIG_IGN);
101 #endif
102 #endif
103 while ((wait_pid = wait(&wstatus)) != _pid && wait_pid != -1) { }
104 #if defined(SIG_BLOCK) && defined(SIG_SETMASK)
105 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
106 #else
107 #ifdef USE_SIGMASK
108 (void) sigsetmask(mask);
109 #else
110 signal(SIGINT, intsave);
111 signal(SIGQUIT, quitsave);
112 signal(SIGHUP, hupsave);
113 #endif
114 #endif
115 if (wait_pid == -1)
116 return -1;
117 return 0;
118 }
119
~procbuf()120 procbuf::~procbuf()
121 {
122 close();
123 }
124