diff --git a/ChangeLog b/ChangeLog index 163f4047eb..09681d5c1a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Feb 8 23:22:35 2009 Tanaka Akira + + * ext/socket/option.c (inspect_peercred): new function to show + SO_PEERCRED socket option. + Sun Feb 8 22:44:20 2009 Nobuyoshi Nakada * include/ruby/backward/rubysig.h (rb_thread_blocking_region_begin), diff --git a/ext/socket/option.c b/ext/socket/option.c index 8fcee4c0c7..283dd7b279 100644 --- a/ext/socket/option.c +++ b/ext/socket/option.c @@ -221,6 +221,23 @@ inspect_timeval(int level, int optname, VALUE data, VALUE ret) } } +#if defined(SOL_SOCKET) && defined(SO_PEERCRED) /* GNU/Linux */ +static int +inspect_peercred(int level, int optname, VALUE data, VALUE ret) +{ + if (RSTRING_LEN(data) == sizeof(struct ucred)) { + struct ucred cred; + memcpy(&cred, RSTRING_PTR(data), sizeof(struct ucred)); + rb_str_catf(ret, " pid=%u uid=%u gid=%u", cred.pid, cred.uid, cred.gid); + rb_str_cat2(ret, " (ucred)"); + return 0; + } + else { + return -1; + } +} +#endif + static VALUE sockopt_inspect(VALUE self) { @@ -299,6 +316,9 @@ sockopt_inspect(VALUE self) # if defined(SO_SNDTIMEO) /* POSIX */ case SO_SNDTIMEO: if (inspect_timeval(level, optname, data, ret) == -1) goto dump; break; # endif +# if defined(SO_PEERCRED) /* GNU/Linux */ + case SO_PEERCRED: if (inspect_peercred(level, optname, data, ret) == -1) goto dump; break; +# endif default: goto dump; } diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb index 31d60e3ff3..ca4b17b3f4 100644 --- a/test/socket/test_unix.rb +++ b/test/socket/test_unix.rb @@ -296,7 +296,23 @@ class TestUNIXSocket < Test::Unit::TestCase } end - def test_cred_ucred + def test_getcred_ucred + return if /linux/ !~ RUBY_PLATFORM + Dir.mktmpdir {|d| + sockpath = "#{d}/sock" + serv = Socket.unix_server_socket(sockpath) + c = Socket.unix(sockpath) + s, = serv.accept + cred = s.getsockopt(:SOCKET, :PEERCRED) + inspect = cred.inspect + assert_match(/ pid=#{$$} /, inspect) + assert_match(/ uid=#{Process.uid} /, inspect) + assert_match(/ gid=#{Process.gid} /, inspect) + assert_match(/ \(ucred\)/, inspect) + } + end + + def test_sendcred_ucred return if /linux/ !~ RUBY_PLATFORM Dir.mktmpdir {|d| sockpath = "#{d}/sock" @@ -310,12 +326,12 @@ class TestUNIXSocket < Test::Unit::TestCase assert_equal("a", msg) assert_match(/ pid=#{$$} /, inspect) assert_match(/ uid=#{Process.uid} /, inspect) - assert_match(/ gid=#{Process.gid}>/, inspect) + assert_match(/ gid=#{Process.gid} /, inspect) assert_match(/ \(ucred\)/, inspect) } end - def test_cred_sockcred + def test_sendcred_sockcred return if /netbsd|freebsd/ !~ RUBY_PLATFORM Dir.mktmpdir {|d| sockpath = "#{d}/sock" @@ -336,7 +352,7 @@ class TestUNIXSocket < Test::Unit::TestCase } end - def test_cred_cmsgcred + def test_sendcred_cmsgcred return if /freebsd/ !~ RUBY_PLATFORM Dir.mktmpdir {|d| sockpath = "#{d}/sock"