Migrate to Fiddle::Importer

This commit is contained in:
0x1eef 2024-07-12 04:41:46 -03:00
parent 82e9445d61
commit 5d8a05fb22

View file

@ -3,8 +3,16 @@
module BSD::Capsicum module BSD::Capsicum
module FFI module FFI
require "fiddle" require "fiddle"
require "fiddle/import"
extend Fiddle::Importer
dlload Dir["/lib/libc.*"].first
include Fiddle::Types include Fiddle::Types
include Constants include Constants
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*, ...)"
module_function module_function
@ -12,11 +20,7 @@ module BSD::Capsicum
# Provides a Ruby interface for cap_enter(2) # Provides a Ruby interface for cap_enter(2)
# @return [Integer] # @return [Integer]
def cap_enter def cap_enter
Fiddle::Function.new( self["cap_enter"].call
libc["cap_enter"],
[],
INT
).call
end end
## ##
@ -24,11 +28,7 @@ module BSD::Capsicum
# @param [Fiddle::Pointer] uintp # @param [Fiddle::Pointer] uintp
# @return [Integer] # @return [Integer]
def cap_getmode(uintp) def cap_getmode(uintp)
Fiddle::Function.new( self["cap_getmode"].call(uintp)
libc["cap_getmode"],
[INTPTR_T],
INT
).call(uintp)
end end
## ##
@ -37,11 +37,7 @@ module BSD::Capsicum
# @param [Fiddle::Pointer] rights # @param [Fiddle::Pointer] rights
# @return [Integer] # @return [Integer]
def cap_rights_limit(fd, rights) def cap_rights_limit(fd, rights)
Fiddle::Function.new( self["cap_rights_limit"].call(fd, rights)
libc["cap_rights_limit"],
[INT, VOIDP],
INT
).call(fd, rights)
end end
## ##
@ -54,21 +50,13 @@ module BSD::Capsicum
# @return [Fiddle::Pointer] # @return [Fiddle::Pointer]
# Returns a pointer to the structure `cap_rights_t` # Returns a pointer to the structure `cap_rights_t`
def cap_rights_init(rights, *capabilities) def cap_rights_init(rights, *capabilities)
Fiddle::Function.new( self["__cap_rights_init"].call(
libc["__cap_rights_init"], CAP_RIGHTS_VERSION,
[INT, VOIDP, VARIADIC], rights,
VOIDP
).call(
CAP_RIGHTS_VERSION, rights,
*capabilities.flat_map { *capabilities.flat_map {
[ULONG_LONG, (Symbol === _1) ? Constants.const_get(_1) : _1] [ULONG_LONG, (Symbol === _1) ? Constants.const_get(_1) : _1]
}) }
end )
##
# @api private
def libc
@libc ||= Fiddle.dlopen Dir["/lib/libc.*"].first
end end
end end
private_constant :FFI private_constant :FFI