August 30, 2014
> I tested it again, and it works fine in both 2.065 and 2.066.

Be aware that you should comment out:
//	close(STDOUT_FILENO);
//	close(STDERR_FILENO);

A daemon normally detaches itself from the terminal by closing the STD* files. All D's runtime exception messages will not appear in that case.

> At least theoretically, whatever happens in the parent should not affect the child, so I don't really believe it has something to do with the way exit() works. You can actually test this by making the parent not exit immediately, but sleep for some time.

Good point. I'll try that.

> And just to be sure: have you tested your program without the call to daemonize?

Yes, it writes a dot to the logfile every 2 seconds :-)
August 30, 2014
On Saturday, 30 August 2014 at 19:52:24 UTC, JD wrote:
>> I tested it again, and it works fine in both 2.065 and 2.066.
>
> Be aware that you should comment out:
> //	close(STDOUT_FILENO);
> //	close(STDERR_FILENO);
>
> A daemon normally detaches itself from the terminal by closing the STD* files. All D's runtime exception messages will not appear in that case.
>
>> At least theoretically, whatever happens in the parent should not affect the child, so I don't really believe it has something to do with the way exit() works. You can actually test this by making the parent not exit immediately, but sleep for some time.
>
> Good point. I'll try that.
>
>> And just to be sure: have you tested your program without the call to daemonize?
>
> Yes, it writes a dot to the logfile every 2 seconds :-)

Last snippet works for me, dots get printed to the logfile as expected.
August 31, 2014
>
> Last snippet works for me, dots get printed to the logfile as expected.

Ok, it works now. Using the recommended _Exit() function with DMD 2.066 on Linux.
Thanks you all for your help!

Best regards,
Jeroen
August 31, 2014
On Sunday, 31 August 2014 at 09:02:55 UTC, JD wrote:
>>
>> Last snippet works for me, dots get printed to the logfile as expected.
>
> Ok, it works now. Using the recommended _Exit() function with DMD 2.066 on Linux.
> Thanks you all for your help!
>
> Best regards,
> Jeroen

On a side note, i've created daemons like this before but then i found a rather nice posix system call to do it all for me:

extern (C)
{
	/**
	 * The daemon() function is for programs wishing to detach themselves
	 * from the controlling terminal and run in the background as system
	 * daemons.
	 *
	 * (This function forks, and if the fork(2) succeeds, the parent calls
	 * _exit(2), so that further errors are seen by the child only.)  On
	 * success daemon() returns zero. If an error occurs, daemon() returns
	 * -1 and sets errno to any of the errors specified for the fork(2) and
	 * setsid(2).
	 *
	 * Params:
	 *     nochdir = If nochdir is zero, daemon() changes the calling process's
	 *     current working directory to the root directory ("/"); otherwise,
	 *     the current working directory is left unchanged.
	 *     noclose = If noclose is zero, daemon() redirects standard input,
	 *     standard output and standard error to /dev/null; otherwise, no
	 *     changes are made to these file descriptors.
	 */
	int daemon(int nochdir, int noclose);
}

This is a lot easier to use. :)
August 31, 2014
On 30/08/2014 19:09, JD wrote:
>
> My questions:
> 1. Are there any special considerations w.r.t. the GC after using
> fork()? Or must it be disabled?
> 2. Is it allowed to use stdlib's exit() without cleaning up after the
> runtime (as a normal end of program probably does)? Or is there a safe D
> exit()?

For the second question, Runtime.terminate() from the core.runtime package might be a possible answer.

http://dlang.org/phobos/core_runtime.html#.Runtime.terminate

I don't know how calling Runtime.terminate() in forks behaves though.

1 2
Next ›   Last »