Module: Seccomp::Notify::Libc

Defined in:
lib/seccomp/notify/libc.rb

Constant Summary collapse

HANDLE =
Fiddle.dlopen(nil)
SYSCALL =
Fiddle::Function.new(
  HANDLE["syscall"],
  [Fiddle::TYPE_LONG] * 4,
  Fiddle::TYPE_LONG,
  need_gvl: true
)
PRCTL =
Fiddle::Function.new(
  HANDLE["prctl"],
  [Fiddle::TYPE_INT] * 5,
  Fiddle::TYPE_INT
)
IOCTL =
Fiddle::Function.new(
  HANDLE["ioctl"],
  [Fiddle::TYPE_INT, Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP],
  Fiddle::TYPE_INT
)
POLL =
Fiddle::Function.new(
  HANDLE["poll"],
  [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG, Fiddle::TYPE_INT],
  Fiddle::TYPE_INT
)

Class Method Summary collapse

Class Method Details

.architectureObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/seccomp/notify/libc.rb', line 33

def architecture
  @architecture ||= case RUBY_PLATFORM
  when /x86_64/
    :x86_64
  when /aarch64|arm64/
    :aarch64
  else
    raise NotSupportedError, "unsupported architecture: #{RUBY_PLATFORM}"
  end
end

.ioctl(fd, request, buffer) ⇒ Object



53
54
55
# File 'lib/seccomp/notify/libc.rb', line 53

def ioctl(fd, request, buffer)
  IOCTL.call(fd, request, Fiddle::Pointer[buffer])
end

.notif_sizesObject

Raises:

  • (SystemCallError)


66
67
68
69
70
71
72
73
# File 'lib/seccomp/notify/libc.rb', line 66

def notif_sizes
  buffer = "\0" * 6
  result = seccomp(Constants::SECCOMP_GET_NOTIF_SIZES, 0, buffer)
  raise SystemCallError.new("seccomp(GET_NOTIF_SIZES)", Fiddle.last_error) if result.negative?

  notif, resp, data = buffer.unpack("S<S<S<")
  {notif: notif, resp: resp, data: data}.freeze
end

.poll_hup?(fd) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (SystemCallError)


57
58
59
60
61
62
63
64
# File 'lib/seccomp/notify/libc.rb', line 57

def poll_hup?(fd)
  buffer = [fd, 1, 0].pack("l<s<s<")
  result = POLL.call(Fiddle::Pointer[buffer], 1, 0)
  raise SystemCallError.new("poll", Fiddle.last_error) if result.negative?

  revents = buffer.unpack1("@6S<")
  (revents & 0x10).positive? && (revents & 0x01).zero?
end

.prctl(option, arg2 = 0, arg3 = 0, arg4 = 0, arg5 = 0) ⇒ Object



44
45
46
# File 'lib/seccomp/notify/libc.rb', line 44

def prctl(option, arg2 = 0, arg3 = 0, arg4 = 0, arg5 = 0)
  PRCTL.call(option, arg2, arg3, arg4, arg5)
end

.seccomp(operation, flags, buffer = 0) ⇒ Object



48
49
50
51
# File 'lib/seccomp/notify/libc.rb', line 48

def seccomp(operation, flags, buffer = 0)
  pointer = buffer.is_a?(String) ? Fiddle::Pointer[buffer] : buffer
  SYSCALL.call(Constants::SYS_SECCOMP.fetch(architecture), operation, flags, pointer.to_i)
end