xref: /reactos/base/services/srvsvc/srvsvc.c (revision eab73ad1)
1 /*
2  *  ReactOS Services
3  *  Copyright (C) 2015 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 /*
20  * COPYRIGHT:        See COPYING in the top level directory
21  * PROJECT:          ReactOS Services
22  * FILE:             base/services/srvsvc/srvsvc.c
23  * PURPOSE:          Server service
24  * PROGRAMMER:       Eric Kohl
25  */
26 
27 /* INCLUDES *****************************************************************/
28 
29 #include "precomp.h"
30 
31 WINE_DEFAULT_DEBUG_CHANNEL(srvsvc);
32 
33 /* GLOBALS ******************************************************************/
34 
35 static WCHAR ServiceName[] = L"Lanmanserver";
36 
37 static SERVICE_STATUS_HANDLE ServiceStatusHandle;
38 static SERVICE_STATUS ServiceStatus;
39 
40 DWORD dwServiceBits = 0;
41 
42 
43 /* FUNCTIONS *****************************************************************/
44 
45 static VOID
46 UpdateServiceStatus(DWORD dwState)
47 {
48     ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
49     ServiceStatus.dwCurrentState = dwState;
50     ServiceStatus.dwControlsAccepted = 0;
51     ServiceStatus.dwWin32ExitCode = 0;
52     ServiceStatus.dwServiceSpecificExitCode = 0;
53     ServiceStatus.dwCheckPoint = 0;
54 
55     if (dwState == SERVICE_START_PENDING ||
56         dwState == SERVICE_STOP_PENDING ||
57         dwState == SERVICE_PAUSE_PENDING ||
58         dwState == SERVICE_CONTINUE_PENDING)
59         ServiceStatus.dwWaitHint = 10000;
60     else
61         ServiceStatus.dwWaitHint = 0;
62 
63     if (dwState == SERVICE_RUNNING)
64         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
65 
66     SetServiceStatus(ServiceStatusHandle,
67                      &ServiceStatus);
68 }
69 
70 static DWORD WINAPI
71 ServiceControlHandler(DWORD dwControl,
72                       DWORD dwEventType,
73                       LPVOID lpEventData,
74                       LPVOID lpContext)
75 {
76     TRACE("ServiceControlHandler() called\n");
77 
78     switch (dwControl)
79     {
80         case SERVICE_CONTROL_STOP:
81             TRACE("  SERVICE_CONTROL_STOP received\n");
82             /* Stop listening to incoming RPC messages */
83             RpcMgmtStopServerListening(NULL);
84             UpdateServiceStatus(SERVICE_STOPPED);
85             return ERROR_SUCCESS;
86 
87         case SERVICE_CONTROL_PAUSE:
88             TRACE("  SERVICE_CONTROL_PAUSE received\n");
89             UpdateServiceStatus(SERVICE_PAUSED);
90             return ERROR_SUCCESS;
91 
92         case SERVICE_CONTROL_CONTINUE:
93             TRACE("  SERVICE_CONTROL_CONTINUE received\n");
94             UpdateServiceStatus(SERVICE_RUNNING);
95             return ERROR_SUCCESS;
96 
97         case SERVICE_CONTROL_INTERROGATE:
98             TRACE("  SERVICE_CONTROL_INTERROGATE received\n");
99             SetServiceStatus(ServiceStatusHandle,
100                              &ServiceStatus);
101             return ERROR_SUCCESS;
102 
103         case SERVICE_CONTROL_SHUTDOWN:
104             TRACE("  SERVICE_CONTROL_SHUTDOWN received\n");
105             UpdateServiceStatus(SERVICE_STOPPED);
106             return ERROR_SUCCESS;
107 
108         default :
109             TRACE("  Control %lu received\n", dwControl);
110             return ERROR_CALL_NOT_IMPLEMENTED;
111     }
112 }
113 
114 
115 static
116 DWORD
117 ServiceInit(VOID)
118 {
119     HANDLE hThread;
120 
121     hThread = CreateThread(NULL,
122                            0,
123                            (LPTHREAD_START_ROUTINE)RpcThreadRoutine,
124                            NULL,
125                            0,
126                            NULL);
127 
128     if (!hThread)
129     {
130         ERR("Can't create PortThread\n");
131         return GetLastError();
132     }
133     else
134         CloseHandle(hThread);
135 
136     /* Report a running server service */
137     SetServiceBits(ServiceStatusHandle,
138                    SV_TYPE_SERVER,
139                    TRUE,
140                    TRUE);
141 
142     return ERROR_SUCCESS;
143 }
144 
145 
146 VOID
147 WINAPI
148 ServiceMain(DWORD argc, LPTSTR *argv)
149 {
150     DWORD dwError;
151 
152     UNREFERENCED_PARAMETER(argc);
153     UNREFERENCED_PARAMETER(argv);
154 
155     TRACE("ServiceMain() called\n");
156 
157     ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
158                                                         ServiceControlHandler,
159                                                         NULL);
160     if (!ServiceStatusHandle)
161     {
162         ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
163         return;
164     }
165 
166     UpdateServiceStatus(SERVICE_START_PENDING);
167 
168     dwError = ServiceInit();
169     if (dwError != ERROR_SUCCESS)
170     {
171         ERR("Service stopped (dwError: %lu\n", dwError);
172         UpdateServiceStatus(SERVICE_STOPPED);
173         return;
174     }
175 
176     UpdateServiceStatus(SERVICE_RUNNING);
177 }
178 
179 
180 BOOL WINAPI
181 DllMain(HINSTANCE hinstDLL,
182         DWORD fdwReason,
183         LPVOID lpvReserved)
184 {
185     switch (fdwReason)
186     {
187         case DLL_PROCESS_ATTACH:
188             DisableThreadLibraryCalls(hinstDLL);
189             break;
190 
191         case DLL_PROCESS_DETACH:
192             break;
193     }
194 
195     return TRUE;
196 }
197