Repository navigation
Expand file tree
/
Copy pathprocess.cc
More file actions
78 lines (67 loc) · 1.43 KB
/
Copy pathprocess.cc
File metadata and controls
78 lines (67 loc) · 1.43 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
#include <cstdio>
#include <unistd.h>
#include <wait.h>
#include <vector>
#ifndef LOGGER_INCLUDED
#include "log.cc"
#endif
#define PROCESS_INCLUDED
struct Process {
// FDs of std streams
int stdin = -1;
int stdout = -1;
int stderr = -1;
int max_fd = 2;
char* path = NULL;
char** args = NULL;
Process();
int execute();
void execute_and_exit();
};
Process::Process() {
stdin = -1;
stdout = -1;
stderr = -1;
max_fd = 2;
path = NULL;
args = NULL;
}
int Process::execute() {
if (path == NULL) {
log(" [Error] No Path found for executable\n");
exit(1);
}
int pid = fork();
if(pid != 0) {
log(" [Info ] Created process for pid: ", pid, " for file ", path);
for(int i=3;i<=max_fd;i++) {
close(i);
}
while(waitpid(-1, NULL, 0)>=0);
return 0;
} else {
for(int i=3;i<=max_fd;i++) {
if(i == stdout || i == stdin || i == stderr)continue;
close(i);
}
if(stdin != -1) dup2(stdin, STDIN_FILENO);
if(stdout != -1) dup2(stdout, STDOUT_FILENO);
if(stderr != -1) dup2(stderr, STDERR_FILENO);
int status = execvp(path, args);
if (status == -1) {
log(" [Error] execvp returned -1 while trying to create process::");
log(" [Error] Path: ", path);
log(" [Error] Args: ");
for(int i=0;true;i++) {
if(args[i] == NULL)break;
log(" [Error] ", args[i]);
}
}
exit(0);
}
return 0;
}
void Process::execute_and_exit() {
execute();
exit(0);
}