bsdcapsicum.rb/lib/bsd/capsicum/ffi.rb

72 lines
1.9 KiB
Ruby
Raw Normal View History

2024-06-27 06:01:24 +02:00
# frozen_string_literal: true
2024-06-25 08:25:53 +02:00
module BSD::Capsicum
2024-06-25 09:37:54 +02:00
module FFI
2024-06-25 08:25:53 +02:00
require "fiddle"
2024-07-12 09:41:46 +02:00
require "fiddle/import"
2024-07-12 10:18:07 +02:00
include Fiddle::Types
include Constants
2024-07-12 09:41:46 +02:00
extend Fiddle::Importer
dlload Dir["/lib/libc.*"].first
extern "int cap_getmode(u_int*)"
extern "int cap_enter(void)"
extern "int cap_rights_limit(int, const cap_rights_t*)"
extern "cap_rights_t* __cap_rights_init(int version, cap_rights_t*, ...)"
2024-06-25 17:17:23 +02:00
2024-06-25 08:25:53 +02:00
module_function
##
# Provides a Ruby interface for cap_enter(2)
# @return [Integer]
def cap_enter
2024-07-12 09:41:46 +02:00
self["cap_enter"].call
2024-06-25 08:25:53 +02:00
end
##
# Provides a Ruby interface for cap_getmode(2)
# @param [Fiddle::Pointer] uintp
# @return [Integer]
def cap_getmode(uintp)
2024-07-12 09:41:46 +02:00
self["cap_getmode"].call(uintp)
2024-06-25 08:25:53 +02:00
end
2024-06-25 15:39:04 +02:00
##
# Provides a Ruby interface for cap_rights_limit(2)
# @param [Integer] fd
2024-07-12 10:21:47 +02:00
# @param [Fiddle::Pointer] rightsp
2024-06-25 15:39:04 +02:00
# @return [Integer]
2024-07-12 10:21:47 +02:00
def cap_rights_limit(fd, rightsp)
self["cap_rights_limit"].call(fd, rightsp)
2024-06-25 15:39:04 +02:00
end
##
# Provides a Ruby interface for cap_rights_init(2)
2024-07-12 08:58:34 +02:00
# @see BSD::Capsicum::Constants See Constants for a full list of capabilities
2024-07-12 10:21:47 +02:00
# @param [Fiddle::Pointer] rightsp
2024-07-12 09:19:26 +02:00
# A pointer to initialize the `cap_rights_t` structure
2024-07-12 10:24:38 +02:00
# @param [Array<Symbol, Integer>] capabilities
2024-07-12 09:19:26 +02:00
# An allowed set of capabilities
2024-06-25 15:39:04 +02:00
# @return [Fiddle::Pointer]
2024-07-12 08:54:34 +02:00
# Returns a pointer to the structure `cap_rights_t`
2024-07-12 10:21:47 +02:00
def cap_rights_init(rightsp, *capabilities)
2024-07-12 09:41:46 +02:00
self["__cap_rights_init"].call(
CAP_RIGHTS_VERSION,
2024-07-12 10:21:47 +02:00
rightsp,
2024-07-12 10:18:07 +02:00
*capabilities.flat_map { |cap|
[ULONG_LONG, cap_all.include?(cap) ? Constants.const_get(cap) : cap]
2024-07-12 09:41:46 +02:00
}
)
2024-06-25 08:25:53 +02:00
end
2024-07-12 10:18:07 +02:00
##
# @api private
# @return [Array<Symbol>]
# Returns all known capabilities
def cap_all
@cap_all ||= Constants.constants.select { _1.to_s.start_with?("CAP_") }
end
2024-06-25 08:25:53 +02:00
end
private_constant :FFI
2024-06-25 08:25:53 +02:00
end