service.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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. bool stdout_copy_and_truncate;
  71. bool stderr_copy_and_truncate;
  72. unsigned long rotate_stdout_online;
  73. unsigned long rotate_stderr_online;
  74. unsigned long rotate_seconds;
  75. unsigned long rotate_bytes_low;
  76. unsigned long rotate_bytes_high;
  77. unsigned long rotate_delay;
  78. unsigned long default_exit_action;
  79. unsigned long restart_delay;
  80. unsigned long throttle_delay;
  81. unsigned long stop_method;
  82. unsigned long kill_console_delay;
  83. unsigned long kill_window_delay;
  84. unsigned long kill_threads_delay;
  85. bool kill_process_tree;
  86. SC_HANDLE handle;
  87. SERVICE_STATUS status;
  88. SERVICE_STATUS_HANDLE status_handle;
  89. HANDLE process_handle;
  90. unsigned long pid;
  91. HANDLE wait_handle;
  92. bool stopping;
  93. bool allow_restart;
  94. unsigned long throttle;
  95. CRITICAL_SECTION throttle_section;
  96. bool throttle_section_initialised;
  97. CONDITION_VARIABLE throttle_condition;
  98. HANDLE throttle_timer;
  99. LARGE_INTEGER throttle_duetime;
  100. FILETIME creation_time;
  101. FILETIME exit_time;
  102. TCHAR *initial_env;
  103. } nssm_service_t;
  104. void WINAPI service_main(unsigned long, TCHAR **);
  105. TCHAR *service_control_text(unsigned long);
  106. TCHAR *service_status_text(unsigned long);
  107. void log_service_control(TCHAR *, unsigned long, bool);
  108. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  109. int affinity_mask_to_string(__int64, TCHAR **);
  110. int affinity_string_to_mask(TCHAR *, __int64 *);
  111. unsigned long priority_mask();
  112. int priority_constant_to_index(unsigned long);
  113. unsigned long priority_index_to_constant(int);
  114. nssm_service_t *alloc_nssm_service();
  115. void set_nssm_service_defaults(nssm_service_t *);
  116. void cleanup_nssm_service(nssm_service_t *);
  117. SC_HANDLE open_service_manager(unsigned long);
  118. SC_HANDLE open_service(SC_HANDLE, TCHAR *, unsigned long, TCHAR *, unsigned long);
  119. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  120. int set_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR *);
  121. int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *, int);
  122. int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *);
  123. int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);
  124. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  125. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  126. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  127. int pre_install_service(int, TCHAR **);
  128. int pre_remove_service(int, TCHAR **);
  129. int pre_edit_service(int, TCHAR **);
  130. int install_service(nssm_service_t *);
  131. int remove_service(nssm_service_t *);
  132. int edit_service(nssm_service_t *, bool);
  133. int control_service(unsigned long, int, TCHAR **);
  134. void set_service_recovery(nssm_service_t *);
  135. int monitor_service(nssm_service_t *);
  136. int start_service(nssm_service_t *);
  137. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  138. void CALLBACK end_service(void *, unsigned char);
  139. void throttle_restart(nssm_service_t *);
  140. int await_shutdown(nssm_service_t *, TCHAR *, unsigned long);
  141. int await_startup(nssm_service_t *);
  142. #endif