diff --git a/inc/common/macro.h b/inc/common/macro.h index 1ba00aa134172feef27e329bad2c29a9d6d0e674..da56e358042a2cbb3ce8579e1ac7fffdf0ef58d9 100644 --- a/inc/common/macro.h +++ b/inc/common/macro.h @@ -75,6 +75,9 @@ #define SELF_EXECUTABLE_FREEBSD "/proc/curproc/file" #define SELF_EXECUTABLE_SOLARIS "/proc/self/path/a.out" +#define LKT_ENV_RESTART "__LKT_RESTART" +#define LKT_ENV_CURRENT "__LKT_CURRENT" + #define LKT_MAX_SQLITE_STATEMENT 1024 #define PROTECTED_DATABASE "disk" diff --git a/inc/lektor/config.h b/inc/lektor/config.h index e7d75bd162a3d1572cc3031285a390db352e3cb7..5746e7b73103e19c4bede54429414962e1974d49 100644 --- a/inc/lektor/config.h +++ b/inc/lektor/config.h @@ -35,3 +35,7 @@ int config_new(volatile sqlite3 *db, const char *conf); /* Prints the default config file. */ void config_default(FILE *output); + +/* Manipulate Environment Variables */ +#define env_set(var, val) setenv(var, val, 1) +#define env_get getenv diff --git a/src/commands.c b/src/commands.c index 9ad34084ef1317c2dadf1b3ef0b0e2488e2a49b6..f4f0c7cfca956bc828b9ebc2ab59a5685551a85c 100644 --- a/src/commands.c +++ b/src/commands.c @@ -23,12 +23,27 @@ inline bool command_restart(struct lkt_state *srv, size_t c) { const char *const argv[] = { executable_name, NULL }; + struct lkt_queue_state sta = {0}; RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false); if (!executable_name) { LOG_ERROR_SCT("GENERAL", "%s", "Can't restart if the executable path was not found at start-up"); return false; } close(srv->fds[0].fd); + database_queue_state(srv->db, &sta); + env_set(LKT_ENV_RESTART, "1"); + int len = long_length(sta.current); + if (len > 0) { + char *pos = calloc(len, sizeof(char)); + if (pos) { + safe_snprintf(pos, len, "%d", sta.current); + env_set(LKT_ENV_CURRENT, pos); + free(pos); + } else + LOG_WARN_SCT("GENERAL", "Failed to malloc, don't set %s to %d", + LKT_ENV_CURRENT, sta.current); + } else + env_set(LKT_ENV_CURRENT, "NULL"); database_close_all(); execv(executable_name, (char *const *) argv); LOG_ERROR_SCT("GENERAL", "%s", "Failed to exec lektor or OS not supported"); diff --git a/src/main/server.c b/src/main/server.c index e13c0d1cc537faf8610c70f5c54c43c477f98fdb..1e611eb08f7108220dc17357a2df13aa2e1d2dec 100644 --- a/src/main/server.c +++ b/src/main/server.c @@ -7,6 +7,7 @@ #include <lektor/reg.h> #include <lektor/database.h> #include <mthread/mthread.h> +#include <lektor/commands.h> #include <assert.h> #include <signal.h> @@ -67,6 +68,8 @@ main(int argc, char *argv[]) normal_launch: LOG_INFO_SCT("GENERAL", "Lektor launched by user %s (shell: %s, home: %s)", pw->pw_name, pw->pw_shell, pw->pw_dir); + if (env_get(LKT_ENV_RESTART)) + LOG_INFO_SCT("GENERAL", "%s", "Lektord has been restarted"); reg_set(server_reg); mthread_init(); pthread_create(&th, NULL, mthread_main, NULL); @@ -114,11 +117,12 @@ normal_launch: srv.kara_prefix = kara_dir; database_config_queue_default(srv.db); - if (autoclear) + if (!env_get(LKT_ENV_RESTART) && autoclear) database_queue_clear(srv.db); RETURN_IF(load_module_by_name(&srv, "player", &srv.win), "Can't load module player", 3); RETURN_IF(load_module_by_name(&srv, "repo", &srv.repo), "Can't load module repo", 3); + lkt_listen(&srv); return EXIT_FAILURE; } diff --git a/src/net/listen.c b/src/net/listen.c index 37d1cbe38b34d83dbcaca97953d4277f0b17ad47..21c74840950285cf82a7d4b950a2fc7b2de8cfc6 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -727,6 +727,15 @@ lkt_listen(struct lkt_state *srv) srv->fds[0].events = POLLIN; srv->fds_len = 1; + /* Get ENV */ + /* Not working -> race condition with player module */ + // char *env_current = env_get(LKT_ENV_CURRENT); + // if (env_current && !STR_MATCH(env_current, "NULL")) { + // LOG_INFO_SCT("INIT", "Restart playback from %s", env_current); + // command_play(srv->db, &srv->win, &env_current, &srv->mpd_idle_events); + // } + + /* Listen */ for (;;) { if (handle_network_events(srv) < 0) break;