Module: Facter::Bsd::FfiHelper

Defined in:
lib/facter/resolvers/bsd/ffi/ffi_helper.rb

Defined Under Namespace

Modules: Libc

Class Method Summary collapse

Class Method Details

.sysctl(type, oids) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/facter/resolvers/bsd/ffi/ffi_helper.rb', line 15

def self.sysctl(type, oids)
  name = FFI::MemoryPointer.new(:uint, oids.size)
  name.write_array_of_uint(oids)
  namelen = oids.size

  oldp = FFI::Pointer::NULL
  oldlenp = FFI::MemoryPointer.new(:size_t)

  newp = FFI::Pointer::NULL
  newlen = 0

  if type == :string
    res = Libc.sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    return nil if res.negative?
  else
    oldlenp.write(:size_t, FFI.type_size(type))
  end

  oldp = FFI::MemoryPointer.new(:uint8_t, oldlenp.read(:size_t))
  res = Libc.sysctl(name, namelen, oldp, oldlenp, newp, newlen)
  return nil if res.negative?

  case type
  when :string
    oldp.read_string
  else
    oldp.read(type)
  end
end