-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchildprocess.cxx
157 lines (142 loc) · 3.18 KB
/
childprocess.cxx
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
/*
* Copyright (c) 2012 Vasily i. Redkin <[email protected]>
* License: MIT (See LICENSE.txt or http://www.opensource.org/licenses/MIT)
*/
#include "childprocess.hpp"
#include "family.hpp"
#include <iostream>
#include <signal.h> // for kill
#if 0
# define XDEBUG(a) a
#else
# define XDEBUG(s)
#endif
int ReadBuffer::do_read()
{
char buf[8192];
int r = ::read(m_fd, buf, sizeof(buf));
// std::cout << "Read " << r << " bytes from fd " << m_fd << std::endl;
if(r > 0)
m_buf.append(buf, r);
return r;
}
bool ReadBuffer::readline(std::string & s)
{
size_t pos = m_buf.find('\n');
if(pos != std::string::npos) {
s = m_buf.substr(0, pos);
m_buf.erase(0, pos+1);
return true;
}
return false;
}
/***************************/
bool ChildProcess::start()
{
std::vector<char> cmdbuf(m_family->cmd().begin(), m_family->cmd().end());
cmdbuf.push_back('\0');
std::vector<char *> args;
char * t = strtok(&cmdbuf[0], " \t");
if(!t)
exit(-1);
args.push_back(t);
while((t = strtok(NULL, " \t"))) { args.push_back(t); }
XDEBUG(for(int i = 0; i < args.size(); i++) { std::cout << "Arg" << i << ": " << args[i] << std::endl; })
args.push_back(NULL);
int fdo[2], fde[2];
pipe(fdo);
pipe(fde);
XDEBUG(std::cout << "Pipes: o: " << fdo[0] << ", " << fdo[1] << ", e: " << fde[0] << ", " << fde[1] << std::endl);
int pid = fork();
if(pid < 0) {
perror("fork");
return false;
}
if(pid) {
if(m_setpgid)
setpgid(pid, 0);
m_pid = pid;
m_fdo = fdo[0];
m_fde = fde[0];
close(fdo[1]);
close(fde[1]);
XDEBUG(std::cout << "Closing pipes: " << fdo[1] << ", " << fde[1] << std::endl);
m_bufo.fd(m_fdo);
m_bufe.fd(m_fde);
disp.register_reader(m_fdo, this);
disp.register_reader(m_fde, this);
disp.register_chldproc(pid, this);
return true;
} else {
if(m_setpgid)
setpgid(0, 0);
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
close(fdo[0]);
close(fde[0]);
dup2(fdo[1], 1);
dup2(fde[1], 2);
close(fdo[1]);
close(fde[1]);
execvp(args[0], &args[0]);
perror("execvp");
sleep(1);
_exit(-1);
}
return false;
}
int ChildProcess::can_read(int fd)
{
int r;
std::string buf;
if(fd == m_fdo) {
r = m_bufo.do_read();
if(!r) {
close(m_fdo);
XDEBUG(std::cout << "Closing pipe: " << m_fdo << std::endl);
m_fdo = -1;
}
while(m_bufo.readline(buf)) {
m_family->output('O', buf, m_pid);
}
} else if(fd == m_fde) {
r = m_bufe.do_read();
if(!r) {
close(m_fde);
XDEBUG(std::cout << "Closing pipe: " << m_fde << std::endl);
m_fde = -1;
}
while(m_bufe.readline(buf)) {
m_family->output('E', buf, m_pid);
}
} else {
std::cerr << "ChildProcess::can_read: Not my fd: " << fd << std::endl;
return -1;
}
if(r == 0)
disp.unregister_reader(fd);
return r;
}
void ChildProcess::dead(int status)
{
if(m_fdo >= 0) {
disp.unregister_reader(m_fdo);
close(m_fdo);
}
if(m_fde >= 0) {
disp.unregister_reader(m_fde);
close(m_fde);
}
disp.unregister_childproc(m_pid);
m_family->celebrate_child_death(this, status);
if(m_setpgid)
::kill(-m_pid, SIGKILL); // XXX
}
int ChildProcess::kill(int signal)
{
return ::kill(m_pid, signal);
}