-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_pinfo.cpp
More file actions
119 lines (106 loc) · 3.67 KB
/
5_pinfo.cpp
File metadata and controls
119 lines (106 loc) · 3.67 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
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
115
116
117
118
119
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <unistd.h>
// Function to get process info for the current process (shell)
void get_shell_process_info() {
pid_t pid = getpid();
std::cout << "pid -- " << pid << std::endl;
char status;
std::ifstream status_file("/proc/" + std::to_string(pid) + "/status");
if (status_file.is_open() && status_file >> status) {
std::string status_code;
switch (status) {
case 'R':
status_code = "R";
break;
case 'S':
status_code = "S";
break;
case 'Z':
status_code = "Z";
break;
case 'T':
status_code = "T";
break;
default:
status_code = "?";
}
std::cout << "Process Status -- {" << status_code;
// Check if the process is in the foreground
if (tcgetpgrp(STDIN_FILENO) == pid) {
std::cout << "+";
}
std::cout << "}" << std::endl;
}
std::ifstream statm_file("/proc/" + std::to_string(pid) + "/statm");
unsigned long virtual_memory;
if (statm_file.is_open() && statm_file >> virtual_memory) {
std::cout << "memory -- " << virtual_memory << " {Virtual Memory}" << std::endl;
}
char exe_path[1024];
ssize_t path_length = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
if (path_length != -1) {
exe_path[path_length] = '\0';
std::cout << "Executable Path -- " << exe_path << std::endl;
}
}
// Function to get process info for a specified PID
void get_process_info(pid_t pid) {
std::cout << "pid -- " << pid << std::endl;
// Open the status file for the specified PID
std::ifstream status_file("/proc/" + std::to_string(pid) + "/status");
if (!status_file.is_open()) {
std::cerr << "Error: Process with PID " << pid << " not found." << std::endl;
return;
}
char status;
if (status_file >> status) {
std::string status_code;
switch (status) {
case 'R':
status_code = "R";
break;
case 'S':
status_code = "S";
break;
case 'Z':
status_code = "Z";
break;
case 'T':
status_code = "T";
break;
default:
status_code = "?";
}
std::cout << "Process Status -- {" << status_code << "}" << std::endl;
}
// Get virtual memory size from statm file
std::ifstream statm_file("/proc/" + std::to_string(pid) + "/statm");
unsigned long virtual_memory;
if (statm_file.is_open() && statm_file >> virtual_memory) {
std::cout << "memory -- " << virtual_memory << " {Virtual Memory}" << std::endl;
}
// Get executable path using /proc/<PID>/exe symlink
char exe_path[1024];
ssize_t path_length = readlink(("/proc/" + std::to_string(pid) + "/exe").c_str(), exe_path, sizeof(exe_path));
if (path_length != -1) {
exe_path[path_length] = '\0';
std::cout << "Executable Path -- " << exe_path << std::endl;
}
}
// int main(int argc, char* argv[]) {
// if (argc == 1) {
// // No PID provided, get info for the current shell process
// get_shell_process_info();
// } else if (argc == 2) {
// // PID provided as an argument, get info for the specified PID
// pid_t pid = std::stoi(argv[1]);
// get_process_info(pid);
// } else {
// std::cerr << "Usage: " << argv[0] << " [pid]" << std::endl;
// return 1;
// }
// return 0;
// }