From 5611df706e5685fbe03412588d23ee31f13c671d Mon Sep 17 00:00:00 2001 From: kosaki Date: Sat, 24 Nov 2012 02:51:24 +0000 Subject: * process.c (proc_getsid): adds new method for getting session id. Contributed from fumiyas (Fumiyasu SATOH). Thank you! [Feature #6757] [ruby-dev:45977] * configure.in: adds getsid check. * test/ruby/test_process.rb (TestProcess#test_setsid): new test for the above. * NEWS: news for the above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37825 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- process.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'process.c') diff --git a/process.c b/process.c index 427168de6c..1bda315b70 100644 --- a/process.c +++ b/process.c @@ -4219,6 +4219,40 @@ proc_setpgid(VALUE obj, VALUE pid, VALUE pgrp) #endif +#ifdef HAVE_GETSID +/* + * call-seq: + * Process.getsid() -> integer + * Process.getsid(pid) -> integer + * + * Returns the session ID for for the given process id. If not give, + * return current process sid. Not available on all platforms. + * + * Process.getsid() #=> 27422 + * Process.getsid(0) #=> 27422 + * Process.getsid(Process.pid()) #=> 27422 + */ +static VALUE +proc_getsid(int argc, VALUE *argv) +{ + rb_pid_t sid; + VALUE pid; + + rb_secure(2); + rb_scan_args(argc, argv, "01", &pid); + + if (NIL_P(pid)) + pid = INT2NUM(0); + + sid = getsid(NUM2PIDT(pid)); + if (sid < 0) rb_sys_fail(0); + return PIDT2NUM(sid); +} +#else +#define proc_getsid rb_f_notimplement +#endif + + #if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY)) #if !defined(HAVE_SETSID) static rb_pid_t ruby_setsid(void); @@ -6609,6 +6643,7 @@ Init_process(void) rb_define_module_function(rb_mProcess, "getpgid", proc_getpgid, 1); rb_define_module_function(rb_mProcess, "setpgid", proc_setpgid, 2); + rb_define_module_function(rb_mProcess, "getsid", proc_getsid, -1); rb_define_module_function(rb_mProcess, "setsid", proc_setsid, 0); rb_define_module_function(rb_mProcess, "getpriority", proc_getpriority, 2); -- cgit v1.2.3