2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS User API Server DLL
4 * FILE: win32ss/user/winsrv/usersrv/shutdown.c
5 * PURPOSE: Logout/shutdown
8 * NOTE: The shutdown code must be rewritten completely. (hbelusca)
11 /* INCLUDES *******************************************************************/
19 /* GLOBALS ********************************************************************/
21 typedef struct tagSHUTDOWN_SETTINGS
25 DWORD WaitToKillAppTimeout
;
26 } SHUTDOWN_SETTINGS
, *PSHUTDOWN_SETTINGS
;
28 #define DEFAULT_AUTO_END_TASKS FALSE
29 #define DEFAULT_HUNG_APP_TIMEOUT 5000
30 #define DEFAULT_WAIT_TO_KILL_APP_TIMEOUT 20000
32 typedef struct tagNOTIFY_CONTEXT
47 PSHUTDOWN_SETTINGS ShutdownSettings
;
48 LPTHREAD_START_ROUTINE SendMessageProc
;
49 } NOTIFY_CONTEXT
, *PNOTIFY_CONTEXT
;
51 #define QUERY_RESULT_ABORT 0
52 #define QUERY_RESULT_CONTINUE 1
53 #define QUERY_RESULT_TIMEOUT 2
54 #define QUERY_RESULT_ERROR 3
55 #define QUERY_RESULT_FORCE 4
57 typedef void (WINAPI
*INITCOMMONCONTROLS_PROC
)(void);
59 typedef struct tagMESSAGE_CONTEXT
66 } MESSAGE_CONTEXT
, *PMESSAGE_CONTEXT
;
68 typedef struct tagPROCESS_ENUM_CONTEXT
71 PCSR_PROCESS
*ProcessData
;
72 TOKEN_ORIGIN TokenOrigin
;
75 } PROCESS_ENUM_CONTEXT
, *PPROCESS_ENUM_CONTEXT
;
78 /* FUNCTIONS ******************************************************************/
82 Win32CsrEnumProcesses(CSRSS_ENUM_PROCESS_PROC EnumProc,
85 return CsrEnumProcesses(EnumProc, Context);
90 UpdateProgressBar(HWND ProgressBar
, PNOTIFY_CONTEXT NotifyContext
)
94 Passed
= GetTickCount() - NotifyContext
->StartTime
;
95 Passed
-= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
96 if (NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
< Passed
)
98 Passed
= NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
;
100 SendMessageW(ProgressBar
, PBM_SETPOS
, Passed
/ 2, 0);
103 static INT_PTR CALLBACK
104 EndNowDlgProc(HWND Dlg
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
107 PNOTIFY_CONTEXT NotifyContext
;
116 NotifyContext
= (PNOTIFY_CONTEXT
) lParam
;
117 NotifyContext
->EndNowResult
= QUERY_RESULT_ABORT
;
118 SetWindowLongPtrW(Dlg
, DWLP_USER
, (LONG_PTR
) lParam
);
119 TitleLength
= SendMessageW(NotifyContext
->WndClient
, WM_GETTEXTLENGTH
,
121 GetWindowTextLengthW(Dlg
);
122 Title
= HeapAlloc(UserServerHeap
, 0, (TitleLength
+ 1) * sizeof(WCHAR
));
125 Len
= GetWindowTextW(Dlg
, Title
, TitleLength
+ 1);
126 SendMessageW(NotifyContext
->WndClient
, WM_GETTEXT
,
127 TitleLength
+ 1 - Len
, (LPARAM
) (Title
+ Len
));
128 SetWindowTextW(Dlg
, Title
);
129 HeapFree(UserServerHeap
, 0, Title
);
131 ProgressBar
= GetDlgItem(Dlg
, IDC_PROGRESS
);
132 SendMessageW(ProgressBar
, PBM_SETRANGE32
, 0,
133 NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
/ 2);
134 UpdateProgressBar(ProgressBar
, NotifyContext
);
135 SetTimer(Dlg
, 0, 200, NULL
);
140 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
141 ProgressBar
= GetDlgItem(Dlg
, IDC_PROGRESS
);
142 UpdateProgressBar(ProgressBar
, NotifyContext
);
147 if (BN_CLICKED
== HIWORD(wParam
) && IDC_END_NOW
== LOWORD(wParam
))
149 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
150 NotifyContext
->EndNowResult
= QUERY_RESULT_FORCE
;
151 SendMessageW(Dlg
, WM_CLOSE
, 0, 0);
166 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
167 NotifyContext
->Dlg
= NULL
;
169 PostQuitMessage(NotifyContext
->EndNowResult
);
182 CallInitCommonControls()
184 static BOOL Initialized
= FALSE
;
186 INITCOMMONCONTROLS_PROC InitProc
;
188 if (Initialized
) return;
190 Lib
= LoadLibraryW(L
"COMCTL32.DLL");
191 if (NULL
== Lib
) return;
193 InitProc
= (INITCOMMONCONTROLS_PROC
) GetProcAddress(Lib
, "InitCommonControls");
194 if (NULL
== InitProc
) return;
202 EndNowThreadProc(LPVOID Parameter
)
204 PNOTIFY_CONTEXT NotifyContext
= (PNOTIFY_CONTEXT
) Parameter
;
207 SetThreadDesktop(NotifyContext
->Desktop
);
208 SwitchDesktop(NotifyContext
->Desktop
);
209 CallInitCommonControls();
210 NotifyContext
->Dlg
= CreateDialogParam(UserServerDllInstance
,
211 MAKEINTRESOURCE(IDD_END_NOW
), NULL
,
212 EndNowDlgProc
, (LPARAM
) NotifyContext
);
213 if (NULL
== NotifyContext
->Dlg
)
217 ShowWindow(NotifyContext
->Dlg
, SW_SHOWNORMAL
);
219 while (GetMessageW(&Msg
, NULL
, 0, 0))
221 if (! IsDialogMessage(NotifyContext
->Dlg
, &Msg
))
223 TranslateMessage(&Msg
);
224 DispatchMessageW(&Msg
);
232 SendQueryEndSession(LPVOID Parameter
)
234 PMESSAGE_CONTEXT Context
= (PMESSAGE_CONTEXT
) Parameter
;
237 if (SendMessageTimeoutW(Context
->Wnd
, WM_QUERYENDSESSION
, Context
->wParam
,
238 Context
->lParam
, SMTO_NORMAL
, Context
->Timeout
,
241 return Result
? QUERY_RESULT_CONTINUE
: QUERY_RESULT_ABORT
;
244 return 0 == GetLastError() ? QUERY_RESULT_TIMEOUT
: QUERY_RESULT_ERROR
;
248 SendEndSession(LPVOID Parameter
)
250 PMESSAGE_CONTEXT Context
= (PMESSAGE_CONTEXT
) Parameter
;
255 if (SendMessageTimeoutW(Context
->Wnd
, WM_ENDSESSION
, Context
->wParam
,
256 Context
->lParam
, SMTO_NORMAL
, Context
->Timeout
,
259 return QUERY_RESULT_CONTINUE
;
261 return 0 == GetLastError() ? QUERY_RESULT_TIMEOUT
: QUERY_RESULT_ERROR
;
265 SendMessage(Context
->Wnd
, WM_ENDSESSION
, Context
->wParam
,
267 return QUERY_RESULT_CONTINUE
;
272 NotifyTopLevelEnum(HWND Wnd
, LPARAM lParam
)
274 PNOTIFY_CONTEXT NotifyContext
= (PNOTIFY_CONTEXT
) lParam
;
275 MESSAGE_CONTEXT MessageContext
;
277 DWORD Timeout
, WaitStatus
;
279 HANDLE MessageThread
;
282 if (0 == GetWindowThreadProcessId(Wnd
, &ProcessId
))
284 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
288 if (ProcessId
== NotifyContext
->ProcessId
)
290 Now
= GetTickCount();
291 if (0 == NotifyContext
->StartTime
)
293 NotifyContext
->StartTime
= Now
;
295 /* Note: Passed is computed correctly even when GetTickCount() wraps due
296 to unsigned arithmetic */
297 Passed
= Now
- NotifyContext
->StartTime
;
298 MessageContext
.Wnd
= Wnd
;
299 MessageContext
.Msg
= NotifyContext
->Msg
;
300 MessageContext
.wParam
= NotifyContext
->wParam
;
301 MessageContext
.lParam
= NotifyContext
->lParam
;
302 MessageContext
.Timeout
= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
303 if (! NotifyContext
->ShutdownSettings
->AutoEndTasks
)
305 MessageContext
.Timeout
+= NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
;
307 if (Passed
< MessageContext
.Timeout
)
309 MessageContext
.Timeout
-= Passed
;
310 MessageThread
= CreateThread(NULL
, 0, NotifyContext
->SendMessageProc
,
311 (LPVOID
) &MessageContext
, 0, NULL
);
312 if (NULL
== MessageThread
)
314 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
317 Timeout
= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
318 if (Passed
< Timeout
)
321 WaitStatus
= WaitForSingleObjectEx(MessageThread
, Timeout
, FALSE
);
325 WaitStatus
= WAIT_TIMEOUT
;
327 if (WAIT_TIMEOUT
== WaitStatus
)
329 NotifyContext
->WndClient
= Wnd
;
330 if (NULL
== NotifyContext
->UIThread
&& NotifyContext
->ShowUI
)
332 NotifyContext
->UIThread
= CreateThread(NULL
, 0,
334 (LPVOID
) NotifyContext
,
337 Threads
[0] = MessageThread
;
338 Threads
[1] = NotifyContext
->UIThread
;
339 WaitStatus
= WaitForMultipleObjectsEx(NULL
== NotifyContext
->UIThread
?
341 Threads
, FALSE
, INFINITE
,
343 if (WAIT_OBJECT_0
== WaitStatus
)
345 if (! GetExitCodeThread(MessageThread
, &NotifyContext
->QueryResult
))
347 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
350 else if (WAIT_OBJECT_0
+ 1 == WaitStatus
)
352 if (! GetExitCodeThread(NotifyContext
->UIThread
,
353 &NotifyContext
->QueryResult
))
355 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
360 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
362 if (WAIT_OBJECT_0
!= WaitStatus
)
364 TerminateThread(MessageThread
, QUERY_RESULT_TIMEOUT
);
367 else if (WAIT_OBJECT_0
== WaitStatus
)
369 if (! GetExitCodeThread(MessageThread
,
370 &NotifyContext
->QueryResult
))
372 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
377 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
379 CloseHandle(MessageThread
);
383 NotifyContext
->QueryResult
= QUERY_RESULT_TIMEOUT
;
387 return QUERY_RESULT_CONTINUE
== NotifyContext
->QueryResult
;
391 NotifyDesktopEnum(LPWSTR DesktopName
, LPARAM lParam
)
393 PNOTIFY_CONTEXT Context
= (PNOTIFY_CONTEXT
) lParam
;
395 Context
->Desktop
= OpenDesktopW(DesktopName
, 0, FALSE
,
396 DESKTOP_ENUMERATE
| DESKTOP_SWITCHDESKTOP
);
397 if (NULL
== Context
->Desktop
)
399 DPRINT1("OpenDesktop failed with error %d\n", GetLastError());
400 Context
->QueryResult
= QUERY_RESULT_ERROR
;
404 Context
->OldDesktop
= GetThreadDesktop(GetCurrentThreadId());
405 SwitchDesktop(Context
->Desktop
);
407 EnumDesktopWindows(Context
->Desktop
, NotifyTopLevelEnum
, lParam
);
409 SwitchDesktop(Context
->OldDesktop
);
411 CloseDesktop(Context
->Desktop
);
413 return QUERY_RESULT_CONTINUE
== Context
->QueryResult
;
417 NotifyTopLevelWindows(PNOTIFY_CONTEXT Context
)
419 HWINSTA WindowStation
;
421 WindowStation
= GetProcessWindowStation();
422 if (NULL
== WindowStation
)
424 DPRINT1("GetProcessWindowStation failed with error %d\n", GetLastError());
428 EnumDesktopsW(WindowStation
, NotifyDesktopEnum
, (LPARAM
) Context
);
434 DtbgIsDesktopVisible(VOID
)
436 return !((BOOL
)NtUserCallNoParam(NOPARAM_ROUTINE_ISCONSOLEMODE
));
439 /* TODO: Find an other way to do it. */
442 ConioConsoleCtrlEventTimeout(DWORD Event
, PCSR_PROCESS ProcessData
, DWORD Timeout
)
446 DPRINT("ConioConsoleCtrlEvent Parent ProcessId = %x\n", ProcessData
->ClientId
.UniqueProcess
);
448 if (ProcessData
->CtrlDispatcher
)
451 Thread
= CreateRemoteThread(ProcessData
->ProcessHandle
, NULL
, 0,
452 (LPTHREAD_START_ROUTINE
) ProcessData
->CtrlDispatcher
,
453 UlongToPtr(Event
), 0, NULL
);
456 DPRINT1("Failed thread creation (Error: 0x%x)\n", GetLastError());
459 WaitForSingleObject(Thread
, Timeout
);
464 /************************************************/
467 NotifyAndTerminateProcess(PCSR_PROCESS 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 // TODO: Find an other way whether or not the process has a console.
481 if (NULL
!= ProcessData
->Console
)
483 ConioConsoleCtrlEventTimeout(CTRL_LOGOFF_EVENT
, ProcessData
,
484 ShutdownSettings
->WaitToKillAppTimeout
);
489 Context
.ProcessId
= (DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
;
491 Context
.lParam
= (0 != (Flags
& EWX_INTERNAL_FLAG_LOGOFF
) ?
492 ENDSESSION_LOGOFF
: 0);
493 Context
.StartTime
= 0;
494 Context
.UIThread
= NULL
;
495 Context
.ShowUI
= DtbgIsDesktopVisible();
497 Context
.ShutdownSettings
= ShutdownSettings
;
498 Context
.SendMessageProc
= SendQueryEndSession
;
500 NotifyTopLevelWindows(&Context
);
502 Context
.wParam
= (QUERY_RESULT_ABORT
!= Context
.QueryResult
);
503 Context
.lParam
= (0 != (Flags
& EWX_INTERNAL_FLAG_LOGOFF
) ?
504 ENDSESSION_LOGOFF
: 0);
505 Context
.SendMessageProc
= SendEndSession
;
506 Context
.ShowUI
= DtbgIsDesktopVisible() &&
507 (QUERY_RESULT_ABORT
!= Context
.QueryResult
);
508 QueryResult
= Context
.QueryResult
;
509 Context
.QueryResult
= QUERY_RESULT_CONTINUE
;
511 NotifyTopLevelWindows(&Context
);
513 if (NULL
!= Context
.UIThread
)
515 if (NULL
!= Context
.Dlg
)
517 SendMessageW(Context
.Dlg
, WM_CLOSE
, 0, 0);
521 TerminateThread(Context
.UIThread
, QUERY_RESULT_ERROR
);
523 CloseHandle(Context
.UIThread
);
527 if (QUERY_RESULT_ABORT
== QueryResult
)
533 /* Terminate this process */
534 Process
= OpenProcess(PROCESS_TERMINATE
, FALSE
,
535 (DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
);
538 DPRINT1("Unable to open process %d, error %d\n", ProcessData
->ClientId
.UniqueProcess
,
542 TerminateProcess(Process
, 0);
543 CloseHandle(Process
);
549 static NTSTATUS WINAPI
550 ExitReactosProcessEnum(PCSR_PROCESS ProcessData
, PVOID Data
)
556 PPROCESS_ENUM_CONTEXT Context
= (PPROCESS_ENUM_CONTEXT
) Data
;
557 PCSR_PROCESS
*NewData
;
559 /* Do not kill winlogon or csrss */
560 if ((DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
== Context
->CsrssProcess
||
561 ProcessData
->ClientId
.UniqueProcess
== LogonProcessId
)
563 return STATUS_SUCCESS
;
566 /* Get the login session of this process */
567 Process
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
,
568 (DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
);
571 DPRINT1("Unable to open process %d, error %d\n", ProcessData
->ClientId
.UniqueProcess
,
573 return STATUS_UNSUCCESSFUL
;
576 if (! OpenProcessToken(Process
, TOKEN_QUERY
, &Token
))
578 DPRINT1("Unable to open token for process %d, error %d\n",
579 ProcessData
->ClientId
.UniqueProcess
, GetLastError());
580 CloseHandle(Process
);
581 return STATUS_UNSUCCESSFUL
;
583 CloseHandle(Process
);
585 if (! GetTokenInformation(Token
, TokenOrigin
, &Origin
,
586 sizeof(TOKEN_ORIGIN
), &ReturnLength
))
588 DPRINT1("GetTokenInformation failed for process %d with error %d\n",
589 ProcessData
->ClientId
.UniqueProcess
, GetLastError());
591 return STATUS_UNSUCCESSFUL
;
595 /* This process will be killed if it's in the correct logon session */
596 if (RtlEqualLuid(&(Context
->TokenOrigin
.OriginatingLogonSession
),
597 &(Origin
.OriginatingLogonSession
)))
599 /* Kill the shell process last */
600 if ((DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
== Context
->ShellProcess
)
602 ProcessData
->ShutdownLevel
= 0;
604 NewData
= HeapAlloc(UserServerHeap
, 0, (Context
->ProcessCount
+ 1)
605 * sizeof(PCSR_PROCESS
));
608 return STATUS_NO_MEMORY
;
610 if (0 != Context
->ProcessCount
)
612 memcpy(NewData
, Context
->ProcessData
,
613 Context
->ProcessCount
* sizeof(PCSR_PROCESS
));
614 HeapFree(UserServerHeap
, 0, Context
->ProcessData
);
616 Context
->ProcessData
= NewData
;
617 Context
->ProcessData
[Context
->ProcessCount
] = ProcessData
;
618 Context
->ProcessCount
++;
621 return STATUS_SUCCESS
;
626 ProcessDataCompare(const void *Elem1
, const void *Elem2
)
628 const PCSR_PROCESS
*ProcessData1
= (PCSR_PROCESS
*) Elem1
;
629 const PCSR_PROCESS
*ProcessData2
= (PCSR_PROCESS
*) Elem2
;
631 if ((*ProcessData1
)->ShutdownLevel
< (*ProcessData2
)->ShutdownLevel
)
635 else if ((*ProcessData2
)->ShutdownLevel
< (*ProcessData1
)->ShutdownLevel
)
639 else if ((*ProcessData1
)->ClientId
.UniqueProcess
< (*ProcessData2
)->ClientId
.UniqueProcess
)
643 else if ((*ProcessData2
)->ClientId
.UniqueProcess
< (*ProcessData1
)->ClientId
.UniqueProcess
)
651 static DWORD FASTCALL
652 GetShutdownSettings(HKEY DesktopKey
, LPCWSTR ValueName
, DWORD DefaultValue
)
654 BYTE ValueBuffer
[16];
658 UNICODE_STRING StringValue
;
661 ValueSize
= sizeof(ValueBuffer
);
662 ErrCode
= RegQueryValueExW(DesktopKey
, ValueName
, NULL
, &Type
, ValueBuffer
,
664 if (ERROR_SUCCESS
!= ErrCode
)
666 DPRINT("GetShutdownSettings for %S failed with error code %ld\n",
673 RtlInitUnicodeString(&StringValue
, (LPCWSTR
) ValueBuffer
);
674 if (! NT_SUCCESS(RtlUnicodeStringToInteger(&StringValue
, 10, &Value
)))
676 DPRINT1("Unable to convert value %S for setting %S\n",
677 StringValue
.Buffer
, ValueName
);
680 return (DWORD
) Value
;
682 else if (REG_DWORD
== Type
)
684 return *((DWORD
*) ValueBuffer
);
687 DPRINT1("Unexpected registry type %d for setting %S\n", Type
, ValueName
);
692 LoadShutdownSettings(PSID Sid
, PSHUTDOWN_SETTINGS ShutdownSettings
)
694 static WCHAR Subkey
[] = L
"\\Control Panel\\Desktop";
696 WCHAR InitialKeyName
[128];
701 ShutdownSettings
->AutoEndTasks
= DEFAULT_AUTO_END_TASKS
;
702 ShutdownSettings
->HungAppTimeout
= DEFAULT_HUNG_APP_TIMEOUT
;
703 ShutdownSettings
->WaitToKillAppTimeout
= DEFAULT_WAIT_TO_KILL_APP_TIMEOUT
;
705 if (! ConvertSidToStringSidW(Sid
, &StringSid
))
707 DPRINT1("ConvertSidToStringSid failed with error %d, using default shutdown settings\n",
711 if (wcslen(StringSid
) + wcslen(Subkey
) + 1 <=
712 sizeof(InitialKeyName
) / sizeof(WCHAR
))
714 KeyName
= InitialKeyName
;
718 KeyName
= HeapAlloc(UserServerHeap
, 0,
719 (wcslen(StringSid
) + wcslen(Subkey
) + 1) *
723 DPRINT1("Failed to allocate memory, using default shutdown settings\n");
724 LocalFree(StringSid
);
728 wcscat(wcscpy(KeyName
, StringSid
), Subkey
);
729 LocalFree(StringSid
);
731 ErrCode
= RegOpenKeyExW(HKEY_USERS
, KeyName
, 0, KEY_QUERY_VALUE
, &DesktopKey
);
732 if (KeyName
!= InitialKeyName
)
734 HeapFree(UserServerHeap
, 0, KeyName
);
736 if (ERROR_SUCCESS
!= ErrCode
)
738 DPRINT1("RegOpenKeyEx failed with error %ld, using default shutdown settings\n", ErrCode
);
742 ShutdownSettings
->AutoEndTasks
= (BOOL
) GetShutdownSettings(DesktopKey
, L
"AutoEndTasks",
743 (DWORD
) DEFAULT_AUTO_END_TASKS
);
744 ShutdownSettings
->HungAppTimeout
= GetShutdownSettings(DesktopKey
,
746 DEFAULT_HUNG_APP_TIMEOUT
);
747 ShutdownSettings
->WaitToKillAppTimeout
= GetShutdownSettings(DesktopKey
,
748 L
"WaitToKillAppTimeout",
749 DEFAULT_WAIT_TO_KILL_APP_TIMEOUT
);
751 RegCloseKey(DesktopKey
);
754 static NTSTATUS FASTCALL
755 InternalExitReactos(DWORD ProcessId
, DWORD ThreadId
, UINT Flags
)
760 PROCESS_ENUM_CONTEXT Context
;
764 char FixedUserInfo
[64];
765 TOKEN_USER
*UserInfo
;
766 SHUTDOWN_SETTINGS ShutdownSettings
;
768 if (ProcessId
!= (DWORD_PTR
) LogonProcessId
)
770 DPRINT1("Internal ExitWindowsEx call not from winlogon\n");
771 return STATUS_ACCESS_DENIED
;
774 DPRINT1("FIXME: Need to close all user processes!\n");
775 return STATUS_SUCCESS
;
777 CallerThread
= OpenThread(THREAD_QUERY_INFORMATION
, FALSE
, ThreadId
);
778 if (NULL
== CallerThread
)
780 DPRINT1("OpenThread failed with error %d\n", GetLastError());
781 return STATUS_UNSUCCESSFUL
;
783 if (! OpenThreadToken(CallerThread
, TOKEN_QUERY
, FALSE
, &CallerToken
))
785 DPRINT1("OpenThreadToken failed with error %d\n", GetLastError());
786 CloseHandle(CallerThread
);
787 return STATUS_UNSUCCESSFUL
;
789 CloseHandle(CallerThread
);
791 Context
.ProcessCount
= 0;
792 Context
.ProcessData
= NULL
;
793 if (! GetTokenInformation(CallerToken
, TokenOrigin
, &Context
.TokenOrigin
,
794 sizeof(TOKEN_ORIGIN
), &ReturnLength
))
796 DPRINT1("GetTokenInformation failed with error %d\n", GetLastError());
797 CloseHandle(CallerToken
);
798 return STATUS_UNSUCCESSFUL
;
800 if (! GetTokenInformation(CallerToken
, TokenUser
, FixedUserInfo
,
801 sizeof(FixedUserInfo
), &ReturnLength
))
803 if (sizeof(FixedUserInfo
) < ReturnLength
)
805 UserInfo
= HeapAlloc(UserServerHeap
, 0, ReturnLength
);
806 if (NULL
== UserInfo
)
808 DPRINT1("Unable to allocate %u bytes for user info\n",
809 (unsigned) ReturnLength
);
810 CloseHandle(CallerToken
);
811 return STATUS_NO_MEMORY
;
813 if (! GetTokenInformation(CallerToken
, TokenUser
, UserInfo
,
814 ReturnLength
, &ReturnLength
))
816 DPRINT1("GetTokenInformation failed with error %d\n",
818 HeapFree(UserServerHeap
, 0, UserInfo
);
819 CloseHandle(CallerToken
);
820 return STATUS_UNSUCCESSFUL
;
825 DPRINT1("GetTokenInformation failed with error %d\n", GetLastError());
826 CloseHandle(CallerToken
);
827 return STATUS_UNSUCCESSFUL
;
832 UserInfo
= (TOKEN_USER
*) FixedUserInfo
;
834 CloseHandle(CallerToken
);
835 LoadShutdownSettings(UserInfo
->User
.Sid
, &ShutdownSettings
);
836 if (UserInfo
!= (TOKEN_USER
*) FixedUserInfo
)
838 HeapFree(UserServerHeap
, 0, UserInfo
);
840 Context
.CsrssProcess
= GetCurrentProcessId();
841 ShellWnd
= GetShellWindow();
842 if (NULL
== ShellWnd
)
844 DPRINT("No shell present\n");
845 Context
.ShellProcess
= 0;
847 else if (0 == GetWindowThreadProcessId(ShellWnd
, &Context
.ShellProcess
))
849 DPRINT1("Can't get process id of shell window\n");
850 Context
.ShellProcess
= 0;
853 // Status = Win32CsrEnumProcesses(ExitReactosProcessEnum, &Context);
854 if (! NT_SUCCESS(Status
))
856 DPRINT1("Failed to enumerate registered processes, status 0x%x\n",
858 if (NULL
!= Context
.ProcessData
)
860 HeapFree(UserServerHeap
, 0, Context
.ProcessData
);
865 qsort(Context
.ProcessData
, Context
.ProcessCount
, sizeof(PCSR_PROCESS
),
868 /* Terminate processes, stop if we find one kicking and screaming it doesn't
870 Status
= STATUS_SUCCESS
;
871 for (ProcessIndex
= 0;
872 ProcessIndex
< Context
.ProcessCount
&& NT_SUCCESS(Status
);
875 if (! NotifyAndTerminateProcess(Context
.ProcessData
[ProcessIndex
],
876 &ShutdownSettings
, Flags
))
878 Status
= STATUS_REQUEST_ABORTED
;
883 if (NULL
!= Context
.ProcessData
)
885 HeapFree(UserServerHeap
, 0, Context
.ProcessData
);
891 static NTSTATUS FASTCALL
892 UserExitReactos(DWORD UserProcessId
, UINT Flags
)
896 /* FIXME Inside 2000 says we should impersonate the caller here */
897 Status
= NtUserCallTwoParam(UserProcessId
, Flags
, TWOPARAM_ROUTINE_EXITREACTOS
);
899 /* If the message isn't handled, the return value is 0, so 0 doesn't indicate
900 success. Success is indicated by a 1 return value, if anything besides 0
901 or 1 it's a NTSTATUS value */
904 Status
= STATUS_SUCCESS
;
906 else if (0 == Status
)
908 Status
= STATUS_NOT_IMPLEMENTED
;
915 /* PUBLIC SERVER APIS *********************************************************/
917 CSR_API(SrvExitWindowsEx
)
919 PUSER_EXIT_REACTOS ExitReactosRequest
= &((PUSER_API_MESSAGE
)ApiMessage
)->Data
.ExitReactosRequest
;
921 if (0 == (ExitReactosRequest
->Flags
& EWX_INTERNAL_FLAG
))
923 return UserExitReactos((DWORD_PTR
) ApiMessage
->Header
.ClientId
.UniqueProcess
,
924 ExitReactosRequest
->Flags
);
928 return InternalExitReactos((DWORD_PTR
) ApiMessage
->Header
.ClientId
.UniqueProcess
,
929 (DWORD_PTR
) ApiMessage
->Header
.ClientId
.UniqueThread
,
930 ExitReactosRequest
->Flags
);