2015-04-18 09:21:51 +00:00
|
|
|
/*
|
2002-10-20 14:54:34 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS user32.dll
|
2015-11-01 09:41:29 +00:00
|
|
|
* FILE: win32ss/user/user32/misc/exit.c
|
2002-10-20 14:54:34 +00:00
|
|
|
* PURPOSE: Shutdown related functions
|
2010-01-09 22:43:16 +00:00
|
|
|
* PROGRAMMER: Eric Kohl
|
2002-10-20 14:54:34 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-19 18:06:53 +00:00
|
|
|
#include <user32.h>
|
2002-10-20 14:54:34 +00:00
|
|
|
|
2006-06-26 13:16:43 +00:00
|
|
|
#include <wine/debug.h>
|
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(user32);
|
|
|
|
|
2004-07-12 20:09:35 +00:00
|
|
|
/*
|
|
|
|
* Sequence of events:
|
|
|
|
*
|
|
|
|
* - App (usually explorer) calls ExitWindowsEx()
|
2005-12-01 22:29:57 +00:00
|
|
|
* - ExitWindowsEx() sends a message to CSRSS
|
2015-02-15 02:07:23 +00:00
|
|
|
* - CSRSS impersonates the caller and sends a message to a hidden Winlogon window
|
|
|
|
* - Winlogon checks if the caller has the required privileges
|
|
|
|
* - Winlogon enters pending log-out state
|
|
|
|
* - Winlogon impersonates the interactive user and calls ExitWindowsEx() again,
|
2004-07-12 20:09:35 +00:00
|
|
|
* passing some special internal flags
|
|
|
|
* - CSRSS loops over all processes of the interactive user (sorted by their
|
|
|
|
* SetProcessShutdownParameters() level), sending WM_QUERYENDSESSION and
|
|
|
|
* WM_ENDSESSION messages to its top-level windows. If the messages aren't
|
|
|
|
* processed within the timeout period (registry key HKCU\Control Panel\Desktop\HungAppTimeout)
|
|
|
|
* CSRSS will put up a dialog box asking if the process should be terminated.
|
|
|
|
* Using the registry key HKCU\Control Panel\Desktop\AutoEndTask you can
|
|
|
|
* specify that the dialog box shouldn't be shown and CSRSS should just
|
2005-12-01 22:29:57 +00:00
|
|
|
* terminate the thread. If the the WM_ENDSESSION message is processed
|
2004-07-12 20:09:35 +00:00
|
|
|
* but the thread doesn't terminate within the timeout specified by
|
|
|
|
* HKCU\Control Panel\Desktop\WaitToKillAppTimeout CSRSS will terminate
|
|
|
|
* the thread. When all the top-level windows have been destroyed CSRSS
|
|
|
|
* will terminate the process.
|
|
|
|
* If the process is a console process, CSRSS will send a CTRL_LOGOFF_EVENT
|
|
|
|
* to the console control handler on logoff. No event is sent on shutdown.
|
|
|
|
* If the handler doesn't respond in time the same activities as for GUI
|
|
|
|
* apps (i.e. display dialog box etc) take place. This also happens if
|
|
|
|
* the handler returns TRUE.
|
2015-02-15 02:07:23 +00:00
|
|
|
* - This ends the processing for the first ExitWindowsEx() call from Winlogon.
|
|
|
|
* Execution continues in Winlogon, which calls ExitWindowsEx() again to
|
2004-07-12 20:09:35 +00:00
|
|
|
* terminate COM processes in the interactive user's session.
|
2015-02-15 02:07:23 +00:00
|
|
|
* - Winlogon stops impersonating the interactive user (whose processes are
|
2005-05-09 01:42:07 +00:00
|
|
|
* all dead by now). and enters log-out state
|
2015-02-15 02:07:23 +00:00
|
|
|
* - If the ExitWindowsEx() request was for a logoff, Winlogon sends a SAS
|
|
|
|
* event (to display the "press ctrl+alt+del") to the GINA. Winlogon then
|
2005-05-09 01:42:07 +00:00
|
|
|
* waits for the GINA to send a SAS event to login.
|
2015-02-15 02:07:23 +00:00
|
|
|
* - If the ExitWindowsEx() request was for shutdown/restart, Winlogon calls
|
2004-07-12 20:09:35 +00:00
|
|
|
* ExitWindowsEx() again in the system process context.
|
|
|
|
* - CSRSS goes through the motions of sending WM_QUERYENDSESSION/WM_ENDSESSION
|
|
|
|
* to GUI processes running in the system process context but won't display
|
|
|
|
* dialog boxes or kill threads/processes. Same for console processes,
|
|
|
|
* using the CTRL_SHUTDOWN_EVENT. The Service Control Manager is one of
|
|
|
|
* these console processes and has a special timeout value WaitToKillServiceTimeout.
|
2015-02-15 02:07:23 +00:00
|
|
|
* - After CSRSS has finished its pass notifying processes that system is shutting down,
|
|
|
|
* Winlogon finishes the shutdown process by calling the executive subsystem
|
|
|
|
* function NtShutdownSystem.
|
2004-07-12 20:09:35 +00:00
|
|
|
*/
|
2014-11-29 21:19:01 +00:00
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
UINT uFlags;
|
|
|
|
DWORD dwReserved;
|
|
|
|
} EXIT_REACTOS_DATA, *PEXIT_REACTOS_DATA;
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
ExitWindowsWorker(UINT uFlags,
|
|
|
|
DWORD dwReserved,
|
|
|
|
BOOL bCalledFromThread);
|
|
|
|
|
|
|
|
static DWORD
|
|
|
|
WINAPI
|
|
|
|
ExitWindowsThread(LPVOID Param)
|
|
|
|
{
|
|
|
|
DWORD dwExitCode;
|
|
|
|
PEXIT_REACTOS_DATA ExitData = (PEXIT_REACTOS_DATA)Param;
|
|
|
|
|
|
|
|
/* Do the exit asynchronously */
|
|
|
|
if (ExitWindowsWorker(ExitData->uFlags, ExitData->dwReserved, TRUE))
|
|
|
|
dwExitCode = ERROR_SUCCESS;
|
|
|
|
else
|
|
|
|
dwExitCode = GetLastError();
|
|
|
|
|
|
|
|
ExitThread(dwExitCode);
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
ExitWindowsWorker(UINT uFlags,
|
|
|
|
DWORD dwReserved,
|
|
|
|
BOOL bCalledFromThread)
|
|
|
|
{
|
|
|
|
EXIT_REACTOS_DATA ExitData;
|
|
|
|
HANDLE hExitThread;
|
|
|
|
DWORD ExitCode;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
USER_API_MESSAGE ApiMessage;
|
2015-04-18 09:21:51 +00:00
|
|
|
PUSER_EXIT_REACTOS ExitReactOSRequest = &ApiMessage.Data.ExitReactOSRequest;
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 1- FIXME: Call NtUserCallOneParam(uFlags, ONEPARAM_ROUTINE_PREPAREFORLOGOFF);
|
|
|
|
* If success we can continue, otherwise we must fail.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2- Send the Exit request to CSRSS (and to Win32k indirectly).
|
|
|
|
* We can shutdown synchronously or asynchronously.
|
|
|
|
*/
|
|
|
|
|
2015-04-18 09:21:51 +00:00
|
|
|
// ExitReactOSRequest->LastError = ERROR_SUCCESS;
|
|
|
|
ExitReactOSRequest->Flags = uFlags;
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
|
|
|
|
NULL,
|
|
|
|
CSR_CREATE_API_NUMBER(USERSRV_SERVERDLL_INDEX, UserpExitWindowsEx),
|
2015-04-18 09:21:51 +00:00
|
|
|
sizeof(*ExitReactOSRequest));
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/* Set the last error accordingly */
|
|
|
|
if (NT_SUCCESS(ApiMessage.Status) || ApiMessage.Status == STATUS_CANT_WAIT)
|
|
|
|
{
|
2015-04-18 09:21:51 +00:00
|
|
|
if (ExitReactOSRequest->LastError != ERROR_SUCCESS)
|
|
|
|
UserSetLastError(ExitReactOSRequest->LastError);
|
2015-02-07 15:26:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UserSetLastNTError(ApiMessage.Status);
|
2015-04-18 09:21:51 +00:00
|
|
|
ExitReactOSRequest->Success = FALSE;
|
2015-02-07 15:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case CSR call succeeded and we did a synchronous exit
|
|
|
|
* (STATUS_CANT_WAIT is considered as a non-success status),
|
|
|
|
* return the real state of the operation now.
|
|
|
|
*/
|
|
|
|
if (NT_SUCCESS(ApiMessage.Status))
|
2015-04-18 09:21:51 +00:00
|
|
|
return ExitReactOSRequest->Success;
|
2015-02-07 15:26:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In case something failed: we have a non-success status and:
|
|
|
|
* - either we were doing a synchronous exit (Status != STATUS_CANT_WAIT), or
|
|
|
|
* - we were doing an asynchronous exit because we were called recursively via
|
|
|
|
* another thread but we failed to exit,
|
|
|
|
* then bail out immediately, otherwise we would enter an infinite loop of exit requests.
|
|
|
|
*
|
|
|
|
* On the contrary if we need to do an asynchronous exit (Status == STATUS_CANT_WAIT
|
|
|
|
* and not called recursively via another thread), then continue and do the exit.
|
|
|
|
*/
|
|
|
|
if (ApiMessage.Status != STATUS_CANT_WAIT || bCalledFromThread)
|
|
|
|
{
|
|
|
|
UserSetLastNTError(ApiMessage.Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 3- Win32k wants us to perform an asynchronous exit. Run the request in a thread.
|
|
|
|
* (ApiMessage.Status == STATUS_CANT_WAIT and not already called from a thread)
|
|
|
|
*/
|
|
|
|
ExitData.uFlags = uFlags;
|
|
|
|
ExitData.dwReserved = dwReserved;
|
|
|
|
hExitThread = CreateThread(NULL, 0, ExitWindowsThread, &ExitData, 0, NULL);
|
|
|
|
if (hExitThread == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Pump and discard any input events sent to the app(s) */
|
|
|
|
while (MsgWaitForMultipleObjectsEx(1, &hExitThread, INFINITE, QS_ALLINPUT, 0))
|
|
|
|
{
|
|
|
|
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
|
|
|
|
DispatchMessageW(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, return to caller */
|
|
|
|
if (!GetExitCodeThread(hExitThread, &ExitCode))
|
|
|
|
ExitCode = GetLastError();
|
|
|
|
|
|
|
|
CloseHandle(hExitThread);
|
|
|
|
|
|
|
|
if (ExitCode != ERROR_SUCCESS)
|
|
|
|
UserSetLastError(ExitCode);
|
|
|
|
|
|
|
|
return (ExitCode == ERROR_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2003-07-10 21:04:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-30 11:42:05 +00:00
|
|
|
BOOL WINAPI
|
2002-10-20 14:54:34 +00:00
|
|
|
ExitWindowsEx(UINT uFlags,
|
2012-10-17 23:10:40 +00:00
|
|
|
DWORD dwReserved)
|
2002-10-20 14:54:34 +00:00
|
|
|
{
|
2015-02-07 15:26:42 +00:00
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* 1- Calling the Exit worker must be done under certain conditions.
|
|
|
|
* We may also need to warn the user if there are other people logged
|
|
|
|
* on this computer (see http://pve.proxmox.com/wiki/Windows_2003_guest_best_practices )
|
|
|
|
* 2- Call SrvRecordShutdownReason.
|
|
|
|
*/
|
2002-10-20 14:54:34 +00:00
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
return ExitWindowsWorker(uFlags, dwReserved, FALSE);
|
2002-10-20 14:54:34 +00:00
|
|
|
|
2015-02-07 15:26:42 +00:00
|
|
|
/* FIXME: Call SrvRecordShutdownReason if we failed */
|
2002-10-20 14:54:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 23:42:40 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
EndTask(HWND hWnd,
|
|
|
|
BOOL fShutDown,
|
|
|
|
BOOL fForce)
|
|
|
|
{
|
|
|
|
USER_API_MESSAGE ApiMessage;
|
|
|
|
PUSER_END_TASK EndTaskRequest = &ApiMessage.Data.EndTaskRequest;
|
|
|
|
|
|
|
|
UNREFERENCED_PARAMETER(fShutDown);
|
|
|
|
|
|
|
|
// EndTaskRequest->LastError = ERROR_SUCCESS;
|
|
|
|
EndTaskRequest->WndHandle = hWnd;
|
|
|
|
EndTaskRequest->Force = fForce;
|
|
|
|
|
|
|
|
CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
|
|
|
|
NULL,
|
|
|
|
CSR_CREATE_API_NUMBER(USERSRV_SERVERDLL_INDEX, UserpEndTask),
|
|
|
|
sizeof(*EndTaskRequest));
|
|
|
|
if (!NT_SUCCESS(ApiMessage.Status))
|
|
|
|
{
|
|
|
|
UserSetLastNTError(ApiMessage.Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EndTaskRequest->LastError != ERROR_SUCCESS)
|
|
|
|
UserSetLastError(EndTaskRequest->LastError);
|
|
|
|
|
|
|
|
return EndTaskRequest->Success;
|
|
|
|
}
|
|
|
|
|
2002-10-20 14:54:34 +00:00
|
|
|
/* EOF */
|