Class: Toaster::SyscallTracer
- Inherits:
-
Object
- Object
- Toaster::SyscallTracer
- Defined in:
- lib/toaster/state/syscall_tracer.rb
Constant Summary collapse
- FILE_DUMPSTATE =
"/tmp/tracer.dumpstate"- FILE_STATEDUMP =
"/tmp/tracer.state.dump"- FILE_TMPDIR =
"/tmp/tracer.tmp.dir"- FILE_ACKS =
"/tmp/tracer.acks"
Instance Method Summary collapse
- #dump_execution_prestate ⇒ Object
- #extract_pid(line) ⇒ Object
- #extract_syscall(line) ⇒ Object
-
#initialize ⇒ SyscallTracer
constructor
A new instance of SyscallTracer.
- #is_correct_strace_line(line) ⇒ Object
- #parse_line(line) ⇒ Object
- #report_file(mod_file, pwd) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ SyscallTracer
Returns a new instance of SyscallTracer.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/toaster/state/syscall_tracer.rb', line 22 def initialize() # type 0 syscalls (see below for parsing) @syscalls_0 = ["open", "write", "openat"] # type 1 syscalls (see below for parsing) @syscalls_1 = ["creat", "mkdir", "rmdir", "link", "unlink", "symlink", "chown", "lchown", "chmod"] # type 2 syscalls (see below for parsing) @syscalls_2 = ["rename", "unlinkat", "mkdirat", "fchownat", "mknodat"] # type 3 syscalls (see below for parsing) @syscalls_3 = ["fchmod", "fchown"] # type 4 syscalls (see below for parsing) @syscalls_4 = ["utimensat"] @acked_syscalls = @syscalls_0.concat(@syscalls_1).concat(@syscalls_2).concat(@syscalls_3).concat(@syscalls_4) @monitored_syscalls = @acked_syscalls.dup.concat(["open", "openat", "chdir"]) #, "close", "dup", "dup2", "dup3" @grep_cmd = "ps aux | grep -v grep | grep -v bash | grep -v screen | grep -v SCREEN | grep strace | grep #{Process.pid}" @pwd_map = {} @num_acks_sent = 0 @num_acks_received = 0 @execution_prestate = {"files"=>{}} end |
Instance Method Details
#dump_execution_prestate ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/toaster/state/syscall_tracer.rb', line 43 def dump_execution_prestate Util.write(FILE_DUMPSTATE, "1", true) # HACK: this causes a syscall which initiates the dump further below.. Dir.mkdir(FILE_TMPDIR) Dir.rmdir(FILE_TMPDIR) state = File.read(FILE_STATEDUMP) if state.strip == "" puts "WARN: could not read pre-state dump from #{FILE_STATEDUMP}" return {} else return MarkupUtil.parse_json(state) end end |
#extract_pid(line) ⇒ Object
178 179 180 |
# File 'lib/toaster/state/syscall_tracer.rb', line 178 def extract_pid(line) line.gsub(/^(\[pid\s+)?([0-9]+)\]?\s+([a-zA-Z0-2]+)\(.*/, '\2').strip end |
#extract_syscall(line) ⇒ Object
175 176 177 |
# File 'lib/toaster/state/syscall_tracer.rb', line 175 def extract_syscall(line) line.gsub(/^(\[pid\s+)?[0-9]+\]?\s+([a-zA-Z0-2]+)\(.*/, '\2').strip end |
#is_correct_strace_line(line) ⇒ Object
172 173 174 |
# File 'lib/toaster/state/syscall_tracer.rb', line 172 def is_correct_strace_line(line) line.match(/^(\[pid\s+)?[0-9]+\]?\s+[a-zA-Z0-2]+\(.*/) end |
#parse_line(line) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/toaster/state/syscall_tracer.rb', line 92 def parse_line(line) pwd_map = @pwd_map if is_correct_strace_line(line) syscall = extract_syscall(line) syscall_pid = extract_pid(line) if !pwd_map[syscall_pid] # get initial pwd for the process which does the syscall pwd_map[syscall_pid] = get_pwd_for_pid(syscall_pid) end pwd = pwd_map[syscall_pid] if syscall == "chdir" pwd = pwd_map[syscall_pid] = get_pwd_for_pid(syscall_pid) elsif syscall == "write" if line.match(/.*write\([0-9]+<(.*)>.*/) mod_file = line.gsub(/.*write\([0-9]+<(.*)>.*/, '\1').strip return report_file(mod_file, pwd) elsif line.match(/.*write\([0-9]+,.*/) fd = line.gsub(/.*write\(([0-9]+),.*/, '\1').strip mod_file = PtraceUtil.get_filename_for_fd(syscall_pid, fd) return report_file(mod_file, pwd) end elsif syscall == "open" || syscall == "openat" if line.match(/O_WRONLY/) || line.match(/O_RDWR/) || line.match(/O_APPEND/) || line.match(/O_CREAT/) if syscall == "open" mod_file = line.gsub(/.*open\("([^"]+)".*/, '\1').strip return report_file(mod_file, pwd) elsif syscall == "openat" if line.match(/.*openat\(AT_FDCWD,\s*"([^"]+)".*/) mod_file = line.gsub(/.*openat\(AT_FDCWD,\s*"([^"]+)".*/, '\1').strip return report_file(mod_file, pwd) elsif line.match(/.*openat\([0-9]+,\s*"([^"]+)".*/) fd = line.gsub(/.*openat\(([0-9]+),\s*"[^"]+".*/, '\1').strip.to_i mod_file = PtraceUtil.get_filename_for_fd(syscall_pid, fd) return report_file(mod_file, pwd) elsif line.match(/.*openat\("([^"]+)",\s*"[^"]+".*/) mod_file = line.gsub(/.*openat\("([^"]+)",\s*"[^"]+".*/, '\1').strip return report_file(mod_file, pwd) end end end else if @syscalls_1.include?(syscall) mod_file = line.gsub(/.*((#{@syscalls_1.join(')|(')}))\("([^"]+)".*/, "\\#{@syscalls_1.size + 2}").strip return report_file(mod_file, pwd) end if @syscalls_2.include?(syscall) mod_file = line.gsub(/.*((#{@syscalls_2.join(')|(')}))\([^,]+,\s*"([^"]+)".*/, "\\#{@syscalls_2.size + 2}").strip return report_file(mod_file, pwd) end if @syscalls_3.include?(syscall) fd = line.gsub(/.*((#{@syscalls_3.join(')|(')}))\(([^,]+),.*/, "\\#{@syscalls_3.size + 2}").strip.to_i mod_file = PtraceUtil.get_filename_for_fd(syscall_pid, fd) #puts "!! syscalls_3: #{mod_file}" return report_file(mod_file, pwd) end if @syscalls_4.include?(syscall) mod_file = nil pattern1 = ".*((#{@syscalls_4.join(')|(')}))\\(([0-9]+),.*" pattern2 = ".*((#{@syscalls_4.join(')|(')}))\\([^,]+,\\s*\"([^\"]+)\".*" if line.match(/#{pattern1}/) fd = line.gsub(/#{pattern1}/, "\\#{@syscalls_4.size + 2}").strip.to_i mod_file = PtraceUtil.get_filename_for_fd(syscall_pid, fd) elsif line.match(/#{pattern2}/) mod_file = line.gsub(/#{pattern2}/, "\\#{@syscalls_4.size + 2}") end #puts "!! syscalls_3: #{mod_file}" return report_file(mod_file, pwd) end end end return nil end |
#report_file(mod_file, pwd) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/toaster/state/syscall_tracer.rb', line 182 def report_file(mod_file, pwd) if !mod_file || mod_file.to_s.strip == "" return end if !mod_file.match(/pipe:.*/) && !mod_file.match(/socket:.*/) && !mod_file.match(/\/dev\/.*/) && !mod_file.match(/\/proc\/.*/) && !mod_file.match(/\/tmp\/chef-script.*/) && !mod_file.match(/\/var\/chef\/cache\/.*/) && !mod_file.match(/\/tmp\/tracer\.acks.*/) if is_correct_strace_line(mod_file) puts "WARN: Apparently could not parse syscall line from strace: #{mod_file}" return end #puts "=====> #{pwd} - #{mod_file}" if mod_file[0] != "/" && pwd != "" mod_file = "#{pwd}/#{mod_file}" end # ignore /tmp files if !mod_file.match(/^\/tmp\//) return mod_file end return nil end end |
#start ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/toaster/state/syscall_tracer.rb', line 59 def start() #puts "INFO: writing values to #{FILE_ACKS}" `echo ":#{@acked_syscalls.join(':')}:" > #{FILE_ACKS}` @monitoring_active = true `echo "" > #{FILE_DUMPSTATE}` `echo "" > #{FILE_STATEDUMP}` # start strace process do_start_process = true do_start_thread = false if do_start_process tracer_pid = start_strace() if do_start_thread __legacy_start_thread() end else __legacy_ruby_ptrace() end end |