-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinfo.c
More file actions
76 lines (72 loc) · 1.64 KB
/
pinfo.c
File metadata and controls
76 lines (72 loc) · 1.64 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
#include "pinfo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
pid_t str_to_pid(char *str_pid) {
pid_t pid = 0;
for (int i = 0; i < strlen(str_pid); i++) {
pid *= 10;
pid += str_pid[i] - '0';
}
return pid;
}
int get_state(pid_t pid, char *store) {
char buff[256];
sprintf(buff, "/proc/%d/stat", pid);
FILE *stat = fopen(buff, "r");
if (stat == NULL) {
return -1;
}
char *tp = (char *)(malloc(1000));
for (int i = 1; i <= 23; i++) {
fscanf(stat, "%s", tp);
if (i == 3) {
strcpy(store, tp);
}
}
free(tp);
fclose(stat);
return 0;
}
void pid_info(pid_t pid) {
char state[5];
char vsize[100];
char buff[256];
sprintf(buff, "/proc/%d/stat", pid);
FILE *stat = fopen(buff, "r");
if (stat == NULL) {
printf("Process not found\n");
return;
}
char *tp = (char *)(malloc(1000));
for (int i = 1; i <= 23; i++) {
fscanf(stat, "%s", tp);
if (i == 3) {
strcpy(state, tp);
}
if (i == 23) {
strcpy(vsize, tp);
}
}
free(tp);
fclose(stat);
printf("pid -- %d\n", pid); // pid
printf("Process Status -- %s\n", state); // state
printf("memory -- %s\n", vsize); // vsize
// Retrieve Executable
sprintf(buff, "/proc/%d/exe", pid);
char location[256];
int ret = readlink(buff, location, 256);
location[ret] = '\0';
printf("Executable Path -- %s\n", location);
}
void pinfo(int argc, char *const *argv) {
if (argc == 1)
pid_info(getpid());
else if (argc == 2)
pid_info(str_to_pid(argv[1]));
else
fprintf(stderr, "Error, pinfo takes only one argument pid\n");
}