ruby-changes:10575
From: akr <ko1@a...>
Date: Sun, 8 Feb 2009 23:23:33 +0900 (JST)
Subject: [ruby-changes:10575] Ruby:r22131 (trunk): * ext/socket/option.c (inspect_peercred): new function to show
akr 2009-02-08 23:23:16 +0900 (Sun, 08 Feb 2009) New Revision: 22131 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22131 Log: * ext/socket/option.c (inspect_peercred): new function to show SO_PEERCRED socket option. Modified files: trunk/ChangeLog trunk/ext/socket/option.c trunk/test/socket/test_unix.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 22130) +++ ChangeLog (revision 22131) @@ -1,3 +1,8 @@ +Sun Feb 8 23:22:35 2009 Tanaka Akira <akr@f...> + + * ext/socket/option.c (inspect_peercred): new function to show + SO_PEERCRED socket option. + Sun Feb 8 22:44:20 2009 Nobuyoshi Nakada <nobu@r...> * include/ruby/backward/rubysig.h (rb_thread_blocking_region_begin), Index: ext/socket/option.c =================================================================== --- ext/socket/option.c (revision 22130) +++ ext/socket/option.c (revision 22131) @@ -221,6 +221,23 @@ } } +#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 @@ # 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; } Index: test/socket/test_unix.rb =================================================================== --- test/socket/test_unix.rb (revision 22130) +++ test/socket/test_unix.rb (revision 22131) @@ -296,13 +296,29 @@ } 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" + serv = Socket.unix_server_socket(sockpath) + c = Socket.unix(sockpath) + s, = serv.accept s.setsockopt(:SOCKET, :PASSCRED, 1) c.print "a" msg, cliend_ai, rflags, cred = s.recvmsg @@ -310,12 +326,12 @@ 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 @@ } end - def test_cred_cmsgcred + def test_sendcred_cmsgcred return if /freebsd/ !~ RUBY_PLATFORM Dir.mktmpdir {|d| sockpath = "#{d}/sock" -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/