X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=6aa9a88f2edcafad5192171212c1383ffbaa7608;hb=586ea54f696a562ed3837b6b55e3fa1bbe1b2b22;hp=ff9eb52d75b90cca980389c1193e00381cdfd332;hpb=1f0b03b38f7d76814d1c7c627f64462362100223;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index ff9eb52..6aa9a88 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -6,6 +6,7 @@ extern imports_t imports; static TCHAR unquoted_imagepath[PATH_LENGTH]; static TCHAR imagepath[PATH_LENGTH]; +static TCHAR imageargv0[PATH_LENGTH]; /* Are two strings case-insensitively equivalent? */ int str_equiv(const TCHAR *a, const TCHAR *b) { @@ -30,6 +31,115 @@ int str_number(const TCHAR *string, unsigned long *number) { return str_number(string, number, &bogus); } +/* Does a char need to be escaped? */ +static bool needs_escape(const TCHAR c) { + if (c == _T('"')) return true; + if (c == _T('&')) return true; + if (c == _T('%')) return true; + if (c == _T('^')) return true; + if (c == _T('<')) return true; + if (c == _T('>')) return true; + if (c == _T('|')) return true; + return false; +} + +/* Does a char need to be quoted? */ +static bool needs_quote(const TCHAR c) { + if (c == _T(' ')) return true; + if (c == _T('\t')) return true; + if (c == _T('\n')) return true; + if (c == _T('\v')) return true; + if (c == _T('"')) return true; + if (c == _T('*')) return true; + return needs_escape(c); +} + +/* https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ */ +/* http://www.robvanderwoude.com/escapechars.php */ +int quote(const TCHAR *unquoted, TCHAR *buffer, size_t buflen) { + size_t i, j, n; + size_t len = _tcslen(unquoted); + if (len > buflen - 1) return 1; + + bool escape = false; + bool quotes = false; + + for (i = 0; i < len; i++) { + if (needs_escape(unquoted[i])) { + escape = quotes = true; + break; + } + if (needs_quote(unquoted[i])) quotes = true; + } + if (! quotes) { + memmove(buffer, unquoted, (len + 1) * sizeof(TCHAR)); + return 0; + } + + /* "" */ + size_t quoted_len = 2; + if (escape) quoted_len += 2; + for (i = 0; ; i++) { + n = 0; + + while (i != len && unquoted[i] == _T('\\')) { + i++; + n++; + } + + if (i == len) { + quoted_len += n * 2; + break; + } + else if (unquoted[i] == _T('"')) quoted_len += n * 2 + 2; + else quoted_len += n + 1; + if (needs_escape(unquoted[i])) quoted_len += n; + } + if (quoted_len > buflen - 1) return 1; + + TCHAR *s = buffer; + if (escape) *s++ = _T('^'); + *s++ = _T('"'); + + for (i = 0; ; i++) { + n = 0; + + while (i != len && unquoted[i] == _T('\\')) { + i++; + n++; + } + + if (i == len) { + for (j = 0; j < n * 2; j++) { + if (escape) *s++ = _T('^'); + *s++ = _T('\\'); + } + break; + } + else if (unquoted[i] == _T('"')) { + for (j = 0; j < n * 2 + 1; j++) { + if (escape) *s++ = _T('^'); + *s++ = _T('\\'); + } + if (escape && needs_escape(unquoted[i])) *s++ = _T('^'); + *s++ = unquoted[i]; + } + else { + for (j = 0; j < n; j++) { + if (escape) *s++ = _T('^'); + *s++ = _T('\\'); + } + if (escape && needs_escape(unquoted[i])) *s++ = _T('^'); + *s++ = unquoted[i]; + } + } + if (escape) *s++ = _T('^'); + *s++ = _T('"'); + *s++ = _T('\0'); + + return 0; +} + /* Remove basename of a path. */ void strip_basename(TCHAR *buffer) { size_t len = _tcslen(buffer); @@ -104,6 +214,10 @@ const TCHAR *nssm_imagepath() { return imagepath; } +const TCHAR *nssm_exe() { + return imageargv0; +} + int _tmain(int argc, TCHAR **argv) { check_console(); @@ -123,6 +237,8 @@ int _tmain(int argc, TCHAR **argv) { if (get_imports()) exit(111); /* Remember our path for later. */ + _sntprintf_s(imageargv0, _countof(imageargv0), _TRUNCATE, _T("%s"), argv[0]); + PathQuoteSpaces(imageargv0); GetModuleFileName(0, unquoted_imagepath, _countof(unquoted_imagepath)); GetModuleFileName(0, imagepath, _countof(imagepath)); PathQuoteSpaces(imagepath); @@ -146,9 +262,10 @@ int _tmain(int argc, TCHAR **argv) { if (str_equiv(argv[1], _T("rotate"))) exit(control_service(NSSM_SERVICE_CONTROL_ROTATE, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("install"))) { if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL)); + create_messages(); exit(pre_install_service(argc - 2, argv + 2)); } - if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) { + if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset")) || str_equiv(argv[1], _T("dump"))) { int ret = pre_edit_service(argc - 1, argv + 1); if (ret == 3 && ! is_admin && argc == 3) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT)); /* There might be a password here. */ @@ -156,6 +273,7 @@ int _tmain(int argc, TCHAR **argv) { exit(ret); } if (str_equiv(argv[1], _T("list"))) exit(list_nssm_services()); + if (str_equiv(argv[1], _T("processes"))) exit(service_process_tree(argc - 2, argv + 2)); if (str_equiv(argv[1], _T("remove"))) { if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE)); exit(pre_remove_service(argc - 2, argv + 2));