From c00f5cce40a65748eadb4488c14c0b06c05873fd Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sat, 30 Jan 2021 21:53:06 +0100
Subject: [PATCH] KLKT: Get the last klkt launched in case of lektord restart

---
 CHANGELOG.md        |  2 ++
 inc/lektor/common.h |  1 +
 man/lektor.template |  5 +++++
 src/main/server.c   | 16 +++++++++++++++-
 4 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index dfe2c0f4..405f9f40 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,12 +4,14 @@
 - Try to print the backtrace at SEGV
 - Assert and abort will trigger a SEGV to try to display the backtrace
 - Check if downloaded karas are matroska files (only the magic number)
+- In case of restart get the klkt instance launched by the previous lektord
 - Add window icon
 - Add the Klkt client (notification for desktops) to the lektor project
 - Add AppImage builds for lektord, lkt and klkt
 - Add basic controls to klkt (play, pause, next, previous, shuffle)
 - Add SDL2-Image lib to the dependencies (--with-depends)
 - Add klkt autolaunch option to config file
+- Add the \_\_flat command to set all priorities to 1 in the queue
 - Remove archlinux package support (replaced by AppImage)
 - Add MPD commands: seek, seekid, seekcur, delete (partial)
 - Fix dl process with the repo module
diff --git a/inc/lektor/common.h b/inc/lektor/common.h
index 0901d39e..62ee6ccd 100644
--- a/inc/lektor/common.h
+++ b/inc/lektor/common.h
@@ -103,6 +103,7 @@ void __lkt_log(enum log_level, const char *section, const char *func, const char
 #define LKT_ENV_RESTART             "__LKT_RESTART"
 #define LKT_ENV_CURRENT             "__LKT_CURRENT"
 #define LKT_ENV_SET_ASSERT          "__LKT_SET_ASSERT"
+#define LKT_ENV_KLKT_PID            "__LKT_KLKT_PID"
 
 #define LKT_LINE_MAX                1024
 #define LKT_MAX_SQLITE_STATEMENT    1024
diff --git a/man/lektor.template b/man/lektor.template
index 60beb55e..922de18f 100644
--- a/man/lektor.template
+++ b/man/lektor.template
@@ -75,6 +75,11 @@ When built in debug mode (the \fBNDEBUG\fP macro is not defined at compile
 time), it is possible to enable asserts by setting this variable to anything
 that is not a null string or empty string. By default, asserts are not enabled
 in \fBlektord\fP and \fBlkt\fP.
+.TP
+\fB__LKT_KLKT_PID\fP
+Set to the pid of klkt that lektord should kill when restarting. Usefull in
+case of a restart with \fBlkt admin restart\fP but you can also set it
+yourself.
 
 .SH "SUPPORTED URIS"
 When searching the database, the following URIs are supported by lektor. Those
diff --git a/src/main/server.c b/src/main/server.c
index 737b63c0..7117c8cc 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -125,7 +125,16 @@ ___kill_klkt(void)
 static inline int
 launch_klkt(va_list UNUSED *___args)
 {
-    /* Patch PATH to first search the basedir */
+    /* Check env variable! */
+    char *env_klkt_pid = getenv(LKT_ENV_KLKT_PID);
+    if (NULL != env_klkt_pid) {
+        LOG_DEBUG("GENERAL", "Found " LKT_ENV_KLKT_PID " := %s", env_klkt_pid);
+        ___klkt_pid = strtol(env_klkt_pid, NULL, 0);
+        return 0;
+    }
+    LOG_DEBUG("GENERAL", "No " LKT_ENV_KLKT_PID " env variable found, launch klkt ourself");
+
+    /* Env var was not found, no existing lklt! Launch it ourself. */
     char exe_path[LKT_LINE_MAX];
     char try_name[LKT_LINE_MAX];
     const char *const appimage = getenv("APPIMAGE");
@@ -183,6 +192,11 @@ error:
 end:
     LOG_INFO("GENERAL", "Klkt launched with pid %ld", ___klkt_pid, try_name);
     posix_spawn_file_actions_destroy(&action);
+
+    /* Export the env var for klkt pid, reuse exe_path */
+    safe_snprintf(exe_path, LKT_LINE_MAX, "%ld", ___klkt_pid);
+    RETURN_IF(setenv(LKT_ENV_KLKT_PID, exe_path, 1),
+              "Failed to set env variable " LKT_ENV_KLKT_PID, 1);
     return 0;
 }
 
-- 
GitLab