Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Flesh out posix_spawn support more with file actions

  • Loading branch information...
commit ac4d23136234d3f1c7ea25b9536eef281cb70b71 1 parent 3776f8f
Wayne Meissner authored
Showing with 160 additions and 34 deletions.
  1. +13 −0 examples/ls.rb
  2. +147 −34 lib/spoon.rb
View
13 examples/ls.rb
@@ -0,0 +1,13 @@
+require 'spoon'
+
+#
+# Do a recursive ls on the current directory, redirecting output to /tmp/ls.out
+#
+
+file_actions = Spoon::FileActions.new
+file_actions.close(1)
+file_actions.open(1, "/tmp/ls.out", File::WRONLY | File::TRUNC | File::CREAT, 0600)
+spawn_attr = Spoon::SpawnAttributes.new
+pid = Spoon.posix_spawn('/usr/bin/env', file_actions, spawn_attr, %w(env ls -R))
+
+Process.waitpid(pid)
View
181 lib/spoon.rb
@@ -1,51 +1,164 @@
require 'ffi'
module Spoon
- extend FFI::Library
- ffi_lib 'c'
-
- # int
- # posix_spawn(pid_t *restrict pid, const char *restrict path,
- # const posix_spawn_file_actions_t *file_actions,
- # const posix_spawnattr_t *restrict attrp, char *const argv[restrict],
- # char *const envp[restrict]);
-
- attach_function :_posix_spawn, :posix_spawn, [:pointer, :string, :pointer, :pointer, :pointer, :pointer], :int
- attach_function :_posix_spawnp, :posix_spawnp, [:pointer, :string, :pointer, :pointer, :pointer, :pointer], :int
-
+
+ class FileActions
+ attr_reader :pointer
+ SIZE = FFI::Platform.mac? ? FFI.type_size(:pointer) : 128
+
+ def initialize
+ @pointer = FFI::AutoPointer.new(LibC.malloc(SIZE), self.class)
+ error = LibC.posix_spawn_file_actions_init(@pointer)
+ raise SystemCallError.new("posix_file_actions_init", error) unless error == 0
+ end
+
+ def self.release(ptr)
+ LibC.posix_spawn_file_actions_destroy(ptr)
+ LibC.free(ptr)
+ end
+
+ def open(fd, path, oflag, mode)
+ error = LibC.posix_spawn_file_actions_addopen(@pointer, fd, path, oflag, mode)
+ raise SystemCallError.new("posix_file_actions_addopen", error) unless error == 0
+ self
+ end
+
+ def close(fd)
+ error = LibC.posix_spawn_file_actions_addclose(@pointer, fd)
+ raise SystemCallError.new("posix_file_actions_addclose", error) unless error == 0
+ self
+ end
+
+ def dup2(fd, newfd)
+ error = LibC.posix_spawn_file_actions_adddup2(@pointer, fd, newfd)
+ raise SystemCallError.new("posix_file_actions_adddup2", error) unless error == 0
+ self
+ end
+ end
+
+ class SpawnAttributes
+ attr_reader :pointer
+ SIZE = FFI::Platform.mac? ? FFI.type_size(:pointer) : 128
+
+ def initialize
+ @pointer = FFI::AutoPointer.new(LibC.malloc(SIZE), self.class)
+ error = LibC.posix_spawnattr_init(@pointer)
+ raise SystemCallError.new("posix_spawnattr_init", error) unless error == 0
+ end
+
+ def self.release(ptr)
+ LibC.posix_spawnattr_destroy(ptr)
+ LibC.free(ptr)
+ end
+
+ def pgroup=(group)
+ error = LibC.posix_spawnattr_setpgroup(pointer, group)
+ raise SystemCallError.new("posix_spawnattr_setpgroup", error) unless error == 0
+ group
+ end
+
+ def pgroup
+ group = FFI::MemoryPointer.new :pid_t
+ error = LibC.posix_spawnattr_getpgroup(pointer, group)
+ raise SystemCallError.new("posix_spawnattr_getpgroup", error) unless error == 0
+ get_pid(group)
+ end
+ end
+
+ def self.posix_spawn(path, file_actions, spawn_attr, argv, env = ENV)
+ pid_ptr, argv_ptr, env_ptr = _prepare_spawn_args(argv, env)
+ error = LibC.posix_spawnp(pid_ptr, path, file_actions, spawn_attr, argv_ptr, env_ptr)
+ raise SystemCallError.new(path, error) unless error == 0
+ get_pid(pid_ptr)
+ end
+
+ def self.posix_spawnp(file, file_actions, spawn_attr, argv, env = ENV)
+ pid_ptr, argv_ptr, env_ptr = _prepare_spawn_args(argv, env)
+ error = LibC.posix_spawnp(pid_ptr, file, file_actions, spawn_attr, argv_ptr, env_ptr)
+ raise SystemCallError.new(file, error) unless error == 0
+ get_pid(pid_ptr)
+ end
+
def self.spawn(*args)
- spawn_args = _prepare_spawn_args(args)
- errno = _posix_spawn(*spawn_args)
- raise SystemCallError.new(args[0], errno) if errno != 0
- spawn_args[0].read_int
+ posix_spawn(args[0], nil, nil, args, ENV)
end
def self.spawnp(*args)
- spawn_args = _prepare_spawn_args(args)
- errno = _posix_spawnp(*spawn_args)
- raise SystemCallError.new(args[0], errno) if errno != 0
- spawn_args[0].read_int
+ posix_spawnp(args[0], nil, nil, args, ENV)
end
private
-
- def self._prepare_spawn_args(args)
+
+ class PointerArray
+ def initialize
+ @ary = []
+ end
+
+ def <<(ptr)
+ @ary << ptr
+ self
+ end
+
+ def pointer
+ if @pointer.nil? || (@pointer.size / @pointer.type_size) <= @ary.length
+ ptr = FFI::MemoryPointer.new(:pointer, @ary.length + 1)
+ ptr.put_array_of_pointer(0, @ary)
+ @pointer = ptr
+ end
+ @pointer
+ end
+ end
+
+ if FFI.type_size(:pid_t) == 4
+ def self.get_pid(ptr)
+ ptr.get_int32(0)
+ end
+ else
+ def self.get_pid(ptr)
+ ptr.get_int64(0)
+ end
+ end
+
+ def self._prepare_spawn_args(argv, env)
pid_ptr = FFI::MemoryPointer.new(:pid_t, 1)
- args_ary = FFI::MemoryPointer.new(:pointer, args.length + 1)
- str_ptrs = args.map {|str| FFI::MemoryPointer.from_string(str)}
- args_ary.put_array_of_pointer(0, str_ptrs)
+ args_ary = argv.inject(PointerArray.new) { |ary, str| ary << FFI::MemoryPointer.from_string(str) }
+ env_ary = PointerArray.new
+ env.each_pair { |key, value| env_ary << FFI::MemoryPointer.from_string("#{key}=#{value}") }
- env_ary = FFI::MemoryPointer.new(:pointer, ENV.length + 1)
- env_ptrs = ENV.map {|key,value| FFI::MemoryPointer.from_string("#{key}=#{value}")}
- env_ary.put_array_of_pointer(0, env_ptrs)
-
- [pid_ptr, args[0], nil, nil, args_ary, env_ary]
+ [pid_ptr, args_ary, env_ary]
end
-end
-if __FILE__ == $0
- pid = Spoon.spawn('/usr/bin/vim')
+ module LibC
+ extend FFI::Library
+ ffi_lib FFI::Library::LIBC
- Process.waitpid(pid)
+ class PointerConverter
+ extend FFI::DataConverter
+ native_type FFI::Type::POINTER
+
+ def self.to_native(value, ctx)
+ value ? value.pointer : nil
+ end
+ end
+
+ typedef PointerConverter, :file_actions
+ typedef PointerConverter, :spawn_attr
+ typedef PointerConverter, :ptr_array
+
+ attach_function :posix_spawn, [:pointer, :string, :file_actions, :spawn_attr, :ptr_array, :ptr_array ], :int
+ attach_function :posix_spawnp, [:pointer, :string, :file_actions, :spawn_attr, :ptr_array, :ptr_array ], :int
+ attach_function :posix_spawn_file_actions_init, [ :pointer ], :int
+ attach_function :posix_spawn_file_actions_destroy, [ :pointer ], :int
+ attach_function :posix_spawn_file_actions_adddup2, [ :pointer, :int, :int ], :int
+ attach_function :posix_spawn_file_actions_addclose, [ :pointer, :int ], :int
+ attach_function :posix_spawn_file_actions_addopen, [ :pointer, :int, :string, :int, :mode_t ], :int
+ attach_function :posix_spawnattr_init, [ :pointer ], :int
+ attach_function :posix_spawnattr_destroy, [ :pointer ], :int
+ attach_function :posix_spawnattr_setpgroup, [ :pointer, :pid_t ], :int
+ attach_function :posix_spawnattr_getpgroup, [ :pointer, :pointer ], :int
+ attach_function :malloc, [ :size_t ], :pointer
+ attach_function :free, [ :pointer ], :void
+ attach_function :strerror, [ :int ], :string
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.