File: CProcess.cpp

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-13.2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 57,848 kB
  • sloc: ansic: 432,690; java: 87,068; cpp: 31,958; sh: 29,445; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,705; csh: 3,934; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (430 lines) | stat: -rw-r--r-- 14,952 bytes parent folder | download | duplicates (7)
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
420
421
422
423
424
425
426
427
428
429
430
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/
#include "stdafx.h"
#include <windows.h>
#include <Tlhelp32.h>
#include <map>
#include <vector>

#include "CProcess.h"
#include "CError.h"
#include "CEnvironment.h"
#include "CException.h"
#include "CHandle.h"
#include "CThread.h"
#include "CTrace.h"
#include "CUser.h"

using namespace std;

namespace GridEngine {

CProcess::CProcess() :
   m_dwProcessId(GetCurrentProcessId()),
   m_dwThreadId(0),
   m_Command(L""),
   m_pUser(NULL)
{
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::CProcess())");
   OpenExistingProcess();
}

CProcess::CProcess(CString Command, Type Type) :
   m_dwProcessId(0),
   m_dwThreadId(0),
   m_Command(Command),
   m_pUser(NULL)
{
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::CProcess(CString, Type))");
   CreateNewProcess(NULL, NULL, Type);
}

CProcess::CProcess(CString Command, const CUser& StartUser, Type Type) :
   m_dwProcessId(0),
   m_dwThreadId(0),
   m_Command(Command),
   m_pUser(new CUser(StartUser))
{
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::CProcess(CString, CUser, Type))");
   CreateNewProcess(m_pUser, NULL, Type);
}

CProcess::CProcess(CString Command, const CUser& StartUser, CEnvironment& Environment, Type Type) :
   m_dwProcessId(0),
   m_dwThreadId(0),
   m_Command(Command),
   m_pUser(new CUser(StartUser))
{
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::CProcess(CString, CUser, CEnvironment, Type))");
   CreateNewProcess(m_pUser, &Environment, Type);
}

CProcess::CProcess(CString Command, CEnvironment& Environment) :
   m_dwProcessId(0),
   m_dwThreadId(0),
   m_Command(Command),
   m_pUser(NULL)
{
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::CProcess(CString, CEnvironment))");
   CreateNewProcess(NULL , &Environment, INVISIBLE);
}

CProcess::CProcess(DWORD dwProcessId) :
   m_dwProcessId(dwProcessId),
   m_dwThreadId(0),
   m_Command(L""),
   m_pUser(NULL)
{
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::CProcess(DWORD)");
   OpenExistingProcess();
}

CProcess::~CProcess() {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::~CProcess()");
}

void CProcess::CreateNewProcess(CUser *StartUser, CEnvironment *Environment, Type Type) {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::CreateNewProcess(CUser, CEnvironment, Type)");
   BOOL bRet;
   PROCESS_INFORMATION ProcessInfo;
   STARTUPINFO StartupInfo;

   // Create a ne Process (with one thread)
   memset(&StartupInfo, 0, sizeof(STARTUPINFO));
   StartupInfo.cb = sizeof(STARTUPINFO);
   if (Type == INVISIBLE) {
      StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow = SW_HIDE;
   } else {
      ;
   }
   if (StartUser) {
      bRet = CreateProcessAsUser(StartUser->GetHandle(), NULL, const_cast<PWSTR>(m_Command.c_str()), NULL, NULL, FALSE,
         CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP
         | CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT,
         Environment ? Environment->GetEnvironmentBlock() : NULL, 
         NULL, 
         &StartupInfo, &ProcessInfo);
   } else {
      bRet = CreateProcess(NULL, const_cast<PWSTR>(m_Command.c_str()), NULL, NULL, FALSE,
         CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP
         | CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT,
         Environment ? Environment->GetEnvironmentBlock() : NULL, 
         NULL, 
         &StartupInfo, &ProcessInfo);
   }
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   } 
   m_dwProcessId = ProcessInfo.dwProcessId;
   m_dwThreadId = ProcessInfo.dwThreadId;
   SetHandle(ProcessInfo.hProcess);

   // We don't need this Handle to the master thread anymore
   CloseHandle(ProcessInfo.hThread);

}

