[fpc-pascal] How to handle External:SIGPIPE on linux

Ewald ewald at yellowcouch.org
Sat Jun 1 14:02:24 CEST 2013

On 01 Jun 2013, at 08:51, Dennis Poon wrote:

> Ewald,
> Please kindly share your sample codes for both approaches.
> Thanks a lot

Right, here you go:

*** fpSignal() ***
First you declare a function which is going to handle the signal (SignalHandler in my example), then you just call 
	fpSignal(SIGPIPE, @SignalHandler);
An example program:

===code begin===
Program SignalTest;

Uses sysutils, baseunix;

Procedure SignalHandler(SigNo: cint); cdecl;
    If SigNo = SIGPIPE Then WriteLn('Received SIGPIPE.');

	fpSignal(SIGPIPE, @SignalHandler);

	while true do sleep(5000);
===code end===

Compile using `fpc SignalTest.pas`. Whilst running it, try sending SIGPIPE to it (`killall -PIPE SignalTest`) and it should write something to stdout.


It basically comes down to:
	- Emtying a signal set
	- Adding the signals you wish to block
	- Calling pthread_sigmask with this set and SIG_BLOCK as arguments.

And example program:

===code begin===
Program SigmaskTest;

Uses sysutils, baseunix, pthreads;

	ToBeBlocked: sigset_t;
	fpsigaddset(ToBeBlocked, SIGPIPE);
	pthread_sigmask(SIG_BLOCK, @ToBeBlocked, nil);

	while true do sleep(5000);
===code end===

Compile using `fpc SigmaskTest.pas`. Whilst running it, try sending SIGPIPE to it (`killall -PIPE SigmaskTest `) and it should continue running.

NOTE: this function will need to be called by each thread that wishes to block this signal, except in the case where the parent thread already has this signal blocked, as newly created threads inherit their parents sigmask (according to http://linux.die.net/man/3/pthread_sigmask)

Hope it helps! 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130601/c13a5657/attachment.html>

More information about the fpc-pascal mailing list