From afbd5661a0ca041ac818c9e5732911c5db21c9f9 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 13 Jul 2010 12:31:17 +0000 Subject: * process.c (rb_daemon): split from proc_daemon. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28630 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- process.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'process.c') diff --git a/process.c b/process.c index 8e601ba850..84fed6053c 100644 --- a/process.c +++ b/process.c @@ -4554,6 +4554,11 @@ proc_setmaxgroups(VALUE obj, VALUE val) } #if defined(HAVE_DAEMON) || (defined(HAVE_FORK) && defined(HAVE_SETSID)) +#ifndef HAVE_DAEMON +static int rb_daemon(int nochdir, int noclose); +#define daemon(nochdir, noclose) rb_daemon(nochdir, noclose) +#endif + /* * call-seq: * Process.daemon() -> 0 @@ -4577,14 +4582,20 @@ proc_daemon(int argc, VALUE *argv) rb_secure(2); rb_scan_args(argc, argv, "02", &nochdir, &noclose); -#if defined(HAVE_DAEMON) prefork(); before_fork(); n = daemon(RTEST(nochdir), RTEST(noclose)); after_fork(); if (n < 0) rb_sys_fail("daemon"); return INT2FIX(n); -#elif defined(HAVE_FORK) +} + +#ifndef HAVE_DAEMON +static int +rb_daemon(int nochdir, int noclose) +{ + int n, err = 0; + switch (rb_fork(0, 0, 0, Qnil)) { case -1: rb_sys_fail("daemon"); @@ -4599,26 +4610,26 @@ proc_daemon(int argc, VALUE *argv) /* must not be process-leader */ switch (rb_fork(0, 0, 0, Qnil)) { case -1: - rb_sys_fail("daemon"); + return -1; case 0: break; default: _exit(EXIT_SUCCESS); } - if (!RTEST(nochdir)) - (void)chdir("/"); + if (!nochdir) + err = chdir("/"); - if (!RTEST(noclose) && (n = open("/dev/null", O_RDWR, 0)) != -1) { + if (!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); -#endif + return err; } +#endif #else #define proc_daemon rb_f_notimplement #endif -- cgit v1.2.3