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