2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# utils.rb -- Miscellaneous utilities
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: utils.rb,v 1.10 2003/02/16 22:22:54 gotoyuzo Exp $
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'fcntl'
|
2014-04-25 06:17:15 -04:00
|
|
|
require 'etc'
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
module Utils
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Sets IO operations on +io+ to be non-blocking
|
2005-07-14 18:59:09 -04:00
|
|
|
def set_non_blocking(io)
|
|
|
|
flag = File::NONBLOCK
|
|
|
|
if defined?(Fcntl::F_GETFL)
|
|
|
|
flag |= io.fcntl(Fcntl::F_GETFL)
|
|
|
|
end
|
|
|
|
io.fcntl(Fcntl::F_SETFL, flag)
|
|
|
|
end
|
|
|
|
module_function :set_non_blocking
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Sets the close on exec flag for +io+
|
2003-07-23 12:51:36 -04:00
|
|
|
def set_close_on_exec(io)
|
|
|
|
if defined?(Fcntl::FD_CLOEXEC)
|
2011-11-01 19:17:53 -04:00
|
|
|
io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :set_close_on_exec
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Changes the process's uid and gid to the ones of +user+
|
2003-09-28 13:50:52 -04:00
|
|
|
def su(user)
|
2014-04-25 06:17:15 -04:00
|
|
|
if pw = Etc.getpwnam(user)
|
2003-09-28 13:50:52 -04:00
|
|
|
Process::initgroups(user, pw.gid)
|
|
|
|
Process::Sys::setgid(pw.gid)
|
|
|
|
Process::Sys::setuid(pw.uid)
|
|
|
|
else
|
|
|
|
warn("WEBrick::Utils::su doesn't work on this platform")
|
|
|
|
end
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
module_function :su
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# The server hostname
|
2003-07-23 12:51:36 -04:00
|
|
|
def getservername
|
|
|
|
host = Socket::gethostname
|
|
|
|
begin
|
|
|
|
Socket::gethostbyname(host)[0]
|
|
|
|
rescue
|
|
|
|
host
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :getservername
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Creates TCP server sockets bound to +address+:+port+ and returns them.
|
|
|
|
#
|
|
|
|
# It will create IPV4 and IPV6 sockets on all interfaces.
|
2003-08-19 02:00:36 -04:00
|
|
|
def create_listeners(address, port, logger=nil)
|
2005-03-23 05:09:07 -05:00
|
|
|
unless port
|
|
|
|
raise ArgumentError, "must specify port"
|
|
|
|
end
|
2013-03-02 03:36:16 -05:00
|
|
|
sockets = Socket.tcp_server_sockets(address, port)
|
|
|
|
sockets = sockets.map {|s|
|
|
|
|
s.autoclose = false
|
|
|
|
TCPServer.for_fd(s.fileno)
|
2003-08-19 02:00:36 -04:00
|
|
|
}
|
|
|
|
return sockets
|
|
|
|
end
|
|
|
|
module_function :create_listeners
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Characters used to generate random strings
|
2003-07-23 12:51:36 -04:00
|
|
|
RAND_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
|
|
"0123456789" +
|
2009-03-05 22:56:38 -05:00
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Generates a random string of length +len+
|
2003-07-23 12:51:36 -04:00
|
|
|
def random_string(len)
|
2008-11-08 04:41:24 -05:00
|
|
|
rand_max = RAND_CHARS.bytesize
|
2009-03-05 22:56:38 -05:00
|
|
|
ret = ""
|
2003-07-23 12:51:36 -04:00
|
|
|
len.times{ ret << RAND_CHARS[rand(rand_max)] }
|
2009-03-05 22:56:38 -05:00
|
|
|
ret
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
module_function :random_string
|
|
|
|
|
2006-05-18 09:42:52 -04:00
|
|
|
###########
|
|
|
|
|
|
|
|
require "thread"
|
|
|
|
require "timeout"
|
|
|
|
require "singleton"
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Class used to manage timeout handlers across multiple threads.
|
|
|
|
#
|
|
|
|
# Timeout handlers should be managed by using the class methods which are
|
|
|
|
# synchronized.
|
|
|
|
#
|
|
|
|
# id = TimeoutHandler.register(10, Timeout::Error)
|
|
|
|
# begin
|
|
|
|
# sleep 20
|
|
|
|
# puts 'foo'
|
|
|
|
# ensure
|
|
|
|
# TimeoutHandler.cancel(id)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# will raise Timeout::Error
|
|
|
|
#
|
|
|
|
# id = TimeoutHandler.register(10, Timeout::Error)
|
|
|
|
# begin
|
|
|
|
# sleep 5
|
|
|
|
# puts 'foo'
|
|
|
|
# ensure
|
|
|
|
# TimeoutHandler.cancel(id)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# will print 'foo'
|
|
|
|
#
|
2006-05-18 09:42:52 -04:00
|
|
|
class TimeoutHandler
|
|
|
|
include Singleton
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Mutex used to synchronize access across threads
|
|
|
|
TimeoutMutex = Mutex.new # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Registers a new timeout handler
|
|
|
|
#
|
|
|
|
# +time+:: Timeout in seconds
|
|
|
|
# +exception+:: Exception to raise when timeout elapsed
|
2006-05-18 09:42:52 -04:00
|
|
|
def TimeoutHandler.register(seconds, exception)
|
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
instance.register(Thread.current, Time.now + seconds, exception)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Cancels the timeout handler +id+
|
2006-05-18 09:42:52 -04:00
|
|
|
def TimeoutHandler.cancel(id)
|
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
instance.cancel(Thread.current, id)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
# Creates a new TimeoutHandler. You should use ::register and ::cancel
|
|
|
|
# instead of creating the timeout handler directly.
|
2006-05-18 09:42:52 -04:00
|
|
|
def initialize
|
|
|
|
@timeout_info = Hash.new
|
|
|
|
Thread.start{
|
|
|
|
while true
|
|
|
|
now = Time.now
|
2012-04-07 19:41:30 -04:00
|
|
|
@timeout_info.keys.each{|thread|
|
|
|
|
ary = @timeout_info[thread]
|
|
|
|
next unless ary
|
2007-01-04 03:32:37 -05:00
|
|
|
ary.dup.each{|info|
|
2006-05-18 09:42:52 -04:00
|
|
|
time, exception = *info
|
|
|
|
interrupt(thread, info.object_id, exception) if time < now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep 0.5
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Interrupts the timeout handler +id+ and raises +exception+
|
2006-05-18 09:42:52 -04:00
|
|
|
def interrupt(thread, id, exception)
|
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
if cancel(thread, id) && thread.alive?
|
|
|
|
thread.raise(exception, "execution timeout")
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Registers a new timeout handler
|
|
|
|
#
|
|
|
|
# +time+:: Timeout in seconds
|
|
|
|
# +exception+:: Exception to raise when timeout elapsed
|
2006-05-18 09:42:52 -04:00
|
|
|
def register(thread, time, exception)
|
|
|
|
@timeout_info[thread] ||= Array.new
|
|
|
|
@timeout_info[thread] << [time, exception]
|
|
|
|
return @timeout_info[thread].last.object_id
|
|
|
|
end
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Cancels the timeout handler +id+
|
2006-05-18 09:42:52 -04:00
|
|
|
def cancel(thread, id)
|
|
|
|
if ary = @timeout_info[thread]
|
|
|
|
ary.delete_if{|info| info.object_id == id }
|
|
|
|
if ary.empty?
|
|
|
|
@timeout_info.delete(thread)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-16 01:55:31 -04:00
|
|
|
##
|
|
|
|
# Executes the passed block and raises +exception+ if execution takes more
|
|
|
|
# than +seconds+.
|
|
|
|
#
|
|
|
|
# If +seconds+ is zero or nil, simply executes the block
|
2006-05-18 09:42:52 -04:00
|
|
|
def timeout(seconds, exception=Timeout::Error)
|
|
|
|
return yield if seconds.nil? or seconds.zero?
|
2006-12-31 10:02:22 -05:00
|
|
|
# raise ThreadError, "timeout within critical session" if Thread.critical
|
2006-05-18 09:42:52 -04:00
|
|
|
id = TimeoutHandler.register(seconds, exception)
|
|
|
|
begin
|
|
|
|
yield(seconds)
|
|
|
|
ensure
|
|
|
|
TimeoutHandler.cancel(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :timeout
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|