1 | |
---|
2 | #include <lsf-1.0/system.hpp> |
---|
3 | #include <lsf-1.0/common.hpp> |
---|
4 | |
---|
5 | #include <unistd.h> |
---|
6 | #include <dirent.h> |
---|
7 | #include <sys/types.h> |
---|
8 | #include <sys/stat.h> |
---|
9 | |
---|
10 | #include <vector> |
---|
11 | #include <string> |
---|
12 | #include <iostream> |
---|
13 | #include <cstdlib> |
---|
14 | #include <sstream> |
---|
15 | #include <fstream> |
---|
16 | |
---|
17 | using namespace std; |
---|
18 | using namespace net::lliurex; |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | bool isNumeric(const char * name) |
---|
23 | { |
---|
24 | int n = 0; |
---|
25 | bool is_num = true; |
---|
26 | |
---|
27 | while(name[n]!='\0') |
---|
28 | { |
---|
29 | if(name[n]<'0' || name[n]>'9') |
---|
30 | { |
---|
31 | is_num=false; |
---|
32 | break; |
---|
33 | } |
---|
34 | n++; |
---|
35 | } |
---|
36 | |
---|
37 | return is_num; |
---|
38 | } |
---|
39 | |
---|
40 | vector<unsigned int> net::lliurex::system::GetProcessList() |
---|
41 | { |
---|
42 | vector<unsigned int> plist; |
---|
43 | |
---|
44 | DIR* dir_proc = NULL ; |
---|
45 | struct dirent* dir_entity; |
---|
46 | |
---|
47 | /* Yeah, I know, the path is hardcoded, feel free to change it, if you wish*/ |
---|
48 | dir_proc = opendir("/proc/") ; |
---|
49 | |
---|
50 | if (dir_proc == NULL) |
---|
51 | { |
---|
52 | throw Exception("Failed to access /proc/"); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | while((dir_entity = readdir(dir_proc))) |
---|
57 | { |
---|
58 | if (dir_entity->d_type == DT_DIR) |
---|
59 | { |
---|
60 | if(isNumeric(dir_entity->d_name)) |
---|
61 | { |
---|
62 | plist.push_back(atoi(dir_entity->d_name)); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | return plist; |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | system::ProcessInfo net::lliurex::system::GetProcessInfo(unsigned int pid) |
---|
73 | { |
---|
74 | system::ProcessInfo pinfo; |
---|
75 | stringstream ss; |
---|
76 | ifstream * ifile; |
---|
77 | char data[256]; |
---|
78 | |
---|
79 | ss<<"/proc/"<<pid<<"/cmdline"; |
---|
80 | |
---|
81 | ifile = new ifstream(); |
---|
82 | ifile->open(ss.str().c_str()); |
---|
83 | ifile->getline(data,256); |
---|
84 | ifile->close(); |
---|
85 | delete ifile; |
---|
86 | |
---|
87 | pinfo.cmdline=string(data); |
---|
88 | |
---|
89 | ss.str(""); |
---|
90 | ss<<"/proc/"<<pid<<"/comm"; |
---|
91 | |
---|
92 | ifile = new ifstream(); |
---|
93 | ifile->open(ss.str().c_str()); |
---|
94 | ifile->getline(data,256); |
---|
95 | ifile->close(); |
---|
96 | delete ifile; |
---|
97 | |
---|
98 | pinfo.comm=string(data); |
---|
99 | |
---|
100 | return pinfo; |
---|
101 | } |
---|
102 | |
---|
103 | std::vector<std::string> net::lliurex::system::GetModuleList() |
---|
104 | { |
---|
105 | std::vector<std::string> mlist; |
---|
106 | ifstream ifile; |
---|
107 | stringstream ss; |
---|
108 | string str; |
---|
109 | char data[256]; |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | ifile.open("/proc/modules"); |
---|
114 | while(true) |
---|
115 | { |
---|
116 | ifile.getline(data,256); |
---|
117 | if(ifile.eof())break; |
---|
118 | |
---|
119 | ss.str(data); |
---|
120 | ss>>str; |
---|
121 | mlist.push_back(str); |
---|
122 | |
---|
123 | } |
---|
124 | ifile.close(); |
---|
125 | |
---|
126 | return mlist; |
---|
127 | } |
---|
128 | |
---|
129 | std::vector<system::MountInfo> net::lliurex::system::GetMountList() |
---|
130 | { |
---|
131 | std::vector<system::MountInfo> mlist; |
---|
132 | ifstream ifile; |
---|
133 | stringstream ss; |
---|
134 | char data[256]; |
---|
135 | system::MountInfo minfo; |
---|
136 | string str; |
---|
137 | |
---|
138 | |
---|
139 | ifile.open("/proc/mounts"); |
---|
140 | while(true) |
---|
141 | { |
---|
142 | |
---|
143 | ifile>>minfo.device; |
---|
144 | ifile>>minfo.mount_point; |
---|
145 | ifile>>minfo.type; |
---|
146 | ifile>>minfo.options; |
---|
147 | |
---|
148 | ifile>>str; |
---|
149 | minfo.dumb=(str=="0") ? false : true; |
---|
150 | |
---|
151 | ifile>>str; |
---|
152 | minfo.pass=(str=="0") ? false : true; |
---|
153 | |
---|
154 | if(ifile.eof())break; |
---|
155 | |
---|
156 | mlist.push_back(minfo); |
---|
157 | } |
---|
158 | |
---|
159 | ifile.close(); |
---|
160 | |
---|
161 | return mlist; |
---|
162 | } |
---|
163 | |
---|
164 | long net::lliurex::system::GetSystemMemory() |
---|
165 | { |
---|
166 | long pages = sysconf(_SC_PHYS_PAGES); |
---|
167 | long page_size = sysconf(_SC_PAGE_SIZE); |
---|
168 | return pages * page_size; |
---|
169 | } |
---|
170 | |
---|
171 | long net::lliurex::system::GetFreeSystemMemory() |
---|
172 | { |
---|
173 | long pages = sysconf(_SC_AVPHYS_PAGES); |
---|
174 | long page_size = sysconf(_SC_PAGE_SIZE); |
---|
175 | return pages * page_size; |
---|
176 | } |
---|
177 | |
---|