-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.cpp
160 lines (137 loc) · 3.42 KB
/
execute.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
#include "execute.h"
string executeCommand(vector<string> input){
int pipearr[2];
string result;
if(pipe(pipearr) == -1){
perror("Error while creating pipe");
exit(1);
}
pid_t pid = fork();
if(pid==0){
close(pipearr[0]);
dup2(pipearr[1], STDOUT_FILENO);
close(pipearr[1]);
char* args[input.size() + 1];
for(int i=0; i<input.size(); i++){
char* tmp = input[i].data();
args[i] = tmp;
}
args[input.size()] = NULL;
execvp(args[0], args);
perror("execvp failed...");
exit(1);
}else if(pid > 0){
close(pipearr[1]);
char buffer[1024];
ssize_t bytesRead;
while((bytesRead = read(pipearr[0], buffer, sizeof(buffer)-1)) > 0){
result.append(buffer, bytesRead);
}
close(pipearr[0]);
int status;
waitpid(pid, &status, 0);
}else{
cout<<" [[ ERROR ]] Fork failed..."<<endl;
return "";
}
return result;
}
vector<pid_t> bgProcesses;
string executeCommand(vector<string> input, bool background) {
int pipearr[2];
string result;
if(background) input.pop_back();
if(pipe(pipearr) == -1){
perror("Error while creating pipe");
exit(1);
}
pid_t pid = fork();
if (pid == 0) {
close(pipearr[0]);
dup2(pipearr[1], STDOUT_FILENO);
close(pipearr[1]);
char* args[input.size() + 1];
for(int i=0; i<input.size(); i++){
char* tmp = input[i].data();
args[i] = tmp;
}
cout<<endl;
args[input.size()] = NULL;
execvp(args[0], args);
perror("execvp failed...");
exit(1);
}
else if (pid > 0) {
if (background) {
bgProcesses.push_back(pid);
cout << "Started background process with PID: " << pid << endl;
} else {
close(pipearr[1]);
char buffer[1024];
ssize_t bytesRead;
while((bytesRead = read(pipearr[0], buffer, sizeof(buffer)-1)) > 0){
result.append(buffer, bytesRead);
}
close(pipearr[0]);
int status;
waitpid(pid, &status, 0);
}
}
else {
perror("fork failed");
}
return result;
}
void checkBackgroundProcesses() {
for (auto it = bgProcesses.begin(); it != bgProcesses.end();) {
if (waitpid(*it, nullptr, WNOHANG) > 0) {
cout << "Process " << *it << " completed\n";
it = bgProcesses.erase(it);
} else {
++it;
}
}
}
string executeCommand(string input, vector<string> commands){
int fd_in[2], fd_out[2];
if(pipe(fd_in) < 0 || pipe(fd_out) < 0){
perror("Error creating pipe...");
exit(1);
}
string result;
pid_t pid = fork();
if(pid == 0){
close(fd_in[1]);
dup2(fd_in[0], STDIN_FILENO);
close(fd_in[0]);
dup2(fd_out[1], STDOUT_FILENO);
close(fd_out[1]);
char* args[commands.size()+1];
for(int i=0; i<commands.size(); i++){
char* tmp = commands[i].data();
args[i] = tmp;
}
args[commands.size()] = NULL;
execvp(args[0], args);
perror("Execvp failed with inputs");
exit(1);
}else if(pid > 0){
close(fd_in[0]);
close(fd_out[1]);
char* inputBuffer = input.data();
write(fd_in[1], inputBuffer, input.size());
close(fd_in[1]);
char outputBuffer[1024];
ssize_t bytesRead;
while((bytesRead = read(fd_out[0], outputBuffer, sizeof(outputBuffer)-1)) > 0){
result.append(outputBuffer, bytesRead);
}
close(fd_out[0]);
int status;
waitpid(pid, &status, 0);
}else{
cout<<"Error fork failed..."<<endl;
return "";
}
return result;
}