mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

ludovicp
27.52.2010 98e131bb4dde9eb6e2befce74256f5caa2eb093b
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License").  You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE
* or https://OpenDS.dev.java.net/OpenDS.LICENSE.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying * information:
*      Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
*      Copyright 2008-2010 Sun Microsystems, Inc.
*/
 
#include "common.h"
#include "service.h"
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <sys/locking.h>
#include <time.h>
 
BOOL DEBUG = TRUE;
char * DEBUG_LOG_NAME = "native-windows.out";
DWORD MAX_DEBUG_LOG_SIZE = 500 * 1000;
char * getDebugLogFileName();
void debugInner(BOOL isError, const char *msg, va_list ap);
void deleteIfLargerThan(char * fileName, DWORD maxSize);
 
// ----------------------------------------------------
// Function used to create a process with the given command.
// The information about the process is stored in procInfo.
// The function returns TRUE if the process could be created
// and FALSE otherwise.
// ----------------------------------------------------
BOOL createChildProcess(char* command, BOOL background,
PROCESS_INFORMATION* procInfo)
{
  BOOL createOk;
  STARTUPINFO startInfo; // info to pass to the new process
  DWORD processFlag; // background process flag
  HANDLE hStdin;                  /* stdin */
  HANDLE hStdout;                 /* stdout */
  HANDLE hStderr;                 /* stderr */
 
  debug("createChildProcess: Attempting to create child process '%s' background=%d.",
      command,
      background);
 
  // reset process info first
  ZeroMemory(procInfo, sizeof(PROCESS_INFORMATION));
 
  // initialize handles to pass to the child process
  ZeroMemory(&startInfo, sizeof(STARTUPINFO));
  startInfo.cb = sizeof(STARTUPINFO);
  startInfo.dwFlags |= STARTF_USESTDHANDLES;  // use handles above
  
  hStdin= GetStdHandle(STD_INPUT_HANDLE);
  SetHandleInformation (hStdin,  HANDLE_FLAG_INHERIT, FALSE);
  hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  SetHandleInformation (hStdout, HANDLE_FLAG_INHERIT, FALSE);
  hStderr = GetStdHandle(STD_ERROR_HANDLE);
  SetHandleInformation (hStderr, HANDLE_FLAG_INHERIT, FALSE);
 
  // Create the child process
  processFlag = background == TRUE ? DETACHED_PROCESS : 0;
  createOk = CreateProcess(
    NULL,          // application name
    command,       // command line
    NULL,          // process security attributes
    NULL,          // primary thread security attributes
    TRUE,          // handles are inherited
    processFlag,   // creation flags
    NULL,          // use parent's environment
    NULL,          // use parent's current directory
    &startInfo,    // STARTUPINFO pointer
    procInfo       // receives PROCESS_INFORMATION
  );
 
  if (createOk)
  {
    debug("createChildProcess: Successfully created child process '%s'.", command);
  }
  else
  {
    debugError(
        "createChildProcess: Failed to create child process '%s'.  Last error = %d.",
        command, GetLastError());
  }
  
  return createOk;
} // createChildProcess
 
