From a9bfa55f9a1d1c96d73367d3bb46f7b9e81fbbf9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 14 Mar 2011 16:38:58 -0700 Subject: linux: unix_listener_stats may scan all paths This matches behavior of the TCP version. --- lib/raindrops/linux.rb | 18 +++++++++++------- test/test_linux.rb | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/raindrops/linux.rb b/lib/raindrops/linux.rb index 889c661..55887f0 100644 --- a/lib/raindrops/linux.rb +++ b/lib/raindrops/linux.rb @@ -34,15 +34,19 @@ module Raindrops::Linux # counterpart due to the latter being able to use inet_diag via netlink. # This parses /proc/net/unix as there is no other (known) way # to expose Unix domain socket statistics over netlink. - def unix_listener_stats(paths) + def unix_listener_stats(paths = nil) rv = Hash.new { |h,k| h[k.freeze] = Raindrops::ListenStats.new(0, 0) } - paths = paths.map do |path| - path = path.dup - path.force_encoding(Encoding::BINARY) if defined?(Encoding) - rv[path] - Regexp.escape(path) + if nil == paths + paths = [ '[^\n]+' ] + else + paths = paths.map do |path| + path = path.dup + path.force_encoding(Encoding::BINARY) if defined?(Encoding) + rv[path] + Regexp.escape(path) + end end - paths = / 00000000 \d+ (\d+)\s+\d+ (#{paths.join('|')})$/n + paths = /^\w+: \d+ \d+ 00000000 \d+ (\d+)\s+\d+ (#{paths.join('|')})$/n # no point in pread since we can't stat for size on this file File.read(*PROC_NET_UNIX_ARGS).scan(paths) do |s| diff --git a/test/test_linux.rb b/test/test_linux.rb index d3c8da7..45dc2e0 100644 --- a/test/test_linux.rb +++ b/test/test_linux.rb @@ -39,6 +39,26 @@ class TestLinux < Test::Unit::TestCase assert_equal 1, stats[tmp.path].queued end + def test_unix_all + tmp = Tempfile.new("\xde\xad\xbe\xef") # valid path, really :) + File.unlink(tmp.path) + us = UNIXServer.new(tmp.path) + uc0 = UNIXSocket.new(tmp.path) + stats = unix_listener_stats + assert_equal 0, stats[tmp.path].active + assert_equal 1, stats[tmp.path].queued + + uc1 = UNIXSocket.new(tmp.path) + stats = unix_listener_stats + assert_equal 0, stats[tmp.path].active + assert_equal 2, stats[tmp.path].queued + + ua0 = us.accept + stats = unix_listener_stats + assert_equal 1, stats[tmp.path].active + assert_equal 1, stats[tmp.path].queued + end + def test_tcp s = TCPServer.new(TEST_ADDR, 0) port = s.addr[1] -- cgit v1.2.3-24-ge0c7