2023-09-30 21:20:53 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2023-11-07 02:41:01 +02:00
|
|
|
#include <errno.h>
|
2023-09-30 21:20:53 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/banan-os.h>
|
|
|
|
|
|
|
|
bool is_only_digits(const char* str)
|
|
|
|
{
|
|
|
|
while (*str)
|
|
|
|
if (!isdigit(*str++))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
DIR* proc = opendir("/proc");
|
|
|
|
if (proc == nullptr)
|
|
|
|
{
|
|
|
|
perror("opendir");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char path_buffer[128] {};
|
|
|
|
while (dirent* proc_ent = readdir(proc))
|
|
|
|
{
|
|
|
|
if (proc_ent->d_type != DT_DIR)
|
|
|
|
continue;
|
|
|
|
if (!is_only_digits(proc_ent->d_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
{
|
2023-09-30 23:17:31 +03:00
|
|
|
strcpy(path_buffer, proc_ent->d_name);
|
|
|
|
strcat(path_buffer, "/cmdline");
|
|
|
|
|
|
|
|
int fd = openat(dirfd(proc), path_buffer, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2023-11-07 02:41:01 +02:00
|
|
|
if (errno != EACCES)
|
|
|
|
perror("openat");
|
2023-09-30 23:17:31 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-07 02:41:01 +02:00
|
|
|
printf("process: ");
|
|
|
|
|
2023-09-30 23:17:31 +03:00
|
|
|
while (ssize_t nread = read(fd, path_buffer, sizeof(path_buffer) - 1))
|
|
|
|
{
|
2023-10-24 16:49:48 +03:00
|
|
|
if (nread < 0)
|
2023-09-30 23:17:31 +03:00
|
|
|
{
|
|
|
|
perror("read");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < nread; i++)
|
|
|
|
if (path_buffer[i] == '\0')
|
|
|
|
path_buffer[i] = ' ';
|
|
|
|
|
|
|
|
path_buffer[nread] = '\0';
|
|
|
|
|
|
|
|
int written = 0;
|
|
|
|
while (written < nread)
|
|
|
|
written += printf("%s ", path_buffer + written);
|
|
|
|
}
|
|
|
|
|
2023-11-07 02:41:01 +02:00
|
|
|
printf("\n");
|
|
|
|
|
2023-09-30 23:17:31 +03:00
|
|
|
close(fd);
|
2023-09-30 21:20:53 +03:00
|
|
|
}
|
|
|
|
|
2023-11-07 02:41:01 +02:00
|
|
|
printf(" pid: %s\n", proc_ent->d_name);
|
2023-09-30 23:17:31 +03:00
|
|
|
|
2023-09-30 21:20:53 +03:00
|
|
|
{
|
2023-09-30 23:17:31 +03:00
|
|
|
strcpy(path_buffer, proc_ent->d_name);
|
|
|
|
strcat(path_buffer, "/meminfo");
|
|
|
|
|
|
|
|
int fd = openat(dirfd(proc), path_buffer, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
perror("openat");
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-30 21:20:53 +03:00
|
|
|
|
2023-09-30 23:17:31 +03:00
|
|
|
proc_meminfo_t meminfo;
|
|
|
|
if (read(fd, &meminfo, sizeof(meminfo)) == -1)
|
|
|
|
perror("read");
|
|
|
|
else
|
|
|
|
{
|
2023-10-03 10:38:30 +03:00
|
|
|
size_t percent_times_100 = 10000 * meminfo.phys_pages / meminfo.virt_pages;
|
2023-09-30 23:17:31 +03:00
|
|
|
printf(" vmem: %zu pages (%zu bytes)\n", meminfo.virt_pages, meminfo.page_size * meminfo.virt_pages);
|
2023-10-03 10:38:30 +03:00
|
|
|
printf(" pmem: %zu pages (%zu bytes) %zu.%02zu%%\n", meminfo.phys_pages, meminfo.page_size * meminfo.phys_pages, percent_times_100 / 100, percent_times_100 % 100);
|
2023-09-30 23:17:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
2023-09-30 21:20:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(proc);
|
|
|
|
}
|