BOOL createBatchFileChildProcess(char* batchFile, BOOL background,
PROCESS_INFORMATION* procInfo)
{
  BOOL createOk;
  STARTUPINFO startInfo; // info to pass to the new process
  DWORD processFlag; // background process flag
  HANDLE hStdin;                  /* stdin */
  HANDLE hStdout;                 /* stdout */
  HANDLE hStderr;                 /* stderr */
  char command[COMMAND_SIZE]; // full command line
 
  if (strlen(batchFile) + 3 >= COMMAND_SIZE)
  {
    debug("createBatchFileChildProcess: the batch file path is too long.");
    return FALSE;
  }
  sprintf(command, "/c %s", batchFile);
  debug("createBatchFileChildProcess: Attempting to create child process '%s' background=%d.",
      command,
      background);
 
  // reset process info first
  ZeroMemory(procInfo, sizeof(PROCESS_INFORMATION));
 
  // initialize handles to pass to the child process
  ZeroMemory(&startInfo, sizeof(STARTUPINFO));
  startInfo.cb = sizeof(STARTUPINFO);
  startInfo.dwFlags |= STARTF_USESTDHANDLES;  // use handles above
  
  hStdin= GetStdHandle(STD_INPUT_HANDLE);
  SetHandleInformation (hStdin,  HANDLE_FLAG_INHERIT, FALSE);
  hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  SetHandleInformation (hStdout, HANDLE_FLAG_INHERIT, FALSE);
  hStderr = GetStdHandle(STD_ERROR_HANDLE);
  SetHandleInformation (hStderr, HANDLE_FLAG_INHERIT, FALSE);
 
  // Create the child process
  processFlag = background == TRUE ? DETACHED_PROCESS : 0;
  createOk = CreateProcess(
    "cmd.exe",          // application name
    command,       // command line
    NULL,          // process security attributes
    NULL,          // primary thread security attributes
    TRUE,          // handles are inherited
    processFlag,   // creation flags
    NULL,          // use parent's environment
    NULL,          // use parent's current directory
    &startInfo,    // STARTUPINFO pointer
    procInfo       // receives PROCESS_INFORMATION
  );
 
  if (createOk)
  {
    debug("createBatchFileChildProcess: Successfully created child process '%s'.", command);
  }
  else
  {
    debugError("createBatchFileChildProcess: Failed to create child process '%s'.  Last error = %d.",
        command, GetLastError());
  }
  
  return createOk;
} // createChildProcess
 
 
// ----------------------------------------------------
// Function used to launch a process for the given command
// If the process could be created it returns the pid of
// the created process and -1 otherwise.
// ----------------------------------------------------
int spawn(char* command, BOOL background)
{
  DWORD childPid = -1; // child's pid
  PROCESS_INFORMATION procInfo; // info on the new process
  BOOL createOk;
 
  createOk = createChildProcess(command, background, &procInfo);
 
  if(createOk)
  {
    childPid = procInfo.dwProcessId;
  }
 
  if (childPid != -1)
  {
    debug("The PID of the spawned process is %d.", childPid);
    return childPid;
  }
  else
  {
    debugError("Could not get the PID of the spawned process.");
    return -1;
  }
} // spawn
 
// ----------------------------------------------------
// Function used to wait for a process.
// The passed waitTime parameter is maximum the time in milliseconds to wait.
// Returns TRUE if the process ended and updates the exitCode 
// parameter with the return value of the process.
// Returns FALSE if the process did not end with the provided
// timeout and the error code returned by WaitForSingleObject
// in the provided exitCode value.
// ----------------------------------------------------
BOOL waitForProcess(PROCESS_INFORMATION* procInfo, DWORD waitTime,
 DWORD* exitCode)
{
  BOOL returnValue = TRUE;
  DWORD waitForSingleCode;
  debug("waitForProcess: wait time is: %d", waitTime);
  waitForSingleCode = WaitForSingleObject (procInfo->hProcess, waitTime);
  if (waitForSingleCode == WAIT_OBJECT_0)
  { 
    debug("waitForProcess: was successful");
    GetExitCodeProcess(procInfo->hProcess, exitCode);
    debug("waitForProcess exitCode: %d", *exitCode);
  }
  else
  {
    returnValue = FALSE;
    switch (waitForSingleCode)
    {
      case WAIT_FAILED:
        debug("waitForProcess: Wait for process failed: %d", GetLastError());
        break;
      case WAIT_TIMEOUT:
        debug("waitForProcess: Process timed out.");
        break;
      default:
        debug("waitForProcess: WaitForSingleObject returned %d", waitForSingleCode);
    }
    *exitCode = waitForSingleCode;
  }
  return returnValue;
}
// ---------------------------------------------------
// Debug utility.
// ---------------------------------------------------
void debug(const char *msg, ...)
{
  va_list ap;
    va_start (ap, msg);
  debugInner(FALSE, msg, ap);
  va_end (ap);
}
 
