nssm.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "nssm.h"
  2. extern unsigned long tls_index;
  3. extern bool is_admin;
  4. extern imports_t imports;
  5. /* Are two strings case-insensitively equivalent? */
  6. int str_equiv(const TCHAR *a, const TCHAR *b) {
  7. size_t len = _tcslen(a);
  8. if (_tcslen(b) != len) return 0;
  9. if (_tcsnicmp(a, b, len)) return 0;
  10. return 1;
  11. }
  12. /* Convert a string to a number. */
  13. int str_number(const TCHAR *string, unsigned long *number, TCHAR **bogus) {
  14. if (! string) return 1;
  15. *number = _tcstoul(string, bogus, 0);
  16. if (**bogus) return 2;
  17. return 0;
  18. }
  19. int str_number(const TCHAR *string, unsigned long *number) {
  20. TCHAR *bogus;
  21. return str_number(string, number, &bogus);
  22. }
  23. /* Remove basename of a path. */
  24. void strip_basename(TCHAR *buffer) {
  25. size_t len = _tcslen(buffer);
  26. size_t i;
  27. for (i = len; i && buffer[i] != _T('\\') && buffer[i] != _T('/'); i--);
  28. /* X:\ is OK. */
  29. if (i && buffer[i - 1] == _T(':')) i++;
  30. buffer[i] = _T('\0');
  31. }
  32. /* How to use me correctly */
  33. int usage(int ret) {
  34. if (GetConsoleWindow()) print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE);
  35. else popup_message(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE);
  36. return(ret);
  37. }
  38. void check_admin() {
  39. is_admin = false;
  40. /* Lifted from MSDN examples */
  41. PSID AdministratorsGroup;
  42. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  43. if (! AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup)) return;
  44. CheckTokenMembership(0, AdministratorsGroup, /*XXX*/(PBOOL) &is_admin);
  45. FreeSid(AdministratorsGroup);
  46. }
  47. /* See if we were launched from a console window. */
  48. static void check_console() {
  49. /* If we're running in a service context there will be no console window. */
  50. HWND console = GetConsoleWindow();
  51. if (! console) return;
  52. unsigned long pid;
  53. if (! GetWindowThreadProcessId(console, &pid)) return;
  54. /*
  55. If the process associated with the console window handle is the same as
  56. this process, we were not launched from an existing console. The user
  57. probably double-clicked our executable.
  58. */
  59. if (GetCurrentProcessId() != pid) return;
  60. /* We close our new console so that subsequent messages appear in a popup. */
  61. FreeConsole();
  62. }
  63. int num_cpus() {
  64. DWORD_PTR i, affinity, system_affinity;
  65. if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64;
  66. for (i = 0; system_affinity & (1LL << i); i++);
  67. return (int) i;
  68. }
  69. int _tmain(int argc, TCHAR **argv) {
  70. check_console();
  71. /* Remember if we are admin */
  72. check_admin();
  73. /* Elevate */
  74. if (argc > 1) {
  75. /*
  76. Valid commands are:
  77. start, stop, pause, continue, install, edit, get, set, reset, unset, remove
  78. */
  79. if (str_equiv(argv[1], _T("start"))) exit(control_service(0, argc - 2, argv + 2));
  80. if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));
  81. if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));
  82. if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));
  83. if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));
  84. if (str_equiv(argv[1], _T("install"))) {
  85. if (! is_admin) {
  86. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);
  87. exit(100);
  88. }
  89. exit(pre_install_service(argc - 2, argv + 2));
  90. }
  91. 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"))) {
  92. if (! is_admin) {
  93. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);
  94. exit(100);
  95. }
  96. int ret = pre_edit_service(argc - 1, argv + 1);
  97. /* There might be a password here. */
  98. for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR));
  99. exit(ret);
  100. }
  101. if (str_equiv(argv[1], _T("remove"))) {
  102. if (! is_admin) {
  103. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);
  104. exit(100);
  105. }
  106. exit(pre_remove_service(argc - 2, argv + 2));
  107. }
  108. }
  109. /* Thread local storage for error message buffer */
  110. tls_index = TlsAlloc();
  111. /* Register messages */
  112. if (is_admin) create_messages();
  113. /*
  114. Optimisation for Windows 2000:
  115. When we're run from the command line the StartServiceCtrlDispatcher() call
  116. will time out after a few seconds on Windows 2000. On newer versions the
  117. call returns instantly. Check for stdin first and only try to call the
  118. function if there's no input stream found. Although it's possible that
  119. we're running with input redirected it's much more likely that we're
  120. actually running as a service.
  121. This will save time when running with no arguments from a command prompt.
  122. */
  123. if (_fileno(stdin) < 0) {
  124. /* Set up function pointers. */
  125. if (get_imports()) exit(111);
  126. /* Start service magic */
  127. SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
  128. if (! StartServiceCtrlDispatcher(table)) {
  129. unsigned long error = GetLastError();
  130. /* User probably ran nssm with no argument */
  131. if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));
  132. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);
  133. free_imports();
  134. exit(100);
  135. }
  136. }
  137. else exit(usage(1));
  138. /* And nothing more to do */
  139. exit(0);
  140. }