-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpps.c
More file actions
548 lines (518 loc) · 11.9 KB
/
pps.c
File metadata and controls
548 lines (518 loc) · 11.9 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "ssu_shell.h"
//전역변수
int is_aOption, is_uOption, is_xOption;
int MAX_WIDTH;
long memtotal;
time_t start_time,cpu_time;
double cpu_usage;
char tokens[25][MAX_TOKEN_SIZE];
//함수
int isNum(char* name);
void set_info();
void set_memtotal();
void set_tty_name();
void print_no_option();
void print_not_uOption();
void print_uOption();
//메인
int main(int argc, char* argv[])
{
FILE* fp;
DIR* dirp;
int user, process_uid;
struct dirent* dp;
struct stat statbuf;
struct passwd *pw;
int i, j, c;
//터미널 너비받아옴
initscr();
getmaxyx(stdscr, i, j);
endwin();
MAX_WIDTH = j;
//옵션처리
if(argc > 1){
for(i = 0; argv[1][i] !='\0'; i++)
switch(argv[1][i]){
case 'a' : is_aOption = 1; break;
case 'u' : is_uOption = 1; break;
case 'x' : is_xOption = 1; break;
default : fprintf(stderr,"option error\n");exit(1);break;
}
}
//u옵션이 있으면
if(is_uOption){
printf("USER PID %%CPU %%MEM VSZ RSS TTY STAT START TIME COMMAND\n");
//창이 작은경우
if(MAX_WIDTH <= 67)
//커맨드 길이 두배늘림
MAX_WIDTH *= 2;
//memory총크기 전역변수에 저장
set_memtotal();
}
//u옵션이 없는경우
else if(is_aOption || is_xOption)
printf(" PID TTY STAT TIME COMMAND\n");
//옵션이 없는경우
else{
printf(" PID TTY TIME CMD\n");
//함수에서 처리후 종료
print_no_option();
exit(0);
}
//process stat parsing
if((dirp = opendir("/proc")) == NULL){
fprintf(stderr,"/proc open fail\n");
exit(1);
}
user = getuid();
chdir("/proc");
while((dp = readdir(dirp)) != NULL){
memset(tokens, 0, sizeof(tokens));
if(isNum(dp->d_name)){
if(stat(dp->d_name, &statbuf) < 0){
fprintf(stderr,"%s stat error\n", dp->d_name);
exit(1);
}
//소유자 아이디 문자열화
process_uid = statbuf.st_uid;
pw = getpwuid(process_uid);
if(strlen(pw->pw_name) > 8){
strncpy(tokens[0],pw->pw_name, 7);
strcat(tokens[0],"+");
}
else
strcpy(tokens[0], pw->pw_name);
chdir(dp->d_name);
if((fp = fopen("stat", "r")) == NULL){
fprintf(stderr,"stat open error\n");
exit(1);
}
//24개의 stat정보 저장
for(i = 1; i < 25; i++){
j = 0;
while((c = fgetc(fp)) != ' '){
if(c == '('){
while((c = fgetc(fp)) != ')')
tokens[i][j++] = c;
}
else
tokens[i][j++] = c;
}
tokens[i][j] = '\0';
}
//a옵션 단독일경우
if(is_aOption && !is_xOption){
if(!strcmp(tokens[7], "0")){
fclose(fp);
chdir("..");
continue;
}
}
//x옵션 단독일 경우
else if(is_xOption && !is_aOption){
if(user != process_uid){
fclose(fp);
chdir("..");
continue;
}
}
//u옵션 단독일 경우
else if(is_uOption && !is_aOption && !is_xOption){
if(user != process_uid || !strcmp(tokens[7], "0")){
fclose(fp);
chdir("..");
continue;
}
}
//print 옵션확인
if(is_uOption)
print_uOption();
//u옵션이 없는경우
else if(is_aOption || is_xOption)
print_not_uOption();
fclose(fp);
chdir("..");
}
}
}
//디렉토리 이름이 숫자인지 확인
int isNum(char* name)
{
int i;
for(i = 0; name[i] != '\0'; i++){
if(name[i] < '0' || name[i] > '9')
return 0;
}
return 1;
}
//cpu_usage, start_time, cpu_time을 구하여 전역변수에 저장
void set_info()
{
FILE* fp;
double uptime, ps_uptime;
int c, i, tmp;
char token[MAX_TOKEN_SIZE];
unsigned long long totaltime, starttime;
time_t t;
totaltime = 0;
//totaltime
for(i = 14; i < 16; i++){
totaltime += strtoul(tokens[i], NULL, 10);
}
//starttime
starttime = strtoull(tokens[22], NULL, 10);
//uptime
if((fp = fopen("/proc/uptime", "r")) == NULL){
fprintf(stderr,"/prco/uptime open error\n");
exit(1);
}
i = 0;
while((c = fgetc(fp)) != ' ')
token[i++] = c;
token[i] = '\0';
uptime = atof(token);
//calculation
ps_uptime = uptime - (starttime/HZ);
cpu_usage = 100 * ((totaltime/HZ) / ps_uptime);
//CPU 점유율
tmp = (int)(cpu_usage * 10);
cpu_usage = tmp/10.0;
//CPU 사용시간
cpu_time = (time_t)(totaltime/HZ);
//프로세스 시작시간
t = time(NULL);
tmp = (int)uptime;
tmp = tmp - (int)(starttime/HZ);
t -= (time_t)tmp;
start_time = t;
fclose(fp);
}
// /proc/meminfo에 total memory를 구하여 전역변수에 저장
void set_memtotal()
{
FILE* fp;
char token[MAX_TOKEN_SIZE];
int c, i;
if((fp = fopen("/proc/meminfo", "r")) == NULL){
fprintf(stderr,"meminfo open error\n");
exit(1);
}
while((c = fgetc(fp)) != '\n'){
if(c >= '0' && c <= '9'){
i = 0;
token[i++] = c;
c = fgetc(fp);
while(c >= '0' && c <= '9'){
token[i++] = c;
c = fgetc(fp);
}
token[i] = '\0';
break;
}
}
memtotal = atol(token);
}
void set_tty_name()
{
int tty_nr;
//is tty0~
tty_nr = atoi(tokens[7]);
if(tty_nr >= 1024 && tty_nr <= 1087){
memset(tokens[7], 0, sizeof(tokens[7]));
sprintf(tokens[7],"tty%d",tty_nr-1024);
}
//is ttyS
else if(tty_nr >= 1088 && tty_nr <= 1119){
memset(tokens[7], 0, sizeof(tokens[7]));
sprintf(tokens[7],"ttyS%d",tty_nr-1088);
}
//is tty
else if(tty_nr == 1280){
memset(tokens[7], 0, sizeof(tokens[7]));
strcpy(tokens[7], "tty");
}
//is consol
else if(tty_nr == 1281){
memset(tokens[7], 0, sizeof(tokens[7]));
strcpy(tokens[7], "console");
}
//is ptmx
else if(tty_nr == 1282){
memset(tokens[7], 0, sizeof(tokens[7]));
strcpy(tokens[7], "/ptmx");
}
//is ttyprintk
else if(tty_nr == 1283){
memset(tokens[7], 0, sizeof(tokens[7]));
strcpy(tokens[7], "ttyprintk");
}
//is ptm
else if(tty_nr == 34768){
memset(tokens[7], 0, sizeof(tokens[7]));
sprintf(tokens[7],"pts/%d",tty_nr-34768);
}
//is pts
else if(tty_nr >= 34816){
memset(tokens[7], 0, sizeof(tokens[7]));
sprintf(tokens[7],"pts/%d",tty_nr-34816);
}
//else
else{
memset(tokens[7], 0, sizeof(tokens[7]));
strcpy(tokens[7], "?");
}
}
//u옵션 print 처리
void print_uOption()
{
long nice, num_thread, rss, vsz;
struct tm *tmbuf;
time_t t;
char path[MAX_INPUT_SIZE], *ptr;
char s_time[MAX_TOKEN_SIZE], c_time[MAX_TOKEN_SIZE];
char line[MAX_WIDTH];
int j, tmp, fd;
int cur;
nice = atol(tokens[19]);
//high-priority
if(nice < 0)
strcat(tokens[3], "<");
//low-priority
else if(nice > 0)
strcat(tokens[3], "N");
//session leader
if(!strcmp(tokens[1], tokens[6]))
strcat(tokens[3], "s");
num_thread = atol(tokens[20]);
//multithread
if(num_thread > 1)
strcat(tokens[3], "l");
//foreground
if(!strcmp(tokens[5], tokens[8]))
strcat(tokens[3], "+");
//set tty
set_tty_name();
//COMMAND
if((fd = open("cmdline", O_RDONLY)) < 0){
fprintf(stderr,"cmdline open error\n");
exit(1);
}
memset(path, 0, sizeof(path));
//임시로 cmdline이 1024자 이하라고 가정
j = read(fd, path, sizeof(path));
if(j != 0){
if(strlen(path) < j){
ptr = path;
while(ptr-path <= j &&(ptr = strchr(ptr, '\0')) != NULL)
*ptr++ = ' ';
path[j-1] = '\0';
}
}
else
sprintf(path,"[%s]",tokens[2]);
close(fd);
//set_info
set_info();
//start time
t = time(NULL);
//하루가 지난 경우
if(t - start_time >= 86400){
tmbuf = localtime(&start_time);
memset(s_time, 0, sizeof(s_time));
strftime(s_time, sizeof(s_time), "%b %d",tmbuf);
}
//24시간 이전
else{
tmbuf = localtime(&start_time);
memset(s_time, 0, sizeof(s_time));
strftime(s_time, sizeof(s_time), "%H:%M",tmbuf);
}
//TIME 값 저장
if(cpu_time >= 60){
memset(c_time, 0, sizeof(c_time));
sprintf(c_time,"%ld:%02ld",cpu_time/60, cpu_time%60);
}
else{
memset(c_time, 0, sizeof(c_time));
sprintf(c_time,"0:%02ld",cpu_time);
}
//vsz rss 값 연산
vsz = atol(tokens[23]) / 1024;
rss = atol(tokens[24]) * 4;
tmp = (int)(((double)rss / memtotal) * 1000);
//print
cur = 0;
memset(line, 0, sizeof(line));
sprintf(line,"%-8s %6s %.1f %.1f %6ld %5ld %s ",tokens[0], tokens[1], cpu_usage, tmp/10.0,vsz,rss, tokens[7]);
printf("%s",line);
//1차 print분기점
cur += strlen(line);
while(cur++ < 48)
printf(" ");
memset(line, 0, sizeof(line));
sprintf(line,"%-4s %s %6s ",tokens[3], s_time, c_time);
printf("%s",line);
cur += strlen(line);
//2차 print분기점
memset(line, 0, sizeof(line));
strncpy(line, path, MAX_WIDTH-cur+1);
printf("%s\n",line);
}
//u옵션이 없는경우 print처리
void print_not_uOption(){
long nice, num_thread;
char path[MAX_INPUT_SIZE], *ptr;
char c_time[MAX_TOKEN_SIZE], line[MAX_WIDTH];
int j, fd, cur;
nice = atol(tokens[19]);
//high-priority
if(nice < 0)
strcat(tokens[3], "<");
//low-priority
else if(nice > 0)
strcat(tokens[3], "N");
//session leader
if(!strcmp(tokens[1], tokens[6]))
strcat(tokens[3], "s");
num_thread = atol(tokens[20]);
//multithread
if(num_thread > 1)
strcat(tokens[3], "l");
//foreground
if(!strcmp(tokens[5], tokens[8]))
strcat(tokens[3], "+");
//set tty
set_tty_name();
//COMMAND
if((fd = open("cmdline", O_RDONLY)) < 0){
fprintf(stderr,"cmdline open error\n");
exit(1);
}
memset(path, 0, sizeof(path));
//임시로 cmdline이 1024자 이하라고 가정
j = read(fd, path, sizeof(path));
if(j != 0){
if(strlen(path) < j){
ptr = path;
while(ptr-path <= j &&(ptr = strchr(ptr, '\0')) != NULL)
*ptr++ = ' ';
path[j-1] = '\0';
}
}
else
sprintf(path,"[%s]",tokens[2]);
close(fd);
//set_info
set_info();
//TIME값 처리
if(cpu_time >= 60){
memset(c_time, 0, sizeof(c_time));
sprintf(c_time,"%ld:%02ld",cpu_time/60, cpu_time%60);
}
else{
memset(c_time, 0, sizeof(c_time));
sprintf(c_time,"0:%02ld",cpu_time);
}
//print
memset(line, 0, sizeof(line));
sprintf(line, "%5s %-8s %-6s %s ", tokens[1], tokens[7], tokens[3], c_time);
printf("%s",line);
cur = strlen(line);
//print분기점
memset(line, 0, sizeof(line));
strncpy(line, path, MAX_WIDTH - cur);
printf("%s\n",line);
}
//옵션이 없는경우 print처리
void print_no_option()
{
FILE* fp;
DIR* dirp;
struct dirent* dp;
char path[MAX_INPUT_SIZE], line[MAX_WIDTH];
char tty_num[MAX_TOKEN_SIZE],c_time[MAX_TOKEN_SIZE];
pid_t pid;
int i, j, c, cur;
pid = getpid();
memset(path, 0, sizeof(path));
sprintf(path, "/proc/%d/stat", pid);
//현재 pid의 stat파일 오픈
if((fp = fopen(path, "r")) == NULL){
fprintf(stderr,"%s open error\n",path);
exit(1);
}
//실행된pid의 터미널 정보가져옴
memset(tty_num, 0, sizeof(path));
j = 0;
for(i = 1; i <= 7; i++){
while((c = fgetc(fp)) != ' '){
if(c == '(')
while((c = fgetc(fp)) != ')');
else if(i == 7)
tty_num[j++] = c;
}
}
tty_num[j] = '\0';
fclose(fp);
//process stat parsing
if((dirp = opendir("/proc")) == NULL){
fprintf(stderr,"/proc open fail\n");
exit(1);
}
chdir("/proc");
while((dp = readdir(dirp)) != NULL){
memset(tokens, 0, sizeof(tokens));
if(isNum(dp->d_name)){
chdir(dp->d_name);
if((fp = fopen("stat", "r")) == NULL){
fprintf(stderr,"stat open error\n");
exit(1);
}
//stat의 24개 정보를 받아옴
for(i = 1; i < 25; i++){
j = 0;
while((c = fgetc(fp)) != ' '){
if(c == '('){
while((c = fgetc(fp)) != ')')
tokens[i][j++] = c;
}
else
tokens[i][j++] = c;
}
tokens[i][j] = '\0';
}
//같은 터미널이 아니면
if(strcmp(tokens[7], tty_num) != 0){
chdir("..");
fclose(fp);
continue;
}
//set tty
set_tty_name();
//set_info
set_info();
//TIME 연산처리
if(cpu_time >= 3600){
memset(c_time, 0, sizeof(c_time));
sprintf(c_time,"%02ld:%02ld:%02ld",cpu_time/3600, (cpu_time%3600)/60, cpu_time%60);
}
else if(cpu_time >= 60){
memset(c_time, 0, sizeof(c_time));
sprintf(c_time,"00:%02ld:%02ld",cpu_time/60, cpu_time%60);
}
else
sprintf(c_time,"00:00:%02ld",cpu_time%60);
//print
memset(line, 0, sizeof(line));
sprintf(line, "%5s %-8s %s ",tokens[1], tokens[7], c_time);
printf("%s",line);
cur = strlen(line);
//print분기점
memset(line, 0, sizeof(line));
strncpy(line, tokens[2], MAX_WIDTH-cur);
printf("%s\n",line);
fclose(fp);
chdir("..");
}
}
}