Last active
August 29, 2015 14:19
-
-
Save sj26/111e35bd0b8cf8eb0d7e to your computer and use it in GitHub Desktop.
Fiddle binding for launch_activate_socket for launchd handing sockets to processes (http://www.manpagez.com/man/3/launch/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Launch | |
extend Fiddle::Importer | |
dlload '/usr/lib/system/liblaunch.dylib' | |
@launch_activate_socket = import_function(*parse_signature("int launch_activate_socket(const char *name, int **fds, size_t *cnt)")) | |
def self.launch_activate_socket(name) | |
raise ArgumentError, "name must be a string" unless name.is_a? String | |
fds_ptr = Fiddle::Pointer.new(0) | |
count_ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_SIZE_T) | |
result = @launch_activate_socket.call(name, fds_ptr.ref, count_ptr) | |
raise "launch_activate_socket returned #{result}" unless result.zero? | |
count = count_ptr.to_s(Fiddle::SIZEOF_SIZE_T).unpack("I!").first | |
fds = fds_ptr.to_s(Fiddle::SIZEOF_INT * count).unpack("i#{count}") | |
Fiddle.free(fds_ptr.to_i) | |
fds | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment