-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_rest.c
136 lines (127 loc) · 3.65 KB
/
execute_rest.c
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
#include"header.h"
#include"variables.h"
void proc_exit(int mysig)
{
//int wstat;
union wait wstat;
pid_t pid;
while (1) {
pid = wait3 (&wstat, WNOHANG, (struct rusage *)NULL );
if (pid == 0){
return;
}
else if (pid == -1){
return;
}
else if (wstat.w_retcode == 0)
{
for(int i = 0;i < background_jobs; i++){
if(job[i].pid == pid)
pop_job(i);
}
printf ("pid:%d Return code: %d\n",pid, wstat.w_retcode);
}
}
}
void proc_killed(){
printf("function called\n");
union wait wstat;
pid_t pid;
while (1) {
pid = wait3 (&wstat, WNOHANG, (struct rusage *)NULL );
if (pid == 0){
return;
}
else if (pid == -1){
return;
}
else if (wstat.w_retcode == 0)
{
printf ("pid:%d Return code: %d\n",pid, wstat.w_retcode);
}
printf("killed\n");
}
}
int execute_rest(char **args, int flag){
int fd[2];
pipe(fd);
int i = 0, background = 0, status;
while(args[i]){ // & can only be applied at last of the process
i++;
}
signal (SIGTERM, proc_killed);
signal (SIGCHLD, proc_exit);
int ret = fork();
if (ret == 0)
exit(rand());
else if (ret == -1)
{
perror("main: fork");
exit(0);
}
else
pause();
if(string_compare(args[i-1], "&")){
background = 1;
args[i-1] = NULL;
}
#define CHECK(x) if(!(x)) { perror(#x " failed"); abort(); /* or whatever */ }
/* Block SIGINT. */
/* sigset_t mask, omask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);*/
pid_t pid = fork();
if(pid == 0){
//if (background!=0) setpgid(0, 0);
// means if it is the child process
// Everything happens only here, after this gets executed, the dup2 are restored as earlier.
if (file_in != 0)
{
dup2(file_in,0);
close(file_in);
}
if (file_out != 1)
{
dup2(file_out,1);
close(file_out);
}
if (flag)
{
dup2(fd[1],1); // the command output is in fd[1], after this process exits, stdout remains 1 only
close(fd[1]);
}
if(execvp(args[0], args) == -1){
fprintf(stderr, "%s: command not found\n", args[0]);
exit(0); // means stop this child process
}
exit(0);
}
else if(pid > 0){ // this is the parent process
if(background == 0){
int status;
child_pid.pid = pid;
strcpy(child_pid.name, args[0]);
strcpy(child_pid.status, "Stopped");
child_pid.flag = 0;
//wait(NULL); // wait till we don't send signal to you or till your child hasn't exited
//waitpid(-1, &status, WUNTRACED); // This waits untill the child process changes it's state.
waitpid(-1, &status, WCONTINUED | WUNTRACED);
}
else
{
//CHECK(sigprocmask(SIG_BLOCK, &mask, &omask) == 0);
job[background_jobs].pid = pid;
strcpy(job[background_jobs].status, "Running");
strcpy(job[background_jobs].name, args[0]);
job[background_jobs].flag = 1;
background_jobs++;
printf("[%d]\n", pid);
}
file_in = fd[0]; // Getting file_in , to use input for next command
close(fd[1]);
}
else{
fprintf(stderr, "%s: forking not done properly\n", args[0]);
}
return 1;
}