UNPKG

4.82 kBtext/x-cView Raw
1/*
2Copyright (c) 2011-2013 Roger Light <roger@atchoo.org>
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
133. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#if defined(WIN32) || defined(__CYGWIN__)
31
32#include <windows.h>
33
34#include <memory_mosq.h>
35
36extern int run;
37SERVICE_STATUS_HANDLE service_handle = 0;
38static SERVICE_STATUS service_status;
39int main(int argc, char *argv[]);
40
41/* Service control callback */
42void __stdcall service_handler(DWORD fdwControl)
43{
44 switch(fdwControl){
45 case SERVICE_CONTROL_CONTINUE:
46 /* Continue from Paused state. */
47 break;
48 case SERVICE_CONTROL_PAUSE:
49 /* Pause service. */
50 break;
51 case SERVICE_CONTROL_SHUTDOWN:
52 /* System is shutting down. */
53 case SERVICE_CONTROL_STOP:
54 /* Service should stop. */
55 service_status.dwCurrentState = SERVICE_STOP_PENDING;
56 SetServiceStatus(service_handle, &service_status);
57 run = 0;
58 break;
59 }
60}
61
62/* Function called when started as a service. */
63void __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
101void 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
126void 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
146void 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