void debugError(const char *msg, ...)
{
  va_list ap;
    va_start (ap, msg);
  debugInner(TRUE, msg, ap);
  va_end (ap);
}
 
void debugInner(BOOL isError, const char *msg, va_list ap)
{
  static DWORD currentProcessPid = 0;
  static BOOL noMessageLogged = TRUE;
    
  // The file containing the log.
  char * logFile;
  FILE *fp;
    time_t rawtime;
  struct tm * timeinfo;
  char formattedTime[100];
  
  if (noMessageLogged)
  {
    currentProcessPid = GetCurrentProcessId();
    noMessageLogged = FALSE;
    debug("--------------- FIRST LOG MESSAGE FROM '%s' ---------------",
        _pgmptr);
  }
 
  // Time-stamp
  time(&rawtime);
  timeinfo = localtime(&rawtime);
  strftime(formattedTime, 100, "%Y/%m/%d %H:%M:%S", timeinfo);
  
  logFile = getDebugLogFileName();
  deleteIfLargerThan(logFile, MAX_DEBUG_LOG_SIZE);
  if ((fp = fopen(logFile, "a")) != NULL)
  {
    fprintf(fp, "%s: (pid=%d)  ", formattedTime, currentProcessPid);
    if (isError) 
    {
      fprintf(fp, "ERROR:  ");
      // It would be nice to echo to stderr, but that doesn't appear to work.
    }
          
    vfprintf(fp, msg, ap);
 
      fprintf(fp, "\n");
    fclose(fp);
  }
  else
  {
    fprintf(stdout, "Could not create log file.\n");
  }
}
 
// ---------------------------------------------------------------
// Get the fully-qualified debug log file name.  The logic in this
// method assumes that the executable of this process is in a 
// direct subdirectory of the instance root.
// ---------------------------------------------------------------
 
char * getDebugLogFileName() 
{
  static char * logFile = NULL;
  char path [MAX_PATH];
  char execName [MAX_PATH];
  char * lastSlash;
 
  if (logFile != NULL) 
  {
    return logFile;
  }
 
  // Get the name of the executable.  
  GetModuleFileName (
    NULL,
    execName,
    MAX_PATH
    ); 
 
  // Cut everything after the last slash, twice.  This will take us back to the
  // instance root.
  // This logic assumes that we are in a directory above the instance root.
  lastSlash = strrchr(execName, '\\');
  lastSlash[0] = '\0';
  lastSlash = strrchr(execName, '\\');
  lastSlash[0] = '\0';
 
  sprintf(path, "%s\\logs\\%s", execName, DEBUG_LOG_NAME);
  logFile = _strdup(path);
 
  return logFile;
}
 
// ---------------------------------------------------------------
// Function called to know if the --debug option was passed
// when calling this executable or not.  The DEBUG variable is
// updated accordingly.
// ---------------------------------------------------------------
 
void updateDebugFlag(char* argv[], int argc)
{
  int i;
  DEBUG = FALSE;
  for (i=1; (i<argc) && !DEBUG; i++)
  {
    if (strcmp(argv[i], "--debug") == 0)
    {
      DEBUG = TRUE;
    }
  }
}
 
// ---------------------------------------------------------------
// Deletes a file if it's larger than the given maximum size.
// ---------------------------------------------------------------
 
void deleteIfLargerThan(char * fileName, DWORD maxSize)
{
  DWORD fileSize = 0;
  HANDLE fileHandle = CreateFile(
    fileName,
    0,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    0,
    NULL
  );
 
  if (fileHandle == INVALID_HANDLE_VALUE) 
  {
    return;
  }
 
  fileSize = GetFileSize(fileHandle, NULL);
 
  CloseHandle(fileHandle);
 
  if (fileSize > maxSize)
  {
    DeleteFile(fileName);
  }
}