xref: /reactos/base/services/wkssvc/wkssvc.c (revision b893124a)
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     SetServiceStatus(ServiceStatusHandle,
61                      &ServiceStatus);
62 }
63 
64 static DWORD WINAPI
65 ServiceControlHandler(DWORD dwControl,
66                       DWORD dwEventType,
67                       LPVOID lpEventData,
68                       LPVOID lpContext)
69 {
70     TRACE("ServiceControlHandler() called\n");
71 
72     switch (dwControl)
73     {
74         case SERVICE_CONTROL_STOP:
75             TRACE("  SERVICE_CONTROL_STOP received\n");
76             /* Stop listening to incoming RPC messages */
77             RpcMgmtStopServerListening(NULL);
78             UpdateServiceStatus(SERVICE_STOPPED);
79             return ERROR_SUCCESS;
80 
81         case SERVICE_CONTROL_PAUSE:
82             TRACE("  SERVICE_CONTROL_PAUSE received\n");
83             UpdateServiceStatus(SERVICE_PAUSED);
84             return ERROR_SUCCESS;
85 
86         case SERVICE_CONTROL_CONTINUE:
87             TRACE("  SERVICE_CONTROL_CONTINUE received\n");
88             UpdateServiceStatus(SERVICE_RUNNING);
89             return ERROR_SUCCESS;
90 
91         case SERVICE_CONTROL_INTERROGATE:
92             TRACE("  SERVICE_CONTROL_INTERROGATE received\n");
93             SetServiceStatus(ServiceStatusHandle,
94                              &ServiceStatus);
95             return ERROR_SUCCESS;
96 
97         case SERVICE_CONTROL_SHUTDOWN:
98             TRACE("  SERVICE_CONTROL_SHUTDOWN received\n");
99             UpdateServiceStatus(SERVICE_STOPPED);
100             return ERROR_SUCCESS;
101 
102         default :
103             TRACE("  Control %lu received\n", dwControl);
104             return ERROR_CALL_NOT_IMPLEMENTED;
105     }
106 }
107 
108 
109 static
110 DWORD
111 ServiceInit(VOID)
112 {
113     HANDLE hThread;
114 
115     hThread = CreateThread(NULL,
116                            0,
117                            (LPTHREAD_START_ROUTINE)RpcThreadRoutine,
118                            NULL,
119                            0,
120                            NULL);
121 
122     if (!hThread)
123     {
124         ERR("Can't create PortThread\n");
125         return GetLastError();
126     }
127     else
128         CloseHandle(hThread);
129 
130     /* Report a running workstation service */
131     SetServiceBits(ServiceStatusHandle,
132                    SV_TYPE_WORKSTATION,
133                    TRUE,
134                    TRUE);
135 
136     return ERROR_SUCCESS;
137 }
138 
139 
140 VOID WINAPI
141 ServiceMain(DWORD argc, LPTSTR *argv)
142 {
143     DWORD dwError;
144 
145     UNREFERENCED_PARAMETER(argc);
146     UNREFERENCED_PARAMETER(argv);
147 
148     TRACE("ServiceMain() called\n");
149 
150     ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
151                                                         ServiceControlHandler,
152                                                         NULL);
153     if (!ServiceStatusHandle)
154     {
155         ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
156         return;
157     }
158 
159     UpdateServiceStatus(SERVICE_START_PENDING);
160 
161     dwError = ServiceInit();
162     if (dwError != ERROR_SUCCESS)
163     {
164         ERR("Service stopped (dwError: %lu\n", dwError);
165         UpdateServiceStatus(SERVICE_STOPPED);
166         return;
167     }
168 
169     UpdateServiceStatus(SERVICE_RUNNING);
170 }
171 
172 
173 BOOL WINAPI
174 DllMain(HINSTANCE hinstDLL,
175         DWORD fdwReason,
176         LPVOID lpvReserved)
177 {
178     switch (fdwReason)
179     {
180         case DLL_PROCESS_ATTACH:
181             DisableThreadLibraryCalls(hinstDLL);
182             break;
183 
184         case DLL_PROCESS_DETACH:
185             break;
186     }
187 
188     return TRUE;
189 }
190