diff --git a/inc/common/common.h b/inc/common/common.h index 1b930726ff83c7dedf59c4ddde86a6c0fd10dbaa..501996fc78aba176e750d2d641b9033bde8bcfdb 100644 --- a/inc/common/common.h +++ b/inc/common/common.h @@ -34,3 +34,5 @@ char *trim(char *str, size_t len, char c); /* Get a line from stdin safely. */ int get_stdin_line(const char *prompt, char *buf, size_t len); + +int read_self_exe(char *path, size_t len); diff --git a/inc/lektor/cmd.h b/inc/lektor/cmd.h index 7861e0e6b45e86ea10a910fc8b10ce6527107bf8..365a87596717660251a8bbd2bfbb73b4cca17a6a 100644 --- a/inc/lektor/cmd.h +++ b/inc/lektor/cmd.h @@ -25,3 +25,5 @@ noreturn void lkt_cmd_parse(struct lkt_cmd_opt *opts, int argc, const char **arg /* Must be setted for the lkt_cmd_parse function to display the correct help in case of errors. */ extern const char *executable_name; + +noreturn void help__(void); diff --git a/meson.build b/meson.build index b84a924c06bfdff7bbc79a2594d7aadf20d29dde..01938dca32d82323e268ea987ddfc04c39b0d4d4 100644 --- a/meson.build +++ b/meson.build @@ -58,6 +58,7 @@ core_sources = [ 'src/mkv/bufferfd.c' , 'src/config.c' , 'src/uri.c' , 'src/thread.c' + , 'src/cmd.c' ] # Global includes @@ -89,8 +90,8 @@ generated_deps = [ declare_dependency( link_with: static_library( 'generated' , [ custom_target( 'manpath' , output: 'manpath.c' , input: 'scripts/getmanpath.sh' - , command: [ find_program('scripts/getmanpath.sh'), '@OUTPUT@' ] ) ] - ) ) ] + , command: [ find_program('scripts/getmanpath.sh'), '@OUTPUT@' ] ) ] ) + ) ] lib = static_library( 'lektor' , files(core_sources) diff --git a/src/cmd.c b/src/cmd.c index b56e868ed499b0c89bbeb5c51b6b93f6d8180e30..70c5e9fa4a373ac1829a96fd168e8ab2833ce7ee 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -13,7 +13,7 @@ const char *executable_name = NULL; extern const char *man_executable_path; noreturn void -help__() +help__(void) { if (!executable_name) exit(EXIT_FAILURE); diff --git a/src/commands.c b/src/commands.c index 8dbff5b2b389cda47de123abc6b39d5c263cf343..bd3dea5d37f13c8c75cf0b095385e4e1a174d4a9 100644 --- a/src/commands.c +++ b/src/commands.c @@ -5,6 +5,7 @@ #include <lektor/database.h> #include <lektor/net.h> #include <lektor/uri.h> +#include <lektor/cmd.h> #include <mthread/mthread.h> #include <errno.h> @@ -21,30 +22,16 @@ inline bool command_restart(struct lkt_state *srv, size_t c) { - char exe[PATH_MAX]; - char *const argv[] = { exe, NULL }; + const char *const argv[] = { executable_name, NULL }; RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false); - - if (readlink(SELF_EXECUTABLE_LINUX, exe, PATH_MAX - 1) > 0) { - LOG_INFO_SCT("GENERAL", "Restart lektord: %s", exe); - close(srv->fds[0].fd); - execv(exe, argv); - } - - if (readlink(SELF_EXECUTABLE_FREEBSD, exe, PATH_MAX - 1) > 0) { - LOG_INFO_SCT("GENERAL", "Restart lektord: %s", exe); - close(srv->fds[0].fd); - execv(exe, argv); - } - - if (readlink(SELF_EXECUTABLE_SOLARIS, exe, PATH_MAX - 1) > 0) { - LOG_INFO_SCT("GENERAL", "Restart lektord: %s", exe); - close(srv->fds[0].fd); - execv(exe, argv); + 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); + execv(executable_name, (char *const *) argv); LOG_ERROR_SCT("GENERAL", "%s", "Failed to exec lektor or OS not supported"); - abort(); + return false; } bool diff --git a/src/common.c b/src/common.c index 95a57368d5fa2f998cb34b0158861678d48df5f3..53158aafaab13288b54d332c48dd64d938bb6a6b 100644 --- a/src/common.c +++ b/src/common.c @@ -149,3 +149,11 @@ safe_malloc(size_t size) } return tmp; } + +int +read_self_exe(char *path, size_t len) +{ + return ! (readlink(SELF_EXECUTABLE_LINUX, path, len - 1) > 0) || + (readlink(SELF_EXECUTABLE_FREEBSD, path, len - 1) > 0) || + (readlink(SELF_EXECUTABLE_SOLARIS, path, len - 1) > 0); +} diff --git a/src/main/server.c b/src/main/server.c index ad3e0959c49d01f6ec0297bf4d8a630a7a021d9d..206686427b03f7dd0cd634bc791d44f2b4bdc422 100644 --- a/src/main/server.c +++ b/src/main/server.c @@ -3,6 +3,7 @@ #include <common/common.h> #include <lektor/config.h> #include <lektor/net.h> +#include <lektor/cmd.h> #include <lektor/database.h> #include <mthread/mthread.h> @@ -14,51 +15,26 @@ #include <pwd.h> #include <pthread.h> -/* Prints the help of the program */ -static void -print_help(void) -{ - const char *HELP = "NAME \n" - " lektord -- lektor player daemon \n" - " \n" - "SYNOPSIS \n" - " lektord [OPTIONS]... \n" - " \n" - "DESCRIPTION \n" - " \n" - " -h / --help \n" - " display the help and exit \n" - " \n" - " -v / --version \n" - " display the version of lektor and exit \n" - "\n"; - - write(1, HELP, strlen(HELP)); -} - int main(int argc, char *argv[]) { struct passwd *pw = getpwuid(getuid()); + char exe[PATH_MAX]; pthread_t th; + executable_name = "lektord"; + UNUSED(argv); if (argc <= 1) goto normal_launch; - else if (strcasecmp(argv[1], "--help") == 0 || - strcasecmp(argv[1], "-h") == 0) { - print_help(); - return EXIT_SUCCESS; - } else if (strcasecmp(argv[1], "--version") == 0 || - strcasecmp(argv[1], "-v") == 0) { - printf("Lektor version 0.1\n"); - return EXIT_SUCCESS; - } - + help__(); return EXIT_FAILURE; normal_launch: LOG_INFO("Lektor launched by user %s (shell: %s, home: %s)", pw->pw_name, pw->pw_shell, pw->pw_dir); mthread_init(); pthread_create(&th, NULL, mthread_main, NULL); + if(read_self_exe(exe, PATH_MAX)) + LOG_WARN_SCT("GENERAL", "%s", "Failed to read self executable path, restart may not work"); + executable_name = exe; return lkt_listen(); }