From 7b85809e2f4f36f1cb587e255c72409b06d549a9 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 4 Apr 2012 22:00:08 +0100 Subject: [PATCH] Removed run hack. Previously we used to attempt to run as a service only when the "run" argument was given, and printed a usage message if no arguments were provided. A more appropriate strategy is to check for StartServiceCtrlDispatcher() returning ERROR_FAILED_SERVICE_CONTROLLER_CONNECT, as that means the application is not running in a service context. As a result we no longer write the undocumented "run" argument to the registry when installing a service. Technically this means that we sacrifice backward compatibility with older versions. --- nssm.cpp | 34 +++++++++++++++++----------------- service.cpp | 5 ++--- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/nssm.cpp b/nssm.cpp index 4203c61..d543c46 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -47,23 +47,20 @@ int check_admin(char *action) { } int main(int argc, char **argv) { - /* Require an argument since users may try to run nssm directly */ - if (argc == 1) exit(usage(1)); - /* Elevate */ - if (str_equiv(argv[1], "install") || str_equiv(argv[1], "remove")) { - if (check_admin(argv[1])) exit(100); - } + if (argc > 1) { + if (str_equiv(argv[1], "install") || str_equiv(argv[1], "remove")) { + if (check_admin(argv[1])) exit(100); + } - /* Valid commands are install or remove */ - if (str_equiv(argv[1], "install")) { - exit(pre_install_service(argc - 2, argv + 2)); + /* Valid commands are install or remove */ + if (str_equiv(argv[1], "install")) { + exit(pre_install_service(argc - 2, argv + 2)); + } + if (str_equiv(argv[1], "remove")) { + exit(pre_remove_service(argc - 2, argv + 2)); + } } - if (str_equiv(argv[1], "remove")) { - exit(pre_remove_service(argc - 2, argv + 2)); - } - /* Undocumented: "run" is used to actually do service stuff */ - if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2)); /* Thread local storage for error message buffer */ tls_index = TlsAlloc(); @@ -74,10 +71,13 @@ int main(int argc, char **argv) { /* Start service magic */ SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } }; if (! StartServiceCtrlDispatcher(table)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(GetLastError()), 0); - return 100; + unsigned long error = GetLastError(); + /* User probably ran nssm with no argument */ + if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1)); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0); + exit(100); } /* And nothing more to do */ - return 0; + exit(0); } diff --git a/service.cpp b/service.cpp index ac329bc..ef4ef51 100644 --- a/service.cpp +++ b/service.cpp @@ -96,13 +96,12 @@ int install_service(char *name, char *exe, char *flags) { /* Construct command */ char command[CMD_LENGTH]; - size_t runlen = strlen(NSSM_RUN); size_t pathlen = strlen(path); - if (pathlen + runlen + 2 >= VALUE_LENGTH) { + if (pathlen + 1 >= VALUE_LENGTH) { fprintf(stderr, "The full path to " NSSM " is too long!\n"); return 3; } - if (_snprintf(command, sizeof(command), "\"%s\" %s", path, NSSM_RUN) < 0) { + if (_snprintf(command, sizeof(command), "\"%s\"", path) < 0) { fprintf(stderr, "Out of memory for ImagePath!\n"); return 4; } -- 2.20.1