-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproclore.c
114 lines (100 loc) · 2.62 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
#include "headers.h"
void process_details(pid_t pid)
{
char *filename = (char*)malloc(sizeof(char) * N);
char *line = NULL;
size_t len = 0;
ssize_t read;
char status[100] = {0}; // Initialize to ensure null-termination
char *tg_id;
pid_t pgid;
if (pid == 0)
{
pid = getpid();
}
pgid = getpgid(pid);
printf("PID: %d\n", pid);
printf("PGID: %d\n", pgid);
sprintf(filename, "/proc/%d/status", pid);
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
perror("Process Status file open error");
free(filename);
return;
}
int line_number = 0;
while ((read = getline(&line, &len, fp)) != -1)
{
if (line_number == 2)
{
char *state = strchr(line, '\t');
if (state != NULL)
{
state = strtok(state, " \t\n");
strncpy(status, state, sizeof(status) - 1);
status[sizeof(status) - 1] = '\0';
}
}
else if (line_number == 3)
{
char *tgid_str = strchr(line, '\t');
if (tgid_str != NULL)
{
tgid_str = strtok(tgid_str, " \t\n");
tg_id = tgid_str;
int tgid = atoi(tg_id);
if (tgid == pgid) // Foreground process
{
strncat(status, "+", sizeof(status) - strlen(status) - 1);
}
printf("TGID: %s\n", tg_id);
printf("Status: %s\n", status);
}
}
else if (line_number == 17)
{
char *virt_mem = strchr(line, '\t');
if (virt_mem != NULL)
{
virt_mem = strtok(virt_mem, " \t\n");
printf("Virtual Memory: %s kB\n", virt_mem);
}
}
line_number++;
}
free(line);
fclose(fp);
sprintf(filename, "/proc/%d/exe", pid);
char exec[N];
int index;
if((index = readlink(filename, exec, N)) < 0)
perror("Executable doesnt exist");
exec[index] = '\0';
printf("Executable path: %s\n", exec);
free(filename);
return;
}
void proclore(char* command)
{
char *dup = strdup(command);
char *tokens[3];
char *tok = strtok(dup, " \t");
int idx = 0;
while (tok != NULL && idx < 2)
{
tokens[idx++] = strdup(tok);
tok = strtok(NULL, " \t");
}
tokens[idx] = NULL;
if (idx == 1)
{
process_details(0);
}
else if (idx == 2)
{
process_details(atoi(tokens[1]));
}
free(dup); // Free the duplicated string
return;
}