forked from Jawoos/SystemPrograming_Team9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
415 lines (356 loc) · 9.27 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <time.h>
#include <linux/limits.h>
#include <sys/times.h>
#include <ncurses.h>
#include <sys/time.h>
#include <signal.h>
#include "rd.h"
#include "dokill.h"
#define MAX 1000
#define BLANK "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
psinfo P[MAX]; //proc에 있는 모든 프로세스 저장
int P_SIZE = 0; //P 배열 크기
psinfo bash[MAX];//bash만 들어있음 터미널에서 돌아가는 프로세스는 그프로세스의 PPID가 bash PID 이다
int bash_SIZE = 0;
psinfo wantkill[MAX]; //죽이고 싶은 프로세스 저장
int WK_SIZE = 0;
int CK[MAX]; //checkTokill store ary
int CK_SIZE = 0;
int lines, cols;
int curindex = 0;
int strsize = strlen("enter what you want to do?(q:exit, k:kill WANTKILL PROCESS, b:kill BASH PROCESS, e:enter exception pid, t:enter time)");
int start = 0; //for문 시작 지점 -> 위아래 스크롤 구현
int map_num = 0; //좌우 이동
int store_pid(); //store P and bash
int getCmdLine(char *file, char *buf, int size); //명령어 반환(comm)
void find_kill(psinfo*, psinfo*, int, int); //터미널에서 돌아가는 모든 프로세스
void get_display(); //프로세스 상태 받기 및 출력
void get_value();
void set_time_except(psinfo* ary, int size, int t); //예외 처리할 시간 입력받기
void set_blank()
{
int i;
for(i = 3; i > 0; i--)
{
move(lines - i, 0);
printw(BLANK);
refresh();
}
}
void blank_all()
{
for(int i = 2; i < lines - 3; i++)
{
move(i, 0);
printw(BLANK);
}
refresh();
}
int set_ticker(int n_msecs)
{
struct itimerval new_timeset;
long n_sec, n_usecs;
n_sec = n_msecs / 1000;
n_usecs = (n_msecs % 1000) *1000L;
new_timeset.it_interval.tv_sec = n_sec;
new_timeset.it_interval.tv_usec = n_usecs;
new_timeset.it_value.tv_sec = n_sec;
new_timeset.it_value.tv_usec = n_usecs;
return setitimer(ITIMER_REAL, &new_timeset, NULL);
}
void update_ps(int num)
{
signal(SIGALRM, SIG_IGN);
get_value();
get_display();
move(lines - 3, strsize);
refresh();
signal(SIGALRM, update_ps);
}
int main(int argc, char* argv[])
{
int input;
char input_temp;
// int delay = 500; //0.5초마다 ps 갱신
int delay = 500; //0.5초마다 ps 갱신
initscr();
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLACK); //default
init_pair(2, COLOR_BLACK, COLOR_YELLOW); //this pid
init_pair(3, COLOR_WHITE, COLOR_MAGENTA); //TOTAL
init_pair(4, COLOR_WHITE, COLOR_BLUE); //COMMAND
clear();
move(0, 0);
attron(COLOR_PAIR(4));
printw("this terminal pid : %d", getppid()); //현재 프로세스가 실행되고 있는 터미널pid
attron(COLOR_PAIR(1));
refresh();
signal(SIGALRM, update_ps);
while(1){
getmaxyx(stdscr,lines,cols); // 현재 띄워진 창의 행,열 크기 저장
signal(SIGALRM, update_ps);
set_ticker(delay);
set_blank();
move(lines - 3, 0);
printw("enter what you want to do?(q:exit, k:kill WANTKILL PROCESS, b:kill BASH PROCESS, e:enter exception pid, t:enter time)");
refresh();
noecho();
input = getch();
if (input != 27) {
switch(input){
case 113: //q
move(lines - 2, 0);
printw("Bye Bye.");
refresh();
sleep(1);
endwin();
return 0;
case 107: //k
set_blank();
move(lines - 3, 0);
do_kill(wantkill, WK_SIZE); //want_kill process 종료
break;
case 101: //e
echo();
set_blank();
move(lines - 3, 0);
printw("enter exception pid"); //예외 프로세서 pid 입력받기
refresh();
get_pid(wantkill, WK_SIZE, bash, bash_SIZE, lines);
store_CK(wantkill, WK_SIZE, CK, &CK_SIZE);
store_CK(bash, bash_SIZE, CK, &CK_SIZE);
set_CK(P, P_SIZE, CK, CK_SIZE);
break;
case 98: //b
echo();
signal(SIGALRM, SIG_IGN);
set_blank();
move(lines - 3, 0);
printw("If you kill bash, that can effect to other linux users of system. Do you really want to kill bash?(Y/N)");
refresh();
scanw("%c", &input_temp);
refresh();
move(lines - 2, 0);
if(input_temp == 'Y' || input_temp == 'y')
do_must_kill(bash, bash_SIZE); //bash 프로세서 종료
signal(SIGALRM, update_ps);
break;
case 116: //t
echo();
signal(SIGALRM, SIG_IGN);
set_blank();
move(lines - 3, 0);
printw("enter time to not kill(exit : -1)"); //일정시간 입력받기
refresh();
int time;
scanw("%d", &time);
refresh();
if (time == -1)
break;
set_time_except(wantkill, WK_SIZE, time);
set_time_except(bash, bash_SIZE, time);
store_CK(wantkill, WK_SIZE, CK, &CK_SIZE);
store_CK(bash, bash_SIZE, CK, &CK_SIZE);
set_CK(P, P_SIZE, CK, CK_SIZE);
signal(SIGALRM, update_ps);
break;
case 410:
break;
default:
set_blank();
move(lines - 3, 0);
printw("you type wrong option please type it right.");
refresh();
sleep(1);
break;
}
} else {
input = getch();
input = getch();
switch (input) {
case 65: //up
if (start > 10)
start -= 10;
else
start = 0;
break;
case 66: //down
if ((map_num % 3) == 0 && start < P_SIZE - lines + 15)
start += 10;
else if ((map_num % 3) == 1 && start < bash_SIZE - lines + 15)
start += 10;
else if ((map_num % 3) == 2 && start < WK_SIZE - lines + 15)
start += 10;
break;
case 68: //left
map_num--;
start = 0;
break;
case 67: //right
map_num++;
start = 0;
break;
default:
move(lines - 3, 0);
printw("you type wrong option please type it right..!");
refresh();
sleep(1);
break;
}
}
}
}
int store_pid()
{
DIR *dir = NULL;
struct dirent *entry = NULL;
struct stat fileStat;
int pid;
char *cmdLine;
char tempPath[500] = "\0";
int i = 0;
dir = opendir("/proc");
while ((entry = readdir(dir)) != NULL)
{
lstat(entry->d_name, &fileStat);
// set_blank();
// move(lines - 3, 0);
// printw("test%d", i);
// refresh();
// sleep(1);
if(S_ISDIR(fileStat.st_mode))
{
pid = atoi(entry->d_name);
if(pid > 0)
{
cmdLine = malloc(sizeof(char)*500);
P[i++].pid = pid;
memset(cmdLine, 0, sizeof(*cmdLine));
sprintf(tempPath, "/proc/%d/cmdline", pid);
if(getCmdLine(tempPath, cmdLine, sizeof(cmdLine)) == -1)
continue;
if(!strcmp(cmdLine, "bash"))
{
bash[bash_SIZE++].pid = pid;
}
free(cmdLine);
}
}
}
// free(cmdLine);
rewinddir(dir);
closedir(dir);
return i;
}
int getCmdLine(char *file, char *buf, int size){
FILE *srcFp;
if((srcFp = fopen(file, "r")) == NULL){
return -1;
}
// memset(buf, 0, sizeof(size));
fgets(buf, 500, srcFp);
// fscanf(srcFp, "%s", buf);
fclose(srcFp);
// free(buf);
return 0;
}
void find_kill(psinfo* ary1, psinfo* ary2, int size1, int size2)
{
int pid, ppid;
for(int i = 0; i < size2; i++)
{
pid = ary2[i].pid;
for(int j = 0; j < size1; j++)
{
ppid = ary1[j].ppid;
if(pid == ppid)
{
wantkill[WK_SIZE].pid = ary1[j].pid;
strcpy(wantkill[WK_SIZE].comm, ary1[j].comm);
wantkill[WK_SIZE].state = ary1[j].state;
wantkill[WK_SIZE].ppid = ary1[j].ppid;
wantkill[WK_SIZE].runningTime = ary1[j].runningTime;
if(wantkill[WK_SIZE].pid == getpid()) // current process and it's bash
wantkill[WK_SIZE].checkTokill = 1;
wantkill[WK_SIZE++].start_time = ary1[j].start_time;
// fprintf(stderr, "%d\n", WK_SIZE);
}
}
}
}
void get_display(){
move(2, 0);
attron(COLOR_PAIR(2));
printw(" PID COMMAND STATE PPID startTime runningTime check");
refresh();
move(1, 0);
if ((map_num % 3) == 0){
attron(COLOR_PAIR(3));
printw("TOTAL : %d\t\tAll Process Table ", P_SIZE);
refresh();
}
else if ((map_num % 3) == 1){
attron(COLOR_PAIR(3));
printw("TOTAL : %d\t\tBash Process Table ", bash_SIZE);
refresh();
}
else if ((map_num % 3) == 2){
attron(COLOR_PAIR(3));
printw("TOTAL : %d\t\tUser Process Table ", WK_SIZE);
refresh();
}
refresh();
attron(COLOR_PAIR(1));
if ((map_num % 3) == 0)
curindex = print_psinfo(P, P_SIZE, curindex, lines);
else if ((map_num % 3) == 1)
curindex = print_psinfo(bash, bash_SIZE, curindex, lines);
else if ((map_num % 3) == 2)
curindex = print_psinfo(wantkill, WK_SIZE, curindex, lines);
}
void get_value()
{
reset_arry(P, &P_SIZE);
reset_arry(bash, &bash_SIZE);
reset_arry(wantkill, &WK_SIZE);
P_SIZE = store_pid(); //store at bash
for(int i = 0; i < P_SIZE; i++)
{
int pid = P[i].pid;
store_psinfo(P, pid, i);
}
for(int i = 0; i < bash_SIZE; i++)
{
int pid = bash[i].pid;
store_psinfo(bash, pid, i);
}
move(lines - 3, 0);
find_kill(P, bash, P_SIZE, bash_SIZE);
set_CK(P, P_SIZE, CK, CK_SIZE);
set_CK(bash, bash_SIZE, CK, CK_SIZE);
set_CK(wantkill, WK_SIZE, CK, CK_SIZE);
}
void set_time_except(psinfo* ary, int size, int t){
for(int i = 0; i < size; i++)
{
if(ary[i].runningTime < t){
move(lines - 2, 0);
printw(BLANK);
move(lines - 2, 0);
ary[i].checkTokill = 1;
// printw("pid(%d) is exception", ary[i].pid);
printw("pid(%d) is exception = %d", ary[i].pid, ary[i].checkTokill);
refresh();
sleep(1); //1초마다 예외된 값 출력
}
}
}