-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.cpp
More file actions
134 lines (112 loc) · 2.58 KB
/
process.cpp
File metadata and controls
134 lines (112 loc) · 2.58 KB
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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "process.h"
#include <fstream>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <ext/stdio_filebuf.h>
#define PARENT_READ readpipe[0]
#define CHILD_WRITE readpipe[1]
#define CHILD_READ writepipe[0]
#define PARENT_WRITE writepipe[1]
#define BUFFER_SIZE 256
using namespace std;
Process::Process(const char *path, char *const argv[],
bool redirectStderrToStdout) {
int writepipe[2], readpipe[2];
if (pipe(writepipe) == -1) {
throw (const char *)strerror(errno);
}
if (pipe(readpipe) == -1) {
close(writepipe[0]);
close(writepipe[1]);
throw (const char *)strerror(errno);
}
cpid = fork();
if (cpid == -1) {
close(writepipe[0]);
close(writepipe[1]);
close(readpipe[0]);
close(readpipe[1]);
throw (const char *)strerror(errno);
}
if (cpid == 0) {
// child process
close(PARENT_WRITE);
close(PARENT_READ);
dup2(CHILD_READ, 0);
close(CHILD_READ);
dup2(CHILD_WRITE, 1);
if (redirectStderrToStdout) {
dup2(CHILD_WRITE, 2);
}
close(CHILD_WRITE);
execv(path, argv);
perror("execv");
exit(255);
}
close(CHILD_READ);
close(CHILD_WRITE);
int status;
waitpid(cpid, &status, WNOHANG);
if (WIFEXITED(status) && WEXITSTATUS(status) == 255)
{
throw (const char *)"program not found";
}
processOut = fdopen(PARENT_READ, "r");
processIn = fdopen(PARENT_WRITE, "w");
}
Process::~Process() {
int status;
waitpid(cpid, &status, WNOHANG);
if (!WIFEXITED(status) && !WIFSIGNALED(status))
{
kill(cpid, SIGHUP);
waitpid(cpid, &status, WNOHANG);
int nseconds = 10;
while (nseconds > 0 && !WIFEXITED(status) && !WIFSIGNALED(status)) {
sleep(1);
waitpid(cpid, &status, WNOHANG);
--nseconds;
}
kill(cpid, SIGKILL);
waitpid(cpid, &status, WNOHANG);
}
fclose(processOut);
fclose(processIn);
}
int Process::getline(std::string &str) {
ssize_t nread;
char *line = NULL;
size_t len = 0;
str.clear();
nread = ::getline(&line, &len, processOut);
ssize_t idx = nread-1;
while (idx > 0 && (line[idx] == '\r' || line[idx] == '\n') ) {
line[idx] = '\0';
--idx;
}
if (nread > 0)
str = line;
if (line) {
free(line);
}
return nread;
}
int Process::wait() {
int status;
waitpid(cpid, &status, 0);
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
return -WTERMSIG(status);
} else {
return -255;
}
}