~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/programs/wineboot/shutdown.c

Version: ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.