2012-05-24 01:39:35 +01:00
|
|
|
require 'socket'
|
2012-06-07 11:44:19 +01:00
|
|
|
require 'thread'
|
2012-06-21 11:37:00 +01:00
|
|
|
require 'rexml/document'
|
|
|
|
require 'rexml/streamlistener'
|
2012-06-07 11:44:19 +01:00
|
|
|
|
|
|
|
Thread.abort_on_exception = true
|
2012-05-24 01:39:35 +01:00
|
|
|
|
2012-06-21 11:37:00 +01:00
|
|
|
|
|
|
|
class Executor
|
|
|
|
attr_reader :pid
|
|
|
|
|
|
|
|
def run( cmd )
|
|
|
|
@pid = fork do exec @cmd end
|
|
|
|
end
|
|
|
|
end # class Executor
|
|
|
|
|
|
|
|
|
|
|
|
class ValgrindExecutor
|
|
|
|
attr_reader :pid
|
|
|
|
|
|
|
|
class Error
|
|
|
|
attr_accessor :what, :kind
|
|
|
|
attr_reader :backtrace
|
|
|
|
def initialize
|
|
|
|
@backtrace=[]
|
|
|
|
@what = ""
|
|
|
|
@kind = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_frame
|
|
|
|
@backtrace << {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_fn(fn)
|
|
|
|
@backtrace.last[:fn] = fn
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_file(file)
|
|
|
|
@backtrace.last[:file] = file
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_line(line)
|
|
|
|
@backtrace.last[:line] = line
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
([@what + " (#{@kind})"] + @backtrace.map{|h| "#{h[:file]}:#{h[:line]} #{h[:fn]}" }).join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
end # class Error
|
|
|
|
|
|
|
|
|
|
|
|
class ErrorListener
|
|
|
|
include REXML::StreamListener
|
|
|
|
def initialize( killer )
|
|
|
|
@killer = killer
|
|
|
|
end
|
|
|
|
|
|
|
|
def text( text )
|
|
|
|
@text = text
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_start(tag, attrs)
|
|
|
|
case tag.to_s
|
|
|
|
when "error"
|
|
|
|
@error = Error.new
|
|
|
|
when "frame"
|
|
|
|
@error.add_frame
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_end(tag)
|
|
|
|
case tag.to_s
|
|
|
|
when "what"
|
|
|
|
@error.what = @text if @error
|
|
|
|
@text = ""
|
|
|
|
when "kind"
|
|
|
|
@error.kind = @text if @error
|
|
|
|
when "file"
|
|
|
|
@error.add_file( @text ) if @error
|
|
|
|
when "fn"
|
|
|
|
@error.add_fn( @text ) if @error
|
|
|
|
when "line"
|
|
|
|
@error.add_line( @text ) if @error
|
|
|
|
when "error"
|
|
|
|
@killer.call( @error )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end # class ErrorListener
|
|
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@pid = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def run( cmd )
|
|
|
|
@io_r, io_w = IO.pipe
|
|
|
|
@pid = fork do exec( "valgrind --xml=yes --xml-fd=#{io_w.fileno} " + cmd ) end
|
|
|
|
launch_watch_thread( @pid, @io_r )
|
|
|
|
@pid
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def call( err )
|
|
|
|
Process.kill( "KILL", @pid )
|
|
|
|
$stderr.puts "*"*72
|
|
|
|
$stderr.puts "* Valgrind error spotted:"
|
|
|
|
$stderr.puts err.to_s.split("\n").map{|s| " #{s}"}
|
|
|
|
$stderr.puts "*"*72
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
def launch_watch_thread(pid, io_r)
|
|
|
|
Thread.start do
|
|
|
|
io_source = REXML::IOSource.new( io_r )
|
|
|
|
listener = ErrorListener.new( self )
|
|
|
|
REXML::Document.parse_stream( io_source, listener )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end # class ValgrindExecutor
|
|
|
|
|
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
# Noddy test class to exercise FlexNBD from the outside for testing.
|
|
|
|
#
|
|
|
|
class FlexNBD
|
|
|
|
attr_reader :bin, :ctrl, :pid, :ip, :port
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-06-21 11:37:00 +01:00
|
|
|
class << self
|
|
|
|
def counter
|
|
|
|
Dir['tmp/*'].select{|f| File.file?(f)}.length + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
def initialize(bin, ip, port)
|
|
|
|
@bin = bin
|
2012-06-07 11:44:19 +01:00
|
|
|
@debug = `#{@bin} serve --help` =~ /--verbose/ ? "--verbose" : ""
|
2012-05-24 01:39:35 +01:00
|
|
|
raise "#{bin} not executable" unless File.executable?(bin)
|
2012-06-21 11:37:00 +01:00
|
|
|
@executor = ENV['VALGRIND'] ? ValgrindExecutor.new : Executor.new
|
2012-05-24 01:39:35 +01:00
|
|
|
@ctrl = "/tmp/.flexnbd.ctrl.#{Time.now.to_i}.#{rand}"
|
|
|
|
@ip = ip
|
|
|
|
@port = port
|
2012-06-07 11:44:19 +01:00
|
|
|
@kill = false
|
|
|
|
end
|
|
|
|
|
2012-06-21 11:37:00 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
def debug?
|
2012-06-13 13:44:21 +01:00
|
|
|
!@debug.empty? || ENV['DEBUG']
|
2012-06-07 11:44:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def debug( msg )
|
|
|
|
$stderr.puts msg if debug?
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def serve_cmd( file, acl )
|
2012-06-21 11:37:00 +01:00
|
|
|
"#{bin} serve "\
|
2012-06-07 11:44:19 +01:00
|
|
|
"--addr #{ip} "\
|
|
|
|
"--port #{port} "\
|
|
|
|
"--file #{file} "\
|
|
|
|
"--sock #{ctrl} "\
|
|
|
|
"#{@debug} "\
|
|
|
|
"#{acl.join(' ')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def read_cmd( offset, length )
|
2012-06-21 11:37:00 +01:00
|
|
|
"#{bin} read "\
|
2012-06-07 11:44:19 +01:00
|
|
|
"--addr #{ip} "\
|
|
|
|
"--port #{port} "\
|
|
|
|
"--from #{offset} "\
|
|
|
|
"#{@debug} "\
|
|
|
|
"--size #{length}"
|
2012-05-24 01:39:35 +01:00
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
|
|
|
|
def write_cmd( offset, data )
|
2012-06-21 11:37:00 +01:00
|
|
|
"#{bin} write "\
|
2012-06-07 11:44:19 +01:00
|
|
|
"--addr #{ip} "\
|
|
|
|
"--port #{port} "\
|
|
|
|
"--from #{offset} "\
|
|
|
|
"#{@debug} "\
|
|
|
|
"--size #{data.length}"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-06-13 13:44:21 +01:00
|
|
|
def mirror_cmd(dest_ip, dest_port)
|
|
|
|
"#{@bin} mirror "\
|
|
|
|
"--addr #{dest_ip} "\
|
|
|
|
"--port #{dest_port} "\
|
|
|
|
"--sock #{ctrl} "\
|
|
|
|
"#{@debug} "
|
|
|
|
end
|
|
|
|
|
2012-06-06 01:28:30 +01:00
|
|
|
def serve(file, *acl)
|
2012-05-29 17:01:54 +01:00
|
|
|
File.unlink(ctrl) if File.exists?(ctrl)
|
2012-06-07 11:44:19 +01:00
|
|
|
cmd =serve_cmd( file, acl )
|
|
|
|
debug( cmd )
|
|
|
|
|
2012-06-21 11:37:00 +01:00
|
|
|
@pid = @executor.run( cmd )
|
2012-06-07 11:44:19 +01:00
|
|
|
start_wait_thread( @pid )
|
|
|
|
|
2012-06-06 01:28:30 +01:00
|
|
|
while !File.socket?(ctrl)
|
|
|
|
pid, status = Process.wait2(@pid, Process::WNOHANG)
|
|
|
|
raise "server did not start (#{cmd})" if pid
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
at_exit { kill }
|
2012-05-24 01:39:35 +01:00
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
def start_wait_thread( pid )
|
|
|
|
Thread.start do
|
|
|
|
Process.waitpid2( pid )
|
2012-06-13 13:44:21 +01:00
|
|
|
if @kill
|
|
|
|
fail "flexnbd quit with a bad status #{$?.exitstatus}" unless
|
|
|
|
$?.exitstatus == @kill
|
|
|
|
else
|
2012-06-07 11:44:19 +01:00
|
|
|
$stderr.puts "flexnbd quit"
|
|
|
|
fail "flexnbd quit early"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-06-13 13:44:21 +01:00
|
|
|
def can_die(status=0)
|
|
|
|
@kill = status
|
|
|
|
end
|
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
def kill
|
2012-06-13 13:44:21 +01:00
|
|
|
can_die()
|
|
|
|
begin
|
|
|
|
Process.kill("INT", @pid)
|
|
|
|
rescue Errno::ESRCH => e
|
|
|
|
# already dead. Presumably this means it went away after a
|
|
|
|
# can_die() call.
|
|
|
|
end
|
2012-05-24 01:39:35 +01:00
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
def read(offset, length)
|
2012-06-07 11:44:19 +01:00
|
|
|
cmd = read_cmd( offset, length )
|
|
|
|
debug( cmd )
|
|
|
|
|
|
|
|
IO.popen(cmd) do |fh|
|
2012-05-24 01:39:35 +01:00
|
|
|
return fh.read
|
|
|
|
end
|
2012-06-07 02:06:08 +01:00
|
|
|
raise IOError.new "NBD read failed" unless $?.success?
|
|
|
|
out
|
2012-05-24 01:39:35 +01:00
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
def write(offset, data)
|
2012-06-07 11:44:19 +01:00
|
|
|
cmd = write_cmd( offset, data )
|
|
|
|
debug( cmd )
|
2012-06-21 11:37:00 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
IO.popen(cmd, "w") do |fh|
|
2012-05-24 01:39:35 +01:00
|
|
|
fh.write(data)
|
|
|
|
end
|
2012-06-07 02:06:08 +01:00
|
|
|
raise IOError.new "NBD write failed" unless $?.success?
|
2012-05-24 01:39:35 +01:00
|
|
|
nil
|
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-06-13 13:44:21 +01:00
|
|
|
def mirror(dest_ip, dest_port, bandwidth=nil, action=nil)
|
|
|
|
cmd = mirror_cmd( dest_ip, dest_port)
|
|
|
|
debug( cmd )
|
|
|
|
system cmd
|
|
|
|
raise IOError.new( "Migrate command failed") unless $?.success?
|
|
|
|
nil
|
2012-05-24 01:39:35 +01:00
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
def acl(*acl)
|
|
|
|
control_command("acl", *acl)
|
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
def status
|
|
|
|
end
|
2012-05-31 13:23:12 +01:00
|
|
|
|
2012-05-24 01:39:35 +01:00
|
|
|
protected
|
|
|
|
def control_command(*args)
|
|
|
|
raise "Server not running" unless @pid
|
|
|
|
args = args.compact
|
|
|
|
UNIXSocket.open(@ctrl) do |u|
|
|
|
|
u.write(args.join("\n") + "\n")
|
|
|
|
code, message = u.readline.split(": ", 2)
|
|
|
|
return [code, message]
|
2012-05-31 13:23:12 +01:00
|
|
|
end
|
2012-05-24 01:39:35 +01:00
|
|
|
end
|
|
|
|
end
|
2012-06-06 13:33:24 +01:00
|
|
|
|