Class: Seccomp::Notifier

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/seccomp/notifier.rb

Overview

Receives and responds to seccomp user notifications.

Instance Method Summary collapse

Constructor Details

#initialize(filter, fd) ⇒ void

Examples:

Notifier.new(filter, fd)

Parameters:

  • filter (Filter)

    loaded owner filter

  • fd (Integer)

    notification listener descriptor



18
19
20
21
22
23
# File 'lib/seccomp/notifier.rb', line 18

def initialize(filter, fd)
  @filter = filter
  @fd = fd
  @io = IO.for_fd(fd, autoclose: false)
  @receive_lock = filter.__send__(:notification_receive_lock)
end

Instance Method Details

#add_fd(id:, source_fd:, flags: 0, newfd: 0, newfd_flags: 0) ⇒ Integer

Returns installed descriptor.

Examples:

notifier.add_fd(id: id, source_fd: file.fileno)

Parameters:

  • id (Integer)

    notification ID

  • source_fd (Integer)

    local descriptor

  • flags (Integer) (defaults to: 0)

    addfd flags

  • newfd (Integer) (defaults to: 0)

    requested target descriptor

  • newfd_flags (Integer) (defaults to: 0)

    target descriptor flags

Returns:

  • (Integer)

    installed descriptor

Raises:



93
94
95
96
97
# File 'lib/seccomp/notifier.rb', line 93

def add_fd(id:, source_fd:, flags: 0, newfd: 0, newfd_flags: 0)
  ensure_open!
  result = LowLevel.notify_addfd(@fd, id, flags, source_fd, newfd, newfd_flags)
  check_notification_result!(result, call: "seccomp_notify_addfd")
end

#closenil

Examples:

notifier.close

Returns:

  • (nil)


101
102
103
# File 'lib/seccomp/notifier.rb', line 101

def close
  @filter.close
end

#closed?Boolean

Returns whether the owner filter is closed.

Examples:

notifier.closed?

Returns:

  • (Boolean)

    whether the owner filter is closed



107
108
109
# File 'lib/seccomp/notifier.rb', line 107

def closed?
  @filter.closed?
end

#each {|Notification| ... } ⇒ Enumerator, Notifier

Returns enumerator or receiver.

Examples:

notifier.each { |notification| notification.continue! }

Yields:

Returns:

  • (Enumerator, Notifier)

    enumerator or receiver

Raises:

  • (Error)

    if receiving fails



46
47
48
49
50
51
52
53
54
# File 'lib/seccomp/notifier.rb', line 46

def each
  return enum_for(__method__) unless block_given?

  loop do
    yield receive
  rescue NotificationCanceledError
    next
  end
end

#id_valid?(id) ⇒ Boolean

Returns whether still valid.

Examples:

notifier.id_valid?(id)

Parameters:

  • id (Integer)

    notification ID

Returns:

  • (Boolean)

    whether still valid

Raises:

  • (Error)

    if validation fails



74
75
76
77
78
79
80
81
82
83
# File 'lib/seccomp/notifier.rb', line 74

def id_valid?(id)
  ensure_open!
  result = LowLevel.notify_id_valid(@fd, id)
  return true if result.zero?

  ensure_open!
  return false if [-Errno::ENOENT::Errno, -Errno::EINVAL::Errno].include?(result)

  check_notification_result!(result, call: "seccomp_notify_id_valid")
end

#receive(timeout: nil) ⇒ Notification?

Returns request or nil on timeout.

Examples:

notifier.receive(timeout: 1)

Parameters:

  • timeout (Numeric, nil) (defaults to: nil)

    nonnegative finite real seconds to wait

Returns:

Raises:



31
32
33
34
35
36
37
38
39
40
# File 'lib/seccomp/notifier.rb', line 31

def receive(timeout: nil)
  validate_timeout!(timeout)
  deadline = monotonic_time + timeout if timeout
  acquired = acquire_receive_lock(deadline)
  return nil unless acquired

  receive_locked(remaining_timeout(deadline))
ensure
  @receive_lock.unlock if acquired
end

#respond(id:, val: 0, error: 0, flags: 0) ⇒ nil

Examples:

notifier.respond(id: id, val: 0)

Parameters:

  • id (Integer)

    notification ID

  • val (Integer) (defaults to: 0)

    syscall return value

  • error (Integer) (defaults to: 0)

    kernel-style negative errno

  • flags (Integer) (defaults to: 0)

    response flags

Returns:

  • (nil)

Raises:

  • (Error)

    if responding fails



63
64
65
66
67
68
# File 'lib/seccomp/notifier.rb', line 63

def respond(id:, val: 0, error: 0, flags: 0)
  ensure_open!
  check_notification_result!(LowLevel.notify_respond(@fd, id, val, error, flags),
                             call: "seccomp_notify_respond")
  nil
end