service.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. unsigned long envlen;
  43. TCHAR *env_extra;
  44. unsigned long env_extralen;
  45. TCHAR stdin_path[MAX_PATH];
  46. unsigned long stdin_sharing;
  47. unsigned long stdin_disposition;
  48. unsigned long stdin_flags;
  49. TCHAR stdout_path[MAX_PATH];
  50. unsigned long stdout_sharing;
  51. unsigned long stdout_disposition;
  52. unsigned long stdout_flags;
  53. TCHAR stderr_path[MAX_PATH];
  54. unsigned long stderr_sharing;
  55. unsigned long stderr_disposition;
  56. unsigned long stderr_flags;
  57. bool rotate_files;
  58. unsigned long rotate_seconds;
  59. unsigned long rotate_bytes_low;
  60. unsigned long rotate_bytes_high;
  61. unsigned long default_exit_action;
  62. unsigned long throttle_delay;
  63. unsigned long stop_method;
  64. unsigned long kill_console_delay;
  65. unsigned long kill_window_delay;
  66. unsigned long kill_threads_delay;
  67. SC_HANDLE handle;
  68. SERVICE_STATUS status;
  69. SERVICE_STATUS_HANDLE status_handle;
  70. HANDLE process_handle;
  71. unsigned long pid;
  72. HANDLE wait_handle;
  73. bool stopping;
  74. bool allow_restart;
  75. unsigned long throttle;
  76. CRITICAL_SECTION throttle_section;
  77. bool throttle_section_initialised;
  78. CONDITION_VARIABLE throttle_condition;
  79. HANDLE throttle_timer;
  80. LARGE_INTEGER throttle_duetime;
  81. FILETIME creation_time;
  82. FILETIME exit_time;
  83. } nssm_service_t;
  84. void WINAPI service_main(unsigned long, TCHAR **);
  85. TCHAR *service_control_text(unsigned long);
  86. void log_service_control(TCHAR *, unsigned long, bool);
  87. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  88. nssm_service_t *alloc_nssm_service();
  89. void set_nssm_service_defaults(nssm_service_t *);
  90. void cleanup_nssm_service(nssm_service_t *);
  91. SC_HANDLE open_service_manager();
  92. SC_HANDLE open_service(SC_HANDLE, TCHAR *, TCHAR *, unsigned long);
  93. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  94. int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);
  95. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  96. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  97. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  98. int grant_logon_as_service(const TCHAR *);
  99. int pre_install_service(int, TCHAR **);
  100. int pre_remove_service(int, TCHAR **);
  101. int pre_edit_service(int, TCHAR **);
  102. int install_service(nssm_service_t *);
  103. int remove_service(nssm_service_t *);
  104. int edit_service(nssm_service_t *, bool);
  105. int control_service(unsigned long, int, TCHAR **);
  106. void set_service_recovery(nssm_service_t *);
  107. int monitor_service(nssm_service_t *);
  108. int start_service(nssm_service_t *);
  109. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  110. void CALLBACK end_service(void *, unsigned char);
  111. void throttle_restart(nssm_service_t *);
  112. int await_shutdown(nssm_service_t *, TCHAR *, unsigned long);
  113. #endif