3 char *ProcFileSystem::file_get_contents(
const char *filename,
size_t & size)
7 size_t data_size = 1024;
9 if ((file = fopen(filename,
"rb")) != NULL)
11 data = (
char *) malloc(data_size);
18 size += fread(data + size, 1, 512, file);
19 if (size + 512 > data_size)
22 data = (
char *) realloc(data, data_size);
39 return file_get_contents(
"/proc/loadavg", size);
45 unsigned long long total_mem = (
unsigned long long)(-1);
48 data = file_get_contents(
"/proc/meminfo", size);
50 return (
unsigned long long)(-1);
51 pos = fast_str_pos(data,
"MemTotal:");
55 for (; pos < (int) size && (data[pos] ==
' ' || data[pos] ==
'\t'); pos++);
56 int numeric_start = pos;
57 for (; data[pos] >=
'0' && data[pos] <=
'9' && pos < (int) size; pos++);
59 total_mem = atoll(data + numeric_start);
68 unsigned long long total_mem = (
unsigned long long)(-1);
71 data = file_get_contents(
"/proc/self/status", size);
73 return (
unsigned long long)(-1);
74 pos = fast_str_pos(data,
"VmSize:");
78 for (; pos < (int) size && (data[pos] ==
' ' || data[pos] ==
'\t'); pos++);
79 int numeric_start = pos;
80 for (; data[pos] >=
'0' && data[pos] <=
'9' && pos < (int) size; pos++);
82 total_mem = atoll(data + numeric_start);
91 unsigned long long total_mem = (
unsigned long long)(-1);
94 data = file_get_contents(
"/proc/meminfo", size);
96 return (
unsigned long long)(-1);
97 pos = fast_str_pos(data,
"MemFree:");
101 for (; pos < (int) size && (data[pos] ==
' ' || data[pos] ==
'\t'); pos++);
102 int numeric_start = pos;
103 for (; data[pos] >=
'0' && data[pos] <=
'9' && pos < (int) size; pos++);
104 total_mem = atoll(data + numeric_start);
106 pos = fast_str_pos(data,
"Cached:");
110 for (; pos < (int) size && (data[pos] ==
' ' || data[pos] ==
'\t'); pos++);
111 int numeric_start = pos;
112 for (; data[pos] >=
'0' && data[pos] <=
'9' && pos < (int) size; pos++);
114 total_mem += atoll(data + numeric_start);
116 pos = fast_str_pos(data,
"Buffers:");
120 for (; pos < (int) size && (data[pos] ==
' ' || data[pos] ==
'\t'); pos++);
121 int numeric_start = pos;
122 for (; data[pos] >=
'0' && data[pos] <=
'9' && pos < (int) size; pos++);
124 total_mem += atoll(data + numeric_start);
134 char *data = file_get_contents(
"/proc/self/stat", size);
136 return (
unsigned long)(-1);
140 int ppid, pgrp, session, tty_nr, tpgid;
142 unsigned long minflt, cminflt, majflt, cmajflt, utime, stime;
143 sscanf(data,
"%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &pid, com, &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime);
151 char *data = file_get_contents(
"/proc/self/stat", size);
153 return (
unsigned long)(-1);
157 int ppid, pgrp, session, tty_nr, tpgid;
159 unsigned long minflt, cminflt, majflt, cmajflt, utime, stime;
160 sscanf(data,
"%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &pid, com, &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime);
168 long long uptime = 0;
171 data = file_get_contents(
"/proc/uptime", size);
174 for (; data[pos] >=
'0' && data[pos] <=
'9' && pos < size; pos++);
176 uptime = atoll(data);
184 return file_get_contents(
"/proc/sys/kernel/hostname", size);
191 file = fopen(
"/proc/sys/kernel/hostname",
"wb");
194 size = fwrite(hostname, 1, strlen(hostname), file);
204 long long shmall = 0;
205 data = file_get_contents(
"/proc/sys/kernel/shmall", size);
208 shmall = atoll(data);
219 file = fopen(
"/proc/sys/kernel/shmall",
"wb");
222 sprintf(body,
"%llu", shmall);
223 size = fwrite(body, 1, strlen(body), file);
233 long long shmall = 0;
234 data = file_get_contents(
"/proc/sys/kernel/shmmax", size);
237 shmall = atoll(data);
248 file = fopen(
"/proc/sys/kernel/shmmax",
"wb");
251 sprintf(body,
"%llu", shmmax);
252 size = fwrite(body, 1, strlen(body), file);
258 int ProcFileSystem::fast_str_pos(
const char *str,
const char *pattern)
263 int slen = strlen(str);
267 plen = strlen(pattern);
268 for (c = 0; c < 256; c++)
270 for (c = 0; c < plen - 1; c++)
271 d[pattern[c] & 0377] = plen - 1 - c;
272 for (endpos = plen - 1; endpos < slen;)
274 for (cmppos = endpos, pindx = (plen - 1); pindx >= 0; cmppos--, pindx--)
275 if (str[cmppos] != pattern[pindx])
277 endpos += d[str[endpos] & 0377];
281 return endpos - (plen - 1);
289 unsigned long long total_mem = (
unsigned long long)(-1);
292 data = file_get_contents(
"/proc/self/status", size);
294 return (
unsigned long long)(-1);
295 pos = fast_str_pos(data,
"VmRSS:");
299 for (; pos < (int) size && (data[pos] ==
' ' || data[pos] ==
'\t'); pos++);
300 int numeric_start = pos;
301 for (; data[pos] >=
'0' && data[pos] <=
'9' && pos < (int) size; pos++);
303 total_mem = atoll(data + numeric_start);
313 if(total > vm)
return total - vm;