Signals

Gary Willoughby via Digitalmars-d digitalmars-d at puremagic.com
Wed May 27 10:29:37 PDT 2015


On Wednesday, 27 May 2015 at 13:52:33 UTC, Robbin wrote:
> I'm writing a daemon in D on linux.  I need to be able to stop 
> (kill) it. The traditional C method is a signal handler.  I can 
> do this, but I can't figure out how to get the handler to do 
> anything to tell the rest of the daemon to quit.
>
> In a C++ handler, I closed the files that were open, removed a 
> PID file and exited.  Nothing complicated in the job.  It was 
> more complicated in the implementation - I did a longjump from 
> the signal handler to execute the cleanup code.
>
> I would love to setup "std.signals" classes and from the 
> "core.stdc.signal" signal handler call "std.signals" emit() to 
> trigger the "std.signals" observer.
>
> Is there a canonical D way of catching a SIGTERM and unwinding 
> everything?
>
> RC

I've usually just done this:

import core.stdc.stdlib : exit;
import core.sys.posix.signal : bsd_signal, SIGTERM;

extern(C) void handleTermination(int signal)
{
	// Clean up code here.
	exit(signal);
}

bsd_signal(SIGTERM, &handleTermination);


More information about the Digitalmars-d mailing list