Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: installer/src/installer-lib/process.cpp

Issue 6003395731128320: Only take into account processes that have our plugin loaded (Closed)
Patch Set: Created March 27, 2014, 3:29 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <stdexcept> 1 #include <stdexcept>
2 #include <functional> 2 #include <functional>
3 #include <wctype.h> 3 #include <wctype.h>
4 // <thread> is C++11, but implemented in VS2012 4 // <thread> is C++11, but implemented in VS2012
5 #include <thread> 5 #include <thread>
6 6
7 #include "process.h" 7 #include "process.h"
8 8
9 //------------------------------------------------------- 9 //-------------------------------------------------------
10 // Windows_Handle 10 // Windows_Handle
(...skipping 13 matching lines...) Expand all
24 { 24 {
25 this -> ~Windows_Handle() ; 25 this -> ~Windows_Handle() ;
26 handle = h ; 26 handle = h ;
27 validate_handle() ; 27 validate_handle() ;
28 } 28 }
29 29
30 void Windows_Handle::validate_handle() 30 void Windows_Handle::validate_handle()
31 { 31 {
32 if ( handle == INVALID_HANDLE_VALUE ) 32 if ( handle == INVALID_HANDLE_VALUE )
33 { 33 {
34 throw std::runtime_error( "Invalid handle" ) ; 34 char tmp[256];
35 » sprintf(tmp, "Invalid handle. Last error: %d", GetLastError());
Eric 2014/03/27 16:52:44 Using sprintf() seems safe here, but it's such a b
36 throw std::runtime_error( tmp ) ;
35 } 37 }
36 } 38 }
37 39
38 //------------------------------------------------------- 40 //-------------------------------------------------------
39 // process_by_name_CI 41 // process_by_name_CI
40 //------------------------------------------------------- 42 //-------------------------------------------------------
41 process_by_name_CI::process_by_name_CI( const wchar_t * name ) 43 process_by_name_CI::process_by_name_CI( const wchar_t * name )
42 : name( name ), length( wcslen( name ) ) 44 : name( name ), length( wcslen( name ) )
43 {} 45 {}
44 46
45 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process ) 47 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process )
46 { 48 {
47 return 0 == wcsncmpi( process.szExeFile, name, length ) ; 49 return 0 == wcsncmpi( process.szExeFile, name, length ) ;
48 } 50 }
49 51
50 //------------------------------------------------------- 52 //-------------------------------------------------------
51 // process_by_any_exe_name_CI 53 // process_by_any_exe_name_CI
52 //------------------------------------------------------- 54 //-------------------------------------------------------
53 bool process_by_any_exe_name_CI::operator()( const PROCESSENTRY32W & process ) 55 bool process_by_any_exe_name_CI::operator()( const PROCESSENTRY32W & process )
54 { 56 {
55 return names.find( process.szExeFile ) != names.end() ; 57 return names.find( process.szExeFile ) != names.end() ;
56 } 58 }
57 59
58 //------------------------------------------------------- 60 //-------------------------------------------------------
61 // process_by_any_exe_name_CI_w_ABP
62 //-------------------------------------------------------
63 bool process_by_any_exe_name_CI_w_ABP::operator()( const PROCESSENTRY32W & proce ss )
64 {
65 bool moduleFound = false;
Eric 2014/03/27 16:52:44 We can eliminate 'moduleFound'
66 if ( names.find( process.szExeFile ) != names.end() )
67 {
68 if (moduleNames.empty())
69 return true;
Eric 2014/03/27 16:52:44 We should document that if no module names are pre
70
71 ModulesSnapshot ms( process.th32ProcessID );
72 return true;
Eric 2014/03/27 16:52:44 Stub return for testing something, I assume?
73 MODULEENTRY32* me = ms.begin();
74 while ( me != ms.end() )
75 {
76 if (moduleNames.find( me->szModule ) != moduleNames.end())
77 {
78 moduleFound = true;
Eric 2014/03/27 16:52:44 return true
79 break;
80 }
81 me = ms.next();
82 }
83 }
84 return moduleFound;
Eric 2014/03/27 16:52:44 return false
85 }
86
87 //-------------------------------------------------------
59 // wcscmpi 88 // wcscmpi
60 //------------------------------------------------------- 89 //-------------------------------------------------------
61 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) 90 int wcscmpi( const wchar_t * s1, const wchar_t * s2 )
62 { 91 {
63 // Note: Equality of character sequences is case-insensitive in all predicates below. 92 // Note: Equality of character sequences is case-insensitive in all predicates below.
64 // Loop invariant: s1[0..j) == s2[0..j) 93 // Loop invariant: s1[0..j) == s2[0..j)
65 const size_t LIMIT( 65535 ) ; // Runaway limit of 2^16 - 1 should be acceptabl y long. 94 const size_t LIMIT( 65535 ) ; // Runaway limit of 2^16 - 1 should be acceptabl y long.
66 for ( size_t j = 0 ; j < LIMIT ; ++j ) 95 for ( size_t j = 0 ; j < LIMIT ; ++j )
67 { 96 {
68 wchar_t c1 = towupper( *s1++ ) ; 97 wchar_t c1 = towupper( *s1++ ) ;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ; 184 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ;
156 } 185 }
157 186
158 void Snapshot::refresh() 187 void Snapshot::refresh()
159 { 188 {
160 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ; 189 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ;
161 } 190 }
162 191
163 192
164 //------------------------------------------------------- 193 //-------------------------------------------------------
194 // ModulesSnapshot
195 //-------------------------------------------------------
196 ModulesSnapshot::ModulesSnapshot(DWORD processId)
197 : handle( ::CreateToolhelp32Snapshot( TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processId ) )
198 {
199 module.dwSize = sizeof( MODULEENTRY32 ) ;
200 }
201
202 MODULEENTRY32W * ModulesSnapshot::begin()
203 {
204 return ::Module32FirstW( handle, & module ) ? ( & module ) : 0 ;
205 }
206
207 MODULEENTRY32W * ModulesSnapshot::next()
208 {
209 return ::Module32NextW( handle, & module ) ? ( & module ) : 0 ;
210 }
211
212 void ModulesSnapshot::refresh(DWORD processId)
Eric 2014/03/27 16:52:44 refresh() not needed.
213 {
214 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processId ) ;
Eric 2014/03/27 16:52:44 Not that it matters, but processID isn't initializ
215 }
216
217
218 //-------------------------------------------------------
165 // send_message, send_endsession_messages 219 // send_message, send_endsession_messages
166 //------------------------------------------------------- 220 //-------------------------------------------------------
167 /** 221 /**
168 * Default process exit wait time (per message) 5000 ms 222 * Default process exit wait time (per message) 5000 ms
169 * 223 *
170 * 5 seconds is time that the system will wait before it considers a process non -responsive. 224 * 5 seconds is time that the system will wait before it considers a process non -responsive.
171 */ 225 */
172 static const unsigned int timeout = 5000 ; // milliseconds 226 static const unsigned int timeout = 5000 ; // milliseconds
173 227
174 /** 228 /**
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 if ( ! is_running() ) 461 if ( ! is_running() )
408 { 462 {
409 return true ; 463 return true ;
410 } 464 }
411 } 465 }
412 // Assert is_running() 466 // Assert is_running()
413 } 467 }
414 // No control path leaves the for-loop. 468 // No control path leaves the for-loop.
415 } ; 469 } ;
416 470
OLDNEW

Powered by Google App Engine
This is Rietveld