Skip to content

Commit 242b919

Browse files
committed
feat: Linux signal handling
1 parent b65b6b7 commit 242b919

File tree

2 files changed

+128
-9
lines changed

2 files changed

+128
-9
lines changed

src/paslogbot.lpi

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<General>
66
<Flags>
77
<MainUnitHasCreateFormStatements Value="False"/>
8+
<MainUnitHasTitleStatement Value="False"/>
89
<MainUnitHasScaledStatement Value="False"/>
910
</Flags>
1011
<SessionStorage Value="InProjectDir"/>
@@ -14,6 +15,65 @@
1415
</General>
1516
<BuildModes>
1617
<Item Name="Default" Default="True"/>
18+
<Item Name="Debug">
19+
<CompilerOptions>
20+
<Version Value="11"/>
21+
<Target>
22+
<Filename Value="../bin/paslogbot"/>
23+
</Target>
24+
<SearchPaths>
25+
<IncludeFiles Value="$(ProjOutDir)"/>
26+
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
27+
</SearchPaths>
28+
<Parsing>
29+
<SyntaxOptions>
30+
<IncludeAssertionCode Value="True"/>
31+
</SyntaxOptions>
32+
</Parsing>
33+
<CodeGeneration>
34+
<Checks>
35+
<IOChecks Value="True"/>
36+
<RangeChecks Value="True"/>
37+
<OverflowChecks Value="True"/>
38+
<StackChecks Value="True"/>
39+
</Checks>
40+
<VerifyObjMethodCallValidity Value="True"/>
41+
</CodeGeneration>
42+
<Linking>
43+
<Debugging>
44+
<DebugInfoType Value="dsDwarf3"/>
45+
<UseHeaptrc Value="True"/>
46+
<TrashVariables Value="True"/>
47+
</Debugging>
48+
</Linking>
49+
</CompilerOptions>
50+
</Item>
51+
<Item Name="Release">
52+
<CompilerOptions>
53+
<Version Value="11"/>
54+
<Target>
55+
<Filename Value="../bin/paslogbot"/>
56+
</Target>
57+
<SearchPaths>
58+
<IncludeFiles Value="$(ProjOutDir)"/>
59+
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
60+
</SearchPaths>
61+
<CodeGeneration>
62+
<SmartLinkUnit Value="True"/>
63+
<Optimizations>
64+
<OptimizationLevel Value="3"/>
65+
</Optimizations>
66+
</CodeGeneration>
67+
<Linking>
68+
<Debugging>
69+
<GenerateDebugInfo Value="False"/>
70+
<RunWithoutDebug Value="True"/>
71+
<StripSymbols Value="True"/>
72+
</Debugging>
73+
<LinkSmart Value="True"/>
74+
</Linking>
75+
</CompilerOptions>
76+
</Item>
1777
</BuildModes>
1878
<PublishOptions>
1979
<Version Value="2"/>
@@ -32,11 +92,11 @@
3292
<CompilerOptions>
3393
<Version Value="11"/>
3494
<Target>
35-
<Filename Value="paslogbot"/>
95+
<Filename Value="../bin/paslogbot"/>
3696
</Target>
3797
<SearchPaths>
3898
<IncludeFiles Value="$(ProjOutDir)"/>
39-
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
99+
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
40100
</SearchPaths>
41101
</CompilerOptions>
42102
<Debugging>

src/paslogbot.lpr

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
uses
66
{$IFDEF UNIX}
77
cthreads,
8+
BaseUnix,
89
{$ENDIF}
910
Classes, SysUtils, CustApp
1011
{ you can add units after this };
1112

1213
type
1314

14-
{ TPasLogBot }
15-
15+
{ TPasLogBot }
1616
TPasLogBot = class(TCustomApplication)
1717
protected
1818
procedure DoRun; override;
@@ -22,6 +22,65 @@ TPasLogBot = class(TCustomApplication)
2222
procedure WriteHelp; virtual;
2323
end;
2424

25+
var
26+
Application: TPasLogBot;
27+
28+
{ Signal Handling }
29+
{$IFDEF UNIX}
30+
procedure SignalHandler(signal: longint; info: psiginfo; context: psigcontext); cdecl;
31+
begin
32+
case signal of
33+
SIGTERM, SIGINT:
34+
begin
35+
WriteLn('Received termination signal');
36+
if Assigned(Application) then
37+
Application.Terminate;
38+
end;
39+
SIGHUP:
40+
begin
41+
WriteLn('Received SIGHUP - could implement config reload here');
42+
// Could implement configuration reload here
43+
end;
44+
end;
45+
end;
46+
47+
procedure SetupSignalHandlers;
48+
var
49+
act: SigActionRec;
50+
begin
51+
FillChar(act, SizeOf(act), 0);
52+
act.sa_handler := @SignalHandler;
53+
act.sa_flags := 0;
54+
55+
// Set up signal handlers
56+
fpSigAction(SIGTERM, @act, nil);
57+
fpSigAction(SIGINT, @act, nil);
58+
fpSigAction(SIGHUP, @act, nil);
59+
end;
60+
{$ENDIF}
61+
62+
{$IFDEF WINDOWS}
63+
function ConsoleCtrlHandler(CtrlType: DWORD): BOOL; stdcall;
64+
begin
65+
case CtrlType of
66+
CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT:
67+
begin
68+
WriteLn('Received termination signal');
69+
if Assigned(Application) then
70+
Application.Terminate;
71+
Result := True;
72+
Exit;
73+
end;
74+
end;
75+
Result := False;
76+
end;
77+
78+
procedure SetupSignalHandlers;
79+
begin
80+
SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
81+
end;
82+
{$ENDIF}
83+
2584
{ TPasLogBot }
2685

2786
procedure TPasLogBot.DoRun;
@@ -43,10 +102,12 @@ procedure TPasLogBot.DoRun;
43102
Exit;
44103
end;
45104

46-
47-
105+
while not Terminated do
106+
begin
107+
Sleep(50);
108+
end;
48109
// stop program loop
49-
Terminate;
110+
//Terminate;
50111
end;
51112

52113
constructor TPasLogBot.Create(TheOwner: TComponent);
@@ -66,8 +127,6 @@ procedure TPasLogBot.WriteHelp;
66127
writeln('Usage: ', ExeName, ' -h');
67128
end;
68129

69-
var
70-
Application: TPasLogBot;
71130
begin
72131
Application:=TPasLogBot.Create(nil);
73132
Application.Title:='Pascal Log Bot';

0 commit comments

Comments
 (0)