Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Setting pgroup requires the flag

Setting pgroup requires the flag set in spawn attributes. Add this flag
and add ability to set flags.

SpawnAttributes#pgroup= sets this flag automatically. Because I do not
see a use case when user wants to change the pgroup attribute but do
not set the flag.
  • Loading branch information...
commit e99fb9fedba56211894d38993af82138d27471bb 1 parent 4454035
@libc authored
Showing with 62 additions and 0 deletions.
  1. +41 −0 examples/pgrp.rb
  2. +21 −0 lib/spoon/unix.rb
View
41 examples/pgrp.rb
@@ -0,0 +1,41 @@
+require 'spoon'
+
+if RUBY_ENGINE == 'jruby'
+ # Taken from Childprocess
+ def posix_fileno_for(obj)
+ channel = ::JRuby.reference(obj).channel
+ begin
+ channel.getFDVal
+ rescue NoMethodError
+ fileno = channel.fd
+ if fileno.kind_of?(Java::JavaIo::FileDescriptor)
+ fileno = fileno.fd
+ end
+
+ fileno == -1 ? obj.fileno : fileno
+ end
+ rescue
+ # fall back
+ obj.fileno
+ end
+else
+ def posix_fileno_for(obj)
+ obj.fileno
+ end
+end
+
+r, w = IO.pipe
+file_actions = Spoon::FileActions.new
+file_actions.close(0)
+file_actions.dup2(posix_fileno_for(r), 0)
+file_actions.close(posix_fileno_for(w))
+spawn_attr = Spoon::SpawnAttributes.new
+spawn_attr.pgroup = 0
+pid = Spoon.posix_spawn('/usr/bin/env', file_actions, spawn_attr, %w(env ruby))
+r.close
+
+w.write "p [$$, Process.getpgrp]\n"
+w.close
+
+Process.waitpid(pid)
+
View
21 lib/spoon/unix.rb
@@ -55,6 +55,10 @@ def self.release(ptr)
def pgroup=(group)
error = LibC.posix_spawnattr_setpgroup(pointer, group)
raise SystemCallError.new("posix_spawnattr_setpgroup", error) unless error == 0
+
+ # it's 2 on Mac OS X and linux
+ self.flags = flags | 2
+
group
end
@@ -64,6 +68,21 @@ def pgroup
raise SystemCallError.new("posix_spawnattr_getpgroup", error) unless error == 0
get_pid(group)
end
+
+ def flags=(flags)
+ error = LibC.posix_spawnattr_setflags(pointer, flags)
+ raise SystemCallError.new("posix_spawnattr_setflags", error) unless error == 0
+ flags
+ end
+
+ def flags
+ flags = FFI::MemoryPointer.new :short
+ error = LibC.posix_spawnattr_getflags(pointer, flags)
+ raise SystemCallError.new("posix_spawnattr_getflags", error) unless error == 0
+ flags_i = flags.get_short 0
+ flags.free
+ flags_i
+ end
end
def self.posix_spawn(path, file_actions, spawn_attr, argv, env = ENV)
@@ -148,6 +167,8 @@ def self.to_native(value, ctx)
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 :posix_spawnattr_setflags, [:pointer, :short], :int
+ attach_function :posix_spawnattr_getflags, [:pointer, :pointer], :int
attach_function :malloc, [ :size_t ], :pointer
attach_function :free, [ :pointer ], :void
attach_function :strerror, [ :int ], :string
Please sign in to comment.
Something went wrong with that request. Please try again.