service.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 timestamp_log;
  76. bool stdout_copy_and_truncate;
  77. bool stderr_copy_and_truncate;
  78. unsigned long rotate_stdout_online;
  79. unsigned long rotate_stderr_online;
  80. unsigned long rotate_seconds;
  81. unsigned long rotate_bytes_low;
  82. unsigned long rotate_bytes_high;
  83. unsigned long rotate_delay;
  84. unsigned long default_exit_action;
  85. unsigned long restart_delay;
  86. unsigned long throttle_delay;
  87. unsigned long stop_method;
  88. unsigned long kill_console_delay;
  89. unsigned long kill_window_delay;
  90. unsigned long kill_threads_delay;
  91. bool kill_process_tree;
  92. SC_HANDLE handle;
  93. SERVICE_STATUS status;
  94. SERVICE_STATUS_HANDLE status_handle;
  95. HANDLE process_handle;
  96. unsigned long pid;
  97. HANDLE wait_handle;
  98. unsigned long exitcode;
  99. bool stopping;
  100. bool allow_restart;
  101. unsigned long throttle;
  102. CRITICAL_SECTION throttle_section;
  103. bool throttle_section_initialised;
  104. CRITICAL_SECTION hook_section;
  105. bool hook_section_initialised;
  106. CONDITION_VARIABLE throttle_condition;
  107. HANDLE throttle_timer;
  108. LARGE_INTEGER throttle_duetime;
  109. FILETIME nssm_creation_time;
  110. FILETIME creation_time;
  111. FILETIME exit_time;
  112. TCHAR *initial_env;
  113. unsigned long last_control;
  114. unsigned long start_requested_count;
  115. unsigned long start_count;
  116. unsigned long exit_count;
  117. } nssm_service_t;
  118. void WINAPI service_main(unsigned long, TCHAR **);
  119. TCHAR *service_control_text(unsigned long);
  120. TCHAR *service_status_text(unsigned long);
  121. void log_service_control(TCHAR *, unsigned long, bool);
  122. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  123. int affinity_mask_to_string(__int64, TCHAR **);
  124. int affinity_string_to_mask(TCHAR *, __int64 *);
  125. unsigned long priority_mask();
  126. int priority_constant_to_index(unsigned long);
  127. unsigned long priority_index_to_constant(int);
  128. nssm_service_t *alloc_nssm_service();
  129. void set_nssm_service_defaults(nssm_service_t *);
  130. void cleanup_nssm_service(nssm_service_t *);
  131. SC_HANDLE open_service_manager(unsigned long);
  132. SC_HANDLE open_service(SC_HANDLE, TCHAR *, unsigned long, TCHAR *, unsigned long);
  133. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  134. int append_to_dependencies(TCHAR *, unsigned long, TCHAR *, TCHAR **, unsigned long *, int);
  135. int remove_from_dependencies(TCHAR *, unsigned long, TCHAR *, TCHAR **, unsigned long *, int);
  136. int set_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR *);
  137. int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *, int);
  138. int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *);
  139. int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);
  140. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  141. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  142. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  143. void set_service_environment(nssm_service_t *);
  144. void unset_service_environment(nssm_service_t *);
  145. int pre_install_service(int, TCHAR **);
  146. int pre_remove_service(int, TCHAR **);
  147. int pre_edit_service(int, TCHAR **);
  148. int install_service(nssm_service_t *);
  149. int remove_service(nssm_service_t *);
  150. int edit_service(nssm_service_t *, bool);
  151. int control_service(unsigned long, int, TCHAR **, bool);
  152. int control_service(unsigned long, int, TCHAR **);
  153. void set_service_recovery(nssm_service_t *);
  154. int monitor_service(nssm_service_t *);
  155. int start_service(nssm_service_t *);
  156. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  157. void CALLBACK end_service(void *, unsigned char);
  158. void throttle_restart(nssm_service_t *);
  159. int await_single_handle(SERVICE_STATUS_HANDLE, SERVICE_STATUS *, HANDLE, TCHAR *, TCHAR *, unsigned long);
  160. int list_nssm_services(int, TCHAR **);
  161. int service_process_tree(int, TCHAR **);
  162. #endif