Class: SeccompTools::CLI::Emu

Inherits:
Base
  • Object
show all
Defined in:
lib/seccomp-tools/cli/emu.rb

Overview

Handle ‘emu’ command.

Constant Summary collapse

SUMMARY =

Summary of this command.

'Emulate seccomp rules.'
USAGE =

Usage of this command.

"emu - #{SUMMARY}\n\nUsage: seccomp-tools emu [options] BPF_FILE [sys_nr [arg0 [arg1 ... arg5]]]"

Instance Attribute Summary

Attributes inherited from Base

#argv, #option

Instance Method Summary collapse

Constructor Details

#initializeEmu

Returns a new instance of Emu.



20
21
22
23
# File 'lib/seccomp-tools/cli/emu.rb', line 20

def initialize(*)
  super
  option[:verbose] = 1
end

Instance Method Details

#handlevoid

This method returns an undefined value.

Handle options.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/seccomp-tools/cli/emu.rb', line 41

def handle
  return unless super

  option[:ifile] = argv.shift
  return CLI.show(parser.help) if option[:ifile].nil?

  raw = input
  insts = SeccompTools::Disasm.to_bpf(raw, option[:arch]).map(&:inst)
  sys, *args = argv
  sys = evaluate_sys_nr(sys) if sys
  args.map! { |v| Integer(v) }
  trace = Set.new
  res = SeccompTools::Emulator.new(insts, sys_nr: sys, args: args, arch: option[:arch]).run do |ctx|
    trace << ctx[:pc]
  end

  if option[:verbose] >= 1
    disasm = SeccompTools::Disasm.disasm(raw, arch: option[:arch]).lines
    output_emulate_path(disasm, trace, res)
  end
  output do
    ret_type = Const::BPF::ACTION.invert[res[:ret] & Const::BPF::SECCOMP_RET_ACTION_FULL]
    errno = ret_type == :ERRNO ? "(#{res[:ret] & Const::BPF::SECCOMP_RET_DATA})" : ''
    format("return %s%s at line %04d\n", ret_type, errno, res[:pc])
  end
end

#parserOptionParser

Define option parser.

Returns:

  • (OptionParser)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/seccomp-tools/cli/emu.rb', line 27

def parser
  @parser ||= OptionParser.new do |opt|
    opt.banner = usage

    option_arch(opt)

    opt.on('-q', '--[no-]quiet', 'Run quietly, only show emulation result.') do |v|
      option[:verbose] = 0 if v
    end
  end
end