service.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 throttle_delay;
  65. unsigned long stop_method;
  66. unsigned long kill_console_delay;
  67. unsigned long kill_window_delay;
  68. unsigned long kill_threads_delay;
  69. SC_HANDLE handle;
  70. SERVICE_STATUS status;
  71. SERVICE_STATUS_HANDLE status_handle;
  72. HANDLE process_handle;
  73. unsigned long pid;
  74. HANDLE wait_handle;
  75. bool stopping;
  76. bool allow_restart;
  77. unsigned long throttle;
  78. CRITICAL_SECTION throttle_section;
  79. bool throttle_section_initialised;
  80. CONDITION_VARIABLE throttle_condition;
  81. HANDLE throttle_timer;
  82. LARGE_INTEGER throttle_duetime;
  83. FILETIME creation_time;
  84. FILETIME exit_time;
  85. } nssm_service_t;
  86. void WINAPI service_main(unsigned long, TCHAR **);
  87. TCHAR *service_control_text(unsigned long);
  88. void log_service_control(TCHAR *, unsigned long, bool);
  89. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  90. int affinity_mask_to_string(__int64, TCHAR **);
  91. int affinity_string_to_mask(TCHAR *, __int64 *);
  92. unsigned long priority_mask();
  93. int priority_constant_to_index(unsigned long);
  94. unsigned long priority_index_to_constant(int);
  95. nssm_service_t *alloc_nssm_service();
  96. void set_nssm_service_defaults(nssm_service_t *);
  97. void cleanup_nssm_service(nssm_service_t *);
  98. SC_HANDLE open_service_manager();
  99. SC_HANDLE open_service(SC_HANDLE, TCHAR *, TCHAR *, unsigned long);
  100. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  101. int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);
  102. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  103. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  104. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  105. int grant_logon_as_service(const TCHAR *);
  106. int pre_install_service(int, TCHAR **);
  107. int pre_remove_service(int, TCHAR **);
  108. int pre_edit_service(int, TCHAR **);
  109. int install_service(nssm_service_t *);
  110. int remove_service(nssm_service_t *);
  111. int edit_service(nssm_service_t *, bool);
  112. int control_service(unsigned long, int, TCHAR **);
  113. void set_service_recovery(nssm_service_t *);
  114. int monitor_service(nssm_service_t *);
  115. int start_service(nssm_service_t *);
  116. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  117. void CALLBACK end_service(void *, unsigned char);
  118. void throttle_restart(nssm_service_t *);
  119. int await_shutdown(nssm_service_t *, TCHAR *, unsigned long);
  120. #endif