File: wvpipe.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (281 lines) | stat: -rw-r--r-- 6,830 bytes parent folder | download | duplicates (3)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 * 
 * Implementation of a WvPipe stream.  WvPipes allow you to create a new
 * process, attaching its stdin/stdout to a WvStream.
 * 
 * See wvpipe.h for more information.
 */
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <assert.h>
#include "wvpipe.h"

// this code is pretty handy for debugging, since 'netstat -nap' can't tell
// you the endpoints of a socketpair(), but it can tell you the name of a
// "real" Unix domain socket.
#if 0
#include "wvaddr.h"
static int socketpair(int d, int type, int protocol, int sv[2])
{
    static int counter = 10;
    
    int f1 = socket(PF_UNIX, SOCK_STREAM, protocol);
    int f2 = socket(PF_UNIX, SOCK_STREAM, protocol);
    
    WvString s("/tmp/sock%s", ++counter);
    WvString s2("/tmp/sock%sb", counter);
    WvUnixAddr a(s), a2(s2);
    
    unlink(s);
    unlink(s2);
    
    bind(f1, a.sockaddr(), a.sockaddr_len());
    bind(f2, a2.sockaddr(), a2.sockaddr_len());
    listen(f1, 10);
    connect(f2, a.sockaddr(), a.sockaddr_len());
    
    socklen_t ll = a.sockaddr_len();
    int f3 = accept(f1, a.sockaddr(), &ll);
    close(f1);
    
    sv[0] = f3;
    sv[1] = f2;
    
    return 0;
}
#endif


// The assorted WvPipe::WvPipe() constructors are described in wvpipe.h

WvPipe::WvPipe(const char *program, const char * const *argv,
	       bool writable, bool readable, bool catch_stderr,
	       int stdin_fd, int stdout_fd, int stderr_fd)
{
    setup(program, argv, writable, readable, catch_stderr,
	  stdin_fd, stdout_fd, stderr_fd);
}


WvPipe::WvPipe(const char *program, const char * const *argv,
	       bool writable, bool readable, bool catch_stderr,
	       WvFDStream *stdin_str, WvFDStream *stdout_str,
	       WvFDStream *stderr_str)
{
    int fd0 = 0, fd1 = 1, fd2 = 2;
    if (stdin_str)
	fd0 = stdin_str->getrfd();
    if (stdout_str)
	fd1 = stdout_str->getwfd();
    if (stderr_str)
	fd2 = stderr_str->getwfd();
    setup(program, argv, writable, readable, catch_stderr, fd0, fd1, fd2);
}


WvPipe::WvPipe(const char *program, const char **argv,
	       bool writable, bool readable, bool catch_stderr,
	       WvFDStream *stdio_str)
{
    if (stdio_str)
    {
	int rfd = stdio_str->getrfd(), wfd = stdio_str->getwfd();
	setup(program, argv, writable, readable, catch_stderr,
	      rfd, wfd, wfd);
    }
    else
	setup(program, argv, writable, readable, catch_stderr, 0, 1, 2);
}


