summaryrefslogtreecommitdiff
path: root/lib/webrick/utils.rb
diff options
context:
space:
mode:
authorgotoyuzo <gotoyuzo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-23 16:51:36 +0000
committergotoyuzo <gotoyuzo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-23 16:51:36 +0000
commit01eba908adcd150a7b816af0dbe167c4c4912a90 (patch)
tree0a0d7cc4e66db930a7f74fd1466338979dae91c2 /lib/webrick/utils.rb
parent02f036ddbcb3409092e397b2df247bd9447e8b3f (diff)
* lib/webrick: imported.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4130 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/webrick/utils.rb')
-rw-r--r--lib/webrick/utils.rb64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/webrick/utils.rb b/lib/webrick/utils.rb
new file mode 100644
index 0000000000..646880d655
--- /dev/null
+++ b/lib/webrick/utils.rb
@@ -0,0 +1,64 @@
+#
+# 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'
+begin
+ require 'etc'
+rescue LoadError
+ nil
+end
+
+module WEBrick
+ module Utils
+
+ def set_close_on_exec(io)
+ if defined?(Fcntl::FD_CLOEXEC)
+ io.fcntl(Fcntl::FD_CLOEXEC, 1)
+ end
+ end
+ module_function :set_close_on_exec
+
+ def su(user, group=nil)
+ if defined?(Etc)
+ pw = Etc.getpwnam(user)
+ gr = group ? Etc.getgrnam(group) : pw
+ Process::gid = gr.gid
+ Process::egid = gr.gid
+ Process::uid = pw.uid
+ Process::euid = pw.uid
+ end
+ end
+ module_function :su
+
+ def getservername
+ host = Socket::gethostname
+ begin
+ Socket::gethostbyname(host)[0]
+ rescue
+ host
+ end
+ end
+ module_function :getservername
+
+ RAND_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
+ "0123456789" +
+ "abcdefghijklmnopqrstuvwxyz"
+
+ def random_string(len)
+ rand_max = RAND_CHARS.size
+ ret = ""
+ len.times{ ret << RAND_CHARS[rand(rand_max)] }
+ ret
+ end
+ module_function :random_string
+
+ end
+end