forked from eidheim/tiny-process-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_unix.cpp
186 lines (163 loc) · 4.65 KB
/
process_unix.cpp
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
#include "process.hpp"
#include <cstdlib>
#include <unistd.h>
#include <signal.h>
#include <stdexcept>
Process::Data::Data(): id(-1) {}
Process::id_type Process::open(const std::string &command, const std::string &path) {
if(open_stdin)
stdin_fd=std::unique_ptr<fd_type>(new fd_type);
if(read_stdout)
stdout_fd=std::unique_ptr<fd_type>(new fd_type);
if(read_stderr)
stderr_fd=std::unique_ptr<fd_type>(new fd_type);
int stdin_p[2], stdout_p[2], stderr_p[2];
if(stdin_fd && pipe(stdin_p)!=0) {
close(stdin_p[0]);close(stdin_p[1]);
return -1;
}
if(stdout_fd && pipe(stdout_p)!=0) {
if(stdin_fd) {close(stdin_p[0]);close(stdin_p[1]);}
close(stdout_p[0]);close(stdout_p[1]);
return -1;
}
if(stderr_fd && pipe(stderr_p)!=0) {
if(stdin_fd) {close(stdin_p[0]);close(stdin_p[1]);}
if(stdout_fd) {close(stdout_p[0]);close(stdout_p[1]);}
close(stderr_p[0]);close(stderr_p[1]);
return -1;
}
id_type pid = fork();
if (pid < 0) {
if(stdin_fd) {close(stdin_p[0]);close(stdin_p[1]);}
if(stdout_fd) {close(stdout_p[0]);close(stdout_p[1]);}
if(stderr_fd) {close(stderr_p[0]);close(stderr_p[1]);}
return pid;
}
else if (pid == 0) {
if(stdin_fd) dup2(stdin_p[0], 0);
if(stdout_fd) dup2(stdout_p[1], 1);
if(stderr_fd) dup2(stderr_p[1], 2);
if(stdin_fd) {close(stdin_p[0]);close(stdin_p[1]);}
if(stdout_fd) {close(stdout_p[0]);close(stdout_p[1]);}
if(stderr_fd) {close(stderr_p[0]);close(stderr_p[1]);}
//Based on http://stackoverflow.com/a/899533/3808293
int fd_max=sysconf(_SC_OPEN_MAX);
for(int fd=3;fd<fd_max;fd++)
close(fd);
setpgid(0, 0);
//TODO: See here on how to emulate tty for colors: http://stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdin-is-interactive-not-a-pipe
//TODO: One solution is: echo "command;exit"|script -q /dev/null
if(!path.empty()) {
auto path_escaped=path;
size_t pos=0;
//Based on https://www.reddit.com/r/cpp/comments/3vpjqg/a_new_platform_independent_process_library_for_c11/cxsxyb7
while((pos=path_escaped.find('\'', pos))!=std::string::npos) {
path_escaped.replace(pos, 1, "'\\''");
pos+=4;
}
execl("/bin/sh", "sh", "-c", ("cd '"+path_escaped+"' && "+command).c_str(), NULL);
}
else
execl("/bin/sh", "sh", "-c", command.c_str(), NULL);
_exit(EXIT_FAILURE);
}
if(stdin_fd) close(stdin_p[0]);
if(stdout_fd) close(stdout_p[1]);
if(stderr_fd) close(stderr_p[1]);
if(stdin_fd) *stdin_fd = stdin_p[1];
if(stdout_fd) *stdout_fd = stdout_p[0];
if(stderr_fd) *stderr_fd = stderr_p[0];
closed=false;
data.id=pid;
return pid;
}
void Process::async_read() {
if(data.id<=0)
return;
if(stdout_fd) {
stdout_thread=std::thread([this](){
char buffer[buffer_size];
ssize_t n;
while ((n=read(*stdout_fd, buffer, buffer_size)) > 0)
read_stdout(buffer, static_cast<size_t>(n));
});
}
if(stderr_fd) {
stderr_thread=std::thread([this](){
char buffer[buffer_size];
ssize_t n;
while ((n=read(*stderr_fd, buffer, buffer_size)) > 0)
read_stderr(buffer, static_cast<size_t>(n));
});
}
}
int Process::get_exit_status() {
if(data.id<=0)
return -1;
int exit_status;
waitpid(data.id, &exit_status, 0);
{
std::lock_guard<std::mutex> lock(close_mutex);
closed=true;
}
close_fds();
return exit_status;
}
void Process::close_fds() {
if(stdout_thread.joinable())
stdout_thread.join();
if(stderr_thread.joinable())
stderr_thread.join();
if(stdin_fd)
close_stdin();
if(stdout_fd) {
if(data.id>0)
close(*stdout_fd);
stdout_fd.reset();
}
if(stderr_fd) {
if(data.id>0)
close(*stderr_fd);
stderr_fd.reset();
}
}
bool Process::write(const char *bytes, size_t n) {
if(!open_stdin)
throw std::invalid_argument("Can't write to an unopened stdin pipe. Please set open_stdin=true when constructing the process.");
std::lock_guard<std::mutex> lock(stdin_mutex);
if(stdin_fd) {
if(::write(*stdin_fd, bytes, n)>=0) {
return true;
}
else {
return false;
}
}
return false;
}
void Process::close_stdin() {
std::lock_guard<std::mutex> lock(stdin_mutex);
if(stdin_fd) {
if(data.id>0)
close(*stdin_fd);
stdin_fd.reset();
}
}
void Process::kill(bool force) {
std::lock_guard<std::mutex> lock(close_mutex);
if(data.id>0 && !closed) {
if(force)
::kill(-data.id, SIGTERM);
else
::kill(-data.id, SIGINT);
}
}
void Process::kill(id_type id, bool force) {
if(id<=0)
return;
if(force)
::kill(-id, SIGTERM);
else
::kill(-id, SIGINT);
}