-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinfo.c
More file actions
67 lines (58 loc) · 1.48 KB
/
Copy pathpinfo.c
File metadata and controls
67 lines (58 loc) · 1.48 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
#include "header.h"
ll PINFO(char **com)
{
ll pid;
if (com[1] == NULL)
pid = getpid();
else
pid = atoi(com[1]);
char *status_path = malloc(1024*sizeof(char));
sprintf(status_path,"/proc/%lld/status",pid);
FILE *fp = fopen(status_path,"r");
if (fp==NULL)
{
fprintf(stderr,"Process with given pid %lld not found\n",pid);
free(status_path);
return 1;
}
char status[2048], memory[2048], path[2048];
ll c=1;
char line[1024];
while (fgets(line,sizeof(line),fp)!=NULL)
{
if (c==3)
{
char **tokens=filter_token(line);
strcpy(status,tokens[1]);
}
if (c==18)
{
char **tokens=filter_token(line);
if(tokens[2]== NULL)
strcpy(memory,tokens[1]);
else
sprintf(memory,"%s %s",tokens[1],tokens[2]); // token[2] = kB
}
c++;
}
printf("pid -- %lld\nProcess Status -- %s\nVirtual memory -- %s\n",pid,status,memory);
char exe_path[2048], exec[2048];
sprintf(exe_path,"/proc/%lld/exe",pid);
ll ret = readlink(exe_path, exec, 1024);
if (ret < 0)
{
perror("readlink failed");
fclose(fp);
free(status_path);
return 1;
}
else
{
exec[ret]='\0';
char *rel_exec = relative_path(exec);
printf("Executable path -- %s\n",rel_exec);
fclose(fp);
free(status_path);
return 0;
}
}