-
Notifications
You must be signed in to change notification settings - Fork 1
/
unixutils.pas
70 lines (58 loc) · 1.58 KB
/
unixutils.pas
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit unixutils;
interface
var
Aborted: Boolean = False;
procedure HookSignalHandlers();
implementation
uses
sysutils, baseunix, exceptions;
procedure SigIntHandler(Signal: Longint; Info: PSigInfo; Context: PSigContext); cdecl;
begin
{$IFDEF DEBUG}
Writeln('caught ^C; aborting at:');
Writeln(GetStackTrace());
Writeln();
{$ENDIF}
Aborted := True;
end;
{$IFNDEF Linux}
procedure SigPipeHandler(Signal: Longint; Info: PSigInfo; Context: PSigContext); cdecl;
begin
{$IFDEF DEBUG}
Writeln();
Writeln('caught SIGPIPE');
Writeln(GetStackTrace());
Writeln();
{$ENDIF}
end;
{$ENDIF}
procedure HookSignalHandlers();
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);
// SIGINT - one-off handler
NewAction^.sa_handler := @SigIntHandler;
NewAction^.sa_flags := SA_ONESHOT;
if (fpSigAction(SIGINT, NewAction, nil) <> 0) then
raise EKernelError.Create(fpGetErrNo);
{$IFNDEF Linux}
// SIGPIPE - persistent handler
// On Linux we use MSG_NOSIGNAL on send() instead -- see corenetwork.pas
NewAction^.sa_handler := @SigPipeHandler;
NewAction^.sa_flags := SA_RESTART;
if (fpSigAction(SIGPIPE, NewAction, nil) <> 0) then
raise EKernelError.Create(fpGetErrNo);
{$ENDIF}
finally
Dispose(NewAction);
end;
end;
end.