1 | #
|
---|
2 | # utils.rb -- Miscellaneous utilities
|
---|
3 | #
|
---|
4 | # Author: IPR -- Internet Programming with Ruby -- writers
|
---|
5 | # Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
---|
6 | # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
---|
7 | # reserved.
|
---|
8 | #
|
---|
9 | # $IPR: utils.rb,v 1.10 2003/02/16 22:22:54 gotoyuzo Exp $
|
---|
10 |
|
---|
11 | require 'socket'
|
---|
12 | require 'fcntl'
|
---|
13 | begin
|
---|
14 | require 'etc'
|
---|
15 | rescue LoadError
|
---|
16 | nil
|
---|
17 | end
|
---|
18 |
|
---|
19 | module WEBrick
|
---|
20 | module Utils
|
---|
21 | def set_non_blocking(io)
|
---|
22 | flag = File::NONBLOCK
|
---|
23 | if defined?(Fcntl::F_GETFL)
|
---|
24 | flag |= io.fcntl(Fcntl::F_GETFL)
|
---|
25 | end
|
---|
26 | io.fcntl(Fcntl::F_SETFL, flag)
|
---|
27 | end
|
---|
28 | module_function :set_non_blocking
|
---|
29 |
|
---|
30 | def set_close_on_exec(io)
|
---|
31 | if defined?(Fcntl::FD_CLOEXEC)
|
---|
32 | io.fcntl(Fcntl::FD_CLOEXEC, 1)
|
---|
33 | end
|
---|
34 | end
|
---|
35 | module_function :set_close_on_exec
|
---|
36 |
|
---|
37 | def su(user)
|
---|
38 | if defined?(Etc)
|
---|
39 | pw = Etc.getpwnam(user)
|
---|
40 | Process::initgroups(user, pw.gid)
|
---|
41 | Process::Sys::setgid(pw.gid)
|
---|
42 | Process::Sys::setuid(pw.uid)
|
---|
43 | else
|
---|
44 | warn("WEBrick::Utils::su doesn't work on this platform")
|
---|
45 | end
|
---|
46 | end
|
---|
47 | module_function :su
|
---|
48 |
|
---|
49 | def getservername
|
---|
50 | host = Socket::gethostname
|
---|
51 | begin
|
---|
52 | Socket::gethostbyname(host)[0]
|
---|
53 | rescue
|
---|
54 | host
|
---|
55 | end
|
---|
56 | end
|
---|
57 | module_function :getservername
|
---|
58 |
|
---|
59 | def create_listeners(address, port, logger=nil)
|
---|
60 | unless port
|
---|
61 | raise ArgumentError, "must specify port"
|
---|
62 | end
|
---|
63 | res = Socket::getaddrinfo(address, port,
|
---|
64 | Socket::AF_UNSPEC, # address family
|
---|
65 | Socket::SOCK_STREAM, # socket type
|
---|
66 | 0, # protocol
|
---|
67 | Socket::AI_PASSIVE) # flag
|
---|
68 | last_error = nil
|
---|
69 | sockets = []
|
---|
70 | res.each{|ai|
|
---|
71 | begin
|
---|
72 | logger.debug("TCPServer.new(#{ai[3]}, #{port})") if logger
|
---|
73 | sock = TCPServer.new(ai[3], port)
|
---|
74 | port = sock.addr[1] if port == 0
|
---|
75 | Utils::set_close_on_exec(sock)
|
---|
76 | sockets << sock
|
---|
77 | rescue => ex
|
---|
78 | logger.warn("TCPServer Error: #{ex}") if logger
|
---|
79 | last_error = ex
|
---|
80 | end
|
---|
81 | }
|
---|
82 | raise last_error if sockets.empty?
|
---|
83 | return sockets
|
---|
84 | end
|
---|
85 | module_function :create_listeners
|
---|
86 |
|
---|
87 | RAND_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
---|
88 | "0123456789" +
|
---|
89 | "abcdefghijklmnopqrstuvwxyz"
|
---|
90 |
|
---|
91 | def random_string(len)
|
---|
92 | rand_max = RAND_CHARS.size
|
---|
93 | ret = ""
|
---|
94 | len.times{ ret << RAND_CHARS[rand(rand_max)] }
|
---|
95 | ret
|
---|
96 | end
|
---|
97 | module_function :random_string
|
---|
98 |
|
---|
99 | end
|
---|
100 | end
|
---|