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: Changes for x64 custom action and addressing comments Created March 27, 2014, 11:22 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 // TODO: This code really deserves use of a proper exception class that pack ages
35 // Windows API errors consistently.
36 char tmp[256];
37 » sprintf(tmp, "Invalid handle. Last error: %d", GetLastError());
Wladimir Palant 2014/03/28 07:30:17 sprintf() is deprecated, please use sprintf_s() in
38 throw std::runtime_error( tmp ) ;
35 } 39 }
36 } 40 }
37 41
38 //------------------------------------------------------- 42 //-------------------------------------------------------
39 // process_by_name_CI 43 // process_by_name_CI
40 //------------------------------------------------------- 44 //-------------------------------------------------------
41 process_by_name_CI::process_by_name_CI( const wchar_t * name ) 45 process_by_name_CI::process_by_name_CI( const wchar_t * name )
42 : name( name ), length( wcslen( name ) ) 46 : name( name ), length( wcslen( name ) )
43 {} 47 {}
44 48
45 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process ) 49 bool process_by_name_CI::operator()( const PROCESSENTRY32W & process )
46 { 50 {
47 return 0 == wcsncmpi( process.szExeFile, name, length ) ; 51 return 0 == wcsncmpi( process.szExeFile, name, length ) ;
48 } 52 }
49 53
54
50 //------------------------------------------------------- 55 //-------------------------------------------------------
51 // process_by_any_exe_name_CI 56 // process_by_any_exe_with_any_module
52 //------------------------------------------------------- 57 //-------------------------------------------------------
Wladimir Palant 2014/03/28 07:30:17 I guess I am missing the point of having of this c
53 bool process_by_any_exe_name_CI::operator()( const PROCESSENTRY32W & process ) 58 bool process_by_any_exe_with_any_module::operator()( const PROCESSENTRY32W & pro cess )
54 { 59 {
55 return names.find( process.szExeFile ) != names.end() ; 60 if ( processNames.find( process.szExeFile ) != processNames.end() )
61 {
62 if (moduleNames.empty())
63 » » return true;
64
65 » ModulesSnapshot ms( process.th32ProcessID );
66 » MODULEENTRY32* me = ms.begin();
67 » while ( me != ms.end() )
68 » {
69 » » if (moduleNames.find( me->szModule ) != moduleNames.end())
70 » » {
71 » » » return true;
72 » » }
73 » » me = ms.next();
74 » }
Wladimir Palant 2014/03/28 07:30:17 Nit: Please use spaces for indentation.
75 }
76 return false;
56 } 77 }
57 78
58 //------------------------------------------------------- 79 //-------------------------------------------------------
59 // wcscmpi 80 // wcscmpi
60 //------------------------------------------------------- 81 //-------------------------------------------------------
61 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) 82 int wcscmpi( const wchar_t * s1, const wchar_t * s2 )
62 { 83 {
63 // Note: Equality of character sequences is case-insensitive in all predicates below. 84 // Note: Equality of character sequences is case-insensitive in all predicates below.
64 // Loop invariant: s1[0..j) == s2[0..j) 85 // 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. 86 const size_t LIMIT( 65535 ) ; // Runaway limit of 2^16 - 1 should be acceptabl y long.
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ; 176 return ::Process32NextW( handle, & process ) ? ( & process ) : 0 ;
156 } 177 }
157 178
158 void Snapshot::refresh() 179 void Snapshot::refresh()
159 { 180 {
160 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ; 181 handle = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ;
161 } 182 }
162 183
163 184
164 //------------------------------------------------------- 185 //-------------------------------------------------------
186 // ModulesSnapshot
187 //-------------------------------------------------------
188 ModulesSnapshot::ModulesSnapshot(DWORD processId)
189 : handle( ::CreateToolhelp32Snapshot( TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processId ) )
Wladimir Palant 2014/03/28 07:30:17 Style nit: That's rather unusual style in this fil
190 {
191 module.dwSize = sizeof( MODULEENTRY32 ) ;
192 }
193
194 MODULEENTRY32W * ModulesSnapshot::begin()
195 {
196 return ::Module32FirstW( handle, & module ) ? ( & module ) : 0 ;
197 }
198
199 MODULEENTRY32W * ModulesSnapshot::next()
200 {
201 return ::Module32NextW( handle, & module ) ? ( & module ) : 0 ;
202 }
Wladimir Palant 2014/03/28 07:30:17 This fakes a C++ iterator interface but does so in
Eric 2014/03/28 12:06:00 Yep. It really ought be a proper iterator class, b
Oleksandr 2014/03/28 12:48:35 I actually like the way it is implemented. The ite
Wladimir Palant 2014/03/28 13:55:24 This is simply misleading, one looks at the loop a
Oleksandr 2014/03/28 14:28:16 Fixed in Patchset 4 On 2014/03/28 13:55:24, Wladim
203
204
205 //-------------------------------------------------------
165 // send_message, send_endsession_messages 206 // send_message, send_endsession_messages
166 //------------------------------------------------------- 207 //-------------------------------------------------------
167 /** 208 /**
168 * Default process exit wait time (per message) 5000 ms 209 * Default process exit wait time (per message) 5000 ms
169 * 210 *
170 * 5 seconds is time that the system will wait before it considers a process non -responsive. 211 * 5 seconds is time that the system will wait before it considers a process non -responsive.
171 */ 212 */
172 static const unsigned int timeout = 5000 ; // milliseconds 213 static const unsigned int timeout = 5000 ; // milliseconds
173 214
174 /** 215 /**
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 if ( ! is_running() ) 448 if ( ! is_running() )
408 { 449 {
409 return true ; 450 return true ;
410 } 451 }
411 } 452 }
412 // Assert is_running() 453 // Assert is_running()
413 } 454 }
414 // No control path leaves the for-loop. 455 // No control path leaves the for-loop.
415 } ; 456 } ;
416 457
OLDNEW

Powered by Google App Engine
This is Rietveld