3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS CSRSS subsystem
5 * FILE: subsys/csrss/win32csr/exitros.c
6 * PURPOSE: Logout/shutdown
9 /* INCLUDES ******************************************************************/
18 static HWND LogonNotifyWindow
= NULL
;
19 static HANDLE LogonProcess
= NULL
;
21 CSR_API(CsrRegisterLogonProcess
)
23 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
24 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) - sizeof(PORT_MESSAGE
);
26 if (Request
->Data
.RegisterLogonProcessRequest
.Register
)
28 if (0 != LogonProcess
)
30 return STATUS_LOGON_SESSION_EXISTS
;
32 LogonProcess
= Request
->Data
.RegisterLogonProcessRequest
.ProcessId
;
36 if (Request
->Header
.ClientId
.UniqueProcess
!= LogonProcess
)
38 DPRINT1("Current logon process 0x%x, can't deregister from process 0x%x\n",
39 LogonProcess
, Request
->Header
.ClientId
.UniqueProcess
);
40 return STATUS_NOT_LOGON_PROCESS
;
45 return STATUS_SUCCESS
;
48 CSR_API(CsrSetLogonNotifyWindow
)
52 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
53 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) -
56 if (0 == GetWindowThreadProcessId(Request
->Data
.SetLogonNotifyWindowRequest
.LogonNotifyWindow
,
59 DPRINT1("Can't get window creator\n");
60 return STATUS_INVALID_HANDLE
;
62 if (WindowCreator
!= (DWORD
)LogonProcess
)
64 DPRINT1("Trying to register window not created by winlogon as notify window\n");
65 return STATUS_ACCESS_DENIED
;
68 LogonNotifyWindow
= Request
->Data
.SetLogonNotifyWindowRequest
.LogonNotifyWindow
;
70 return STATUS_SUCCESS
;
73 typedef struct tagSHUTDOWN_SETTINGS
77 DWORD WaitToKillAppTimeout
;
78 } SHUTDOWN_SETTINGS
, *PSHUTDOWN_SETTINGS
;
80 #define DEFAULT_AUTO_END_TASKS FALSE
81 #define DEFAULT_HUNG_APP_TIMEOUT 5000
82 #define DEFAULT_WAIT_TO_KILL_APP_TIMEOUT 20000
84 typedef struct tagNOTIFY_CONTEXT
98 PSHUTDOWN_SETTINGS ShutdownSettings
;
99 LPTHREAD_START_ROUTINE SendMessageProc
;
100 } NOTIFY_CONTEXT
, *PNOTIFY_CONTEXT
;
102 #define QUERY_RESULT_ABORT 0
103 #define QUERY_RESULT_CONTINUE 1
104 #define QUERY_RESULT_TIMEOUT 2
105 #define QUERY_RESULT_ERROR 3
106 #define QUERY_RESULT_FORCE 4
109 UpdateProgressBar(HWND ProgressBar
, PNOTIFY_CONTEXT NotifyContext
)
113 Passed
= GetTickCount() - NotifyContext
->StartTime
;
114 Passed
-= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
115 if (NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
< Passed
)
117 Passed
= NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
;
119 SendMessageW(ProgressBar
, PBM_SETPOS
, Passed
/ 2, 0);
122 static INT_PTR CALLBACK
123 EndNowDlgProc(HWND Dlg
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
126 PNOTIFY_CONTEXT NotifyContext
;
135 NotifyContext
= (PNOTIFY_CONTEXT
) lParam
;
136 NotifyContext
->EndNowResult
= QUERY_RESULT_ABORT
;
137 SetWindowLongPtrW(Dlg
, DWLP_USER
, (LONG_PTR
) lParam
);
138 TitleLength
= SendMessageW(NotifyContext
->WndClient
, WM_GETTEXTLENGTH
,
140 GetWindowTextLengthW(Dlg
);
141 Title
= HeapAlloc(Win32CsrApiHeap
, 0, (TitleLength
+ 1) * sizeof(WCHAR
));
144 Len
= GetWindowTextW(Dlg
, Title
, TitleLength
+ 1);
145 SendMessageW(NotifyContext
->WndClient
, WM_GETTEXT
,
146 TitleLength
+ 1 - Len
, (LPARAM
) (Title
+ Len
));
147 SetWindowTextW(Dlg
, Title
);
148 HeapFree(Win32CsrApiHeap
, 0, Title
);
150 ProgressBar
= GetDlgItem(Dlg
, IDC_PROGRESS
);
151 SendMessageW(ProgressBar
, PBM_SETRANGE32
, 0,
152 NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
/ 2);
153 UpdateProgressBar(ProgressBar
, NotifyContext
);
154 SetTimer(Dlg
, 0, 200, NULL
);
159 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
160 ProgressBar
= GetDlgItem(Dlg
, IDC_PROGRESS
);
161 UpdateProgressBar(ProgressBar
, NotifyContext
);
166 if (BN_CLICKED
== HIWORD(wParam
) && IDC_END_NOW
== LOWORD(wParam
))
168 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
169 NotifyContext
->EndNowResult
= QUERY_RESULT_FORCE
;
170 SendMessageW(Dlg
, WM_CLOSE
, 0, 0);
185 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
186 NotifyContext
->Dlg
= NULL
;
188 PostQuitMessage(NotifyContext
->EndNowResult
);
200 typedef void (STDCALL
*INITCOMMONCONTROLS_PROC
)(void);
203 CallInitCommonControls()
205 static BOOL Initialized
= FALSE
;
207 INITCOMMONCONTROLS_PROC InitProc
;
214 Lib
= LoadLibraryW(L
"COMCTL32.DLL");
219 InitProc
= (INITCOMMONCONTROLS_PROC
) GetProcAddress(Lib
, "InitCommonControls");
220 if (NULL
== InitProc
)
231 EndNowThreadProc(LPVOID Parameter
)
233 PNOTIFY_CONTEXT NotifyContext
= (PNOTIFY_CONTEXT
) Parameter
;
236 SetThreadDesktop(NotifyContext
->Desktop
);
237 SwitchDesktop(NotifyContext
->Desktop
);
238 CallInitCommonControls();
239 NotifyContext
->Dlg
= CreateDialogParam(GetModuleHandleW(L
"win32csr"),
240 MAKEINTRESOURCE(IDD_END_NOW
), NULL
,
241 EndNowDlgProc
, (LPARAM
) NotifyContext
);
242 if (NULL
== NotifyContext
->Dlg
)
246 ShowWindow(NotifyContext
->Dlg
, SW_SHOWNORMAL
);
248 while (GetMessageW(&Msg
, NULL
, 0, 0))
250 if (! IsDialogMessage(NotifyContext
->Dlg
, &Msg
))
252 TranslateMessage(&Msg
);
253 DispatchMessageW(&Msg
);
260 typedef struct tagMESSAGE_CONTEXT
267 } MESSAGE_CONTEXT
, *PMESSAGE_CONTEXT
;
270 SendQueryEndSession(LPVOID Parameter
)
272 PMESSAGE_CONTEXT Context
= (PMESSAGE_CONTEXT
) Parameter
;
275 if (SendMessageTimeoutW(Context
->Wnd
, WM_QUERYENDSESSION
, Context
->wParam
,
276 Context
->lParam
, SMTO_NORMAL
, Context
->Timeout
,
279 return Result
? QUERY_RESULT_CONTINUE
: QUERY_RESULT_ABORT
;
282 return 0 == GetLastError() ? QUERY_RESULT_TIMEOUT
: QUERY_RESULT_ERROR
;
286 SendEndSession(LPVOID Parameter
)
288 PMESSAGE_CONTEXT Context
= (PMESSAGE_CONTEXT
) Parameter
;
293 if (SendMessageTimeoutW(Context
->Wnd
, WM_ENDSESSION
, Context
->wParam
,
294 Context
->lParam
, SMTO_NORMAL
, Context
->Timeout
,
297 return QUERY_RESULT_CONTINUE
;
299 return 0 == GetLastError() ? QUERY_RESULT_TIMEOUT
: QUERY_RESULT_ERROR
;
303 SendMessage(Context
->Wnd
, WM_ENDSESSION
, Context
->wParam
,
305 return QUERY_RESULT_CONTINUE
;
310 NotifyTopLevelEnum(HWND Wnd
, LPARAM lParam
)
312 PNOTIFY_CONTEXT NotifyContext
= (PNOTIFY_CONTEXT
) lParam
;
313 MESSAGE_CONTEXT MessageContext
;
315 DWORD Timeout
, WaitStatus
;
317 HANDLE MessageThread
;
320 if (0 == GetWindowThreadProcessId(Wnd
, &ProcessId
))
322 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
326 if (ProcessId
== NotifyContext
->ProcessId
)
328 Now
= GetTickCount();
329 if (0 == NotifyContext
->StartTime
)
331 NotifyContext
->StartTime
= Now
;
333 /* Note: Passed is computed correctly even when GetTickCount() wraps due
334 to unsigned arithmetic */
335 Passed
= Now
- NotifyContext
->StartTime
;
336 MessageContext
.Wnd
= Wnd
;
337 MessageContext
.Msg
= NotifyContext
->Msg
;
338 MessageContext
.wParam
= NotifyContext
->wParam
;
339 MessageContext
.lParam
= NotifyContext
->lParam
;
340 MessageContext
.Timeout
= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
341 if (! NotifyContext
->ShutdownSettings
->AutoEndTasks
)
343 MessageContext
.Timeout
+= NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
;
345 if (Passed
< MessageContext
.Timeout
)
347 MessageContext
.Timeout
-= Passed
;
348 MessageThread
= CreateThread(NULL
, 0, NotifyContext
->SendMessageProc
,
349 (LPVOID
) &MessageContext
, 0, NULL
);
350 if (NULL
== MessageThread
)
352 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
355 Timeout
= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
356 if (Passed
< Timeout
)
359 WaitStatus
= WaitForSingleObjectEx(MessageThread
, Timeout
, FALSE
);
363 WaitStatus
= WAIT_TIMEOUT
;
365 if (WAIT_TIMEOUT
== WaitStatus
)
367 NotifyContext
->WndClient
= Wnd
;
368 if (NULL
== NotifyContext
->UIThread
&& NotifyContext
->ShowUI
)
370 NotifyContext
->UIThread
= CreateThread(NULL
, 0,
372 (LPVOID
) NotifyContext
,
375 Threads
[0] = MessageThread
;
376 Threads
[1] = NotifyContext
->UIThread
;
377 WaitStatus
= WaitForMultipleObjectsEx(NULL
== NotifyContext
->UIThread
?
379 Threads
, FALSE
, INFINITE
,
381 if (WAIT_OBJECT_0
== WaitStatus
)
383 if (! GetExitCodeThread(MessageThread
, &NotifyContext
->QueryResult
))
385 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
388 else if (WAIT_OBJECT_0
+ 1 == WaitStatus
)
390 if (! GetExitCodeThread(NotifyContext
->UIThread
,
391 &NotifyContext
->QueryResult
))
393 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
398 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
400 if (WAIT_OBJECT_0
!= WaitStatus
)
402 TerminateThread(MessageThread
, QUERY_RESULT_TIMEOUT
);
405 else if (WAIT_OBJECT_0
== WaitStatus
)
407 if (! GetExitCodeThread(MessageThread
,
408 &NotifyContext
->QueryResult
))
410 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
415 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
417 CloseHandle(MessageThread
);
421 NotifyContext
->QueryResult
= QUERY_RESULT_TIMEOUT
;
425 return QUERY_RESULT_CONTINUE
== NotifyContext
->QueryResult
;
429 NotifyDesktopEnum(LPWSTR DesktopName
, LPARAM lParam
)
431 PNOTIFY_CONTEXT Context
= (PNOTIFY_CONTEXT
) lParam
;
433 Context
->Desktop
= OpenDesktopW(DesktopName
, 0, FALSE
,
434 DESKTOP_ENUMERATE
| DESKTOP_SWITCHDESKTOP
);
435 if (NULL
== Context
->Desktop
)
437 DPRINT1("OpenDesktop failed with error %d\n", GetLastError());
438 Context
->QueryResult
= QUERY_RESULT_ERROR
;
442 EnumDesktopWindows(Context
->Desktop
, NotifyTopLevelEnum
, lParam
);
444 CloseDesktop(Context
->Desktop
);
446 return QUERY_RESULT_CONTINUE
== Context
->QueryResult
;
450 NotifyTopLevelWindows(PNOTIFY_CONTEXT Context
)
452 HWINSTA WindowStation
;
454 WindowStation
= GetProcessWindowStation();
455 if (NULL
== WindowStation
)
457 DPRINT1("GetProcessWindowStation failed with error %d\n", GetLastError());
461 EnumDesktopsW(WindowStation
, NotifyDesktopEnum
, (LPARAM
) Context
);
467 NotifyAndTerminateProcess(PCSRSS_PROCESS_DATA ProcessData
,
468 PSHUTDOWN_SETTINGS ShutdownSettings
,
471 NOTIFY_CONTEXT Context
;
473 DWORD QueryResult
= QUERY_RESULT_CONTINUE
;
475 Context
.QueryResult
= QUERY_RESULT_CONTINUE
;
477 if (0 == (Flags
& EWX_FORCE
))
479 if (NULL
!= ProcessData
->Console
)
481 ConioConsoleCtrlEventTimeout(CTRL_LOGOFF_EVENT
, ProcessData
,
482 ShutdownSettings
->WaitToKillAppTimeout
);
486 Context
.ProcessId
= (DWORD
) ProcessData
->ProcessId
;
488 Context
.lParam
= (0 != (Flags
& EWX_INTERNAL_FLAG_LOGOFF
) ?
489 ENDSESSION_LOGOFF
: 0);
490 Context
.StartTime
= 0;
491 Context
.UIThread
= NULL
;
492 Context
.ShowUI
= DtbgIsDesktopVisible();
494 Context
.ShutdownSettings
= ShutdownSettings
;
495 Context
.SendMessageProc
= SendQueryEndSession
;
497 NotifyTopLevelWindows(&Context
);
499 Context
.wParam
= (QUERY_RESULT_ABORT
!= Context
.QueryResult
);
500 Context
.lParam
= (0 != (Flags
& EWX_INTERNAL_FLAG_LOGOFF
) ?
501 ENDSESSION_LOGOFF
: 0);
502 Context
.SendMessageProc
= SendEndSession
;
503 Context
.ShowUI
= DtbgIsDesktopVisible() &&
504 (QUERY_RESULT_ABORT
!= Context
.QueryResult
);
505 QueryResult
= Context
.QueryResult
;
506 Context
.QueryResult
= QUERY_RESULT_CONTINUE
;
508 NotifyTopLevelWindows(&Context
);
510 if (NULL
!= Context
.UIThread
)
512 if (NULL
!= Context
.Dlg
)
514 SendMessageW(Context
.Dlg
, WM_CLOSE
, 0, 0);
518 TerminateThread(Context
.UIThread
, QUERY_RESULT_ERROR
);
520 CloseHandle(Context
.UIThread
);
524 if (QUERY_RESULT_ABORT
== QueryResult
)
530 /* Terminate this process */
531 Process
= OpenProcess(PROCESS_TERMINATE
, FALSE
,
532 (DWORD
) ProcessData
->ProcessId
);
535 DPRINT1("Unable to open process %d, error %d\n", ProcessData
->ProcessId
,
539 TerminateProcess(Process
, 0);
540 CloseHandle(Process
);
545 typedef struct tagPROCESS_ENUM_CONTEXT
548 PCSRSS_PROCESS_DATA
*ProcessData
;
549 TOKEN_ORIGIN TokenOrigin
;
552 } PROCESS_ENUM_CONTEXT
, *PPROCESS_ENUM_CONTEXT
;
554 static NTSTATUS STDCALL
555 ExitReactosProcessEnum(PCSRSS_PROCESS_DATA ProcessData
, PVOID Data
)
561 PPROCESS_ENUM_CONTEXT Context
= (PPROCESS_ENUM_CONTEXT
) Data
;
562 PCSRSS_PROCESS_DATA
*NewData
;
564 /* Do not kill winlogon or csrss */
565 if ((DWORD
) ProcessData
->ProcessId
== Context
->CsrssProcess
||
566 ProcessData
->ProcessId
== LogonProcess
)
568 return STATUS_SUCCESS
;
571 /* Get the login session of this process */
572 Process
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
,
573 (DWORD
) ProcessData
->ProcessId
);
576 DPRINT1("Unable to open process %d, error %d\n", ProcessData
->ProcessId
,
578 return STATUS_UNSUCCESSFUL
;
581 if (! OpenProcessToken(Process
, TOKEN_QUERY
, &Token
))
583 DPRINT1("Unable to open token for process %d, error %d\n",
584 ProcessData
->ProcessId
, GetLastError());
585 CloseHandle(Process
);
586 return STATUS_UNSUCCESSFUL
;
588 CloseHandle(Process
);
590 if (! GetTokenInformation(Token
, TokenOrigin
, &Origin
,
591 sizeof(TOKEN_ORIGIN
), &ReturnLength
))
593 DPRINT1("GetTokenInformation failed for process %d with error %d\n",
594 ProcessData
->ProcessId
, GetLastError());
596 return STATUS_UNSUCCESSFUL
;
600 /* This process will be killed if it's in the correct logon session */
601 if (RtlEqualLuid(&(Context
->TokenOrigin
.OriginatingLogonSession
),
602 &(Origin
.OriginatingLogonSession
)))
604 /* Kill the shell process last */
605 if ((DWORD
) ProcessData
->ProcessId
== Context
->ShellProcess
)
607 ProcessData
->ShutdownLevel
= 0;
609 NewData
= HeapAlloc(Win32CsrApiHeap
, 0, (Context
->ProcessCount
+ 1)
610 * sizeof(PCSRSS_PROCESS_DATA
));
613 return STATUS_NO_MEMORY
;
615 if (0 != Context
->ProcessCount
)
617 memcpy(NewData
, Context
->ProcessData
,
618 Context
->ProcessCount
* sizeof(PCSRSS_PROCESS_DATA
));
619 HeapFree(Win32CsrApiHeap
, 0, Context
->ProcessData
);
621 Context
->ProcessData
= NewData
;
622 Context
->ProcessData
[Context
->ProcessCount
] = ProcessData
;
623 Context
->ProcessCount
++;
626 return STATUS_SUCCESS
;
630 ProcessDataCompare(const void *Elem1
, const void *Elem2
)
632 const PCSRSS_PROCESS_DATA
*ProcessData1
= (PCSRSS_PROCESS_DATA
*) Elem1
;
633 const PCSRSS_PROCESS_DATA
*ProcessData2
= (PCSRSS_PROCESS_DATA
*) Elem2
;
635 if ((*ProcessData1
)->ShutdownLevel
< (*ProcessData2
)->ShutdownLevel
)
639 else if ((*ProcessData2
)->ShutdownLevel
< (*ProcessData1
)->ShutdownLevel
)
643 else if ((*ProcessData1
)->ProcessId
< (*ProcessData2
)->ProcessId
)
647 else if ((*ProcessData2
)->ProcessId
< (*ProcessData1
)->ProcessId
)
655 static DWORD FASTCALL
656 GetShutdownSetting(HKEY DesktopKey
, LPCWSTR ValueName
, DWORD DefaultValue
)
658 BYTE ValueBuffer
[16];
662 UNICODE_STRING StringValue
;
665 ValueSize
= sizeof(ValueBuffer
);
666 ErrCode
= RegQueryValueExW(DesktopKey
, ValueName
, NULL
, &Type
, ValueBuffer
,
668 if (ERROR_SUCCESS
!= ErrCode
)
670 DPRINT("GetShutdownSetting for %S failed with error code %ld\n",
677 RtlInitUnicodeString(&StringValue
, (LPCWSTR
) ValueBuffer
);
678 if (! NT_SUCCESS(RtlUnicodeStringToInteger(&StringValue
, 10, &Value
)))
680 DPRINT1("Unable to convert value %S for setting %S\n",
681 StringValue
.Buffer
, ValueName
);
684 return (DWORD
) Value
;
686 else if (REG_DWORD
== Type
)
688 return *((DWORD
*) ValueBuffer
);
691 DPRINT1("Unexpected registry type %d for setting %S\n", Type
, ValueName
);
696 LoadShutdownSettings(PSID Sid
, PSHUTDOWN_SETTINGS ShutdownSettings
)
698 static WCHAR Subkey
[] = L
"\\Control Panel\\Desktop";
700 WCHAR InitialKeyName
[128];
705 ShutdownSettings
->AutoEndTasks
= DEFAULT_AUTO_END_TASKS
;
706 ShutdownSettings
->HungAppTimeout
= DEFAULT_HUNG_APP_TIMEOUT
;
707 ShutdownSettings
->WaitToKillAppTimeout
= DEFAULT_WAIT_TO_KILL_APP_TIMEOUT
;
709 if (! ConvertSidToStringSidW(Sid
, &StringSid
))
711 DPRINT1("ConvertSidToStringSid failed with error %d, using default shutdown settings\n",
715 if (wcslen(StringSid
) + wcslen(Subkey
) + 1 <=
716 sizeof(InitialKeyName
) / sizeof(WCHAR
))
718 KeyName
= InitialKeyName
;
722 KeyName
= HeapAlloc(Win32CsrApiHeap
, 0,
723 (wcslen(StringSid
) + wcslen(Subkey
) + 1) *
727 DPRINT1("Failed to allocate memory, using default shutdown settings\n");
728 LocalFree(StringSid
);
732 wcscat(wcscpy(KeyName
, StringSid
), Subkey
);
733 LocalFree(StringSid
);
735 ErrCode
= RegOpenKeyExW(HKEY_USERS
, KeyName
, 0, KEY_QUERY_VALUE
, &DesktopKey
);
736 if (KeyName
!= InitialKeyName
)
738 HeapFree(Win32CsrApiHeap
, 0, KeyName
);
740 if (ERROR_SUCCESS
!= ErrCode
)
742 DPRINT1("RegOpenKeyEx failed with error %ld, using default shutdown settings\n", ErrCode
);
746 ShutdownSettings
->AutoEndTasks
= (BOOL
) GetShutdownSetting(DesktopKey
, L
"AutoEndTasks",
747 (DWORD
) DEFAULT_AUTO_END_TASKS
);
748 ShutdownSettings
->HungAppTimeout
= GetShutdownSetting(DesktopKey
,
750 DEFAULT_HUNG_APP_TIMEOUT
);
751 ShutdownSettings
->WaitToKillAppTimeout
= GetShutdownSetting(DesktopKey
,
752 L
"WaitToKillAppTimeout",
753 DEFAULT_WAIT_TO_KILL_APP_TIMEOUT
);
755 RegCloseKey(DesktopKey
);
758 static NTSTATUS FASTCALL
759 InternalExitReactos(DWORD ProcessId
, DWORD ThreadId
, UINT Flags
)
764 PROCESS_ENUM_CONTEXT Context
;
768 char FixedUserInfo
[64];
769 TOKEN_USER
*UserInfo
;
770 SHUTDOWN_SETTINGS ShutdownSettings
;
772 if (ProcessId
!= (DWORD
) LogonProcess
)
774 DPRINT1("Internal ExitWindowsEx call not from winlogon\n");
775 return STATUS_ACCESS_DENIED
;
778 DPRINT1("FIXME: Need to close all user processes!\n");
779 return STATUS_SUCCESS
;
781 CallerThread
= OpenThread(THREAD_QUERY_INFORMATION
, FALSE
, ThreadId
);
782 if (NULL
== CallerThread
)
784 DPRINT1("OpenThread failed with error %d\n", GetLastError());
785 return STATUS_UNSUCCESSFUL
;
787 if (! OpenThreadToken(CallerThread
, TOKEN_QUERY
, FALSE
, &CallerToken
))
789 DPRINT1("OpenThreadToken failed with error %d\n", GetLastError());
790 CloseHandle(CallerThread
);
791 return STATUS_UNSUCCESSFUL
;
793 CloseHandle(CallerThread
);
795 Context
.ProcessCount
= 0;
796 Context
.ProcessData
= NULL
;
797 if (! GetTokenInformation(CallerToken
, TokenOrigin
, &Context
.TokenOrigin
,
798 sizeof(TOKEN_ORIGIN
), &ReturnLength
))
800 DPRINT1("GetTokenInformation failed with error %d\n", GetLastError());
801 CloseHandle(CallerToken
);
802 return STATUS_UNSUCCESSFUL
;
804 if (! GetTokenInformation(CallerToken
, TokenUser
, FixedUserInfo
,
805 sizeof(FixedUserInfo
), &ReturnLength
))
807 if (sizeof(FixedUserInfo
) < ReturnLength
)
809 UserInfo
= HeapAlloc(Win32CsrApiHeap
, 0, ReturnLength
);
810 if (NULL
== UserInfo
)
812 DPRINT1("Unable to allocate %u bytes for user info\n",
813 (unsigned) ReturnLength
);
814 CloseHandle(CallerToken
);
815 return STATUS_NO_MEMORY
;
817 if (! GetTokenInformation(CallerToken
, TokenUser
, UserInfo
,
818 ReturnLength
, &ReturnLength
))
820 DPRINT1("GetTokenInformation failed with error %d\n",
822 HeapFree(Win32CsrApiHeap
, 0, UserInfo
);
823 CloseHandle(CallerToken
);
824 return STATUS_UNSUCCESSFUL
;
829 DPRINT1("GetTokenInformation failed with error %d\n", GetLastError());
830 CloseHandle(CallerToken
);
831 return STATUS_UNSUCCESSFUL
;
836 UserInfo
= (TOKEN_USER
*) FixedUserInfo
;
838 CloseHandle(CallerToken
);
839 LoadShutdownSettings(UserInfo
->User
.Sid
, &ShutdownSettings
);
840 if (UserInfo
!= (TOKEN_USER
*) FixedUserInfo
)
842 HeapFree(Win32CsrApiHeap
, 0, UserInfo
);
844 Context
.CsrssProcess
= GetCurrentProcessId();
845 ShellWnd
= GetShellWindow();
846 if (NULL
== ShellWnd
)
848 DPRINT("No shell present\n");
849 Context
.ShellProcess
= 0;
851 else if (0 == GetWindowThreadProcessId(ShellWnd
, &Context
.ShellProcess
))
853 DPRINT1("Can't get process id of shell window\n");
854 Context
.ShellProcess
= 0;
857 Status
= Win32CsrEnumProcesses(ExitReactosProcessEnum
, &Context
);
858 if (! NT_SUCCESS(Status
))
860 DPRINT1("Failed to enumerate registered processes, status 0x%x\n",
862 if (NULL
!= Context
.ProcessData
)
864 HeapFree(Win32CsrApiHeap
, 0, Context
.ProcessData
);
869 qsort(Context
.ProcessData
, Context
.ProcessCount
, sizeof(PCSRSS_PROCESS_DATA
),
872 /* Terminate processes, stop if we find one kicking and screaming it doesn't
874 Status
= STATUS_SUCCESS
;
875 for (ProcessIndex
= 0;
876 ProcessIndex
< Context
.ProcessCount
&& NT_SUCCESS(Status
);
879 if (! NotifyAndTerminateProcess(Context
.ProcessData
[ProcessIndex
],
880 &ShutdownSettings
, Flags
))
882 Status
= STATUS_REQUEST_ABORTED
;
887 if (NULL
!= Context
.ProcessData
)
889 HeapFree(Win32CsrApiHeap
, 0, Context
.ProcessData
);
895 static NTSTATUS FASTCALL
896 UserExitReactos(DWORD UserProcessId
, UINT Flags
)
900 if (NULL
== LogonNotifyWindow
)
902 DPRINT1("No LogonNotifyWindow registered\n");
903 return STATUS_NOT_FOUND
;
906 /* FIXME Inside 2000 says we should impersonate the caller here */
907 Status
= SendMessageW(LogonNotifyWindow
, PM_WINLOGON_EXITWINDOWS
,
908 (WPARAM
) UserProcessId
,
910 /* If the message isn't handled, the return value is 0, so 0 doesn't indicate
911 success. Success is indicated by a 1 return value, if anything besides 0
912 or 1 it's a NTSTATUS value */
915 Status
= STATUS_SUCCESS
;
917 else if (0 == Status
)
919 Status
= STATUS_NOT_IMPLEMENTED
;
925 CSR_API(CsrExitReactos
)
927 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
928 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) -
929 sizeof(PORT_MESSAGE
);
931 if (0 == (Request
->Data
.ExitReactosRequest
.Flags
& EWX_INTERNAL_FLAG
))
933 return UserExitReactos((DWORD
) Request
->Header
.ClientId
.UniqueProcess
,
934 Request
->Data
.ExitReactosRequest
.Flags
);
938 return InternalExitReactos((DWORD
) Request
->Header
.ClientId
.UniqueProcess
,
939 (DWORD
) Request
->Header
.ClientId
.UniqueThread
,
940 Request
->Data
.ExitReactosRequest
.Flags
);