DWORD CProcess::GetExitCode() const {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::GetExitCode()");
   DWORD dwExitCode;

   GetExitCodeProcess(GetHandle(), &dwExitCode);
   return dwExitCode;
}

DWORD CProcess::GetProcessId() const {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::GetProcessId()");
   return m_dwProcessId;
}

HANDLE CProcess::GetSnapshot() {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::GetSnapshot()");
   static const DWORD dwDelay = 100;
   static const DWORD dwMaxRetries = 10;
   HANDLE hSnapshot;
   DWORD dwRetries;

   // Threads which create new threads with a short delay will result in 
   // an error of the CreateToolhelp32Snapshot function. We will retry 
   // to get a snapshot several times to solve this problem

   hSnapshot = INVALID_HANDLE_VALUE;
   dwRetries = 0;
   while (hSnapshot == INVALID_HANDLE_VALUE && dwRetries < dwMaxRetries) {
      if (dwRetries != 0) {
         Sleep(dwDelay);
      }
      hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, m_dwProcessId);
      dwRetries++;
   }
   return hSnapshot;
}

void CProcess::OpenExistingProcess() {
   HANDLE hProcess;

   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::OpenExistingProcess()");
   hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, m_dwProcessId);
   if (hProcess == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   } else {
      SetHandle(hProcess);
   }
}

void CProcess::Resume() {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::Resume()");
   CHandle Snapshot;

   Snapshot.SetHandle(GetSnapshot());
   if (Snapshot.GetHandle() == INVALID_HANDLE_VALUE) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   } else {
      THREADENTRY32 ThreadEntry;
      BOOL bContinue;

      ThreadEntry.dwSize = sizeof(THREADENTRY32);
      for (bContinue = Thread32First(Snapshot.GetHandle(), &ThreadEntry); 
           bContinue; 
           bContinue = Thread32Next(Snapshot.GetHandle(), &ThreadEntry)) {
         if (ThreadEntry.th32OwnerProcessID == m_dwProcessId) {
            try {
               if (m_pUser) {
                  CThread NewThread(ThreadEntry.th32ThreadID, *m_pUser);
                  NewThread.Resume();
               } else {
                  CThread NewThread(ThreadEntry.th32ThreadID);
                  NewThread.Resume();
               }
            } catch (CCodineException&) {
               ;
            }
         }
      }
   }
}

void CProcess::Start() {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::Start()");

   Resume();
}

