[r7651]: / main / trunk / rexxapi / client / platform / windows / SysLocalAPIManager.cpp  Maximize  Restore  History

Download this file

420 lines (376 with data), 14.7 kB

  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
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*----------------------------------------------------------------------------*/
/* */
/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
/* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the Common Public License v1.0 which accompanies this */
/* distribution. A copy is also available at the following address: */
/* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the distribution. */
/* */
/* Neither the name of Rexx Language Association nor the names */
/* of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/*----------------------------------------------------------------------------*/
#include "SysLocalAPIManager.hpp"
#include "LocalAPIManager.hpp"
#include <stdio.h>
/* - - - - Temporary stuff for debugging help, will be removed - - - - - - - */
#if 0
#include <shlobj.h>
#include <shlwapi.h>
TCHAR fileBuf[MAX_PATH];
static const char *fileName = NULL;
void __cdecl DebugMsg(const char* pszFormat, ...)
{
FILE *stream;
va_list arglist;
char buf[1024];
sprintf(buf, "[%s](%lu): ", SERVICENAME, GetCurrentThreadId());
va_start(arglist, pszFormat);
vsprintf(&buf[strlen(buf)], pszFormat, arglist);
va_end(arglist);
strcat(buf, "\n");
if ( fileName == NULL )
{
SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS | CSIDL_FLAG_CREATE, NULL, 0, fileBuf);
PathAppend(fileBuf, "clientApi.log");
fileName = fileBuf;
}
stream = fopen(fileName, "a+");
if ( stream )
{
fwrite(buf, 1, strlen(buf), stream);
fclose(stream);
}
}
#endif
/* - - End Temporary stuff for debugging help - - - - - - - - - - - - - - - - */
#define SERVICENAME "RXAPI"
// Windows Service status codes
typedef enum
{
WS_IS_RUNNING = 0,
WS_NOT_INSTALLED,
WS_IS_DISABLED,
WS_IS_STOPPED,
WS_NOT_PRIVILEGED,
WS_UNKNOWN
} WinServiceStatusT;
/**
* Reports if the rxapi service is in the running state at the time this
* function returns. If the service is in the START PENDING state the function
* waits untils the service is running or has timed out.
*
* @param hService Opened service handle, must have the SERVICE_QUERY_STATUS
* privilege.
*
* @return True the service is not running, otherwise false.
*/
static bool hasServiceStarted(SC_HANDLE hService)
{
SERVICE_STATUS_PROCESS ssp;
DWORD needed;
if ( QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &needed) == 0 )
{
return false;
}
if ( ssp.dwCurrentState == SERVICE_RUNNING || ssp.dwCurrentState == SERVICE_STOPPED ||
ssp.dwCurrentState == SERVICE_STOP_PENDING )
{
return ssp.dwCurrentState == SERVICE_RUNNING;
}
// Save the tick count and initial checkpoint.
uint32_t startTicks = GetTickCount();
uint32_t oldCheck = ssp.dwCheckPoint;
uint32_t waitTime;
// Check the status until the service is no longer start pending. rxapi is
// not pausable, so PAUSED or PAUSED_PENDING should not be possible.
while ( ssp.dwCurrentState == SERVICE_START_PENDING )
{
// Do not wait longer than the wait hint, which for rxapi will be 2000
// milliseconds.
//
// Microsoft suggests that a good interval is one tenth the wait hint,
// but not less than 1 second and no more than 10 seconds. rxapi usually
// starts in less than 200 milliseconds.
waitTime = ssp.dwWaitHint / 10;
if( waitTime < 200 )
{
waitTime = 200;
}
else if ( waitTime > 10000 )
{
waitTime = 10000;
}
Sleep(waitTime);
BOOL success = QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &needed);
if ( ! success || ssp.dwCurrentState == SERVICE_RUNNING )
{
break;
}
if ( ssp.dwCheckPoint > oldCheck )
{
// The service is making progress, so continue.
startTicks = GetTickCount();
oldCheck = ssp.dwCheckPoint;
}
else
{
if( (GetTickCount() - startTicks) > ssp.dwWaitHint )
{
// The wait hint interval has expired and we are still not
// started, so quit.
break;
}
}
}
return ssp.dwCurrentState == SERVICE_RUNNING ? true : false;
}
/**
* Get the status of the rxapi service, which could be not installed as a
* service.
*
* @param phSCM Pointer to a handle for the Service Control Manager. If rxapi
* is installed as a service, and not disabled, an open handle is
* returned here. Otherwise the handle is set to NULL.
*
* @return A WinServiceStatusT enum indicating the status of rxapi as a Windows
* service.
*/
static WinServiceStatusT getServiceStatus(SC_HANDLE *phSCM)
{
WinServiceStatusT status = WS_UNKNOWN;
SC_HANDLE hService = NULL;
// Open the Service Control Manager
SC_HANDLE hSCM = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
if ( hSCM )
{
// Open the service with the query access rights.
hService = OpenService(hSCM, SERVICENAME, SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS);
if ( hService )
{
// The service is installed, make sure it is not currently disabled.
LPQUERY_SERVICE_CONFIG serviceCfg = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, 4096);
if ( serviceCfg )
{
DWORD needed;
if ( QueryServiceConfig(hService, serviceCfg, 4096, &needed) )
{
if ( serviceCfg->dwStartType == SERVICE_DISABLED )
{
status = WS_IS_DISABLED;
}
}
LocalFree(serviceCfg);
}
else
{
status = WS_NOT_PRIVILEGED;
}
if ( status != WS_UNKNOWN )
{
CloseServiceHandle(hService);
hService = NULL;
}
}
}
else
{
status = WS_NOT_PRIVILEGED;
}
if ( status != WS_UNKNOWN )
{
if ( hSCM != NULL )
{
CloseServiceHandle(hSCM);
}
return status;
}
*phSCM = hSCM;
if ( hasServiceStarted(hService) )
{
status = WS_IS_RUNNING;
}
else
{
status = WS_IS_STOPPED;
}
CloseServiceHandle(hService);
return status;
}
/**
* Determines if rxapi is installed and running as a service.
*
* If rxapi is installed as a service, but not running an attempt is made to
* start the service. When the service is not disabled, and the user (owner of
* this running process) has the authority to start the rxapi service, then the
* attempt should succeed.
*
* If the function attempts to start the service, it will wait to see that the
* service actually starts. If the above conditions are met, then the service
* will start.
*
* @return True if the service is started and running, otherwise false.
*
* @note Both the service control manager and the service itself are opened
* with the minimum access rights needed to accomplish the task. On
* Vista this is especially important because it prevents unnecessary
* failures.
*/
bool startAsService(void)
{
SC_HANDLE hSCM = NULL;
// First see if rxapi is already running as a service, or if not running, if
// there is at least a possibility of starting it.
WinServiceStatusT status = getServiceStatus(&hSCM);
if ( status == WS_NOT_INSTALLED || status == WS_IS_DISABLED || status == WS_NOT_PRIVILEGED )
{
return false;
}
else if ( status == WS_IS_RUNNING )
{
CloseServiceHandle(hSCM);
return true;
}
// Okay, the service is not running, but it is installed and should be
// startable.
SC_HANDLE hService = OpenService(hSCM, SERVICENAME, SERVICE_QUERY_CONFIG | SERVICE_START | SERVICE_QUERY_STATUS);
if ( hService == NULL )
{
// Seems the user has sufficient privileges to query the service, but
// not to start it.
if ( hSCM != NULL )
{
CloseServiceHandle(hSCM);
}
return false;
}
bool hasStarted = false;
if ( ! StartService(hService, 0, NULL) )
{
goto done_out;
}
hasStarted = hasServiceStarted(hService);
done_out:
CloseServiceHandle(hSCM);
CloseServiceHandle(hService);
return hasStarted;
}
void SysLocalAPIManager::startServerProcess()
{
// First try to start rxapi as a Windows service. If that fails, then start
// it as a normal process.
if ( startAsService() )
{
return;
}
char apiExeName[] = "RXAPI.EXE";
LPCTSTR lpszImageName = NULL; // address of module name
LPTSTR lpszCommandLine = NULL; // address of command line
LPSECURITY_ATTRIBUTES
lpsaProcess = NULL; // address of process security attrs
LPSECURITY_ATTRIBUTES
lpsaThread = NULL; // address of thread security attrs */
/* don't inherit handles, because otherwise files are inherited
and inaccessible until RXAPI.EXE is stopped again */
BOOL fInheritHandles = FALSE; // new process doesn't inherit handles
DWORD fdwCreate = DETACHED_PROCESS; // creation flags
LPVOID lpvEnvironment = NULL; // address of new environment block
char szSysDir[256]; // retrieve system directory
LPCTSTR lpszCurDir = szSysDir; // address of command line
LPSTARTUPINFO lpsiStartInfo; // address of STARTUPINFO
STARTUPINFO siStartInfo;
LPPROCESS_INFORMATION lppiProcInfo; // address of PROCESS_INFORMATION
PROCESS_INFORMATION MemMgrProcessInfo;
lppiProcInfo = &MemMgrProcessInfo;
siStartInfo.cb = sizeof(siStartInfo);
siStartInfo.lpReserved = NULL;
siStartInfo.lpDesktop = NULL;
siStartInfo.lpTitle = NULL;
siStartInfo.dwX = 0;
siStartInfo.dwY = 0;
siStartInfo.dwXSize = 0;
siStartInfo.dwYSize = 0;
siStartInfo.dwXCountChars = 0;
siStartInfo.dwYCountChars = 0;
siStartInfo.dwFillAttribute = 0;
siStartInfo.dwFlags = 0;
siStartInfo.wShowWindow = 0;
siStartInfo.cbReserved2 = 0;
siStartInfo.lpReserved2 = NULL;
siStartInfo.hStdInput = NULL;
siStartInfo.hStdOutput = NULL;
siStartInfo.hStdError = NULL;
lpsiStartInfo = &siStartInfo;
lpszCommandLine = apiExeName;
/* start RXAPI process out of system directory */
if (!GetSystemDirectory(szSysDir, 255))
{
lpszCurDir = NULL;
}
if(!CreateProcess(lpszImageName, lpszCommandLine, lpsaProcess,
lpsaThread, fInheritHandles, fdwCreate, lpvEnvironment,
lpszCurDir, lpsiStartInfo, lppiProcInfo))
{
throw new ServiceException(API_FAILURE, "Unable to start API server");
}
}
/**
* Check to see if we've inherited a session queue from a calling process. This shows
* up as an environment variable value.
*
* @param sessionQueue
* The returned session queue handle, if it exists.
*
* @return true if the session queue is inherited, false if a new once needs to be created.
*/
bool SysLocalAPIManager::getActiveSessionQueue(QueueHandle &sessionQueue)
{
// check to see if we have an env variable set...if we do we
// inherit from our parent session
char envbuffer[MAX_QUEUE_NAME_LENGTH+1];
DWORD envchars = GetEnvironmentVariable("RXQUEUESESSION", (LPTSTR) envbuffer, MAX_QUEUE_NAME_LENGTH);
if (envchars != 0)
{
sscanf(envbuffer, "%p", &sessionQueue);
return true;
}
return false;
}
/**
* Set the active session queue as an environment variable.
*
* @param sessionQueue
* The session queue handle.
*/
void SysLocalAPIManager::setActiveSessionQueue(QueueHandle sessionQueue)
{
char envbuffer[MAX_QUEUE_NAME_LENGTH+1];
// and set this as an environment variable for programs we call
sprintf(envbuffer, "%p", sessionQueue);
SetEnvironmentVariable("RXQUEUESESSION", (LPTSTR) envbuffer);
}