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 *******************************************************************/
26 /* GLOBALS ********************************************************************/
28 typedef struct tagSHUTDOWN_SETTINGS
32 DWORD WaitToKillAppTimeout
;
33 } SHUTDOWN_SETTINGS
, *PSHUTDOWN_SETTINGS
;
35 #define DEFAULT_AUTO_END_TASKS FALSE
36 #define DEFAULT_HUNG_APP_TIMEOUT 5000
37 #define DEFAULT_WAIT_TO_KILL_APP_TIMEOUT 20000
39 typedef struct tagNOTIFY_CONTEXT
54 PSHUTDOWN_SETTINGS ShutdownSettings
;
55 LPTHREAD_START_ROUTINE SendMessageProc
;
56 } NOTIFY_CONTEXT
, *PNOTIFY_CONTEXT
;
58 #define QUERY_RESULT_ABORT 0
59 #define QUERY_RESULT_CONTINUE 1
60 #define QUERY_RESULT_TIMEOUT 2
61 #define QUERY_RESULT_ERROR 3
62 #define QUERY_RESULT_FORCE 4
64 typedef void (WINAPI
*INITCOMMONCONTROLS_PROC
)(void);
66 typedef struct tagMESSAGE_CONTEXT
73 } MESSAGE_CONTEXT
, *PMESSAGE_CONTEXT
;
75 typedef struct tagPROCESS_ENUM_CONTEXT
78 PCSR_PROCESS
*ProcessData
;
79 TOKEN_ORIGIN TokenOrigin
;
82 } PROCESS_ENUM_CONTEXT
, *PPROCESS_ENUM_CONTEXT
;
85 /* FUNCTIONS ******************************************************************/
89 Win32CsrEnumProcesses(CSRSS_ENUM_PROCESS_PROC EnumProc,
92 return CsrEnumProcesses(EnumProc, Context);
97 UpdateProgressBar(HWND ProgressBar
, PNOTIFY_CONTEXT NotifyContext
)
101 Passed
= GetTickCount() - NotifyContext
->StartTime
;
102 Passed
-= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
103 if (NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
< Passed
)
105 Passed
= NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
;
107 SendMessageW(ProgressBar
, PBM_SETPOS
, Passed
/ 2, 0);
110 static INT_PTR CALLBACK
111 EndNowDlgProc(HWND Dlg
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
114 PNOTIFY_CONTEXT NotifyContext
;
123 NotifyContext
= (PNOTIFY_CONTEXT
) lParam
;
124 NotifyContext
->EndNowResult
= QUERY_RESULT_ABORT
;
125 SetWindowLongPtrW(Dlg
, DWLP_USER
, (LONG_PTR
) lParam
);
126 TitleLength
= SendMessageW(NotifyContext
->WndClient
, WM_GETTEXTLENGTH
,
128 GetWindowTextLengthW(Dlg
);
129 Title
= HeapAlloc(UserServerHeap
, 0, (TitleLength
+ 1) * sizeof(WCHAR
));
132 Len
= GetWindowTextW(Dlg
, Title
, TitleLength
+ 1);
133 SendMessageW(NotifyContext
->WndClient
, WM_GETTEXT
,
134 TitleLength
+ 1 - Len
, (LPARAM
) (Title
+ Len
));
135 SetWindowTextW(Dlg
, Title
);
136 HeapFree(UserServerHeap
, 0, Title
);
138 ProgressBar
= GetDlgItem(Dlg
, IDC_PROGRESS
);
139 SendMessageW(ProgressBar
, PBM_SETRANGE32
, 0,
140 NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
/ 2);
141 UpdateProgressBar(ProgressBar
, NotifyContext
);
142 SetTimer(Dlg
, 0, 200, NULL
);
147 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
148 ProgressBar
= GetDlgItem(Dlg
, IDC_PROGRESS
);
149 UpdateProgressBar(ProgressBar
, NotifyContext
);
154 if (BN_CLICKED
== HIWORD(wParam
) && IDC_END_NOW
== LOWORD(wParam
))
156 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
157 NotifyContext
->EndNowResult
= QUERY_RESULT_FORCE
;
158 SendMessageW(Dlg
, WM_CLOSE
, 0, 0);
173 NotifyContext
= (PNOTIFY_CONTEXT
) GetWindowLongPtrW(Dlg
, DWLP_USER
);
174 NotifyContext
->Dlg
= NULL
;
176 PostQuitMessage(NotifyContext
->EndNowResult
);
189 CallInitCommonControls()
191 static BOOL Initialized
= FALSE
;
193 INITCOMMONCONTROLS_PROC InitProc
;
195 if (Initialized
) return;
197 Lib
= LoadLibraryW(L
"COMCTL32.DLL");
198 if (NULL
== Lib
) return;
200 InitProc
= (INITCOMMONCONTROLS_PROC
) GetProcAddress(Lib
, "InitCommonControls");
201 if (NULL
== InitProc
) return;
209 EndNowThreadProc(LPVOID Parameter
)
211 PNOTIFY_CONTEXT NotifyContext
= (PNOTIFY_CONTEXT
) Parameter
;
214 SetThreadDesktop(NotifyContext
->Desktop
);
215 SwitchDesktop(NotifyContext
->Desktop
);
216 CallInitCommonControls();
217 NotifyContext
->Dlg
= CreateDialogParam(UserServerDllInstance
,
218 MAKEINTRESOURCE(IDD_END_NOW
), NULL
,
219 EndNowDlgProc
, (LPARAM
) NotifyContext
);
220 if (NULL
== NotifyContext
->Dlg
)
224 ShowWindow(NotifyContext
->Dlg
, SW_SHOWNORMAL
);
226 while (GetMessageW(&Msg
, NULL
, 0, 0))
228 if (! IsDialogMessage(NotifyContext
->Dlg
, &Msg
))
230 TranslateMessage(&Msg
);
231 DispatchMessageW(&Msg
);
239 SendQueryEndSession(LPVOID Parameter
)
241 PMESSAGE_CONTEXT Context
= (PMESSAGE_CONTEXT
) Parameter
;
244 if (SendMessageTimeoutW(Context
->Wnd
, WM_QUERYENDSESSION
, Context
->wParam
,
245 Context
->lParam
, SMTO_NORMAL
, Context
->Timeout
,
248 return Result
? QUERY_RESULT_CONTINUE
: QUERY_RESULT_ABORT
;
251 return 0 == GetLastError() ? QUERY_RESULT_TIMEOUT
: QUERY_RESULT_ERROR
;
255 SendEndSession(LPVOID Parameter
)
257 PMESSAGE_CONTEXT Context
= (PMESSAGE_CONTEXT
) Parameter
;
262 if (SendMessageTimeoutW(Context
->Wnd
, WM_ENDSESSION
, Context
->wParam
,
263 Context
->lParam
, SMTO_NORMAL
, Context
->Timeout
,
266 return QUERY_RESULT_CONTINUE
;
268 return 0 == GetLastError() ? QUERY_RESULT_TIMEOUT
: QUERY_RESULT_ERROR
;
272 SendMessage(Context
->Wnd
, WM_ENDSESSION
, Context
->wParam
,
274 return QUERY_RESULT_CONTINUE
;
279 NotifyTopLevelEnum(HWND Wnd
, LPARAM lParam
)
281 PNOTIFY_CONTEXT NotifyContext
= (PNOTIFY_CONTEXT
) lParam
;
282 MESSAGE_CONTEXT MessageContext
;
284 DWORD Timeout
, WaitStatus
;
286 HANDLE MessageThread
;
289 if (0 == GetWindowThreadProcessId(Wnd
, &ProcessId
))
291 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
295 if (ProcessId
== NotifyContext
->ProcessId
)
297 Now
= GetTickCount();
298 if (0 == NotifyContext
->StartTime
)
300 NotifyContext
->StartTime
= Now
;
302 /* Note: Passed is computed correctly even when GetTickCount() wraps due
303 to unsigned arithmetic */
304 Passed
= Now
- NotifyContext
->StartTime
;
305 MessageContext
.Wnd
= Wnd
;
306 MessageContext
.Msg
= NotifyContext
->Msg
;
307 MessageContext
.wParam
= NotifyContext
->wParam
;
308 MessageContext
.lParam
= NotifyContext
->lParam
;
309 MessageContext
.Timeout
= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
310 if (! NotifyContext
->ShutdownSettings
->AutoEndTasks
)
312 MessageContext
.Timeout
+= NotifyContext
->ShutdownSettings
->WaitToKillAppTimeout
;
314 if (Passed
< MessageContext
.Timeout
)
316 MessageContext
.Timeout
-= Passed
;
317 MessageThread
= CreateThread(NULL
, 0, NotifyContext
->SendMessageProc
,
318 (LPVOID
) &MessageContext
, 0, NULL
);
319 if (NULL
== MessageThread
)
321 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
324 Timeout
= NotifyContext
->ShutdownSettings
->HungAppTimeout
;
325 if (Passed
< Timeout
)
328 WaitStatus
= WaitForSingleObjectEx(MessageThread
, Timeout
, FALSE
);
332 WaitStatus
= WAIT_TIMEOUT
;
334 if (WAIT_TIMEOUT
== WaitStatus
)
336 NotifyContext
->WndClient
= Wnd
;
337 if (NULL
== NotifyContext
->UIThread
&& NotifyContext
->ShowUI
)
339 NotifyContext
->UIThread
= CreateThread(NULL
, 0,
341 (LPVOID
) NotifyContext
,
344 Threads
[0] = MessageThread
;
345 Threads
[1] = NotifyContext
->UIThread
;
346 WaitStatus
= WaitForMultipleObjectsEx(NULL
== NotifyContext
->UIThread
?
348 Threads
, FALSE
, INFINITE
,
350 if (WAIT_OBJECT_0
== WaitStatus
)
352 if (! GetExitCodeThread(MessageThread
, &NotifyContext
->QueryResult
))
354 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
357 else if (WAIT_OBJECT_0
+ 1 == WaitStatus
)
359 if (! GetExitCodeThread(NotifyContext
->UIThread
,
360 &NotifyContext
->QueryResult
))
362 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
367 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
369 if (WAIT_OBJECT_0
!= WaitStatus
)
371 TerminateThread(MessageThread
, QUERY_RESULT_TIMEOUT
);
374 else if (WAIT_OBJECT_0
== WaitStatus
)
376 if (! GetExitCodeThread(MessageThread
,
377 &NotifyContext
->QueryResult
))
379 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
384 NotifyContext
->QueryResult
= QUERY_RESULT_ERROR
;
386 CloseHandle(MessageThread
);
390 NotifyContext
->QueryResult
= QUERY_RESULT_TIMEOUT
;
394 return QUERY_RESULT_CONTINUE
== NotifyContext
->QueryResult
;
398 NotifyDesktopEnum(LPWSTR DesktopName
, LPARAM lParam
)
400 PNOTIFY_CONTEXT Context
= (PNOTIFY_CONTEXT
) lParam
;
402 Context
->Desktop
= OpenDesktopW(DesktopName
, 0, FALSE
,
403 DESKTOP_ENUMERATE
| DESKTOP_SWITCHDESKTOP
);
404 if (NULL
== Context
->Desktop
)
406 DPRINT1("OpenDesktop failed with error %d\n", GetLastError());
407 Context
->QueryResult
= QUERY_RESULT_ERROR
;
411 Context
->OldDesktop
= GetThreadDesktop(GetCurrentThreadId());
412 SwitchDesktop(Context
->Desktop
);
414 EnumDesktopWindows(Context
->Desktop
, NotifyTopLevelEnum
, lParam
);
416 SwitchDesktop(Context
->OldDesktop
);
418 CloseDesktop(Context
->Desktop
);
420 return QUERY_RESULT_CONTINUE
== Context
->QueryResult
;
424 NotifyTopLevelWindows(PNOTIFY_CONTEXT Context
)
426 HWINSTA WindowStation
;
428 WindowStation
= GetProcessWindowStation();
429 if (NULL
== WindowStation
)
431 DPRINT1("GetProcessWindowStation failed with error %d\n", GetLastError());
435 EnumDesktopsW(WindowStation
, NotifyDesktopEnum
, (LPARAM
) Context
);
441 DtbgIsDesktopVisible(VOID
)
443 return !((BOOL
)NtUserCallNoParam(NOPARAM_ROUTINE_ISCONSOLEMODE
));
446 /* TODO: Find an other way to do it. */
449 ConioConsoleCtrlEventTimeout(DWORD Event
, PCSR_PROCESS ProcessData
, DWORD Timeout
)
453 DPRINT("ConioConsoleCtrlEvent Parent ProcessId = %x\n", ProcessData
->ClientId
.UniqueProcess
);
455 if (ProcessData
->CtrlDispatcher
)
458 Thread
= CreateRemoteThread(ProcessData
->ProcessHandle
, NULL
, 0,
459 (LPTHREAD_START_ROUTINE
) ProcessData
->CtrlDispatcher
,
460 UlongToPtr(Event
), 0, NULL
);
463 DPRINT1("Failed thread creation (Error: 0x%x)\n", GetLastError());
466 WaitForSingleObject(Thread
, Timeout
);
471 /************************************************/
474 NotifyAndTerminateProcess(PCSR_PROCESS ProcessData
,
475 PSHUTDOWN_SETTINGS ShutdownSettings
,
478 NOTIFY_CONTEXT Context
;
480 DWORD QueryResult
= QUERY_RESULT_CONTINUE
;
482 Context
.QueryResult
= QUERY_RESULT_CONTINUE
;
484 if (0 == (Flags
& EWX_FORCE
))
486 // TODO: Find an other way whether or not the process has a console.
488 if (NULL
!= ProcessData
->Console
)
490 ConioConsoleCtrlEventTimeout(CTRL_LOGOFF_EVENT
, ProcessData
,
491 ShutdownSettings
->WaitToKillAppTimeout
);
496 Context
.ProcessId
= (DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
;
498 Context
.lParam
= (0 != (Flags
& EWX_INTERNAL_FLAG_LOGOFF
) ?
499 ENDSESSION_LOGOFF
: 0);
500 Context
.StartTime
= 0;
501 Context
.UIThread
= NULL
;
502 Context
.ShowUI
= DtbgIsDesktopVisible();
504 Context
.ShutdownSettings
= ShutdownSettings
;
505 Context
.SendMessageProc
= SendQueryEndSession
;
507 NotifyTopLevelWindows(&Context
);
509 Context
.wParam
= (QUERY_RESULT_ABORT
!= Context
.QueryResult
);
510 Context
.lParam
= (0 != (Flags
& EWX_INTERNAL_FLAG_LOGOFF
) ?
511 ENDSESSION_LOGOFF
: 0);
512 Context
.SendMessageProc
= SendEndSession
;
513 Context
.ShowUI
= DtbgIsDesktopVisible() &&
514 (QUERY_RESULT_ABORT
!= Context
.QueryResult
);
515 QueryResult
= Context
.QueryResult
;
516 Context
.QueryResult
= QUERY_RESULT_CONTINUE
;
518 NotifyTopLevelWindows(&Context
);
520 if (NULL
!= Context
.UIThread
)
522 if (NULL
!= Context
.Dlg
)
524 SendMessageW(Context
.Dlg
, WM_CLOSE
, 0, 0);
528 TerminateThread(Context
.UIThread
, QUERY_RESULT_ERROR
);
530 CloseHandle(Context
.UIThread
);
534 if (QUERY_RESULT_ABORT
== QueryResult
)
540 /* Terminate this process */
541 Process
= OpenProcess(PROCESS_TERMINATE
, FALSE
,
542 (DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
);
545 DPRINT1("Unable to open process %d, error %d\n", ProcessData
->ClientId
.UniqueProcess
,
549 TerminateProcess(Process
, 0);
550 CloseHandle(Process
);
556 static NTSTATUS WINAPI
557 ExitReactosProcessEnum(PCSR_PROCESS ProcessData
, PVOID Data
)
563 PPROCESS_ENUM_CONTEXT Context
= (PPROCESS_ENUM_CONTEXT
) Data
;
564 PCSR_PROCESS
*NewData
;
566 /* Do not kill winlogon or csrss */
567 if ((DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
== Context
->CsrssProcess
||
568 ProcessData
->ClientId
.UniqueProcess
== LogonProcessId
)
570 return STATUS_SUCCESS
;
573 /* Get the login session of this process */
574 Process
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
,
575 (DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
);
578 DPRINT1("Unable to open process %d, error %d\n", ProcessData
->ClientId
.UniqueProcess
,
580 return STATUS_UNSUCCESSFUL
;
583 if (! OpenProcessToken(Process
, TOKEN_QUERY
, &Token
))
585 DPRINT1("Unable to open token for process %d, error %d\n",
586 ProcessData
->ClientId
.UniqueProcess
, GetLastError());
587 CloseHandle(Process
);
588 return STATUS_UNSUCCESSFUL
;
590 CloseHandle(Process
);
592 if (! GetTokenInformation(Token
, TokenOrigin
, &Origin
,
593 sizeof(TOKEN_ORIGIN
), &ReturnLength
))
595 DPRINT1("GetTokenInformation failed for process %d with error %d\n",
596 ProcessData
->ClientId
.UniqueProcess
, GetLastError());
598 return STATUS_UNSUCCESSFUL
;
602 /* This process will be killed if it's in the correct logon session */
603 if (RtlEqualLuid(&(Context
->TokenOrigin
.OriginatingLogonSession
),
604 &(Origin
.OriginatingLogonSession
)))
606 /* Kill the shell process last */
607 if ((DWORD_PTR
) ProcessData
->ClientId
.UniqueProcess
== Context
->ShellProcess
)
609 ProcessData
->ShutdownLevel
= 0;
611 NewData
= HeapAlloc(UserServerHeap
, 0, (Context
->ProcessCount
+ 1)
612 * sizeof(PCSR_PROCESS
));
615 return STATUS_NO_MEMORY
;
617 if (0 != Context
->ProcessCount
)
619 memcpy(NewData
, Context
->ProcessData
,
620 Context
->ProcessCount
* sizeof(PCSR_PROCESS
));
621 HeapFree(UserServerHeap
, 0, Context
->ProcessData
);
623 Context
->ProcessData
= NewData
;
624 Context
->ProcessData
[Context
->ProcessCount
] = ProcessData
;
625 Context
->ProcessCount
++;
628 return STATUS_SUCCESS
;
633 ProcessDataCompare(const void *Elem1
, const void *Elem2
)
635 const PCSR_PROCESS
*ProcessData1
= (PCSR_PROCESS
*) Elem1
;
636 const PCSR_PROCESS
*ProcessData2
= (PCSR_PROCESS
*) Elem2
;
638 if ((*ProcessData1
)->ShutdownLevel
< (*ProcessData2
)->ShutdownLevel
)
642 else if ((*ProcessData2
)->ShutdownLevel
< (*ProcessData1
)->ShutdownLevel
)
646 else if ((*ProcessData1
)->ClientId
.UniqueProcess
< (*ProcessData2
)->ClientId
.UniqueProcess
)
650 else if ((*ProcessData2
)->ClientId
.UniqueProcess
< (*ProcessData1
)->ClientId
.UniqueProcess
)
658 static DWORD FASTCALL
659 GetShutdownSettings(HKEY DesktopKey
, LPCWSTR ValueName
, DWORD DefaultValue
)
661 BYTE ValueBuffer
[16];
665 UNICODE_STRING StringValue
;
668 ValueSize
= sizeof(ValueBuffer
);
669 ErrCode
= RegQueryValueExW(DesktopKey
, ValueName
, NULL
, &Type
, ValueBuffer
,
671 if (ERROR_SUCCESS
!= ErrCode
)
673 DPRINT("GetShutdownSettings for %S failed with error code %ld\n",
680 RtlInitUnicodeString(&StringValue
, (LPCWSTR
) ValueBuffer
);
681 if (! NT_SUCCESS(RtlUnicodeStringToInteger(&StringValue
, 10, &Value
)))
683 DPRINT1("Unable to convert value %S for setting %S\n",
684 StringValue
.Buffer
, ValueName
);
687 return (DWORD
) Value
;
689 else if (REG_DWORD
== Type
)
691 return *((DWORD
*) ValueBuffer
);
694 DPRINT1("Unexpected registry type %d for setting %S\n", Type
, ValueName
);
699 LoadShutdownSettings(PSID Sid
, PSHUTDOWN_SETTINGS ShutdownSettings
)
701 static WCHAR Subkey
[] = L
"\\Control Panel\\Desktop";
703 WCHAR InitialKeyName
[128];
708 ShutdownSettings
->AutoEndTasks
= DEFAULT_AUTO_END_TASKS
;
709 ShutdownSettings
->HungAppTimeout
= DEFAULT_HUNG_APP_TIMEOUT
;
710 ShutdownSettings
->WaitToKillAppTimeout
= DEFAULT_WAIT_TO_KILL_APP_TIMEOUT
;
712 if (! ConvertSidToStringSidW(Sid
, &StringSid
))
714 DPRINT1("ConvertSidToStringSid failed with error %d, using default shutdown settings\n",
718 if (wcslen(StringSid
) + wcslen(Subkey
) + 1 <=
719 sizeof(InitialKeyName
) / sizeof(WCHAR
))
721 KeyName
= InitialKeyName
;
725 KeyName
= HeapAlloc(UserServerHeap
, 0,
726 (wcslen(StringSid
) + wcslen(Subkey
) + 1) *
730 DPRINT1("Failed to allocate memory, using default shutdown settings\n");
731 LocalFree(StringSid
);
735 wcscat(wcscpy(KeyName
, StringSid
), Subkey
);
736 LocalFree(StringSid
);
738 ErrCode
= RegOpenKeyExW(HKEY_USERS
, KeyName
, 0, KEY_QUERY_VALUE
, &DesktopKey
);
739 if (KeyName
!= InitialKeyName
)
741 HeapFree(UserServerHeap
, 0, KeyName
);
743 if (ERROR_SUCCESS
!= ErrCode
)
745 DPRINT1("RegOpenKeyEx failed with error %ld, using default shutdown settings\n", ErrCode
);
749 ShutdownSettings
->AutoEndTasks
= (BOOL
) GetShutdownSettings(DesktopKey
, L
"AutoEndTasks",
750 (DWORD
) DEFAULT_AUTO_END_TASKS
);
751 ShutdownSettings
->HungAppTimeout
= GetShutdownSettings(DesktopKey
,
753 DEFAULT_HUNG_APP_TIMEOUT
);
754 ShutdownSettings
->WaitToKillAppTimeout
= GetShutdownSettings(DesktopKey
,
755 L
"WaitToKillAppTimeout",
756 DEFAULT_WAIT_TO_KILL_APP_TIMEOUT
);
758 RegCloseKey(DesktopKey
);
761 static NTSTATUS FASTCALL
762 InternalExitReactos(DWORD ProcessId
, DWORD ThreadId
, UINT Flags
)
767 PROCESS_ENUM_CONTEXT Context
;
771 char FixedUserInfo
[64];
772 TOKEN_USER
*UserInfo
;
773 SHUTDOWN_SETTINGS ShutdownSettings
;
775 if (ProcessId
!= (DWORD_PTR
) LogonProcessId
)
777 DPRINT1("Internal ExitWindowsEx call not from winlogon\n");
778 return STATUS_ACCESS_DENIED
;
781 DPRINT1("FIXME: Need to close all user processes!\n");
782 return STATUS_SUCCESS
;
784 CallerThread
= OpenThread(THREAD_QUERY_INFORMATION
, FALSE
, ThreadId
);
785 if (NULL
== CallerThread
)
787 DPRINT1("OpenThread failed with error %d\n", GetLastError());
788 return STATUS_UNSUCCESSFUL
;
790 if (! OpenThreadToken(CallerThread
, TOKEN_QUERY
, FALSE
, &CallerToken
))
792 DPRINT1("OpenThreadToken failed with error %d\n", GetLastError());
793 CloseHandle(CallerThread
);
794 return STATUS_UNSUCCESSFUL
;
796 CloseHandle(CallerThread
);
798 Context
.ProcessCount
= 0;
799 Context
.ProcessData
= NULL
;
800 if (! GetTokenInformation(CallerToken
, TokenOrigin
, &Context
.TokenOrigin
,
801 sizeof(TOKEN_ORIGIN
), &ReturnLength
))
803 DPRINT1("GetTokenInformation failed with error %d\n", GetLastError());
804 CloseHandle(CallerToken
);
805 return STATUS_UNSUCCESSFUL
;
807 if (! GetTokenInformation(CallerToken
, TokenUser
, FixedUserInfo
,
808 sizeof(FixedUserInfo
), &ReturnLength
))
810 if (sizeof(FixedUserInfo
) < ReturnLength
)
812 UserInfo
= HeapAlloc(UserServerHeap
, 0, ReturnLength
);
813 if (NULL
== UserInfo
)
815 DPRINT1("Unable to allocate %u bytes for user info\n",
816 (unsigned) ReturnLength
);
817 CloseHandle(CallerToken
);
818 return STATUS_NO_MEMORY
;
820 if (! GetTokenInformation(CallerToken
, TokenUser
, UserInfo
,
821 ReturnLength
, &ReturnLength
))
823 DPRINT1("GetTokenInformation failed with error %d\n",
825 HeapFree(UserServerHeap
, 0, UserInfo
);
826 CloseHandle(CallerToken
);
827 return STATUS_UNSUCCESSFUL
;
832 DPRINT1("GetTokenInformation failed with error %d\n", GetLastError());
833 CloseHandle(CallerToken
);
834 return STATUS_UNSUCCESSFUL
;
839 UserInfo
= (TOKEN_USER
*) FixedUserInfo
;
841 CloseHandle(CallerToken
);
842 LoadShutdownSettings(UserInfo
->User
.Sid
, &ShutdownSettings
);
843 if (UserInfo
!= (TOKEN_USER
*) FixedUserInfo
)
845 HeapFree(UserServerHeap
, 0, UserInfo
);
847 Context
.CsrssProcess
= GetCurrentProcessId();
848 ShellWnd
= GetShellWindow();
849 if (NULL
== ShellWnd
)
851 DPRINT("No shell present\n");
852 Context
.ShellProcess
= 0;
854 else if (0 == GetWindowThreadProcessId(ShellWnd
, &Context
.ShellProcess
))
856 DPRINT1("Can't get process id of shell window\n");
857 Context
.ShellProcess
= 0;
860 // Status = Win32CsrEnumProcesses(ExitReactosProcessEnum, &Context);
861 if (! NT_SUCCESS(Status
))
863 DPRINT1("Failed to enumerate registered processes, status 0x%x\n",
865 if (NULL
!= Context
.ProcessData
)
867 HeapFree(UserServerHeap
, 0, Context
.ProcessData
);
872 qsort(Context
.ProcessData
, Context
.ProcessCount
, sizeof(PCSR_PROCESS
),
875 /* Terminate processes, stop if we find one kicking and screaming it doesn't
877 Status
= STATUS_SUCCESS
;
878 for (ProcessIndex
= 0;
879 ProcessIndex
< Context
.ProcessCount
&& NT_SUCCESS(Status
);
882 if (! NotifyAndTerminateProcess(Context
.ProcessData
[ProcessIndex
],
883 &ShutdownSettings
, Flags
))
885 Status
= STATUS_REQUEST_ABORTED
;
890 if (NULL
!= Context
.ProcessData
)
892 HeapFree(UserServerHeap
, 0, Context
.ProcessData
);
898 static NTSTATUS FASTCALL
899 UserExitReactos(DWORD UserProcessId
, UINT Flags
)
903 /* FIXME Inside 2000 says we should impersonate the caller here */
904 Status
= NtUserCallTwoParam(UserProcessId
, Flags
, TWOPARAM_ROUTINE_EXITREACTOS
);
906 /* If the message isn't handled, the return value is 0, so 0 doesn't indicate
907 success. Success is indicated by a 1 return value, if anything besides 0
908 or 1 it's a NTSTATUS value */
911 Status
= STATUS_SUCCESS
;
913 else if (0 == Status
)
915 Status
= STATUS_NOT_IMPLEMENTED
;
922 /* PUBLIC SERVER APIS *********************************************************/
924 CSR_API(SrvExitWindowsEx
)
926 PUSER_EXIT_REACTOS ExitReactosRequest
= &((PUSER_API_MESSAGE
)ApiMessage
)->Data
.ExitReactosRequest
;
928 if (0 == (ExitReactosRequest
->Flags
& EWX_INTERNAL_FLAG
))
930 return UserExitReactos((DWORD_PTR
) ApiMessage
->Header
.ClientId
.UniqueProcess
,
931 ExitReactosRequest
->Flags
);
935 return InternalExitReactos((DWORD_PTR
) ApiMessage
->Header
.ClientId
.UniqueProcess
,
936 (DWORD_PTR
) ApiMessage
->Header
.ClientId
.UniqueThread
,
937 ExitReactosRequest
->Flags
);
943 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
944 return STATUS_NOT_IMPLEMENTED
;
949 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
950 return STATUS_NOT_IMPLEMENTED
;
953 CSR_API(SrvRecordShutdownReason
)
955 DPRINT1("%s not yet implemented\n", __FUNCTION__
);
956 return STATUS_NOT_IMPLEMENTED
;