service.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 service name and service display name are limited to 256 characters.
  8. A registry key is limited to 255 characters.
  9. A registry value is limited to 16383 characters.
  10. Therefore we limit the service name to accommodate the path under HKLM.
  11. */
  12. #define EXE_LENGTH PATH_LENGTH
  13. #define CMD_LENGTH 32768
  14. #define KEY_LENGTH 255
  15. #define VALUE_LENGTH 16383
  16. #define SERVICE_NAME_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. #define NSSM_ROTATE_OFFLINE 0
  27. #define NSSM_ROTATE_ONLINE 1
  28. #define NSSM_ROTATE_ONLINE_ASAP 2
  29. typedef struct {
  30. bool native;
  31. TCHAR name[SERVICE_NAME_LENGTH];
  32. TCHAR displayname[SERVICE_NAME_LENGTH];
  33. TCHAR description[VALUE_LENGTH];
  34. unsigned long startup;
  35. TCHAR *username;
  36. size_t usernamelen;
  37. TCHAR *password;
  38. size_t passwordlen;
  39. unsigned long type;
  40. TCHAR image[PATH_LENGTH];
  41. TCHAR exe[EXE_LENGTH];
  42. TCHAR flags[VALUE_LENGTH];
  43. TCHAR dir[DIR_LENGTH];
  44. TCHAR *env;
  45. __int64 affinity;
  46. unsigned long envlen;
  47. TCHAR *env_extra;
  48. unsigned long env_extralen;
  49. unsigned long priority;
  50. unsigned long no_console;
  51. TCHAR stdin_path[PATH_LENGTH];
  52. unsigned long stdin_sharing;
  53. unsigned long stdin_disposition;
  54. unsigned long stdin_flags;
  55. TCHAR stdout_path[PATH_LENGTH];
  56. unsigned long stdout_sharing;
  57. unsigned long stdout_disposition;
  58. unsigned long stdout_flags;
  59. HANDLE stdout_pipe;
  60. HANDLE stdout_thread;
  61. unsigned long stdout_tid;
  62. TCHAR stderr_path[PATH_LENGTH];
  63. unsigned long stderr_sharing;
  64. unsigned long stderr_disposition;
  65. unsigned long stderr_flags;
  66. HANDLE stderr_pipe;
  67. HANDLE stderr_thread;
  68. unsigned long stderr_tid;
  69. bool rotate_files;
  70. unsigned long rotate_stdout_online;
  71. unsigned long rotate_stderr_online;
  72. unsigned long rotate_seconds;
  73. unsigned long rotate_bytes_low;
  74. unsigned long rotate_bytes_high;
  75. unsigned long default_exit_action;
  76. unsigned long restart_delay;
  77. unsigned long throttle_delay;
  78. unsigned long stop_method;
  79. unsigned long kill_console_delay;
  80. unsigned long kill_window_delay;
  81. unsigned long kill_threads_delay;
  82. SC_HANDLE handle;
  83. SERVICE_STATUS status;
  84. SERVICE_STATUS_HANDLE status_handle;
  85. HANDLE process_handle;
  86. unsigned long pid;
  87. HANDLE wait_handle;
  88. bool stopping;
  89. bool allow_restart;
  90. unsigned long throttle;
  91. CRITICAL_SECTION throttle_section;
  92. bool throttle_section_initialised;
  93. CONDITION_VARIABLE throttle_condition;
  94. HANDLE throttle_timer;
  95. LARGE_INTEGER throttle_duetime;
  96. FILETIME creation_time;
  97. FILETIME exit_time;
  98. TCHAR *initial_env;
  99. } nssm_service_t;
  100. void WINAPI service_main(unsigned long, TCHAR **);
  101. TCHAR *service_control_text(unsigned long);
  102. TCHAR *service_status_text(unsigned long);
  103. void log_service_control(TCHAR *, unsigned long, bool);
  104. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  105. int affinity_mask_to_string(__int64, TCHAR **);
  106. int affinity_string_to_mask(TCHAR *, __int64 *);
  107. unsigned long priority_mask();
  108. int priority_constant_to_index(unsigned long);
  109. unsigned long priority_index_to_constant(int);
  110. nssm_service_t *alloc_nssm_service();
  111. void set_nssm_service_defaults(nssm_service_t *);
  112. void cleanup_nssm_service(nssm_service_t *);
  113. SC_HANDLE open_service_manager();
  114. SC_HANDLE open_service(SC_HANDLE, TCHAR *, TCHAR *, unsigned long);
  115. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  116. int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);
  117. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  118. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  119. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  120. int grant_logon_as_service(const TCHAR *);
  121. int pre_install_service(int, TCHAR **);
  122. int pre_remove_service(int, TCHAR **);
  123. int pre_edit_service(int, TCHAR **);
  124. int install_service(nssm_service_t *);
  125. int remove_service(nssm_service_t *);
  126. int edit_service(nssm_service_t *, bool);
  127. int control_service(unsigned long, int, TCHAR **);
  128. void set_service_recovery(nssm_service_t *);
  129. int monitor_service(nssm_service_t *);
  130. int start_service(nssm_service_t *);
  131. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  132. void CALLBACK end_service(void *, unsigned char);
  133. void throttle_restart(nssm_service_t *);
  134. int await_shutdown(nssm_service_t *, TCHAR *, unsigned long);
  135. #endif