-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproclore.c
138 lines (121 loc) · 3.02 KB
/
proclore.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
#include "headers.h"
int proclore(char commands[1000][1000])
{
int error = 0;
pid_t pid;
if (strcmp(commands[1], "NULL") == 0)
pid = getpid();
else
pid = atoi(commands[1]);
printf("pid : %d\n", pid);
error += print_state(pid);
if (error == 1)
return 1;
error += print_pgid(pid);
if (error == 1)
return 1;
error += Virtual_Memory(pid);
if (error == 1)
return 1;
char path[1024];
snprintf(path, sizeof(path), "/proc/%d/exe", pid);
char buffer[1024];
ssize_t len = readlink(path, buffer, sizeof(buffer) - 1);
if (len != -1)
{
buffer[len] = '\0';
printf("Executable path : %s\n", buffer);
}
else
{
fprintf(stderr, "\x1b[31mError reading process path\x1b[0m\n");
return 1;
}
return error;
}
int Virtual_Memory(int pid)
{
char status_path[50];
snprintf(status_path, sizeof(status_path), STATUS_FILE_PATH, pid);
FILE *status_file = fopen(status_path, "r");
if (status_file == NULL)
{
fprintf(stderr, "\x1b[31mError opening status file\x1b[0m\n");
return 1;
}
int i = 0;
char *vm = (char *)malloc(sizeof(char) * 256);
char word[256] = {'\0'};
while (fgets(word, sizeof(word), status_file))
{
i++;
if (i == 18) // on line 18 vm
{
strcpy(vm, word);
break;
}
}
i = 0;
char *temp = strtok_r(vm, " \t", &vm);
while (i < 2)
{
strcpy(word, temp);
temp = strtok_r(NULL, " \t", &vm);
i++;
}
printf("Virtual memory: %s\n", word);
fclose(status_file);
}
int print_pgid(int pid)
{
char stat_path[50];
snprintf(stat_path, sizeof(stat_path), STAT_FILE_PATH, pid);
FILE *stat_file = fopen(stat_path, "r");
if (stat_file == NULL)
{
fprintf(stderr, "\x1b[31mError opening stat file\x1b[0m\n");
return 1;
}
int i = 0;
char pgrp_read[256];
char word[256] = {'\0'};
while (fscanf(stat_file, "%s", word) != EOF) // on line 5 pgid
{
i++;
if (i == 4)
{
strcpy(pgrp_read, word);
break;
}
}
printf("Process Group : %s\n", pgrp_read);
return 0;
}
int print_state(int pid)
{
char status_path[50];
snprintf(status_path, sizeof(status_path), STATUS_FILE_PATH, pid);
pid_t pgid = tcgetpgrp(STDIN_FILENO);
FILE *status_file = fopen(status_path, "r");
if (status_file == NULL)
{
fprintf(stderr, "\x1b[31mError opening status file or invalid pid\x1b[0m\n");
return 1;
}
char line[256];
while (fgets(line, sizeof(line), status_file) != NULL)
{
if (strncmp(line, "State:", 6) == 0)
{
char temp[100];
strcpy(temp, line + 7);
if (pid == pgid)
printf("process Status : %c+\n", temp[0]);
else
printf("process Status : %c\n", temp[0]);
break;
}
}
fclose(status_file);
return 0;
}