I've recently tried to write a program that monitors activity on filesystem. I'd like it to record action, PID, executable name and EUID. I've used fanotify
for this purpose and it can retrieve everything except EUID. So far I've been able to fetch user using following code:
#include <proc/readproc.h>
struct passwd* get_user_from_pid(pid_t pid)
{
proc = openproc(PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS| PROC_FILLUSR | PROC_PID, &pid);
memset(&proc_info, 0, sizeof(proc_info));
if (readproc(proc, &proc_info) == NULL)
return NULL;
struct passwd* user_info = getpwnam(proc_info.euser);
closeproc(proc);
return user_info;
}
The problem is that as far as I know underneath it just reads files from /proc/<pid> which is ofcourse slow when dealing with huge amount of I/O traffic, almost always keeping CPU at 100% utilization.
Is there a cheaper way to achieve the same?