void WvPipe::setup(const char *program, const char * const *argv,
	      bool writable, bool readable, bool catch_stderr,
	      int stdin_fd, int stdout_fd, int stderr_fd)
{
    int socks[2];
    int flags;
    int waitfd;
    int pid;

    if (!program || !argv)
    {
	seterr(EINVAL);
	return;
    }

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, socks))
    {
	seterr(errno);
	return;
    }

    fcntl(socks[0], F_SETFL, O_RDWR|O_NONBLOCK);
    setfd(socks[0]);

    pid = proc.fork(&waitfd);

    if (!pid)
    {
	// child process
	::close(socks[0]);

	if (writable)
	    dup2(socks[1], 0); // writable means redirect child stdin
	else if (stdin_fd == -1)
	    ::close(0);
	else
	    dup2(stdin_fd, 0);
	if (readable)
	    dup2(socks[1], 1); // readable means we redirect child stdout
	else if (stdout_fd == -1)
	    ::close(1);
	else
	    dup2(stdout_fd, 1);
	if (catch_stderr)
	    dup2(socks[1], 2); // but catch_stderr does what you think
	else if (stderr_fd == -1)
	    ::close(2);
	else
	    dup2(stderr_fd, 2);

	/* never close stdin/stdout/stderr */
	fcntl(0, F_SETFD, 0);
	fcntl(1, F_SETFD, 0);
	fcntl(2, F_SETFD, 0);

	/* drop the O_NONBLOCK from stdin/stdout/stderr, it confuses
	 * some programs */
	flags = fcntl(0, F_GETFL);
	fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
	flags = fcntl(1, F_GETFL);
	fcntl(1, F_SETFL, flags & ~O_NONBLOCK);
	flags = fcntl(2, F_GETFL);
	fcntl(2, F_SETFL, flags & ~O_NONBLOCK);

	/* If we're not capturing any of these through the socket, it
	 * means that the child end of the socket will be closed right
	 * at the execvp, which is bad. If we set the close-on-exec to
	 * false, the child end of the socket will be closed when the
	 * child (or sub-) process exits. */
	if (!writable && !readable && !catch_stderr)
	    fcntl(socks[1], F_SETFD, 0);  // never close the socketpair
	else
	    ::close(socks[1]); // has already been duplicated
	
	// this will often fail, but when it does work it is probably
	// the Right Thing To Do (tm)
	if (!readable && stdout_fd != 1)
	{
	    setsid();
// Only on some OSes will we find TIOCSCTTY to set the controlling tty.
// On others, we need to use TCSETCTTY, but we are too lazy to implement that.
#ifdef TIOCSCTTY
	    ioctl(1, TIOCSCTTY, 1);
#else
# ifdef TCSETCTTY
#  warning You should implement TCSETCTTY here.  Thanks!
# endif
#endif
	}

	::close(waitfd);
	
	// now run the program.  If it fails, use _exit() so no destructors
	// get called and make a mess.
	execvp(program, (char * const *)argv);
	_exit(242);
    }
    else if (pid > 0)
    {
	// parent process.
	// now that we've forked, it's okay to close this fd if we fork again.
	fcntl(socks[0], F_SETFD, 1);
	::close(socks[1]);
    }
    else
    {
	::close(socks[0]);
	::close(socks[1]);
	return;
    }
}


// send the child process a signal
void WvPipe::kill(int signum)
{
    if (proc.running)
	proc.kill(signum);
}


// wait for the child to die
int WvPipe::finish(bool wait_children)
{
    shutdown(getwfd(), SHUT_WR);
    close();
    while (proc.running)
	proc.wait(1000, wait_children);
    
    return proc.estatus;
}


bool WvPipe::child_exited()
{
    /* FIXME: bug in WvSubProc? */
    proc.wait(0);
    proc.wait(0);
    return !proc.running;
}


// if child_exited(), return true if it died because of a signal, or
// false if it died due to a call to exit().
bool WvPipe::child_killed() const
{
    int st = proc.estatus;
    assert (WIFEXITED(st) || WIFSIGNALED(st));
    return WIFSIGNALED(st);
}


// return the numeric exit status of the child (if it exited) or the
// signal that killed the child (if it was killed).
int WvPipe::exit_status()
{
    /* FIXME: bug in WvSubProc? */
    proc.wait(0);
    proc.wait(0);

    int st = proc.estatus;
    assert (WIFEXITED(st) || WIFSIGNALED(st));
    if (child_killed())
	return WTERMSIG(st);
    else
	return WEXITSTATUS(st);
}


WvPipe::~WvPipe()
{
    close();
}


// this is necessary when putting, say, sendmail through a WvPipe on the
// globallist so we can forget about it.  We call nowrite() so that it'll
// get the EOF and then go away when it's done, but we need to read from it
// for it the WvPipe stop selecting true and get deleted.
void WvPipe::ignore_read(WvStream& s, void *userdata)
{
    char c;
    s.read(&c, 1);
}