Skip to content

Instantly share code, notes, and snippets.

@mistydemeo
Last active July 13, 2020 21:55
Show Gist options
  • Save mistydemeo/f253d4fa6f86e8d6927961e0dd9e7f66 to your computer and use it in GitHub Desktop.
Save mistydemeo/f253d4fa6f86e8d6927961e0dd9e7f66 to your computer and use it in GitHub Desktop.
require 'fiddle'
require 'fiddle/import'
module Sysctl
extend Fiddle::Importer
dlload "/usr/lib/libSystem.dylib"
extern "int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)"
end
ret = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
ret_size = Fiddle::Pointer[[Fiddle::SIZEOF_INT].pack("L")]
puts Sysctl.sysctlbyname("sysctl.proc_translated", ret, ret_size, nil, 0).inspect
puts ret.to_s(Fiddle::SIZEOF_INT).unpack("L").first
puts ret_size.to_s(Fiddle::SIZEOF_INT).inspect
@mistydemeo
Copy link
Author

$ ruby sysctl_fiddle.rb                                  
0
0
"\x04\x00\x00\x00"

$ arch -x86_64 ruby sysctl_fiddle.rb                     
0
1
"\x04\x00\x00\x00"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment