33
44#include "../headers.h"
55#include "../data_types.h"
6+ #include "../utils/utils.h"
67
78static __always_inline int trace_file_operation (struct pt_regs * ctx , struct file * file , const char * operation )
89{
910 if (!file )
1011 return 0 ;
1112
13+ // Declare the ring buffer for events
1214 struct data_t * data = bpf_ringbuf_reserve (& events , sizeof (struct data_t ), 0 );
1315 if (!data )
1416 {
1517 return 0 ;
1618 }
1719 __builtin_memset (data , 0 , sizeof (* data ));
1820
21+ // Check if the file pointer is valid, get the dentry from the file structure
1922 struct dentry * de = NULL ;
2023 bpf_core_read (& de , sizeof (de ), & file -> f_path .dentry );
2124 if (!de )
@@ -24,6 +27,7 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
2427 return 0 ;
2528 }
2629
30+ // Get filename
2731 struct qstr d_name = {};
2832 bpf_core_read (& d_name , sizeof (d_name ), & de -> d_name );
2933 if (d_name .len == 0 )
@@ -32,17 +36,25 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
3236 return 0 ;
3337 }
3438
35- char fname [FILE_NAME_SIZE ] = {};
36- bpf_core_read_str (fname , sizeof (fname ), d_name .name );
37-
38- data -> pid = bpf_get_current_pid_tgid () >> 32 ;
39- data -> uid = bpf_get_current_uid_gid ();
40- data -> timestamp = bpf_ktime_get_ns ();
39+ // Get parent dentry
40+ struct dentry * parent_de = NULL ;
41+ bpf_core_read (& parent_de , sizeof (parent_de ), & de -> d_parent );
42+ if (!parent_de )
43+ {
44+ bpf_ringbuf_discard (data , 0 );
45+ return 0 ;
46+ }
4147
42- __builtin_memcpy (data -> filename , fname , sizeof (data -> filename ));
43- __builtin_memcpy (data -> otype , operation , sizeof (data -> otype ));
44- bpf_get_current_comm (& data -> comm , sizeof (data -> comm ));
48+ // Get parent filename
49+ struct qstr parent_d_name = {};
50+ bpf_core_read (& parent_d_name , sizeof (parent_d_name ), & parent_de -> d_name );
51+ if (parent_d_name .len == 0 )
52+ {
53+ bpf_ringbuf_discard (data , 0 );
54+ return 0 ;
55+ }
4556
57+ // Get Inode from file structure
4658 struct inode * inode_ptr = NULL ;
4759 bpf_core_read (& inode_ptr , sizeof (inode_ptr ), & file -> f_inode );
4860 if (!inode_ptr )
@@ -51,23 +63,24 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
5163 return 0 ;
5264 }
5365
54- u32 inode_num = 0 ;
55- bpf_core_read ( & inode_num , sizeof ( inode_num ), & inode_ptr -> i_ino );
66+ // Get the current cgroup ID
67+ int cgroup_id = bpf_get_current_cgroup_id ( );
5668
57- struct inode_key key = {.inode = inode_num };
69+ // Copy the data into the event structure
70+ data -> pid = bpf_get_current_pid_tgid () >> 32 ;
71+ data -> uid = bpf_get_current_uid_gid ();
72+ data -> timestamp = bpf_ktime_get_ns ();
73+ data -> cgroup_id = cgroup_id ;
5874
59- bpf_trace_printk ("LOG: filename=%s otype=%s comm=%s\n" , sizeof ("LOG: filename=%s otype=%s comm=%s\n" ), data -> filename , data -> otype , data -> comm );
60- bpf_trace_printk ("LOG: inode=%u\n" , sizeof ("LOG: inode=%u\n" ), inode_num );
75+ bpf_core_read_str (data -> filename , sizeof (data -> filename ), d_name .name );
76+ bpf_core_read_str (data -> parent_filename , sizeof (data -> parent_filename ), parent_d_name .name );
77+ bpf_core_read (& data -> inode , sizeof (data -> inode ), & inode_ptr -> i_ino );
78+ __builtin_memcpy (data -> otype , operation , sizeof (data -> otype ));
6179
62- u32 * monitored = bpf_map_lookup_elem (& monitored_inodes , & key );
63- if (!monitored )
64- {
65- bpf_ringbuf_discard (data , 0 );
66- return 0 ;
67- }
80+ bpf_get_current_comm (& data -> comm , sizeof (data -> comm ));
6881
69- int cgroup_id = bpf_get_current_cgroup_id ( );
70- data -> cgroup_id = cgroup_id ;
82+ bpf_trace_printk ( "LOG: filename=%s otype=%s comm=%s\n" , sizeof ( "LOG: filename=%s otype=%s comm=%s\n" ), data -> filename , data -> otype , data -> comm );
83+ bpf_trace_printk ( "LOG: inode=%u\n" , sizeof ( "LOG: inode=%u\n" ), data -> inode ) ;
7184
7285 bpf_ringbuf_submit (data , 0 );
7386
0 commit comments