-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsigint.pas
More file actions
47 lines (37 loc) · 1.14 KB
/
sigint.pas
File metadata and controls
47 lines (37 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit sigint;
interface
uses sysutils, baseunix, exceptions;
// Consider unixutils.pas if you need SIGPIPE handling also.
var
Aborted: Boolean = False;
procedure InstallAbortHandler(); // listens for SIGINT and SIGTERM
implementation
procedure AbortHandler(Signal: Longint; Info: PSigInfo; Context: PSigContext); cdecl;
begin
Aborted := True;
end;
procedure InstallAbortHandler();
var
NewAction: PSigActionRec;
begin
New(NewAction);
if (not Assigned(NewAction)) then
OutOfMemoryError();
try
{$IFDEF Linux} NewAction^.sa_restorer := nil; {$ENDIF}
FillByte(NewAction^.sa_mask, SizeOf(NewAction^.sa_mask), 0);
NewAction^.sa_handler := @AbortHandler;
NewAction^.sa_flags := SA_ONESHOT;
if (fpSigAction(baseunix.SIGINT, NewAction, nil) <> 0) then
raise EKernelError.Create(fpGetErrNo);
NewAction^.sa_handler := @AbortHandler;
NewAction^.sa_flags := SA_ONESHOT;
if (fpSigAction(baseunix.SIGTERM, NewAction, nil) <> 0) then
raise EKernelError.Create(fpGetErrNo);
finally
Dispose(NewAction);
end;
end;
end.