Module: Facter::Freebsd::FfiHelper

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

Defined Under Namespace

Modules: Libc

Class Method Summary collapse

Class Method Details

.kenv(action, name, value = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/facter/resolvers/freebsd/ffi/ffi_helper.rb', line 20

def self.kenv(action, name, value = nil)
  case action
  when :get
    len = Libc::KENV_MVALLEN + 1
    value = FFI::MemoryPointer.new(:char, len)
    res = Libc.kenv(Libc::KENV_GET, name, value, len)
    return nil if res.negative?

    value.read_string(res).chomp("\0")
  else
    raise "Action #{action} not supported"
  end
end

.sysctl_by_name(type, name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/facter/resolvers/freebsd/ffi/ffi_helper.rb', line 34

def self.sysctl_by_name(type, name)
  oldp = FFI::Pointer::NULL
  oldlenp = FFI::MemoryPointer.new(:size_t)

  newp = FFI::Pointer::NULL
  newlenp = 0

  if type == :string
    res = Libc.sysctlbyname(name, oldp, oldlenp, newp, newlenp)
    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.sysctlbyname(name, oldp, oldlenp, newp, newlenp)
  return nil if res.negative?

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