~ubuntu-branches/debian/jessie/armory/jessie

« back to all changes in this revision

Viewing changes to cppForSwig/guardian/guardian.cpp

  • Committer: Package Import Robot
  • Author(s): Joseph Bisch
  • Date: 2014-10-07 10:22:45 UTC
  • Revision ID: package-import@ubuntu.com-20141007102245-2s3x3rhjxg689hek
Tags: upstream-0.92.3
ImportĀ upstreamĀ versionĀ 0.92.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
//                                                                            //
 
3
//  Copyright (C) 2011-2014, Armory Technologies, Inc.                        //
 
4
//  Distributed under the GNU Affero General Public License (AGPL v3)         //
 
5
//  See LICENSE or http://www.gnu.org/licenses/agpl.html                      //
 
6
//                                                                            //
 
7
////////////////////////////////////////////////////////////////////////////////
 
8
// 
 
9
// A very simple program that simply monitors one PID, and kills another one
 
10
// when it disappears.  This is needed for when Armory spawns a bitcoind 
 
11
// process in the background, but crashes before it can close it.  Armory 
 
12
// will launch this "guardian" to kill bitcoind.exe if Armory.exe disappears.
 
13
// 
 
14
#include <windows.h>
 
15
#include <tlhelp32.h>
 
16
#include <tchar.h>
 
17
#include <iomanip>
 
18
#include <stdio.h>
 
19
 
 
20
//  Forward declarations:
 
21
BOOL GetProcessList( );
 
22
BOOL ListProcessModules( DWORD dwPID );
 
23
BOOL ListProcessThreads( DWORD dwOwnerPID );
 
24
 
 
25
bool processIsStillRunning(HANDLE hndl)
 
26
{
 
27
   DWORD exitCode = 0;
 
28
   GetExitCodeProcess(hndl, &exitCode);
 
29
   return (exitCode==STILL_ACTIVE);
 
30
}
 
31
 
 
32
DWORD getProcessWithParent(int pid)
 
33
{
 
34
  HANDLE hProcessSnap;
 
35
  PROCESSENTRY32 pe32;
 
36
 
 
37
  // Take a snapshot of all processes in the system.
 
38
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
 
39
  if( hProcessSnap == INVALID_HANDLE_VALUE )
 
40
  {
 
41
    return( FALSE );
 
42
  }
 
43
 
 
44
  // Set the size of the structure before using it.
 
45
  pe32.dwSize = sizeof( PROCESSENTRY32 );
 
46
 
 
47
  // Retrieve information about the first process,
 
48
  // and exit if unsuccessful
 
49
  if( !Process32First( hProcessSnap, &pe32 ) )
 
50
  {
 
51
    CloseHandle( hProcessSnap );          // clean the snapshot object
 
52
    return( FALSE );
 
53
  }
 
54
 
 
55
  // Now walk the snapshot of processes, and
 
56
  // display information about each process in turn
 
57
  DWORD parent = static_cast<DWORD>(pid);
 
58
  DWORD childID = 0xffffffff;
 
59
  do
 
60
  {
 
61
      if(pe32.th32ParentProcessID == parent)
 
62
         return static_cast<int>(pe32.th32ProcessID);
 
63
  } while( Process32Next( hProcessSnap, &pe32 ) );
 
64
 
 
65
  _tprintf( TEXT("Never found process with parent!") );
 
66
  return childID;
 
67
}
 
68
 
 
69
int main(int argc, char** argv )
 
70
{
 
71
   if(argc != 3)
 
72
   {
 
73
      _tprintf( TEXT("\nInvalid arguments:"));
 
74
      _tprintf( TEXT("\nUSAGE:  guardian.exe pidWatch pidKill"));
 
75
      // I know I should use argv[0] instead of "guardian.exe", but I 
 
76
      // don't feel like messing with strings<->windows.h.  I had a bad
 
77
      // experience once...
 
78
      return 1;
 
79
   }
 
80
   int pidWatch = atoi(argv[1]);
 
81
   int pidKillA = atoi(argv[2]);
 
82
   int pidKillB = getProcessWithParent(pidKillA);
 
83
 
 
84
   _tprintf( TEXT("Found two processes to kill: "), pidKillA, TEXT(" "), pidKillB);
 
85
 
 
86
   HANDLE hWatch = OpenProcess(PROCESS_ALL_ACCESS, FALSE, static_cast<DWORD>(pidWatch));
 
87
   HANDLE hKillA = OpenProcess(PROCESS_ALL_ACCESS, FALSE, static_cast<DWORD>(pidKillA));
 
88
   HANDLE hKillB = OpenProcess(PROCESS_ALL_ACCESS, FALSE, static_cast<DWORD>(pidKillB));
 
89
 
 
90
   while(processIsStillRunning(hWatch))
 
91
      Sleep(static_cast<DWORD>(2000));
 
92
 
 
93
   _tprintf( TEXT("\nThe watched process died!"));
 
94
   TerminateProcess(hKillA, 0);
 
95
   TerminateProcess(hKillB, 0);
 
96
   _tprintf( TEXT("\nAttempted to kill the two processes! "), pidKillA, TEXT(" "), pidKillB);
 
97
   return 0;
 
98
}
 
99