From d53b4937547279dd59de176bbdfe4368aecbd68a Mon Sep 17 00:00:00 2001 From: lda Date: Fri, 5 Jan 2024 01:01:16 +0100 Subject: [PATCH] [ADD] Add PID option --- Schema/Config.json | 1 + src/Main.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Schema/Config.json b/Schema/Config.json index 9bdc593..8bdba91 100644 --- a/Schema/Config.json +++ b/Schema/Config.json @@ -75,6 +75,7 @@ "serverName": { "type": "string", "required": true }, "baseUrl": { "type": "string", "required": false }, "identityServer": { "type": "string", "required": false }, + "pid": { "type": "string", "required": false }, "maxCache": { "type": "integer", "required": false }, diff --git a/src/Main.c b/src/Main.c index bb73b09..c495c4b 100644 --- a/src/Main.c +++ b/src/Main.c @@ -104,6 +104,9 @@ Main(Array * args) /* Program configuration */ Config *tConfig; Stream *logFile; + Stream *pidFile = NULL; + + char *pidPath = NULL; /* User validation */ struct passwd *userInfo; @@ -335,6 +338,21 @@ start: Free(token); } + if (tConfig->pid) + { + pidFile = StreamOpen(tConfig->pid, "w+"); + if (!pidFile) + { + char *msg = "Couldn't lock PID file at '%s'"; + Log(LOG_ERR, msg, tConfig->pid); + exit = EXIT_FAILURE; + goto finish; + } + pidPath = StrDuplicate(tConfig->pid); + StreamPrintf(pidFile, "%ld", (long) getpid()); + StreamClose(pidFile); + } + Log(LOG_DEBUG, "Configuration:"); LogConfigIndent(LogConfigGlobal()); Log(LOG_DEBUG, "Server Name: %s", tConfig->serverName); @@ -604,6 +622,12 @@ finish: HttpRouterFree(matrixArgs.router); Log(LOG_DEBUG, "Freed routing tree."); + if (pidPath) + { + remove(pidPath); + Free(pidPath); + } + /* * Uninstall the memory hook because it uses the Log * API, whose configuration is being freed now, so it