-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfo.c
97 lines (71 loc) · 1.61 KB
/
pinfo.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
#include "pinfo.h"
#include "prompt.h"
#include "headers.h"
int join_open(char * dir, char * file)
{
char * processFile = (char*) malloc(1024);
processFile[0] = 0;
strcat(processFile, dir);
strcat(processFile, file);
int fd = open(processFile, O_RDONLY);
free(processFile);
return fd;
}
void pinfo(char **parsed)
{
if(parsed[2] != NULL)
{
printf("pinfo: too many arguments!\n");
}
else if(parsed[1] == NULL)
{
pid_info("self");
}
else
{
pid_info(parsed[1]);
}
}
void pid_info(char *pid)
{
char *processDir = (char*) malloc(1024);
char *buffer = (char*) malloc(1024);
int fd;
sprintf(processDir, "/proc/%s/", pid);
// Process State
fd = join_open(processDir, "stat");
read(fd, buffer, 1024);
if(fd < 0)
{
printf("Process with pid %s not found\n", pid);
return;
}
if(!strcmp(pid, "self"))
{
printf("pid -- %d\n", getpid());
}
else
{
printf("pid -- %s\n", pid);
}
strtok(buffer, " ");
strtok(NULL, " ");
char * status = strtok(NULL, " ");
printf("Process status: %s\n", status);
close(fd);
// Memory
fd = join_open(processDir, "statm");
read(fd, buffer, 1024);
char * memory = strtok(buffer, " ");
printf("Memory -- %s\n", memory);
close(fd);
// Executable Path
strcat(processDir, "exe");
int l = readlink(processDir, buffer, 1024);
buffer[l] = 0;
char * path = tilda(buffer);
printf("Executable path -- %s\n", path);
free(path);
free(buffer);
free(processDir);
}