xref: /reactos/base/services/wkssvc/wkssvc.c (revision 3435c3b5)
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/wkssvc/wkssvc.c
23  * PURPOSE:          Workstation service
24  * PROGRAMMER:       Eric Kohl
25  */
26 
27 /* INCLUDES *****************************************************************/
28 
29 #include "precomp.h"
30 
31 WINE_DEFAULT_DEBUG_CHANNEL(wkssvc);
32 
33 /* GLOBALS ******************************************************************/
34 
35 static WCHAR ServiceName[] = L"lanmanworkstation";
36 
37 static SERVICE_STATUS_HANDLE ServiceStatusHandle;
38 static SERVICE_STATUS ServiceStatus;
39 
40 /* FUNCTIONS *****************************************************************/
41 
42 static VOID
43 UpdateServiceStatus(DWORD dwState)
44 {
45     ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
46     ServiceStatus.dwCurrentState = dwState;
47     ServiceStatus.dwControlsAccepted = 0;
48     ServiceStatus.dwWin32ExitCode = 0;
49     ServiceStatus.dwServiceSpecificExitCode = 0;
50     ServiceStatus.dwCheckPoint = 0;
51 
52     if (dwState == SERVICE_START_PENDING ||
53         dwState == SERVICE_STOP_PENDING ||
54         dwState == SERVICE_PAUSE_PENDING ||
55         dwState == SERVICE_CONTINUE_PENDING)
56         ServiceStatus.dwWaitHint = 10000;
57     else
58         ServiceStatus.dwWaitHint = 0;
59 
60     if (dwState == SERVICE_RUNNING)
61         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
62 
63     SetServiceStatus(ServiceStatusHandle,
64                      &ServiceStatus);
65 }
66 
67 static DWORD WINAPI
68 ServiceControlHandler(DWORD dwControl,
69                       DWORD dwEventType,
70                       LPVOID lpEventData,
71                       LPVOID lpContext)
72 {
73     TRACE("ServiceControlHandler() called\n");
74 
75     switch (dwControl)
76     {
77         case SERVICE_CONTROL_STOP:
78             TRACE("  SERVICE_CONTROL_STOP received\n");
79             UpdateServiceStatus(SERVICE_STOP_PENDING);
80             /* Stop listening to incoming RPC messages */
81             RpcMgmtStopServerListening(NULL);
82             UpdateServiceStatus(SERVICE_STOPPED);
83             return ERROR_SUCCESS;
84 
85         case SERVICE_CONTROL_PAUSE:
86             TRACE("  SERVICE_CONTROL_PAUSE received\n");
87             UpdateServiceStatus(SERVICE_PAUSED);
88             return ERROR_SUCCESS;
89 
90         case SERVICE_CONTROL_CONTINUE:
91             TRACE("  SERVICE_CONTROL_CONTINUE received\n");
92             UpdateServiceStatus(SERVICE_RUNNING);
93             return ERROR_SUCCESS;
94 
95         case SERVICE_CONTROL_INTERROGATE:
96             TRACE("  SERVICE_CONTROL_INTERROGATE received\n");
97             SetServiceStatus(ServiceStatusHandle,
98                              &ServiceStatus);
99             return ERROR_SUCCESS;
100 
101         case SERVICE_CONTROL_SHUTDOWN:
102             TRACE("  SERVICE_CONTROL_SHUTDOWN received\n");
103             UpdateServiceStatus(SERVICE_STOP_PENDING);
104             /* Stop listening to incoming RPC messages */
105             RpcMgmtStopServerListening(NULL);
106             UpdateServiceStatus(SERVICE_STOPPED);
107             return ERROR_SUCCESS;
108 
109         default :
110             TRACE("  Control %lu received\n", dwControl);
111             return ERROR_CALL_NOT_IMPLEMENTED;
112     }
113 }
114 
115 
116 static
117 DWORD
118 ServiceInit(VOID)
119 {
120     HANDLE hThread;
121 
122     hThread = CreateThread(NULL,
123                            0,
124                            (LPTHREAD_START_ROUTINE)RpcThreadRoutine,
125                            NULL,
126                            0,
127                            NULL);
128 
129     if (!hThread)
130     {
131         ERR("Can't create PortThread\n");
132         return GetLastError();
133     }
134     else
135         CloseHandle(hThread);
136 
137     /* Report a running workstation service */
138     SetServiceBits(ServiceStatusHandle,
139                    SV_TYPE_WORKSTATION,
140                    TRUE,
141                    TRUE);
142 
143     return ERROR_SUCCESS;
144 }
145 
146 
147 VOID 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