service.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. typedef struct {
  20. bool native;
  21. TCHAR name[SERVICE_NAME_LENGTH];
  22. TCHAR displayname[SERVICE_DISPLAYNAME_LENGTH];
  23. TCHAR description[VALUE_LENGTH];
  24. unsigned long startup;
  25. TCHAR *username;
  26. size_t usernamelen;
  27. TCHAR *password;
  28. size_t passwordlen;
  29. unsigned long type;
  30. TCHAR image[MAX_PATH];
  31. TCHAR exe[EXE_LENGTH];
  32. TCHAR flags[VALUE_LENGTH];
  33. TCHAR dir[MAX_PATH];
  34. TCHAR *env;
  35. unsigned long envlen;
  36. TCHAR *env_extra;
  37. unsigned long env_extralen;
  38. TCHAR stdin_path[MAX_PATH];
  39. unsigned long stdin_sharing;
  40. unsigned long stdin_disposition;
  41. unsigned long stdin_flags;
  42. TCHAR stdout_path[MAX_PATH];
  43. unsigned long stdout_sharing;
  44. unsigned long stdout_disposition;
  45. unsigned long stdout_flags;
  46. TCHAR stderr_path[MAX_PATH];
  47. unsigned long stderr_sharing;
  48. unsigned long stderr_disposition;
  49. unsigned long stderr_flags;
  50. bool rotate_files;
  51. unsigned long rotate_seconds;
  52. unsigned long rotate_bytes_low;
  53. unsigned long rotate_bytes_high;
  54. unsigned long default_exit_action;
  55. unsigned long throttle_delay;
  56. unsigned long stop_method;
  57. unsigned long kill_console_delay;
  58. unsigned long kill_window_delay;
  59. unsigned long kill_threads_delay;
  60. SC_HANDLE handle;
  61. SERVICE_STATUS status;
  62. SERVICE_STATUS_HANDLE status_handle;
  63. HANDLE process_handle;
  64. unsigned long pid;
  65. HANDLE wait_handle;
  66. bool stopping;
  67. bool allow_restart;
  68. unsigned long throttle;
  69. CRITICAL_SECTION throttle_section;
  70. bool throttle_section_initialised;
  71. CONDITION_VARIABLE throttle_condition;
  72. HANDLE throttle_timer;
  73. LARGE_INTEGER throttle_duetime;
  74. FILETIME creation_time;
  75. FILETIME exit_time;
  76. } nssm_service_t;
  77. void WINAPI service_main(unsigned long, TCHAR **);
  78. TCHAR *service_control_text(unsigned long);
  79. void log_service_control(TCHAR *, unsigned long, bool);
  80. unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);
  81. nssm_service_t *alloc_nssm_service();
  82. void set_nssm_service_defaults(nssm_service_t *);
  83. void cleanup_nssm_service(nssm_service_t *);
  84. SC_HANDLE open_service_manager();
  85. QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);
  86. int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);
  87. int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);
  88. int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);
  89. int pre_install_service(int, TCHAR **);
  90. int pre_remove_service(int, TCHAR **);
  91. int pre_edit_service(int, TCHAR **);
  92. int install_service(nssm_service_t *);
  93. int remove_service(nssm_service_t *);
  94. int edit_service(nssm_service_t *, bool);
  95. void set_service_recovery(nssm_service_t *);
  96. int monitor_service(nssm_service_t *);
  97. int start_service(nssm_service_t *);
  98. int stop_service(nssm_service_t *, unsigned long, bool, bool);
  99. void CALLBACK end_service(void *, unsigned char);
  100. void throttle_restart(nssm_service_t *);
  101. int await_shutdown(nssm_service_t *, TCHAR *, unsigned long);
  102. #endif