Exception: Seccomp::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/seccomp/errors.rb

Overview

Base error carrying the libseccomp errno and C call name.

Constant Summary collapse

ERRORS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

errno-to-exception lookup.

{
  Errno::EACCES::Errno => :PermissionError,
  Errno::ECANCELED::Errno => :KernelError,
  Errno::EDOM::Errno => :ArchError,
  Errno::EEXIST::Errno => :ExistsError,
  Errno::EINVAL::Errno => :InvalidArgumentError,
  Errno::ENOENT::Errno => :NotFoundError,
  Errno::ENOMEM::Errno => :OutOfMemoryError,
  Errno::EOPNOTSUPP::Errno => :NotSupportedError,
  Errno::ERANGE::Errno => :ValueRangeError,
  Errno::ESRCH::Errno => :ThreadSyncError
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, errno: nil, call: nil) ⇒ void

Examples:

Error.new("failed", errno: 22, call: "seccomp_init")

Parameters:

  • message (String, nil) (defaults to: nil)

    error message

  • errno (Integer, nil) (defaults to: nil)

    positive errno

  • call (String, nil) (defaults to: nil)

    C function name



28
29
30
31
32
# File 'lib/seccomp/errors.rb', line 28

def initialize(message = nil, errno: nil, call: nil)
  @errno = errno
  @call = call
  super(message)
end

Instance Attribute Details

#callObject (readonly)

Returns the value of attribute call.



6
7
8
# File 'lib/seccomp/errors.rb', line 6

def call
  @call
end

#errnoObject (readonly)

Returns the value of attribute errno.



6
7
8
# File 'lib/seccomp/errors.rb', line 6

def errno
  @errno
end

Class Method Details

.check!(result, call:, hint: nil) ⇒ Integer

Returns nonnegative result.

Examples:

Error.check!(0, call: "seccomp_load")

Parameters:

  • result (Integer)

    libseccomp return code

  • call (String)

    C function name

  • hint (String, nil) (defaults to: nil)

    contextual hint

Returns:

  • (Integer)

    nonnegative result

Raises:

  • (Error)

    for a negative result



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/seccomp/errors.rb', line 40

def self.check!(result, call:, hint: nil)
  return result unless result.negative?

  errno = -result
  name = Errno.constants.find do |constant|
    error_class = Errno.const_get(constant)
    error_class.is_a?(Class) && error_class < SystemCallError && errno == error_class::Errno
  end
  klass = Seccomp.const_get(ERRORS.fetch(errno, :Error))
  message = "#{call} failed: #{name || "errno #{errno}"}"
  message += " (#{hint})" if hint
  raise klass.new(message, errno: errno, call: call)
end