summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-29 17:41:32 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-29 17:41:32 +0000
commit316a0aa03d8dda2f7b8d901602d515fbde9010be (patch)
tree741c93105cc6a54a5a94c25433bf53a038966908
parent098adfab2881d75fdf29bf19b6332e4f935ab992 (diff)
* prelude.rb (Process.daemon): New method.
* .document: Add prelude.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@27093 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--.document3
-rw-r--r--ChangeLog6
-rw-r--r--NEWS4
-rw-r--r--prelude.rb35
4 files changed, 48 insertions, 0 deletions
diff --git a/.document b/.document
index 230c50e387..f73474c126 100644
--- a/.document
+++ b/.document
@@ -6,6 +6,9 @@
# Process all the C source files
*.c
+# prelude
+prelude.rb
+
# the lib/ directory (which has its own .document file)
lib
diff --git a/ChangeLog b/ChangeLog
index 2335d02654..aa579796f0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Mar 30 02:40:31 2010 Akinori MUSHA <[email protected]>
+
+ * prelude.rb (Process.daemon): New method.
+
+ * .document: Add prelude.rb.
+
Mon Mar 29 17:38:24 2010 Keiju Ishitsuka <[email protected]>
* ext/rational/lib/rational.rb: fix [Bug #1397].
diff --git a/NEWS b/NEWS
index 5e561cdfb5..1152b248cd 100644
--- a/NEWS
+++ b/NEWS
@@ -102,6 +102,10 @@ with all sufficient information, see the ChangeLog file.
New method primarily for use in the case-when construct.
+ * Process.daemon
+
+ New method.
+
* Range#cover?
New alias to #include? for the forward compatibility with 1.9, in
diff --git a/prelude.rb b/prelude.rb
index 2265a4b9d1..cbdddef8ab 100644
--- a/prelude.rb
+++ b/prelude.rb
@@ -1 +1,36 @@
# currently empty
+
+module Process
+ # call-seq:
+ # Process.daemon() => 0
+ # Process.daemon(nochdir=nil,noclose=nil) => 0
+ #
+ # Detach the process from controlling terminal and run in
+ # the background as system daemon. Unless the argument
+ # nochdir is true (i.e. non false), it changes the current
+ # working directory to the root ("/"). Unless the argument
+ # noclose is true, daemon() will redirect standard input,
+ # standard output and standard error to /dev/null.
+ # Return zero on success, or raise one of Errno::*.
+ def self.daemon(nochdir = nil, noclose = nil)
+ if $SAFE >= 2
+ raise SecurityError, "Insecure operation `%s' at level %d", __method__, $SAFE
+ end
+
+ fork && exit!(0)
+
+ Process.setsid()
+
+ fork && exit!(0)
+
+ Dir.chdir('/') unless nochdir
+
+ File.open('/dev/null', 'r+') { |f|
+ STDIN.reopen(f)
+ STDOUT.reopen(f)
+ STDERR.reopen(f)
+ } unless noclose
+
+ return 0
+ end
+end