nssm.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /* Remove basename of a path. */
  13. void strip_basename(TCHAR *buffer) {
  14. size_t len = _tcslen(buffer);
  15. size_t i;
  16. for (i = len; i && buffer[i] != _T('\\') && buffer[i] != _T('/'); i--);
  17. /* X:\ is OK. */
  18. if (i && buffer[i - 1] == _T(':')) i++;
  19. buffer[i] = _T('\0');
  20. }
  21. /* How to use me correctly */
  22. int usage(int ret) {
  23. print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);
  24. return(ret);
  25. }
  26. void check_admin() {
  27. is_admin = false;
  28. /* Lifted from MSDN examples */
  29. PSID AdministratorsGroup;
  30. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  31. if (! AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup)) return;
  32. CheckTokenMembership(0, AdministratorsGroup, /*XXX*/(PBOOL) &is_admin);
  33. FreeSid(AdministratorsGroup);
  34. }
  35. int _tmain(int argc, TCHAR **argv) {
  36. /* Remember if we are admin */
  37. check_admin();
  38. /* Elevate */
  39. if (argc > 1) {
  40. /* Valid commands are install or remove */
  41. if (str_equiv(argv[1], _T("install"))) {
  42. if (! is_admin) {
  43. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);
  44. exit(100);
  45. }
  46. exit(pre_install_service(argc - 2, argv + 2));
  47. }
  48. if (str_equiv(argv[1], _T("remove"))) {
  49. if (! is_admin) {
  50. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);
  51. exit(100);
  52. }
  53. exit(pre_remove_service(argc - 2, argv + 2));
  54. }
  55. }
  56. /* Thread local storage for error message buffer */
  57. tls_index = TlsAlloc();
  58. /* Register messages */
  59. if (is_admin) create_messages();
  60. /*
  61. Optimisation for Windows 2000:
  62. When we're run from the command line the StartServiceCtrlDispatcher() call
  63. will time out after a few seconds on Windows 2000. On newer versions the
  64. call returns instantly. Check for stdin first and only try to call the
  65. function if there's no input stream found. Although it's possible that
  66. we're running with input redirected it's much more likely that we're
  67. actually running as a service.
  68. This will save time when running with no arguments from a command prompt.
  69. */
  70. if (_fileno(stdin) < 0) {
  71. /* Set up function pointers. */
  72. if (get_imports()) exit(111);
  73. /* Start service magic */
  74. SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
  75. if (! StartServiceCtrlDispatcher(table)) {
  76. unsigned long error = GetLastError();
  77. /* User probably ran nssm with no argument */
  78. if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));
  79. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);
  80. free_imports();
  81. exit(100);
  82. }
  83. }
  84. else exit(usage(1));
  85. /* And nothing more to do */
  86. exit(0);
  87. }