-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinfo.c
More file actions
77 lines (59 loc) · 1.65 KB
/
Copy pathpinfo.c
File metadata and controls
77 lines (59 loc) · 1.65 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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
void exec_pinfo(char * pid) {
char path[1024], path_2[1024], buf[1024], * temp, field[1024];
for(int i=0; i<1024; i++) {
buf[i] = 0; field[i] = 0;
} // since read, readlink wont null terminate
strcpy(path, "/proc/");
strcat(path, pid);
/*
if(kill(atoi(pid), 0) < 0)
{
printf("No process with pid %s exists\n", pid);
return;
}*/
// pid
printf("pid -- %s\n", pid);
// process status and memory
strcpy(path_2, path);
strcat(path_2, "/stat");
// open /proc/pid/stat
int fd = open(path_2, O_RDONLY);
if(fd == -1) {
perror("");
return;
}
if( read(fd, buf, 1023) < 0) {
perror("");
close(fd);
return;
}
temp = buf;
// get the 3rd field : status
for(int i=0; i<3; i++)
strcpy(field, strtok_r(temp , " ", &temp));
if(field != NULL) printf("Process Status -- %s\n", field);
// get the 23rd = 3 + 20 th field : virtual memory
for(int i=0; i<20; i++)
strcpy(field, strtok_r(temp , " ", &temp));
if(buf != NULL) printf("Memory -- %s\n", field);
close(fd);
// path to the executable : readlink to read the symbolic link /proc/pid/exe
strcpy(path_2, path);
strcat(path_2, "/exe");
for(int i=0; i<1024; i++) {
buf[i] = 0;
} // since readlink wont null terminate
if( (readlink(path_2, buf, 1023)) < 0 ) {
perror("");
return;
}
printf("Exeecutable Path -- %s\n", buf);
}