service.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef SERVICE_H
  2. #define SERVICE_H
  3. /*
  4. MSDN says the commandline in CreateProcess() is limited to 32768 characters
  5. and the application name to MAX_PATH.
  6. A service name and service display name are limited to 256 characters.
  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 PATH_LENGTH
  12. #define CMD_LENGTH 32768
  13. #define KEY_LENGTH 255
  14. #define VALUE_LENGTH 16383
  15. #define SERVICE_NAME_LENGTH 256
  16. #define ACTION_LEN 16
  17. #define NSSM_KERNEL_DRIVER _T("SERVICE_KERNEL_DRIVER")
  18. #define NSSM_FILE_SYSTEM_DRIVER _T("SERVICE_FILE_SYSTEM_DRIVER")
  19. #define NSSM_WIN32_OWN_PROCESS _T("SERVICE_WIN32_OWN_PROCESS")
  20. #define NSSM_WIN32_SHARE_PROCESS _T("SERVICE_WIN32_SHARE_PROCESS")
  21. #define NSSM_INTERACTIVE_PROCESS _T("SERVICE_INTERACTIVE_PROCESS")
  22. #define NSSM_SHARE_INTERACTIVE_PROCESS NSSM_WIN32_SHARE_PROCESS _T("|") NSSM_INTERACTIVE_PROCESS
  23. #define NSSM_UNKNOWN _T("?")
  24. #define NSSM_ROTATE_OFFLINE 0
  25. #define NSSM_ROTATE_ONLINE 1
  26. #define NSSM_ROTATE_ONLINE_ASAP 2
  27. typedef struct {
  28. bool native;
  29. TCHAR name[SERVICE_NAME_LENGTH];
  30. TCHAR displayname[SERVICE_NAME_LENGTH];
  31. TCHAR description[VALUE_LENGTH];
  32. unsigned long startup;
  33. TCHAR *username;
  34. size_t usernamelen;
  35. TCHAR *password;
  36. size_t passwordlen;
  37. unsigned long type;
  38. TCHAR image[PATH_LENGTH];
  39. TCHAR exe[EXE_LENGTH];
  40. TCHAR flags[VALUE_LENGTH];
  41. TCHAR dir[DIR_LENGTH];
  42. TCHAR *env;
  43. __int64 affinity;
  44. TCHAR *dependencies;
  45. unsigned long dependencieslen;
  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. bool use_stdout_pipe;
  60. HANDLE stdout_si;
  61. HANDLE stdout_pipe;
  62. HANDLE stdout_thread;
  63. unsigned long stdout_tid;
  64. TCHAR stderr_path[PATH_LENGTH];
  65. unsigned long stderr_sharing;
  66. unsigned long stderr_disposition;
  67. unsigned long stderr_flags;
  68. bool use_stderr_pipe;
  69. HANDLE stderr_si;
  70. HANDLE stderr_pipe;
  71. HANDLE stderr_thread;
  72. unsigned long stderr_tid;
  73. bool hook_share_output_handles;
  74. bool rotate_files;
  75. bool stdout_copy_and_truncate;
  76. bool stderr_copy_and_truncate;
  77. unsigned long rotate_stdout_online;
  78. unsigned long rotate_stderr_online;
  79. unsigned long rotate_seconds;
  80. unsigned long rotate_bytes_low;
  81. unsigned long rotate_bytes_high;
  82. unsigned long rotate_delay;
  83. unsigned long default_exit_action;
  84. unsigned long restart_delay;
  85. unsigned long throttle_delay;
  86. unsigned long stop_method;
  87. unsigned long kill_console_delay;
  88. unsigned long kill_window_delay;
  89. unsigned long kill_threads_delay;
  90. bool kill_process_tree;
  91. SC_HANDLE handle;
  92. SERVICE_STATUS status;
  93. SERVICE_STATUS_HANDLE status_handle;
  94. HANDLE process_handle;
  95. unsigned long pid;
  96. HANDLE wait_handle;
  97. unsigned long exitcode;
  98. bool stopping;
  99. bool allow_restart;
  100. unsigned long throttle;
  101. CRITICAL_SECTION throttle_section;
  102. bool throttle_section_initialised;
  103. CRITICAL_SECTION hook_section;
  104. bool hook_section_initialised;
  105. CONDITION_VARIABLE throttle_condition;
  106. HANDLE throttle_timer;
  107. LARGE_INTEGER throttle_duetime;
  108. FILETIME nssm_creation_time;
  109. FILETIME creation_time;
  110. FILETIME exit_time;
  111. TCHAR *initial_env;
  112. unsigned long last_control;
  113. unsigned long start_requested_count;
  114. unsigned long start_count;
  115. unsigned long exit_count;
  116. } nssm_service_t;
  117. void WINAPI service_main(unsigned long, TCHAR **);
  118. TCHAR *service_control_text(unsigned long);
  119. TCHAR *service_status_text(unsigned long);
  120. void log_service_control(TCHAR *, unsigned long, bool);
  121. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  122. int affinity_mask_to_string(__int64, TCHAR **);
  123. int affinity_string_to_mask(TCHAR *, __int64 *);
  124. unsigned long priority_mask();
  125. int priority_constant_to_index(unsigned long);
  126. unsigned long priority_index_to_constant(int);
  127. nssm_service_t *alloc_nssm_service();
  128. void set_nssm_service_defaults(nssm_service_t *);
  129. void cleanup_nssm_service(nssm_service_t *);
  130. SC_HANDLE open_service_manager(unsigned long);
  131. SC_HANDLE open_service(SC_HANDLE, TCHAR *, unsigned long, TCHAR *, unsigned long);
  132. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  133. int append_to_dependencies(TCHAR *, unsigned long, TCHAR *, TCHAR **, unsigned long *, int);
  134. int remove_from_dependencies(TCHAR *, unsigned long, TCHAR *, TCHAR **, unsigned long *, int);
  135. int set_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR *);
  136. int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *, int);
  137. int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *);
  138. int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);
  139. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  140. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  141. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  142. void set_service_environment(nssm_service_t *);
  143. void unset_service_environment(nssm_service_t *);
  144. int pre_install_service(int, TCHAR **);
  145. int pre_remove_service(int, TCHAR **);
  146. int pre_edit_service(int, TCHAR **);
  147. int install_service(nssm_service_t *);
  148. int remove_service(nssm_service_t *);
  149. int edit_service(nssm_service_t *, bool);
  150. int control_service(unsigned long, int, TCHAR **);
  151. void set_service_recovery(nssm_service_t *);
  152. int monitor_service(nssm_service_t *);
  153. int start_service(nssm_service_t *);
  154. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  155. void CALLBACK end_service(void *, unsigned char);
  156. void throttle_restart(nssm_service_t *);
  157. int await_single_handle(SERVICE_STATUS_HANDLE, SERVICE_STATUS *, HANDLE, TCHAR *, TCHAR *, unsigned long);
  158. int list_nssm_services();
  159. int service_process_tree(int, TCHAR **);
  160. #endif