-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_maker.c
277 lines (249 loc) · 6.84 KB
/
process_maker.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
//
#include "headers.h"
#include "process_maker.h"
#include <signal.h>
#include "util.h"
// ***** process handling *******
// signal - send signal to a process
// waitpid - makes the current process wait until the other is terminated
// getpid - get pid of the calling process
// kill - can be used to send any signal to process or process group
int stack[1000];
char *commands[1000];
int top = 1; // the next position to insert the process
int pid_to_job(int pid) {
for (int i = 1; i < top; i++) {
if (stack[i] == pid) {
return i;
}
}
return -1;
}
int job_to_pid(int job) {
if (job <= 0 || job >= 1000 || stack[job] == 0) {
return -1;
} else {
return stack[job];
}
}
void kjob_handler(char *tokens[], int n) {
if (n != 3) {
fprintf(stderr, "kjob : Invalid format : kjob <job number> <signal number>\n");
exit_code = 1;
return;
}
int t = (int) strtol(tokens[1], NULL, 10);
int pid = job_to_pid(t);
int signal = (int) strtol(tokens[2], NULL, 10);
if (pid <= 0) {
fprintf(stderr, "kjob : Job does not exist \n");
exit_code = 1;
return;
}
if (signal < 0) {
fprintf(stderr, "kjob : invalid signal \n");
exit_code = 1;
return;
}
if (kill(pid, signal) == -1) {
perror("Signal Failed");
exit_code = 1;
}
}
int remove_child(int pid) {
int job = pid_to_job(pid);
stack[job] = 0;
if (commands[job] != NULL) {
free(commands[job]);
commands[job] = NULL;
}
while (top > 1 && stack[top - 1] == 0) {
top -= 1;
}
return job;
}
void print_job_data(int pid) {
int job = pid_to_job(pid);
char location[size_buff];
sprintf(location, "/proc/%d/cmdline", pid);
//FILE *fg = fopen(location, "r");
//char cmd_name[size_buff];
/*if (fg != NULL)
fgets(cmd_name, size_buff, fg);*/
sprintf(location, "/proc/%d/stat", pid);
FILE *f = fopen(location, "r");
if (f == NULL) {
fprintf(stderr, "%d process not found\n", pid);
return;
}
char state = 0;
int pd;
char new_name[size_buff];
fscanf(f, " %d ", &pd);
fscanf(f, " %s ", new_name);
fscanf(f, " %c ", &state);
char statee[100];
if (state == 'R')
strcpy(statee, "Running");
else if (state == 'T')
strcpy(statee, "Stopped");
else if (state == 'Z')
strcpy(statee, "Zombie");
else if (state == 'S')
strcpy(statee, "Running");
else
strcpy(statee, "Unknown State");
fclose(f);
/*if (fg != NULL)
fclose(fg);
else
strcpy(cmd_name, new_name);*/
//strcpy(cmd_name, c)
printf("[%d] %s %s (%d)\n", job, statee, commands[job], pid);
}
void job_printer() {
for (int i = 1; i < top; i++) {
if (stack[i] != 0) {
print_job_data(stack[i]);
}
}
}
int add_child(int pid, char *command) {
if (top == 1) {
for (int i = 0; i < 1000; i++) {
stack[i] = 0;
commands[i] = NULL;
}
}
if (top <= 0) {
perror("Error adding child process to stack");
exit_code = 1;
return -1;
} else {
stack[top] = pid;
//fprintf(stderr, "top %d", top);
if (commands[top] != NULL) {
free(commands[top]);
commands[top] = NULL;
}
commands[top] = malloc(size_buff);
strcpy(commands[top], command);
top++;
return top - 1;
}
}
void wait_n_switch(int child_pid) {
//signal(SIGTTIN, SIG_IGN);
tcsetpgrp(STDIN_FILENO, child_pid);
kill(child_pid, SIGCONT);
int status;
waitpid(child_pid, &status, WUNTRACED);
/* if (tcsetpgrp(STDOUT_FILENO, getpgid(getpid())) == -1) {
//perror("cant get terminal back");
}*/
if (tcsetpgrp(STDIN_FILENO, getpgid(getpid())) == -1) {
//perror("cant get terminal back");
}
//signal(SIGTTIN, SIG_DFL);
//signal(SIGTTOU, SIG_DFL);
if (WIFEXITED(status)) {
remove_child(child_pid);
exit_code = WEXITSTATUS(status);
} else if (WIFSTOPPED(status)) {
exit_code = 1;
exit_code = WSTOPSIG(status);
printf("\n");
int job = pid_to_job(child_pid);
fprintf(stderr, "\n[%d] Stopped %s (%d)\n", job, commands[job], child_pid);
//print_job_data(child_pid);
} else if (WIFSIGNALED(status)) {
remove_child(child_pid);
exit_code = WTERMSIG(status);
}
}
void make_process(char *tokens[], int num, int bg, int *pipe, int prev_open, char *oldcommand) {
char *cmd = strdup(tokens[0]);
char *argv[num + 1];
for (int i = 0; i < num; i++) {
//fprintf(stderr, "--%d--\n", bg);
argv[i] = strdup(tokens[i]);
}
argv[num] = NULL;
int rc = fork();
if (rc < 0) {
perror("creating child process failed\n");
exit_code = 1;
return;
}
int job = add_child(rc, oldcommand);
if (rc == 0) {
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
// if bg the child process is now in a new session with no terminal
if (pipe != NULL) {
close(pipe[1]);// required
//perror("pipe1 close in child");
close(pipe[0]); // should close the input (Not req ig)
//perror("pipe0 close in child");
}
setpgid(0, 0);
if (execvp(cmd, argv) == -1) {
fprintf(stderr, "invalid command : %s\n", cmd);
exit(1);
}
} else if (rc > 0) {
if (pipe != NULL) {
close(pipe[1]);
if (prev_open != -1)
close(prev_open);
//perror("pipe1 closed in parent");
}
if (!bg) {
//perror("lolol");
wait_n_switch(rc);
} else {
printf("+[%d] (%d)\n", job, rc);
}
}
}
void bg_handler(char **tokens, int n) {
if (n != 2) {
fprintf(stderr, "bg: invalid format! bg <job number>\n");
exit_code = 1;
return;
}
int t = (int) strtol(tokens[1], NULL, 10);
int pid = job_to_pid(t);
if (pid <= 0) {
exit_code = 1;
fprintf(stderr, "Job does not exist \n");
return;
}
if (kill(pid, SIGCONT) == -1) {
exit_code = 1;
}
}
void fg_handler(char **tokens, int n) {
if (n != 2) {
fprintf(stderr, "fg: invalid format! correct format is fg <job number>\n");
exit_code = 1;
return;
}
int t = (int) strtol(tokens[1], NULL, 10);
int pid = job_to_pid(t);
if (pid <= 0) {
fprintf(stderr, "Job does not exist \n");
exit_code = 1;
return;
}
wait_n_switch(pid);
}
void overkill_handler(char **tokens, int n) {
for (int i = 1; i < top; i++)
if (stack[i] > 0) {
int pid = stack[i];
kill(pid, 9);
}
}