wineboot: Added support for doing a proper shutdown before rebooting.

Send WM_QUERYENDSESSION messages to all windows of a given process
before moving on to the next.
Switch option parsing to getopt.
Added a bunch of options to control shutdown behavior.
This commit is contained in:
Alexandre Julliard 2006-08-17 20:54:15 +02:00
parent 59dc73b911
commit 54da7fb8cf
3 changed files with 250 additions and 16 deletions

View file

@ -4,9 +4,10 @@ SRCDIR = @srcdir@
VPATH = @srcdir@
MODULE = wineboot.exe
APPMODE = -mconsole
IMPORTS = version advapi32 kernel32
IMPORTS = version user32 advapi32 kernel32
C_SRCS = \
shutdown.c \
wineboot.c
@MAKE_PROG_RULES@

View file

@ -0,0 +1,189 @@
/*
* Copyright (C) 2006 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include <stdlib.h>
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "tlhelp32.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
struct window_info
{
HWND hwnd;
DWORD pid;
DWORD tid;
};
static UINT win_count;
static UINT win_max;
static struct window_info *windows;
static DWORD desktop_pid;
/* store a new window; callback for EnumWindows */
static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lp )
{
if (win_count >= win_max)
{
UINT new_count = win_max * 2;
struct window_info *new_win = HeapReAlloc( GetProcessHeap(), 0, windows,
new_count * sizeof(windows[0]) );
if (!new_win) return FALSE;
windows = new_win;
win_max = new_count;
}
windows[win_count].hwnd = hwnd;
windows[win_count].tid = GetWindowThreadProcessId( hwnd, &windows[win_count].pid );
win_count++;
return TRUE;
}
/* compare two window info structures; callback for qsort */
static int cmp_window( const void *ptr1, const void *ptr2 )
{
const struct window_info *info1 = ptr1;
const struct window_info *info2 = ptr2;
int ret = info1->pid - info2->pid;
if (!ret) ret = info1->tid - info2->tid;
return ret;
}
/* build the list of all windows (FIXME: handle multiple desktops) */
static BOOL get_all_windows(void)
{
win_count = 0;
win_max = 16;
windows = HeapAlloc( GetProcessHeap(), 0, win_max * sizeof(windows[0]) );
if (!windows) return FALSE;
if (!EnumWindows( enum_proc, 0 )) return FALSE;
/* sort windows by processes */
qsort( windows, win_count, sizeof(windows[0]), cmp_window );
return TRUE;
}
/* send WM_QUERYENDSESSION and WM_ENDSESSION to all windows of a given process */
/* FIXME: should display a confirmation dialog if process doesn't respond to the messages */
static DWORD_PTR send_end_session_messages( struct window_info *win, UINT count, UINT flags )
{
unsigned int i;
DWORD_PTR result, ret = 1;
/* don't kill the desktop process */
if (win[0].pid == desktop_pid) return 1;
for (i = 0; ret && i < count; i++)
{
if (SendMessageTimeoutW( win[i].hwnd, WM_QUERYENDSESSION, 0, 0, flags, 0, &result ))
{
WINE_TRACE( "sent MW_QUERYENDSESSION hwnd %p pid %04lx result %ld\n",
win[i].hwnd, win[i].pid, result );
ret = result;
}
else win[i].hwnd = 0; /* ignore this window */
}
for (i = 0; i < count; i++)
{
if (!win[i].hwnd) continue;
WINE_TRACE( "sending WM_ENDSESSION hwnd %p pid %04lx wp %ld\n", win[i].hwnd, win[i].pid, ret );
SendMessageTimeoutW( win[i].hwnd, WM_ENDSESSION, ret, 0, flags, 0, &result );
}
if (ret)
{
HANDLE handle = OpenProcess( PROCESS_TERMINATE, FALSE, win[0].pid );
if (handle)
{
WINE_TRACE( "terminating process %04lx\n", win[0].pid );
TerminateProcess( handle, 0 );
CloseHandle( handle );
}
}
return ret;
}
/* close all top-level windows and terminate processes cleanly */
BOOL shutdown_close_windows( BOOL force )
{
UINT send_flags = force ? SMTO_ABORTIFHUNG : SMTO_NORMAL;
DWORD_PTR result = 1;
UINT i, n;
if (!get_all_windows()) return FALSE;
GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
for (i = n = 0; result && i < win_count; i++, n++)
{
if (n && windows[i-1].pid != windows[i].pid)
{
result = send_end_session_messages( windows + i - n, n, send_flags );
n = 0;
}
}
if (n && result)
result = send_end_session_messages( windows + win_count - n, n, send_flags );
HeapFree( GetProcessHeap(), 0, windows );
return (result != 0);
}
/* forcibly kill all processes without any cleanup */
void kill_processes( BOOL kill_desktop )
{
BOOL res;
UINT killed;
HANDLE handle, snapshot;
PROCESSENTRY32W process;
GetWindowThreadProcessId( GetDesktopWindow(), &desktop_pid );
do
{
if (!(snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ))) break;
killed = 0;
process.dwSize = sizeof(process);
for (res = Process32FirstW( snapshot, &process ); res; res = Process32NextW( snapshot, &process ))
{
if (process.th32ProcessID == GetCurrentProcessId()) continue;
if (process.th32ProcessID == desktop_pid) continue;
WINE_TRACE("killing process %04lx %s\n",
process.th32ProcessID, wine_dbgstr_w(process.szExeFile) );
if (!(handle = OpenProcess( PROCESS_TERMINATE, FALSE, process.th32ProcessID )))
continue;
if (TerminateProcess( handle, 0 )) killed++;
CloseHandle( handle );
}
CloseHandle( snapshot );
} while (killed > 0);
if (desktop_pid && kill_desktop) /* do this last */
{
if ((handle = OpenProcess( PROCESS_TERMINATE, FALSE, desktop_pid )))
{
TerminateProcess( handle, 0 );
CloseHandle( handle );
}
}
}

