2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2003-07-23 20: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'
|
2015-05-17 11:22:11 +03:00
|
|
|
require 'io/nonblock'
|
2014-04-25 14:17:15 +04:00
|
|
|
require 'etc'
|
2003-07-23 20:51:36 +04:00
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
module Utils
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Sets IO operations on +io+ to be non-blocking
|
2005-07-15 02:59:09 +04:00
|
|
|
def set_non_blocking(io)
|
2015-05-17 11:22:11 +03:00
|
|
|
io.nonblock = true if io.respond_to?(:nonblock=)
|
2005-07-15 02:59:09 +04:00
|
|
|
end
|
|
|
|
module_function :set_non_blocking
|
2003-07-23 20:51:36 +04:00
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Sets the close on exec flag for +io+
|
2003-07-23 20:51:36 +04:00
|
|
|
def set_close_on_exec(io)
|
2015-05-17 11:22:11 +03:00
|
|
|
io.close_on_exec = true if io.respond_to?(:close_on_exec=)
|
2003-07-23 20:51:36 +04:00
|
|
|
end
|
|
|
|
module_function :set_close_on_exec
|
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Changes the process's uid and gid to the ones of +user+
|
2003-09-28 21:50:52 +04:00
|
|
|
def su(user)
|
2014-04-25 14:17:15 +04:00
|
|
|
if pw = Etc.getpwnam(user)
|
2003-09-28 21:50:52 +04:00
|
|
|
Process::initgroups(user, pw.gid)
|
|
|
|
Process::Sys::setgid(pw.gid)
|
|
|
|
Process::Sys::setuid(pw.uid)
|
|
|
|
else
|
2017-12-12 14:56:25 +03:00
|
|
|
warn("WEBrick::Utils::su doesn't work on this platform", uplevel: 1)
|
2003-09-28 21:50:52 +04:00
|
|
|
end
|
|
|
|
end
|
2003-07-23 20:51:36 +04:00
|
|
|
module_function :su
|
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# The server hostname
|
2003-07-23 20:51:36 +04:00
|
|
|
def getservername
|
|
|
|
host = Socket::gethostname
|
|
|
|
begin
|
|
|
|
Socket::gethostbyname(host)[0]
|
|
|
|
rescue
|
|
|
|
host
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :getservername
|
|
|
|
|
2011-06-16 09: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.
|
2015-01-02 09:53:12 +03:00
|
|
|
def create_listeners(address, port)
|
2005-03-23 13:09:07 +03:00
|
|
|
unless port
|
|
|
|
raise ArgumentError, "must specify port"
|
|
|
|
end
|
2013-03-02 12:36:16 +04:00
|
|
|
sockets = Socket.tcp_server_sockets(address, port)
|
|
|
|
sockets = sockets.map {|s|
|
|
|
|
s.autoclose = false
|
2014-06-28 10:33:44 +04:00
|
|
|
ts = TCPServer.for_fd(s.fileno)
|
|
|
|
s.close
|
|
|
|
ts
|
2003-08-19 10:00:36 +04:00
|
|
|
}
|
|
|
|
return sockets
|
|
|
|
end
|
|
|
|
module_function :create_listeners
|
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Characters used to generate random strings
|
2003-07-23 20:51:36 +04:00
|
|
|
RAND_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
|
|
"0123456789" +
|
2009-03-06 06:56:38 +03:00
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
2003-07-23 20:51:36 +04:00
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Generates a random string of length +len+
|
2003-07-23 20:51:36 +04:00
|
|
|
def random_string(len)
|
2008-11-08 12:41:24 +03:00
|
|
|
rand_max = RAND_CHARS.bytesize
|
2009-03-06 06:56:38 +03:00
|
|
|
ret = ""
|
2003-07-23 20:51:36 +04:00
|
|
|
len.times{ ret << RAND_CHARS[rand(rand_max)] }
|
2009-03-06 06:56:38 +03:00
|
|
|
ret
|
2003-07-23 20:51:36 +04:00
|
|
|
end
|
|
|
|
module_function :random_string
|
|
|
|
|
2006-05-18 17:42:52 +04:00
|
|
|
###########
|
|
|
|
|
|
|
|
require "timeout"
|
|
|
|
require "singleton"
|
|
|
|
|
2011-06-16 09: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 17:42:52 +04:00
|
|
|
class TimeoutHandler
|
|
|
|
include Singleton
|
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Mutex used to synchronize access across threads
|
2016-08-30 09:22:30 +03:00
|
|
|
TimeoutMutex = Thread::Mutex.new # :nodoc:
|
2011-06-16 09:55:31 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Registers a new timeout handler
|
|
|
|
#
|
|
|
|
# +time+:: Timeout in seconds
|
|
|
|
# +exception+:: Exception to raise when timeout elapsed
|
2006-05-18 17:42:52 +04:00
|
|
|
def TimeoutHandler.register(seconds, exception)
|
2016-10-14 04:48:52 +03:00
|
|
|
at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds
|
|
|
|
instance.register(Thread.current, at, exception)
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Cancels the timeout handler +id+
|
2006-05-18 17:42:52 +04:00
|
|
|
def TimeoutHandler.cancel(id)
|
2015-12-15 19:03:00 +03:00
|
|
|
instance.cancel(Thread.current, id)
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
|
|
|
|
2016-01-05 09:09:17 +03:00
|
|
|
def self.terminate
|
|
|
|
instance.terminate
|
|
|
|
end
|
|
|
|
|
2013-01-26 05:12:54 +04:00
|
|
|
##
|
|
|
|
# Creates a new TimeoutHandler. You should use ::register and ::cancel
|
|
|
|
# instead of creating the timeout handler directly.
|
2006-05-18 17:42:52 +04:00
|
|
|
def initialize
|
2015-12-15 19:03:00 +03:00
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
@timeout_info = Hash.new
|
|
|
|
}
|
2016-08-30 09:22:30 +03:00
|
|
|
@queue = Thread::Queue.new
|
2016-01-05 09:09:17 +03:00
|
|
|
@watcher = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
private \
|
|
|
|
def watch
|
2015-12-15 18:26:47 +03:00
|
|
|
to_interrupt = []
|
2006-05-18 17:42:52 +04:00
|
|
|
while true
|
2016-10-14 04:48:52 +03:00
|
|
|
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
2015-07-14 05:21:53 +03:00
|
|
|
wakeup = nil
|
2015-12-15 18:26:47 +03:00
|
|
|
to_interrupt.clear
|
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
@timeout_info.each {|thread, ary|
|
|
|
|
next unless ary
|
|
|
|
ary.each{|info|
|
|
|
|
time, exception = *info
|
|
|
|
if time < now
|
|
|
|
to_interrupt.push [thread, info.object_id, exception]
|
|
|
|
elsif !wakeup || time < wakeup
|
|
|
|
wakeup = time
|
|
|
|
end
|
|
|
|
}
|
2006-05-18 17:42:52 +04:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 18:26:47 +03:00
|
|
|
to_interrupt.each {|arg| interrupt(*arg)}
|
2015-07-14 05:21:53 +03:00
|
|
|
if !wakeup
|
2015-12-18 15:32:53 +03:00
|
|
|
@queue.pop
|
2015-07-14 05:21:53 +03:00
|
|
|
elsif (wakeup -= now) > 0
|
2015-12-18 15:32:53 +03:00
|
|
|
begin
|
2015-12-19 11:16:54 +03:00
|
|
|
(th = Thread.start {@queue.pop}).join(wakeup)
|
|
|
|
ensure
|
|
|
|
th&.kill&.join
|
2015-12-18 15:32:53 +03:00
|
|
|
end
|
2015-07-14 05:21:53 +03:00
|
|
|
end
|
2015-12-18 15:32:53 +03:00
|
|
|
@queue.clear
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
2016-01-05 09:09:17 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
private \
|
|
|
|
def watcher
|
|
|
|
(w = @watcher)&.alive? and return w # usual case
|
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
(w = @watcher)&.alive? and next w # pathological check
|
|
|
|
@watcher = Thread.start(&method(:watch))
|
|
|
|
}
|
|
|
|
end
|
2006-05-18 17:42:52 +04:00
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Interrupts the timeout handler +id+ and raises +exception+
|
2006-05-18 17:42:52 +04:00
|
|
|
def interrupt(thread, id, exception)
|
2015-12-15 19:03:00 +03:00
|
|
|
if cancel(thread, id) && thread.alive?
|
|
|
|
thread.raise(exception, "execution timeout")
|
|
|
|
end
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Registers a new timeout handler
|
|
|
|
#
|
|
|
|
# +time+:: Timeout in seconds
|
|
|
|
# +exception+:: Exception to raise when timeout elapsed
|
2006-05-18 17:42:52 +04:00
|
|
|
def register(thread, time, exception)
|
2015-12-15 19:03:00 +03:00
|
|
|
info = nil
|
|
|
|
TimeoutMutex.synchronize{
|
2016-01-05 09:09:17 +03:00
|
|
|
(@timeout_info[thread] ||= []) << (info = [time, exception])
|
2015-12-15 19:03:00 +03:00
|
|
|
}
|
2015-12-18 15:32:53 +03:00
|
|
|
@queue.push nil
|
2016-01-05 09:09:17 +03:00
|
|
|
watcher
|
2015-07-14 05:22:51 +03:00
|
|
|
return info.object_id
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
|
|
|
|
2011-06-16 09:55:31 +04:00
|
|
|
##
|
|
|
|
# Cancels the timeout handler +id+
|
2006-05-18 17:42:52 +04:00
|
|
|
def cancel(thread, id)
|
2015-12-15 19:03:00 +03:00
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
if ary = @timeout_info[thread]
|
|
|
|
ary.delete_if{|info| info.object_id == id }
|
|
|
|
if ary.empty?
|
|
|
|
@timeout_info.delete(thread)
|
|
|
|
end
|
|
|
|
return true
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
2015-12-15 19:03:00 +03:00
|
|
|
return false
|
|
|
|
}
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
2016-01-05 09:09:17 +03:00
|
|
|
|
|
|
|
##
|
|
|
|
def terminate
|
|
|
|
TimeoutMutex.synchronize{
|
|
|
|
@timeout_info.clear
|
|
|
|
@watcher&.kill&.join
|
|
|
|
}
|
|
|
|
end
|
2006-05-18 17:42:52 +04:00
|
|
|
end
|
|
|
|
|
2011-06-16 09: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 17:42:52 +04:00
|
|
|
def timeout(seconds, exception=Timeout::Error)
|
|
|
|
return yield if seconds.nil? or seconds.zero?
|
2006-12-31 18:02:22 +03:00
|
|
|
# raise ThreadError, "timeout within critical session" if Thread.critical
|
2006-05-18 17:42:52 +04:00
|
|
|
id = TimeoutHandler.register(seconds, exception)
|
|
|
|
begin
|
|
|
|
yield(seconds)
|
|
|
|
ensure
|
|
|
|
TimeoutHandler.cancel(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :timeout
|
2003-07-23 20:51:36 +04:00
|
|
|
end
|
|
|
|
end
|