spoon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/spoon.rb +49 -0
  2. metadata +55 -0
data/lib/spoon.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'ffi'
2
+
3
+ module Spoon
4
+ extend FFI::Library
5
+ ffi_lib 'c'
6
+
7
+ # int
8
+ # posix_spawn(pid_t *restrict pid, const char *restrict path,
9
+ # const posix_spawn_file_actions_t *file_actions,
10
+ # const posix_spawnattr_t *restrict attrp, char *const argv[restrict],
11
+ # char *const envp[restrict]);
12
+
13
+ attach_function :_posix_spawn, :posix_spawn, [:pointer, :string, :pointer, :pointer, :pointer, :pointer], :int
14
+ attach_function :_posix_spawnp, :posix_spawnp, [:pointer, :string, :pointer, :pointer, :pointer, :pointer], :int
15
+
16
+ def self.spawn(*args)
17
+ spawn_args = _prepare_spawn_args(args)
18
+ _posix_spawn(*spawn_args)
19
+ spawn_args[0].read_int
20
+ end
21
+
22
+ def self.spawnp(*args)
23
+ spawn_args = _prepare_spawn_args(args)
24
+ _posix_spawnp(*spawn_args)
25
+ spawn_args[0].read_int
26
+ end
27
+
28
+ private
29
+
30
+ def self._prepare_spawn_args(args)
31
+ pid_ptr = FFI::MemoryPointer.new(:pid_t, 1)
32
+
33
+ args_ary = FFI::MemoryPointer.new(:pointer, args.length + 1)
34
+ str_ptrs = args.map {|str| FFI::MemoryPointer.from_string(str)}
35
+ args_ary.put_array_of_pointer(0, str_ptrs)
36
+
37
+ env_ary = FFI::MemoryPointer.new(:pointer, ENV.length + 1)
38
+ env_ptrs = ENV.map {|key,value| FFI::MemoryPointer.from_string("#{key}=#{value}")}
39
+ env_ary.put_array_of_pointer(0, env_ptrs)
40
+
41
+ [pid_ptr, args[0], nil, nil, args_ary, env_ary]
42
+ end
43
+ end
44
+
45
+ if __FILE__ == $0
46
+ pid = Spoon.spawn('/usr/bin/vim')
47
+
48
+ Process.waitpid(pid)
49
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spoon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Charles Oliver Nutter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-10 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Spoon is an FFI binding of the posix_spawn function, providing fork+exec functionality in a single shot.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/spoon.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Spoon is an FFI binding of the posix_spawn function, providing fork+exec functionality in a single shot.
54
+ test_files: []
55
+