Sha256: 02dd7e811307dad67aba8093544bd73b2798c245cae784c5fad90dbf608ba2cd

Contents?: true

Size: 1.07 KB

Versions: 51

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require 'ffi'

module Facter
  module Bsd
    module FfiHelper
      module Libc
        extend FFI::Library

        ffi_lib 'c'
        attach_function :sysctl, %i[pointer uint pointer pointer pointer size_t], :int
      end

      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
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
facter-4.2.10 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.9 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.8 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.7 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.6 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.5 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.4 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.3 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.2 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.1 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.2.0 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.1.1 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.1.0 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.0.52 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.0.51 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.0.50 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.0.49 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.0.48 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.0.47 lib/facter/resolvers/bsd/ffi/ffi_helper.rb
facter-4.0.46 lib/facter/resolvers/bsd/ffi/ffi_helper.rb