void CProcess::Suspend() {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::Suspend()");
   map<DWORD, BOOL> Threads;             
   map<DWORD, BOOL>::iterator CurrentThread;
   map<DWORD, int> ErrorThreads;             
   map<DWORD, int>::iterator ErrorThread;
   BOOL bNewThreads;

   // A)

   // Seach all threads which belong to this process and suspend them. To do this we create
   // a snapshot and suspend all threads contained in the snapshot.
   // Between A1 and A2 it is possible that new threads will be created. This makes it necessary to 
   // create a new snapshot and to suspend all new threads which we not suspended in a previous 
   // iteration.

   bNewThreads = TRUE;
   while (bNewThreads) {
      CHandle Snapshot;

      bNewThreads = FALSE;
      Snapshot.SetHandle(GetSnapshot());
      // A1

      if (Snapshot.GetHandle() == INVALID_HANDLE_VALUE) {
         throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
      } else {
         THREADENTRY32 ThreadEntry;
         BOOL bContinue;

         ThreadEntry.dwSize = sizeof(THREADENTRY32);
         for (bContinue = Thread32First(Snapshot.GetHandle(), &ThreadEntry); 
              bContinue; 
              bContinue = Thread32Next(Snapshot.GetHandle(), &ThreadEntry)) {
            if (ThreadEntry.th32OwnerProcessID == m_dwProcessId) {
               if (Threads.find(ThreadEntry.th32ThreadID) == Threads.end()) {
                  try {
                     if (m_pUser) {
                        CThread NewThread(ThreadEntry.th32ThreadID, *m_pUser);
             
                        NewThread.Suspend();
                     } else {
                        CThread NewThread(ThreadEntry.th32ThreadID);
             
                        NewThread.Suspend();
                     }

                     Threads[ThreadEntry.th32ThreadID] = TRUE;
                     bNewThreads = TRUE;
                  } catch (CCodineException& ex) {
                     ErrorThread = ErrorThreads.find(ThreadEntry.th32ThreadID);
                     if (ErrorThread == Threads.end() 
                         || (ErrorThread != Threads.end() && ErrorThread->second < 3)) {

                        // Possible reasons for the exception
                        //    * We didn't get a handle
                        //       - Thread exited
                        //    * Thread belongs to a different process 
                        //      (no access)
                        //       - Thread exited and another process created 
                        //         a thread with the same id between A1 and A2
                        //
                        // => we try it once more -> the next snapshot won't 
                        //    contain the current thread

                        CTrace::Print(CTrace::Layer::TEST, L"Catched Exception during suspended"
                           L" for thread %ld", ThreadEntry.th32ThreadID);

                        ErrorThreads[ThreadEntry.th32ThreadID]++;
                        bNewThreads = TRUE;
                     } else {

                        // If we get an error three times for the same thread
                        // then we will assume that this thread belongs really 
                        // to this process
                        //
                        // => we could not solve the problem => ERROR
                        throw ex;
                     }
                  }
               }
            }
         }
         // A2
      }
   }

   // B

   // Between A and B we created  a map of suspended threads (Threads) 
   // but we can not be sure that all these threads belong to this process 
   // (=> recycling of thread id's). It is possible, that we suspended 
   // threads of foreign processes!. Now we will set pair.second to false 
   // for all threads which belong to the current process
   {
      CHandle Snapshot;
      THREADENTRY32 ThreadEntry;
      BOOL bContinue;

      Snapshot.SetHandle(GetSnapshot());
      if (Snapshot.GetHandle() == INVALID_HANDLE_VALUE) {
         throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
      } else {
         try {
            ThreadEntry.dwSize = sizeof(THREADENTRY32);
            for (bContinue = Thread32First(Snapshot.GetHandle(), &ThreadEntry); 
                 bContinue; 
                 bContinue = Thread32Next(Snapshot.GetHandle(), &ThreadEntry)) {
               if (ThreadEntry.th32OwnerProcessID == m_dwProcessId) {
                  CurrentThread = Threads.find(ThreadEntry.th32ThreadID);
                  if (CurrentThread == Threads.end()) {
                     // If we get a new thread id here, then someone else
                     // created a new thread for this process (CreateRemoteThread())
                     CTrace::Print(CTrace::Layer::TEST, L"Unexpected new thread %ld", ThreadEntry.th32ThreadID);
                     throw CCodineException(L"", __FILE__, __LINE__);
                  } else {
                     CurrentThread->second = FALSE;
                  }
               }
            }
         } catch (CCodineException& ex) {
            // If we come here then we will rollback previous activities
            // Therefore we have to resume all threads contained in Threads            
            for (CurrentThread = Threads.begin();
                 CurrentThread != Threads.end();
                 CurrentThread++) {
               try {
                  if (m_pUser) {
                     CThread NewThread(CurrentThread->first, *m_pUser);
          
                     NewThread.Resume();
                  } else {
                     CThread NewThread(CurrentThread->first);
          
                     NewThread.Resume();
                  }
               } catch (CCodineException&) {
                  ;
               }
            }
            throw ex;
         }
      }
   }

   // C

   // Between B and C we set pair.second to false for all threads 
   // which belong to the current process
   // Now we have to resume the remaining threads
   // We will also add the suspended threads of this process to
   // the member m_Threads. 
   try {
      // Resume and copy
      for (CurrentThread = Threads.begin();
           CurrentThread != Threads.end();
           CurrentThread++) {
         if (CurrentThread->second == TRUE) {
            if (m_pUser) {
               CThread ForeignThread(CurrentThread->first, *m_pUser);
          
               ForeignThread.Resume();
            } else {
               CThread ForeignThread(CurrentThread->first);
          
               ForeignThread.Resume();
            }
         } 
      }
   } catch (CCodineException& ex) {
      
      // What can we do here?

      throw ex;
   }
}

void CProcess::Terminate(DWORD dwExitCode) {
   CTrace::CEnter Enter(CTrace::Layer::KERNEL, L"CProcess::Terminate()");
   BOOL bRet;

   bRet = TerminateProcess(GetHandle(), dwExitCode);
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   }
}

} // namespace