service.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef SERVICE_H
  2. #define SERVICE_H
  3. #include <ntsecapi.h>
  4. /*
  5. MSDN says the commandline in CreateProcess() is limited to 32768 characters
  6. and the application name to MAX_PATH.
  7. A registry key is limited to 255 characters.
  8. A registry value is limited to 16383 characters.
  9. Therefore we limit the service name to accommodate the path under HKLM.
  10. */
  11. #define EXE_LENGTH MAX_PATH
  12. #define CMD_LENGTH 32768
  13. #define KEY_LENGTH 255
  14. #define VALUE_LENGTH 16383
  15. #define SERVICE_NAME_LENGTH KEY_LENGTH - 55
  16. #define SERVICE_DISPLAYNAME_LENGTH 256
  17. #define ACTION_LEN 16
  18. #define NSSM_LOCALSYSTEM_ACCOUNT _T("LocalSystem")
  19. #define NSSM_KERNEL_DRIVER _T("SERVICE_KERNEL_DRIVER")
  20. #define NSSM_FILE_SYSTEM_DRIVER _T("SERVICE_FILE_SYSTEM_DRIVER")
  21. #define NSSM_WIN32_OWN_PROCESS _T("SERVICE_WIN32_OWN_PROCESS")
  22. #define NSSM_WIN32_SHARE_PROCESS _T("SERVICE_WIN32_SHARE_PROCESS")
  23. #define NSSM_INTERACTIVE_PROCESS _T("SERVICE_INTERACTIVE_PROCESS")
  24. #define NSSM_SHARE_INTERACTIVE_PROCESS NSSM_WIN32_SHARE_PROCESS _T("|") NSSM_INTERACTIVE_PROCESS
  25. #define NSSM_UNKNOWN _T("?")
  26. typedef struct {
  27. bool native;
  28. TCHAR name[SERVICE_NAME_LENGTH];
  29. TCHAR displayname[SERVICE_DISPLAYNAME_LENGTH];
  30. TCHAR description[VALUE_LENGTH];
  31. unsigned long startup;
  32. TCHAR *username;
  33. size_t usernamelen;
  34. TCHAR *password;
  35. size_t passwordlen;
  36. unsigned long type;
  37. TCHAR image[MAX_PATH];
  38. TCHAR exe[EXE_LENGTH];
  39. TCHAR flags[VALUE_LENGTH];
  40. TCHAR dir[MAX_PATH];
  41. TCHAR *env;
  42. __int64 affinity;
  43. unsigned long envlen;
  44. TCHAR *env_extra;
  45. unsigned long env_extralen;
  46. unsigned long priority;
  47. TCHAR stdin_path[MAX_PATH];
  48. unsigned long stdin_sharing;
  49. unsigned long stdin_disposition;
  50. unsigned long stdin_flags;
  51. TCHAR stdout_path[MAX_PATH];
  52. unsigned long stdout_sharing;
  53. unsigned long stdout_disposition;
  54. unsigned long stdout_flags;
  55. TCHAR stderr_path[MAX_PATH];
  56. unsigned long stderr_sharing;
  57. unsigned long stderr_disposition;
  58. unsigned long stderr_flags;
  59. bool rotate_files;
  60. unsigned long rotate_seconds;
  61. unsigned long rotate_bytes_low;
  62. unsigned long rotate_bytes_high;
  63. unsigned long default_exit_action;
  64. unsigned long restart_delay;
  65. unsigned long throttle_delay;
  66. unsigned long stop_method;
  67. unsigned long kill_console_delay;
  68. unsigned long kill_window_delay;
  69. unsigned long kill_threads_delay;
  70. SC_HANDLE handle;
  71. SERVICE_STATUS status;
  72. SERVICE_STATUS_HANDLE status_handle;
  73. HANDLE process_handle;
  74. unsigned long pid;
  75. HANDLE wait_handle;
  76. bool stopping;
  77. bool allow_restart;
  78. unsigned long throttle;
  79. CRITICAL_SECTION throttle_section;
  80. bool throttle_section_initialised;
  81. CONDITION_VARIABLE throttle_condition;
  82. HANDLE throttle_timer;
  83. LARGE_INTEGER throttle_duetime;
  84. FILETIME creation_time;
  85. FILETIME exit_time;
  86. } nssm_service_t;
  87. void WINAPI service_main(unsigned long, TCHAR **);
  88. TCHAR *service_control_text(unsigned long);
  89. void log_service_control(TCHAR *, unsigned long, bool);
  90. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  91. int affinity_mask_to_string(__int64, TCHAR **);
  92. int affinity_string_to_mask(TCHAR *, __int64 *);
  93. unsigned long priority_mask();
  94. int priority_constant_to_index(unsigned long);
  95. unsigned long priority_index_to_constant(int);
  96. nssm_service_t *alloc_nssm_service();
  97. void set_nssm_service_defaults(nssm_service_t *);
  98. void cleanup_nssm_service(nssm_service_t *);
  99. SC_HANDLE open_service_manager();
  100. SC_HANDLE open_service(SC_HANDLE, TCHAR *, TCHAR *, unsigned long);
  101. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  102. int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);
  103. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  104. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  105. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  106. int grant_logon_as_service(const TCHAR *);
  107. int pre_install_service(int, TCHAR **);
  108. int pre_remove_service(int, TCHAR **);
  109. int pre_edit_service(int, TCHAR **);
  110. int install_service(nssm_service_t *);
  111. int remove_service(nssm_service_t *);
  112. int edit_service(nssm_service_t *, bool);
  113. int control_service(unsigned long, int, TCHAR **);
  114. void set_service_recovery(nssm_service_t *);
  115. int monitor_service(nssm_service_t *);
  116. int start_service(nssm_service_t *);
  117. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  118. void CALLBACK end_service(void *, unsigned char);
  119. void throttle_restart(nssm_service_t *);
  120. int await_shutdown(nssm_service_t *, TCHAR *, unsigned long);
  121. #endif