mirror of http://git.nssm.cc/nssm/nssm.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
6.3 KiB
C++
179 lines
6.3 KiB
C++
#include "nssm.h"
|
|
|
|
extern unsigned long tls_index;
|
|
extern bool is_admin;
|
|
extern imports_t imports;
|
|
|
|
/* Are two strings case-insensitively equivalent? */
|
|
int str_equiv(const TCHAR *a, const TCHAR *b) {
|
|
size_t len = _tcslen(a);
|
|
if (_tcslen(b) != len) return 0;
|
|
if (_tcsnicmp(a, b, len)) return 0;
|
|
return 1;
|
|
}
|
|
|
|
/* Convert a string to a number. */
|
|
int str_number(const TCHAR *string, unsigned long *number, TCHAR **bogus) {
|
|
if (! string) return 1;
|
|
|
|
*number = _tcstoul(string, bogus, 0);
|
|
if (**bogus) return 2;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int str_number(const TCHAR *string, unsigned long *number) {
|
|
TCHAR *bogus;
|
|
return str_number(string, number, &bogus);
|
|
}
|
|
|
|
/* Remove basename of a path. */
|
|
void strip_basename(TCHAR *buffer) {
|
|
size_t len = _tcslen(buffer);
|
|
size_t i;
|
|
for (i = len; i && buffer[i] != _T('\\') && buffer[i] != _T('/'); i--);
|
|
/* X:\ is OK. */
|
|
if (i && buffer[i - 1] == _T(':')) i++;
|
|
buffer[i] = _T('\0');
|
|
}
|
|
|
|
/* How to use me correctly */
|
|
int usage(int ret) {
|
|
if (GetConsoleWindow()) print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE);
|
|
else popup_message(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE);
|
|
return(ret);
|
|
}
|
|
|
|
void check_admin() {
|
|
is_admin = false;
|
|
|
|
/* Lifted from MSDN examples */
|
|
PSID AdministratorsGroup;
|
|
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
|
|
if (! AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup)) return;
|
|
CheckTokenMembership(0, AdministratorsGroup, /*XXX*/(PBOOL) &is_admin);
|
|
FreeSid(AdministratorsGroup);
|
|
}
|
|
|
|
/* See if we were launched from a console window. */
|
|
static void check_console() {
|
|
/* If we're running in a service context there will be no console window. */
|
|
HWND console = GetConsoleWindow();
|
|
if (! console) return;
|
|
|
|
unsigned long pid;
|
|
if (! GetWindowThreadProcessId(console, &pid)) return;
|
|
|
|
/*
|
|
If the process associated with the console window handle is the same as
|
|
this process, we were not launched from an existing console. The user
|
|
probably double-clicked our executable.
|
|
*/
|
|
if (GetCurrentProcessId() != pid) return;
|
|
|
|
/* We close our new console so that subsequent messages appear in a popup. */
|
|
FreeConsole();
|
|
}
|
|
|
|
int num_cpus() {
|
|
DWORD_PTR i, affinity, system_affinity;
|
|
if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64;
|
|
for (i = 0; system_affinity & (1LL << i); i++);
|
|
return (int) i;
|
|
}
|
|
|
|
int _tmain(int argc, TCHAR **argv) {
|
|
check_console();
|
|
|
|
#ifdef UNICODE
|
|
/*
|
|
Ensure we write in UTF-16 mode, so that non-ASCII characters don't get
|
|
mangled. If we were compiled in ANSI mode it won't work.
|
|
*/
|
|
_setmode(_fileno(stdout), _O_U16TEXT);
|
|
_setmode(_fileno(stderr), _O_U16TEXT);
|
|
#endif
|
|
|
|
/* Remember if we are admin */
|
|
check_admin();
|
|
|
|
/* Elevate */
|
|
if (argc > 1) {
|
|
/*
|
|
Valid commands are:
|
|
start, stop, pause, continue, install, edit, get, set, reset, unset, remove
|
|
*/
|
|
if (str_equiv(argv[1], _T("start"))) exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));
|
|
if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));
|
|
if (str_equiv(argv[1], _T("restart"))) {
|
|
int ret = control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2);
|
|
if (ret) exit(ret);
|
|
exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));
|
|
}
|
|
if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));
|
|
if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));
|
|
if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));
|
|
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) {
|
|
print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);
|
|
exit(100);
|
|
}
|
|
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 (! is_admin) {
|
|
print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);
|
|
exit(100);
|
|
}
|
|
int ret = pre_edit_service(argc - 1, argv + 1);
|
|
/* There might be a password here. */
|
|
for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR));
|
|
exit(ret);
|
|
}
|
|
if (str_equiv(argv[1], _T("remove"))) {
|
|
if (! is_admin) {
|
|
print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);
|
|
exit(100);
|
|
}
|
|
exit(pre_remove_service(argc - 2, argv + 2));
|
|
}
|
|
}
|
|
|
|
/* Thread local storage for error message buffer */
|
|
tls_index = TlsAlloc();
|
|
|
|
/* Register messages */
|
|
if (is_admin) create_messages();
|
|
|
|
/*
|
|
Optimisation for Windows 2000:
|
|
When we're run from the command line the StartServiceCtrlDispatcher() call
|
|
will time out after a few seconds on Windows 2000. On newer versions the
|
|
call returns instantly. Check for stdin first and only try to call the
|
|
function if there's no input stream found. Although it's possible that
|
|
we're running with input redirected it's much more likely that we're
|
|
actually running as a service.
|
|
This will save time when running with no arguments from a command prompt.
|
|
*/
|
|
if (_fileno(stdin) < 0) {
|
|
/* Set up function pointers. */
|
|
if (get_imports()) exit(111);
|
|
|
|
/* Start service magic */
|
|
SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
|
|
if (! StartServiceCtrlDispatcher(table)) {
|
|
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);
|
|
free_imports();
|
|
exit(100);
|
|
}
|
|
}
|
|
else exit(usage(1));
|
|
|
|
/* And nothing more to do */
|
|
exit(0);
|
|
}
|