View file

@ -51,9 +51,15 @@
* processed (requires translations from Unicode to Ansi).
*/
#include "config.h"
#include "wine/port.h"
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#include <windows.h>
#include <wine/debug.h>
@ -61,6 +67,9 @@ WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
#define MAX_LINE_LENGTH (2*MAX_PATH+2)
extern BOOL shutdown_close_windows( BOOL force );
extern void kill_processes( BOOL kill_desktop );
static BOOL GetLine( HANDLE hFile, char *buf, size_t buflen )
{
unsigned int i=0;
@ -607,6 +616,31 @@ static int ProcessWindowsFileProtection(void)
return 1;
}
static void usage(void)
{
WINE_MESSAGE( "Usage: wineboot [options]\n" );
WINE_MESSAGE( "Options;\n" );
WINE_MESSAGE( " -h,--help Display this help message\n" );
WINE_MESSAGE( " -e,--end-session End the current session cleanly\n" );
WINE_MESSAGE( " -f,--force Force exit for processes that don't exit cleanly\n" );
WINE_MESSAGE( " -k,--kill Kill running processes without any cleanup\n" );
WINE_MESSAGE( " -r,--restart Restart only, don't do normal startup operations\n" );
WINE_MESSAGE( " -s,--shutdown Shutdown only, don't reboot\n" );
}
static const char short_options[] = "efhkrs";
static const struct option long_options[] =
{
{ "help", 0, 0, 'h' },
{ "end-session", 0, 0, 'e' },
{ "force", 0, 0, 'f' },
{ "kill", 0, 0, 'k' },
{ "restart", 0, 0, 'r' },
{ "shutdown", 0, 0, 's' },
{ NULL, 0, 0, 0 }
};
struct op_mask {
BOOL w9xonly; /* Perform only operations done on Windows 9x */
BOOL ntonly; /* Perform only operations done on Windows NT */
@ -618,14 +652,15 @@ struct op_mask {
static const struct op_mask SESSION_START={FALSE, FALSE, TRUE, TRUE, TRUE, TRUE},
SETUP={FALSE, FALSE, FALSE, TRUE, TRUE, TRUE};
#define DEFAULT SESSION_START
int main( int argc, char *argv[] )
{
struct op_mask ops; /* Which of the ops do we want to perform? */
struct op_mask ops = SESSION_START; /* Which of the ops do we want to perform? */
/* First, set the current directory to SystemRoot */
TCHAR gen_path[MAX_PATH];
DWORD res;
int optc;
int end_session = 0, force = 0, kill = 0, restart = 0, shutdown = 0;
res=GetWindowsDirectory( gen_path, sizeof(gen_path) );
@ -651,22 +686,31 @@ int main( int argc, char *argv[] )
return 100;
}
if( argc>1 )
while ((optc = getopt_long(argc, argv, short_options, long_options, NULL )) != -1)
{
switch( argv[1][0] )
switch(optc)
{
case 'r': /* Restart */
ops=SETUP;
break;
case 's': /* Full start */
ops=SESSION_START;
break;
default:
ops=DEFAULT;
break;
case 'e': end_session = 1; break;
case 'f': force = 1; break;
case 'k': kill = 1; break;
case 'r': restart = 1; break;
case 's': shutdown = 1; break;
case 'h': usage(); return 0;
case '?': usage(); return 1;
}
} else
ops=DEFAULT;
}
if (end_session)
{
if (!shutdown_close_windows( force )) return 1;
}
if (end_session || kill) kill_processes( shutdown );
if (shutdown) return 0;
if (restart) ops = SETUP;
/* Perform the ops by order, stopping if one fails, skipping if necessary */
/* Shachar: Sorry for the perl syntax */