File: daemonsystemdinstallerunit.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (65 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download | duplicates (3)
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
unit DaemonSystemdInstallerUnit;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, DaemonApp, IniFiles;

// ---------------------------------------------------------------------
// Helper functions for Unix service install/uninstall support (systemd)
// 2/2022 by arminlinder@arminlinder.de
// ---------------------------------------------------------------------

const
  DAEMON_CONFIG_FILE_PATH = '/lib/systemd/system';   // Unix systemd config file path

function GetSystemdControlFilePath(aDaemonName: string): string;
function CreateSystemdControlFile(aDaemon: TDaemon; aFilePath: string): boolean;
function RemoveSystemdControlFile(aFilePath: string): boolean;

implementation

function GetSystemdControlFilePath(aDaemonName: string): string;

begin
  Result := IncludetrailingBackslash(DAEMON_CONFIG_FILE_PATH) + aDaemonName + '.service';
end;

function CreateSystemdControlFile(aDaemon: TDaemon; aFilePath: string): boolean;

var
  f: TIniFile;

begin
  Result := False;
  try
    f := TIniFile.Create(aFilePath, []);
    // We use the definition given in the mapper class
    // The mapper class is assigned to the "Definition" property
    f.WriteString('Unit', 'Description', aDaemon.Definition.Description);
    f.WriteString('Unit', 'After', 'network.target');
    f.WriteString('Service', 'Type', 'simple');
    f.WriteString('Service', 'ExecStart', Application.ExeName + ' -r');
    f.WriteString('Install', 'WantedBy', 'multi-user.target');
    Result := True;
    // Removed Taken from old file sample, IMHO not required
    // f.WriteString('Service', 'TimeoutSec', '25');
    // f.WriteString('Service', 'RemainAfterExit', 'yes');
  finally
    f.Free;
  end;
end;

function RemoveSystemdControlFile(aFilePath: string): boolean;

  // Remove the control file, if it does exist

begin
  Result := True;
  if FileExists(aFilePath) then
    Result := DeleteFile(aFilePath);
end;

end.