1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | #if defined(WIN32) || defined(__CYGWIN__)
|
31 |
|
32 | #include <windows.h>
|
33 |
|
34 | #include <memory_mosq.h>
|
35 |
|
36 | extern int run;
|
37 | SERVICE_STATUS_HANDLE service_handle = 0;
|
38 | static SERVICE_STATUS service_status;
|
39 | int main(int argc, char *argv[]);
|
40 |
|
41 |
|
42 | void __stdcall service_handler(DWORD fdwControl)
|
43 | {
|
44 | switch(fdwControl){
|
45 | case SERVICE_CONTROL_CONTINUE:
|
46 |
|
47 | break;
|
48 | case SERVICE_CONTROL_PAUSE:
|
49 |
|
50 | break;
|
51 | case SERVICE_CONTROL_SHUTDOWN:
|
52 |
|
53 | case SERVICE_CONTROL_STOP:
|
54 |
|
55 | service_status.dwCurrentState = SERVICE_STOP_PENDING;
|
56 | SetServiceStatus(service_handle, &service_status);
|
57 | run = 0;
|
58 | break;
|
59 | }
|
60 | }
|
61 |
|
62 |
|
63 | void __stdcall service_main(DWORD dwArgc, LPTSTR *lpszArgv)
|
64 | {
|
65 | char **argv;
|
66 | int argc = 1;
|
67 | char conf_path[MAX_PATH + 20];
|
68 | int rc;
|
69 |
|
70 | service_handle = RegisterServiceCtrlHandler("mosquitto", service_handler);
|
71 | if(service_handle){
|
72 | rc = GetEnvironmentVariable("MOSQUITTO_DIR", conf_path, MAX_PATH);
|
73 | if(!rc || rc == MAX_PATH){
|
74 | service_status.dwCurrentState = SERVICE_STOPPED;
|
75 | SetServiceStatus(service_handle, &service_status);
|
76 | return;
|
77 | }
|
78 | strcat(conf_path, "/mosquitto.conf");
|
79 |
|
80 | argv = _mosquitto_malloc(sizeof(char *)*3);
|
81 | argv[0] = "mosquitto";
|
82 | argv[1] = "-c";
|
83 | argv[2] = conf_path;
|
84 | argc = 3;
|
85 |
|
86 | service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
87 | service_status.dwCurrentState = SERVICE_RUNNING;
|
88 | service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
|
89 | service_status.dwWin32ExitCode = NO_ERROR;
|
90 | service_status.dwCheckPoint = 0;
|
91 | SetServiceStatus(service_handle, &service_status);
|
92 |
|
93 | main(argc, argv);
|
94 | _mosquitto_free(argv);
|
95 |
|
96 | service_status.dwCurrentState = SERVICE_STOPPED;
|
97 | SetServiceStatus(service_handle, &service_status);
|
98 | }
|
99 | }
|
100 |
|
101 | void service_install(void)
|
102 | {
|
103 | SC_HANDLE sc_manager, svc_handle;
|
104 | char exe_path[MAX_PATH + 5];
|
105 | SERVICE_DESCRIPTION svc_desc;
|
106 |
|
107 | GetModuleFileName(NULL, exe_path, MAX_PATH);
|
108 | strcat(exe_path, " run");
|
109 |
|
110 | sc_manager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
|
111 | if(sc_manager){
|
112 | svc_handle = CreateService(sc_manager, "mosquitto", "Mosquitto Broker",
|
113 | SERVICE_START | SERVICE_STOP | SERVICE_CHANGE_CONFIG,
|
114 | SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
|
115 | exe_path, NULL, NULL, NULL, NULL, NULL);
|
116 |
|
117 | if(svc_handle){
|
118 | svc_desc.lpDescription = "MQTT v3.1 broker";
|
119 | ChangeServiceConfig2(svc_handle, SERVICE_CONFIG_DESCRIPTION, &svc_desc);
|
120 | CloseServiceHandle(svc_handle);
|
121 | }
|
122 | CloseServiceHandle(sc_manager);
|
123 | }
|
124 | }
|
125 |
|
126 | void service_uninstall(void)
|
127 | {
|
128 | SC_HANDLE sc_manager, svc_handle;
|
129 | SERVICE_STATUS status;
|
130 |
|
131 | sc_manager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
|
132 | if(sc_manager){
|
133 | svc_handle = OpenService(sc_manager, "mosquitto", SERVICE_QUERY_STATUS | DELETE);
|
134 | if(svc_handle){
|
135 | if(QueryServiceStatus(svc_handle, &status)){
|
136 | if(status.dwCurrentState == SERVICE_STOPPED){
|
137 | DeleteService(svc_handle);
|
138 | }
|
139 | }
|
140 | CloseServiceHandle(svc_handle);
|
141 | }
|
142 | CloseServiceHandle(sc_manager);
|
143 | }
|
144 | }
|
145 |
|
146 | void service_run(void)
|
147 | {
|
148 | SERVICE_TABLE_ENTRY ste[] = {
|
149 | { "mosquitto", service_main },
|
150 | { NULL, NULL }
|
151 | };
|
152 |
|
153 | StartServiceCtrlDispatcher(ste);
|
154 | }
|
155 |
|
156 | #endif
|