shutdown.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (C) 2006 Alexandre Julliard
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include "windef.h"
  21. #include "winbase.h"
  22. #include "winuser.h"
  23. #include "tlhelp32.h"
  24. #include "wine/debug.h"
  25. #include "resource.h"
  26. WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
  27. #define MESSAGE_TIMEOUT 5000
  28. struct window_info
  29. {
  30. HWND hwnd;
  31. DWORD pid;
  32. DWORD tid;
  33. };
  34. static UINT win_count;
  35. static UINT win_max;
  36. static struct window_info *windows;
  37. static DWORD desktop_pid;
  38. /* store a new window; callback for EnumWindows */
  39. static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lp )
  40. {
  41. if (win_count >= win_max)
  42. {
  43. UINT new_count = win_max * 2;
  44. struct window_info *new_win = HeapReAlloc( GetProcessHeap(), 0, windows,
  45. new_count * sizeof(windows[0]) );
  46. if (!new_win) return FALSE;
  47. windows = new_win;
  48. win_max = new_count;
  49. }
  50. windows[win_count].hwnd = hwnd;
  51. windows[win_count].tid = GetWindowThreadProcessId( hwnd, &windows[win_count].pid );
  52. win_count++;
  53. return TRUE;
  54. }
  55. /* compare two window info structures; callback for qsort */
  56. static int __cdecl cmp_window( const void *ptr1, const void *ptr2 )
  57. {
  58. const struct window_info *info1 = ptr1;
  59. const struct window_info *info2 = ptr2;
  60. int ret = info1->pid - info2->pid;
  61. if (!ret) ret = info1->tid - info2->tid;
  62. return ret;
  63. }
  64. /* build the list of all windows (FIXME: handle multiple desktops) */
  65. static BOOL get_all_windows(void)
  66. {
  67. win_count = 0;
  68. win_max = 16;
  69. windows = HeapAlloc( GetProcessHeap(), 0, win_max * sizeof(windows[0]) );
  70. if (!windows) return FALSE;
  71. if (!EnumWindows( enum_proc, 0 )) return FALSE;
  72. /* sort windows by processes */
  73. qsort( windows, win_count, sizeof(windows[0]), cmp_window );
  74. return TRUE;
  75. }
  76. struct callback_data
  77. {
  78. UINT window_count;
  79. BOOL timed_out;
  80. LRESULT result;
  81. };
  82. static void CALLBACK end_session_message_callback( HWND hwnd, UINT msg, ULONG_PTR data, LRESULT lresult )
  83. {
  84. struct callback_data *cb_data = (struct callback_data *)data;
  85. WINE_TRACE( "received response %s hwnd %p lresult %ld\n",
  86. msg == WM_QUERYENDSESSION ? "WM_QUERYENDSESSION" : (msg == WM_ENDSESSION ? "WM_ENDSESSION" : "Unknown"),
  87. hwnd, lresult );
  88. /* If the window was destroyed while the message was in its queue, SendMessageCallback()
  89. calls us with a default 0 result. Ignore it. */
  90. if (!lresult && !IsWindow( hwnd ))
  91. {
  92. WINE_TRACE( "window was destroyed; ignoring FALSE lresult\n" );
  93. lresult = TRUE;
  94. }
  95. /* we only care if a WM_QUERYENDSESSION response is FALSE */
  96. cb_data->result = cb_data->result && lresult;
  97. /* cheap way of ref-counting callback_data whilst freeing memory at correct
  98. * time */
  99. if (!(cb_data->window_count--) && cb_data->timed_out)
  100. HeapFree( GetProcessHeap(), 0, cb_data );
  101. }
  102. struct endtask_dlg_data
  103. {
  104. struct window_info *win;
  105. BOOL cancelled;
  106. BOOL terminated;
  107. };
  108. static INT_PTR CALLBACK endtask_dlg_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
  109. {
  110. struct endtask_dlg_data *data;
  111. HANDLE handle;
  112. switch (msg)
  113. {
  114. case WM_INITDIALOG:
  115. SetWindowLongPtrW( hwnd, DWLP_USER, lparam );
  116. ShowWindow( hwnd, SW_SHOWNORMAL );
  117. return TRUE;
  118. case WM_COMMAND:
  119. data = (struct endtask_dlg_data *)GetWindowLongPtrW( hwnd, DWLP_USER );
  120. switch (wparam)
  121. {
  122. case MAKEWPARAM(IDOK, BN_CLICKED):
  123. handle = OpenProcess( PROCESS_TERMINATE, FALSE, data->win[0].pid );
  124. if (handle)
  125. {
  126. WINE_TRACE( "terminating process %04x\n", data->win[0].pid );
  127. TerminateProcess( handle, 0 );
  128. CloseHandle( handle );
  129. data->terminated = TRUE;
  130. }
  131. return TRUE;
  132. case MAKEWPARAM(IDCANCEL, BN_CLICKED):
  133. data->cancelled = TRUE;
  134. return TRUE;
  135. }
  136. break;
  137. }
  138. return FALSE;
  139. }
  140. /* Sends a message to a set of windows, displaying a dialog if the window
  141. * doesn't respond to the message within a set amount of time.
  142. * If the process has already been terminated, the function returns -1.
  143. * If the user or application cancels the process, the function returns 0.
  144. * Otherwise the function returns 0. */
  145. static LRESULT send_messages_with_timeout_dialog(
  146. struct window_info *win, UINT count, HANDLE process_handle,
  147. UINT msg, WPARAM wparam, LPARAM lparam )
  148. {
  149. unsigned int i;
  150. DWORD ret;
  151. DWORD start_time;
  152. struct callback_data *cb_data;
  153. HWND hwnd_endtask = NULL;
  154. struct endtask_dlg_data dlg_data;
  155. LRESULT result;
  156. cb_data = HeapAlloc( GetProcessHeap(), 0, sizeof(*cb_data) );
  157. if (!cb_data)
  158. return 1;
  159. cb_data->result = TRUE; /* we only care if a WM_QUERYENDSESSION response is FALSE */
  160. cb_data->timed_out = FALSE;
  161. cb_data->window_count = count;
  162. dlg_data.win = win;
  163. dlg_data.terminated = FALSE;
  164. dlg_data.cancelled = FALSE;
  165. for (i = 0; i < count; i++)
  166. {
  167. if (!SendMessageCallbackW( win[i].hwnd, msg, wparam, lparam,
  168. end_session_message_callback, (ULONG_PTR)cb_data ))
  169. cb_data->window_count --;
  170. }
  171. start_time = GetTickCount();
  172. while (TRUE)
  173. {
  174. DWORD current_time = GetTickCount();
  175. ret = MsgWaitForMultipleObjects( 1, &process_handle, FALSE,
  176. MESSAGE_TIMEOUT - (current_time - start_time),
  177. QS_ALLINPUT );
  178. if (ret == WAIT_OBJECT_0) /* process exited */
  179. {
  180. HeapFree( GetProcessHeap(), 0, cb_data );
  181. result = 1;
  182. goto cleanup;
  183. }
  184. else if (ret == WAIT_OBJECT_0 + 1) /* window message */
  185. {
  186. MSG msg;
  187. while(PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ))
  188. {
  189. if (!hwnd_endtask || !IsDialogMessageW( hwnd_endtask, &msg ))
  190. {
  191. TranslateMessage( &msg );
  192. DispatchMessageW( &msg );
  193. }
  194. }
  195. if (!cb_data->window_count)
  196. {
  197. result = dlg_data.terminated || cb_data->result;
  198. HeapFree( GetProcessHeap(), 0, cb_data );
  199. if (!result)
  200. goto cleanup;
  201. break;
  202. }
  203. if (dlg_data.cancelled)
  204. {
  205. cb_data->timed_out = TRUE;
  206. result = 0;
  207. goto cleanup;
  208. }
  209. }
  210. else if ((ret == WAIT_TIMEOUT) && !hwnd_endtask)
  211. {
  212. hwnd_endtask = CreateDialogParamW( GetModuleHandleW(NULL),
  213. MAKEINTRESOURCEW(IDD_ENDTASK),
  214. NULL, endtask_dlg_proc,
  215. (LPARAM)&dlg_data );
  216. }
  217. else break;
  218. }
  219. result = 1;
  220. cleanup:
  221. if (hwnd_endtask) DestroyWindow( hwnd_endtask );
  222. return result;
  223. }
  224. /* send WM_QUERYENDSESSION and WM_ENDSESSION to all windows of a given process */
  225. static DWORD_PTR send_end_session_messages( struct window_info *win, UINT count, UINT flags )
  226. {
  227. LRESULT result, end_session;
  228. HANDLE process_handle;
  229. DWORD ret;
  230. /* FIXME: Use flags to implement EWX_FORCEIFHUNG! */
  231. /* don't kill the desktop process */
  232. if (win[0].pid == desktop_pid) return 1;
  233. process_handle = OpenProcess( SYNCHRONIZE, FALSE, win[0].pid );
  234. if (!process_handle)
  235. return 1;
  236. end_session = send_messages_with_timeout_dialog( win, count, process_handle,
  237. WM_QUERYENDSESSION, 0, 0 );
  238. if (end_session == -1)
  239. {
  240. CloseHandle( process_handle );
  241. return 1;
  242. }
  243. result = send_messages_with_timeout_dialog( win, count, process_handle,
  244. WM_ENDSESSION, end_session, 0 );
  245. if (end_session == 0)
  246. {
  247. CloseHandle( process_handle );
  248. return 0;
  249. }
  250. if (result == -1)
  251. {
  252. CloseHandle( process_handle );
  253. return 1;
  254. }
  255. /* Check whether the app quit on its own */
  256. ret = WaitForSingleObject( process_handle, 0 );
  257. CloseHandle( process_handle );
  258. if (ret == WAIT_TIMEOUT)
  259. {
  260. /* If not, it returned from all WM_ENDSESSION and is finished cleaning
  261. * up, so we can safely kill the process. */
  262. HANDLE handle = OpenProcess( PROCESS_TERMINATE, FALSE, win[0].pid );
  263. if (handle)
  264. {
  265. WINE_TRACE( "terminating process %04x\n", win[0].pid );
  266. TerminateProcess( handle, 0 );
  267. CloseHandle( handle );
  268. }
  269. }
  270. return 1;
  271. }
  272. /* close all top-level windows and terminate processes cleanly */
  273. BOOL shutdown_close_windows( BOOL force )
  274. {
  275. UINT send_flags = force ? SMTO_ABORTIFHUNG : SMTO_NORMAL;
  276. DWORD_PTR result = 1;
  277. UINT i, n;
  278. if (!get_all_windows()) return FALSE;
  279. GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
  280. for (i = n = 0; result && i < win_count; i++, n++)
  281. {
  282. if (n && windows[i-1].pid != windows[i].pid)
  283. {
  284. result = send_end_session_messages( windows + i - n, n, send_flags );
  285. n = 0;
  286. }
  287. }
  288. if (n && result)
  289. result = send_end_session_messages( windows + win_count - n, n, send_flags );
  290. HeapFree( GetProcessHeap(), 0, windows );
  291. return (result != 0);
  292. }
  293. static BOOL CALLBACK shutdown_one_desktop( LPWSTR name, LPARAM force )
  294. {
  295. HDESK hdesk;
  296. WINE_TRACE("Shutting down desktop %s\n", wine_dbgstr_w(name));
  297. hdesk = OpenDesktopW( name, 0, FALSE, GENERIC_ALL );
  298. if (hdesk == NULL)
  299. {
  300. WINE_ERR("Cannot open desktop %s, err=%i\n", wine_dbgstr_w(name), GetLastError());
  301. return FALSE;
  302. }
  303. if (!SetThreadDesktop( hdesk ))
  304. {
  305. CloseDesktop( hdesk );
  306. WINE_ERR("Cannot set thread desktop %s, err=%i\n", wine_dbgstr_w(name), GetLastError());
  307. return FALSE;
  308. }
  309. CloseDesktop( hdesk );
  310. return shutdown_close_windows( force );
  311. }
  312. BOOL shutdown_all_desktops( BOOL force )
  313. {
  314. BOOL ret;
  315. HDESK prev_desktop;
  316. prev_desktop = GetThreadDesktop(GetCurrentThreadId());
  317. ret = EnumDesktopsW( NULL, shutdown_one_desktop, (LPARAM)force );
  318. SetThreadDesktop(prev_desktop);
  319. return ret;
  320. }
  321. /* forcibly kill all processes without any cleanup */
  322. void kill_processes( BOOL kill_desktop )
  323. {
  324. BOOL res;
  325. UINT killed;
  326. HANDLE handle, snapshot;
  327. PROCESSENTRY32W process;
  328. GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
  329. do
  330. {
  331. if (!(snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ))) break;
  332. killed = 0;
  333. process.dwSize = sizeof(process);
  334. for (res = Process32FirstW( snapshot, &process ); res; res = Process32NextW( snapshot, &process ))
  335. {
  336. if (process.th32ProcessID == GetCurrentProcessId()) continue;
  337. if (process.th32ProcessID == desktop_pid) continue;
  338. WINE_TRACE("killing process %04x %s\n",
  339. process.th32ProcessID, wine_dbgstr_w(process.szExeFile) );
  340. if (!(handle = OpenProcess( PROCESS_TERMINATE, FALSE, process.th32ProcessID )))
  341. continue;
  342. if (TerminateProcess( handle, 0 )) killed++;
  343. CloseHandle( handle );
  344. }
  345. CloseHandle( snapshot );
  346. } while (killed > 0);
  347. if (desktop_pid && kill_desktop) /* do this last */
  348. {
  349. if ((handle = OpenProcess( PROCESS_TERMINATE, FALSE, desktop_pid )))
  350. {
  351. TerminateProcess( handle, 0 );
  352. CloseHandle( handle );
  353. }
  354. }
  355. }