OLD | NEW |
| (Empty) |
1 // Copyright 2003-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #ifndef OMAHA_COMMON_PROC_UTILS_H_ | |
17 #define OMAHA_COMMON_PROC_UTILS_H_ | |
18 | |
19 #include <windows.h> | |
20 #include <tlhelp32.h> | |
21 #include <atlstr.h> | |
22 #include <vector> | |
23 #include "base/basictypes.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 // Moved here from installation directory. Generic enought to be in common. | |
28 class ProcessTerminator { | |
29 public: | |
30 | |
31 // constants for specifying which methods to attempt when killing a process | |
32 static const int KILL_METHOD_1_WINDOW_MESSAGE = 0x01; | |
33 static const int KILL_METHOD_2_THREAD_MESSAGE = 0x02; | |
34 static const int KILL_METHOD_4_TERMINATE_PROCESS = 0x08; | |
35 static const int INVALID_SESSION_ID = 0xFFFF; | |
36 | |
37 // Creates the object given the process name to kill. | |
38 explicit ProcessTerminator(const CString& process_name); | |
39 ProcessTerminator(const CString& process_name, const CString& user_sid); | |
40 ProcessTerminator(const CString& process_name, | |
41 const CString& user_sid, | |
42 int session_id); | |
43 | |
44 // Performs necessary cleanup. | |
45 ~ProcessTerminator(); | |
46 | |
47 // Go through process list try to find the required one to kill, | |
48 // trying three methods to kill, from easiest and cleanest to a | |
49 // harsh one. S_OK if no process by the right name was found, or if it was | |
50 // found and was killed. E_FAIL otherwise. was_found returns true if | |
51 // process was found. Kills all instances of a process. | |
52 HRESULT KillTheProcess(uint32 timeout_msec, | |
53 bool* was_found, | |
54 uint32 method_mask, | |
55 bool flash_window); | |
56 | |
57 // Wait for all instances of the process to die. | |
58 HRESULT WaitForAllToDie(uint32 timeout_msec); | |
59 | |
60 // Finds all process ids for the process of a given name. | |
61 bool FindProcessInstances(); | |
62 | |
63 private: | |
64 | |
65 // Will try to open handle to each instance. | |
66 // Leaves process handles open (in member process_handles_) | |
67 // Will use access rights for opening appropriate for the purpose_of_opening | |
68 bool PrepareToKill(uint32 method_mask); | |
69 | |
70 // Wait for process instances to die for timeout_msec | |
71 // return true if all are dead and false if timed out. | |
72 bool WaitForProcessInstancesToDie(uint32 timeout_msec) const; | |
73 | |
74 // Will close all currently opened handles. | |
75 void CloseAllHandles(); | |
76 | |
77 | |
78 // | |
79 // Killing via messages to window | |
80 // | |
81 // Function which meet win32 requirements for callback | |
82 // function passed into EnumWindows function. | |
83 BOOL static CALLBACK EnumAllWindowsProc(HWND hwnd, LPARAM lparam); | |
84 | |
85 // Will return true if it succeeds in finding a window for the process | |
86 // to be killed, otherwise false. If there are such top-level windows | |
87 // then returns an array of window handles. | |
88 bool FindProcessWindows(); | |
89 | |
90 // Will try to kill the process via posting windows messages | |
91 // returns true on success otherwise false. | |
92 // Timeout is maximum time to wait for WM_CLOSE to work before going to | |
93 // next method. | |
94 bool KillProcessViaWndMessages(uint32 timeout_msec); | |
95 | |
96 // | |
97 // Killing via messages to thread | |
98 // | |
99 // Try to find the threads than run in | |
100 // the process in question. | |
101 bool FindProcessThreads(std::vector<uint32>* thread_ids); | |
102 | |
103 // Will try to kill the process via posing thread messages | |
104 // returns true on success otherwise false. | |
105 // Timeout is maximum time to wait for message to work before going to | |
106 // next method. | |
107 bool KillProcessViaThreadMessages(uint32 timeout_msec); | |
108 | |
109 // The last and crude method to kill the process. | |
110 // Calls TerminateProcess function. | |
111 bool KillProcessViaTerminate(uint32 timeout_msec); | |
112 | |
113 // Private member variables: | |
114 CString process_name_; | |
115 // One process can have several instances | |
116 // running. This array will keep handles to all | |
117 // instances of the process. | |
118 std::vector<HANDLE> process_handles_; | |
119 // Array of process ids which correspond to different | |
120 // instances of the same process. | |
121 std::vector<uint32> process_ids_; | |
122 // Function PrepareToKill can call itself | |
123 // recursively under some conditions. | |
124 // We need to stop the recursion at some point. | |
125 // This is the purpose of this member. | |
126 int recursion_level_; | |
127 // Array of window handles. | |
128 std::vector<HWND> window_handles_; | |
129 // The sid of the user whose process needs to be terminated. | |
130 CString user_sid_; | |
131 // True if the window flashes on shut down. | |
132 bool flash_window_; | |
133 // The session to search the processes in. | |
134 int session_id_; | |
135 | |
136 // Disable copy constructor and assignment operator. | |
137 DISALLOW_EVIL_CONSTRUCTORS(ProcessTerminator); | |
138 }; | |
139 | |
140 // Application calling this function will be shut down | |
141 // by the system without displaying message boxes if the application | |
142 // fails to shutdown itself properly as a result of processing | |
143 // WM_QUERYENDSESSION. | |
144 HRESULT SetProcessSilentShutdown(); | |
145 | |
146 } // namespace omaha | |
147 | |
148 #endif // OMAHA_COMMON_PROC_UTILS_H_ | |
OLD | NEW |