я себе не сохранял этот файл
вот то что работает у меня (собирал еще на старой версии фпц на последней возможно нужно что-то править)
Код: Выделить всё
program acKerneld;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
SysUtils,BaseUnix, uConst;
Var
{ vars for daemonizing }
bTerm : boolean;
aOld,
aTerm: pSigActionRec;
ps1 : psigset;
sSet : cardinal;
pid : pid_t;
zerosigs : sigset_t;
status: integer;
{ handle SIGTERM }
procedure DoSig(sig : longint);cdecl;
begin
case sig of
SIGTERM : bTerm := true;
end;
end;
procedure AddLog(str : string);
begin
AddText2File(DateTimeMs2Str(Now)+' '+str,ChangeFileExt(ParamStr(0),'.log'));
end;
Begin
{$hints off}
FpsigEmptySet(zerosigs);
{$hints on}
{ set global daemon booleans }
bTerm := false;
{ block all signals except -TERM }
sSet := $ffffbffe;
ps1 := @sSet;
fpsigprocmask(sig_block,ps1,nil);
{ setup the signal handlers }
new(aOld);
new(aTerm);
aTerm^.sa_handler{.sh} := SigactionHandler(@DoSig);
aTerm^.sa_mask := zerosigs;
aTerm^.sa_flags := 0;
fpSigAction(SIGTERM,aTerm,aOld);
{ daemonize }
pid := fpFork;
case pid of
0 : begin { we are in the child }
Close(input); { close standard in }
Close(output); { close standard out }
Assign(output,'/dev/null');
ReWrite(output);
Close(stderr); { close standard error }
Assign(stderr,'/dev/null');
ReWrite(stderr);
end;
-1 :begin
WriteLn('forking error, so halt 1');
halt(1);
end;
else begin
Halt; { successful fork, so parent dies }
end;
end;
AddLog('Daemon start');
{ begin processing loop }
repeat
pid := fpFork;
case pid of
0 : begin { we are in the child }
FpExecv(ExtractFilePath(ParamStr(0))+'ackernel2',nil);
fpExit(127); //If the execve fails, we return an exitvalue of 127
end;
-1 : begin
WriteLn('running error, so halt 2');
halt(2);
end;
else begin
AddLog('Programm running, pid = '+IntToStr(pid));
end;
end;
while (FpWaitPid(pid,@status,WNOHANG) = 0) do
begin
sleep(1000);
if bTerm then
begin
AddLog('Demon was terminate.');
status := FpKill(pid,SIGKILL);
AddLog('Programm with pid '+IntToStr(pid)+' was killed, return '+IntToStr(status));
FpExit(0);
end;
end;
AddLog('Programm with pid '+IntToStr(pid)+' exit whith code '+IntToStr(status));
Until bTerm;
End.