about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2024-07-31 19:56:32 +0200
committerBaitinq <manuelpalenzuelamerino@gmail.com>2024-07-31 19:56:32 +0200
commitd31785235df1374719e927bf514e109bcc20a42a (patch)
tree16d511e2e490ccaff8ab94c86944ec657d4635a2
parentfs-tracer: keep track of seen file's contents (diff)
downloadfs-tracer-d31785235df1374719e927bf514e109bcc20a42a.tar.gz
fs-tracer-d31785235df1374719e927bf514e109bcc20a42a.tar.bz2
fs-tracer-d31785235df1374719e927bf514e109bcc20a42a.zip
fs-tracer: handle append mode for sys_open
-rw-r--r--fs-tracer/src/main.rs2
-rw-r--r--fs-tracer/src/syscall_handler.rs12
2 files changed, 12 insertions, 2 deletions
diff --git a/fs-tracer/src/main.rs b/fs-tracer/src/main.rs
index b36707e..827f562 100644
--- a/fs-tracer/src/main.rs
+++ b/fs-tracer/src/main.rs
@@ -111,7 +111,7 @@ async fn main() -> Result<(), anyhow::Error> {
 
     drop(resolved_files_send);
 
-    let batch_timeout = Duration::from_secs(20);
+    let batch_timeout = Duration::from_secs(7);
     let mut last_sent_time = Instant::now();
 
     let mut resolved_files_for_request: Vec<FSTracerFile> = vec![];
diff --git a/fs-tracer/src/syscall_handler.rs b/fs-tracer/src/syscall_handler.rs
index 3ec18b2..34a5dab 100644
--- a/fs-tracer/src/syscall_handler.rs
+++ b/fs-tracer/src/syscall_handler.rs
@@ -1,3 +1,4 @@
+use libc::O_APPEND;
 use log::info;
 use std::collections::HashMap;
 use std::io::Read;
@@ -18,6 +19,7 @@ struct OpenFile {
     filename: String,
     offset: i64,
     contents: String,
+    has_append_mode: bool,
 }
 
 pub struct SyscallHandler {
@@ -45,7 +47,7 @@ impl SyscallHandler {
     }
 
     fn handle_write(&mut self, write_syscall: WriteSyscallBPF) -> Result<(), ()> {
-        let open_file = match self.open_files.get(&(write_syscall.fd, write_syscall.pid)) {
+        let mut open_file = match self.open_files.get(&(write_syscall.fd, write_syscall.pid)) {
             None => {
                 info!(
                     "DIDNT FIND AN OPEN FILE FOR THE WRITE SYSCALL (fd: {}, ret: {})",
@@ -60,6 +62,10 @@ impl SyscallHandler {
             .to_str()
             .unwrap_or_default();
 
+        if open_file.has_append_mode {
+            open_file.offset = open_file.contents.len() as i64;
+        }
+
         let mut new_contents = open_file.contents.clone();
         let buf_size = buf.len();
         let start = open_file.offset as usize;
@@ -94,6 +100,7 @@ impl SyscallHandler {
                 filename: open_file.filename,
                 offset: open_file.offset + write_syscall.count,
                 contents: new_contents,
+                has_append_mode: open_file.has_append_mode,
             },
         );
         Ok(())
@@ -130,6 +137,7 @@ impl SyscallHandler {
                 filename: open_file.filename,
                 offset: final_offset,
                 contents: open_file.contents,
+                has_append_mode: open_file.has_append_mode,
             },
         );
         Ok(())
@@ -178,12 +186,14 @@ impl SyscallHandler {
         }
 
         let fd = open_syscall.ret;
+        let has_append_mode = open_syscall.flags == O_APPEND;
         self.open_files.insert(
             (fd, open_syscall.pid),
             OpenFile {
                 filename: filename.to_string(),
                 offset: 0,
                 contents,
+                has_append_mode,
             },
         );
         Ok(())