summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog10
-rw-r--r--NEWS4
-rw-r--r--configure.in2
-rw-r--r--process.c35
-rw-r--r--test/ruby/test_process.rb15
5 files changed, 65 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 8ad03354a0..77af307f40 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Sat Nov 24 11:47:14 2012 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
+
+ * 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.
+
Sat Nov 24 10:59:14 2012 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* thread.c (thread_create_core): don't use th->thread_id before
diff --git a/NEWS b/NEWS
index 2fa1c32bf0..2da55a3c2a 100644
--- a/NEWS
+++ b/NEWS
@@ -109,6 +109,10 @@ with all sufficient information, see the ChangeLog file.
* added method:
* added nil.to_h which returns {}
+ * Process
+ * added method:
+ * added getsid for getting sessin id (unix only).
+
* Range
* added method:
* added Range#size for lazy size evaluation.
diff --git a/configure.in b/configure.in
index d7d4a46660..7fa99c6d55 100644
--- a/configure.in
+++ b/configure.in
@@ -1554,7 +1554,7 @@ AC_CHECK_FUNCS(fmod killpg wait4 waitpid fork spawnv syscall __syscall chroot ge
getpgrp setpgrp getpgid setpgid initgroups getgroups setgroups\
getpriority getrlimit setrlimit sysconf close getpwnam_r getgrnam_r\
dlopen sigprocmask sigaction sigsetjmp _setjmp _longjmp\
- setsid telldir seekdir fchmod cosh sinh tanh log2 round llabs\
+ getsid setsid telldir seekdir fchmod cosh sinh tanh log2 round llabs\
setuid setgid daemon select_large_fdset setenv unsetenv\
mktime timegm gmtime_r clock_gettime gettimeofday poll ppoll\
pread sendfile shutdown sigaltstack dl_iterate_phdr\
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);
diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb
index b549b6f770..c4427be33c 100644
--- a/test/ruby/test_process.rb
+++ b/test/ruby/test_process.rb
@@ -1558,4 +1558,19 @@ class TestProcess < Test::Unit::TestCase
}
end if File.executable?("/bin/sh")
+ def test_setsid
+ return unless Process.respond_to?(:setsid)
+ return unless Process.respond_to?(:getsid)
+
+ IO.popen(["./ruby-trunk", "-e", <<EOS]) do|io|
+ Marshal.dump(Process.getsid, STDOUT)
+ newsid = Process.setsid
+ Marshal.dump(newsid, STDOUT)
+ STDOUT.flush
+EOS
+
+ assert_equal(Marshal.load(io), Process.getsid)
+ assert_equal(Marshal.load(io), Process.getsid(io.pid))
+ end
+ end
end