summaryrefslogtreecommitdiff
path: root/process.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-08-17 09:02:40 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-08-17 09:02:40 +0000
commit67232b21511423f82ab18db04f941e6925338a7b (patch)
tree922299c561755f4a987420495fbf1392ef7ea1cc /process.c
parent4af25f5813cd98baa93c459c49718ef62882913a (diff)
* io.c (rb_io_reopen): should clear allocated OpenFile. pointed
out by Guy Decoux. [ruby-core:03288] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6781 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'process.c')
-rw-r--r--process.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/process.c b/process.c
index 67171ba5b5..0132c31606 100644
--- a/process.c
+++ b/process.c
@@ -2663,6 +2663,60 @@ proc_setmaxgroups(obj, val)
return INT2FIX(maxgroups);
}
+/*
+ * call-seq:
+ * Process.daemon() => fixnum
+ * Process.daemon(nochdir=0,noclose=0) => fixnum
+ *
+ * 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.
+ */
+
+static VALUE
+proc_daemon(argc, argv)
+ int argc;
+ VALUE *argv;
+{
+ VALUE nochdir, noclose;
+ int n;
+
+ rb_scan_args(argc, argv, "02", &nochdir, &noclose);
+
+#if defined(HAVE_DAEMON)
+ n = daemon(RTEST(nochdir), RTEST(noclose));
+ if (n < 0) rb_sys_fail("daemon");
+ return INT2FIX(n);
+#elif defined(HAVE_FORK)
+ switch (rb_fork(0, 0, 0)) {
+ case -1:
+ return (-1);
+ case 0:
+ break;
+ default:
+ _exit(0);
+ }
+
+ proc_setsid();
+
+ if (!RTEST(nochdir))
+ (void)chdir("/");
+
+ if (!RTEST(noclose) && (n = open("/dev/null", O_RDWR, 0)) != -1) {
+ (void)dup2(n, 0);
+ (void)dup2(n, 1);
+ (void)dup2(n, 2);
+ if (n > 2)
+ (void)close (n);
+ }
+ return INT2FIX(0);
+#else
+ rb_notimplement();
+#endif
+}
/********************************************************************
*
@@ -3543,6 +3597,8 @@ Init_process()
rb_define_module_function(rb_mProcess, "maxgroups", proc_getmaxgroups, 0);
rb_define_module_function(rb_mProcess, "maxgroups=", proc_setmaxgroups, 1);
+ rb_define_module_function(rb_mProcess, "daemon", proc_daemon, -1);
+
rb_define_module_function(rb_mProcess, "times", rb_proc_times, 0);
#if defined(HAVE_TIMES) || defined(_WIN32)