[fpc-pascal] Deamon & signals

Koenraad Lelong fpascal at brouwerij.homelinux.net
Sat Jun 14 16:39:18 CEST 2008


Hi,
I'm trying to make a daemon, to run on Linux. I got a testversion
working, but I have a question about signals.
I can respond to signals but every signal I didn't define stops the
daemon, in an "unmanageable" way.
Is this the proper behaviour ?

The source :
program my_service;

{$ifdef FPC}{$mode objfpc}{$h+}{$endif}
uses
 {$ifdef FPC}{$ifdef linux}cthreads,{$endif}{$endif}
 baseunix,crt,sysutils;

var
 pid : Tpid;
 finished : boolean;
 oa_sigterm, na_sigterm : PSigActionRec ;
 oa_sigusr1, na_sigusr1 : PSigActionRec ;
 oa_sigusr2, na_sigusr2 : PSigActionRec ;

Procedure DoSig(sig : cint); cdecl ;
begin
 writeln ('Receiving signal : ', sig) ;
 finished:=(sig=SigTerm);
end ;

procedure daemon;
begin
{Do the actual work here.}
repeat
until finished;
writeln('finished in daemon');
end;

begin
finished:=false;
new(na_sigterm) ;
new(oa_sigterm) ;
na_sigterm^.sa_Handler:=SigActionHandler(@DoSig) ;
fillchar(na_sigterm^.Sa_Mask, sizeof(na_sigterm^.sa_mask), #0);
na_sigterm^.Sa_Flags:=0 ;
{$ifdef Linux}   // Linux specific
na_sigterm^.Sa_Restorer:= Nil;
{$endif}
if fpSigAction(Sigterm,na_sigterm,oa_sigterm)<>0 then
 begin
  writeln ('Error : ',fpgeterrno , '(SigTerm).') ;
  halt (1);
 end;
new(na_sigusr1) ;
new(oa_sigusr1) ;
na_sigusr1^.sa_Handler:=SigActionHandler(@DoSig) ;
fillchar(na_sigusr1^.Sa_Mask, sizeof(na_sigusr1^.sa_mask), #0);
na_sigusr1^.Sa_Flags:=0 ;
{$ifdef Linux}   // Linux specific
na_sigusr1^.Sa_Restorer:= Nil;
{$endif}
if fpSigAction(SigUsr1,na_sigusr1,oa_sigusr1)<>0 then
 begin
  writeln ('Error : ',fpgeterrno , '(SigUsr1).') ;
  halt (1);
 end;
new(na_sigusr2) ;
new(oa_sigusr2) ;
na_sigusr2^.sa_Handler:=SigActionHandler(@DoSig) ;
fillchar(na_sigusr2^.Sa_Mask, sizeof(na_sigusr2^.sa_mask), #0);
na_sigusr2^.Sa_Flags:=0 ;
{$ifdef Linux}   // Linux specific
na_sigusr2^.Sa_Restorer:= Nil;
{$endif}
if fpSigAction(sigusr2,na_sigusr2,oa_sigusr2)<>0 then
 begin
  writeln ('Error : ',fpgeterrno , '(SigUsr2).') ;
  halt (1);
 end;
pid:=fpfork;
if pid=0 then
 daemon;
writeln('finished main');
end.




More information about the fpc-pascal mailing list