xref: /reactos/win32ss/user/winsrv/init.c (revision 40462c92)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS User API Server DLL
4  * FILE:            win32ss/user/winsrv/init.c
5  * PURPOSE:         Initialization
6  * PROGRAMMERS:     Dmitry Philippov (shedon@mail.ru)
7  *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8  */
9 
10 /* INCLUDES *******************************************************************/
11 
12 #include "winsrv.h"
13 
14 #define NDEBUG
15 #include <debug.h>
16 
17 /* GLOBALS ********************************************************************/
18 
19 #define DEFAULT_AUTO_END_TASKS              FALSE
20 #define DEFAULT_HUNG_APP_TIMEOUT            5000
21 #define DEFAULT_WAIT_TO_KILL_APP_TIMEOUT    20000
22 #define DEFAULT_PROCESS_TERMINATE_TIMEOUT   90000
23 
24 SHUTDOWN_SETTINGS ShutdownSettings =
25 {
26     DEFAULT_AUTO_END_TASKS,
27     DEFAULT_HUNG_APP_TIMEOUT,
28     DEFAULT_WAIT_TO_KILL_APP_TIMEOUT,
29     DEFAULT_WAIT_TO_KILL_APP_TIMEOUT,
30     DEFAULT_PROCESS_TERMINATE_TIMEOUT
31 };
32 
33 /* FUNCTIONS ******************************************************************/
34 
35 static ULONG
36 GetRegIntFromID(IN HANDLE KeyHandle,
37                 IN PWCHAR ValueName,
38                 IN ULONG DefaultValue)
39 {
40     UNICODE_STRING ValueString;
41     ULONG Length;
42     UCHAR Buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 32 * sizeof(WCHAR)];
43     PKEY_VALUE_PARTIAL_INFORMATION PartialInfo = (PVOID)Buffer;
44     NTSTATUS Status;
45     ULONG Value;
46 
47     /* Open the key */
48     RtlInitUnicodeString(&ValueString, ValueName);
49     Status = NtQueryValueKey(KeyHandle,
50                              &ValueString,
51                              KeyValuePartialInformation,
52                              PartialInfo,
53                              sizeof(Buffer),
54                              &Length);
55     if (NT_SUCCESS(Status))
56     {
57         if (PartialInfo->Type == REG_SZ)
58         {
59             /* Convert to integer */
60             RtlInitUnicodeString(&ValueString, (PWCHAR)PartialInfo->Data);
61             Status = RtlUnicodeStringToInteger(&ValueString, 10, &Value);
62         }
63         else if (PartialInfo->Type == REG_DWORD)
64         {
65             /* Directly retrieve the data */
66             Value = *(PULONG)PartialInfo->Data;
67             Status = STATUS_SUCCESS;
68         }
69         else
70         {
71             DPRINT1("Unexpected registry type %d for setting %S\n", PartialInfo->Type, ValueName);
72             Status = STATUS_UNSUCCESSFUL;
73         }
74     }
75 
76     if (!NT_SUCCESS(Status))
77     {
78         /* Use default value instead */
79         Value = DefaultValue;
80     }
81 
82     /* Return the value */
83     return Value;
84 }
85 
86 VOID FASTCALL
87 GetTimeouts(IN PSHUTDOWN_SETTINGS ShutdownSettings)
88 {
89     NTSTATUS Status;
90     UNICODE_STRING RegistryString;
91     OBJECT_ATTRIBUTES ObjectAttributes;
92     HANDLE CurrentUserKeyHandle;
93     HANDLE KeyHandle;
94 
95     /* Initialize with defaults first */
96     ShutdownSettings->AutoEndTasks         = DEFAULT_AUTO_END_TASKS;
97     ShutdownSettings->HungAppTimeout       = DEFAULT_HUNG_APP_TIMEOUT;
98     ShutdownSettings->WaitToKillAppTimeout = DEFAULT_WAIT_TO_KILL_APP_TIMEOUT;
99     ShutdownSettings->WaitToKillServiceTimeout = ShutdownSettings->WaitToKillAppTimeout;
100     ShutdownSettings->ProcessTerminateTimeout  = DEFAULT_PROCESS_TERMINATE_TIMEOUT;
101 
102     /* Open the per-user desktop key */
103     Status = RtlOpenCurrentUser(MAXIMUM_ALLOWED, &CurrentUserKeyHandle);
104     if (NT_SUCCESS(Status))
105     {
106         RtlInitUnicodeString(&RegistryString,
107                              L"Control Panel\\Desktop");
108         InitializeObjectAttributes(&ObjectAttributes,
109                                    &RegistryString,
110                                    OBJ_CASE_INSENSITIVE,
111                                    CurrentUserKeyHandle,
112                                    NULL);
113         Status = NtOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
114         if (NT_SUCCESS(Status))
115         {
116             /* Read timeouts */
117             ShutdownSettings->HungAppTimeout = GetRegIntFromID(KeyHandle,
118                                                               L"HungAppTimeout",
119                                                               DEFAULT_HUNG_APP_TIMEOUT);
120             ShutdownSettings->WaitToKillAppTimeout = GetRegIntFromID(KeyHandle,
121                                                                     L"WaitToKillAppTimeout",
122                                                                     DEFAULT_WAIT_TO_KILL_APP_TIMEOUT);
123             ShutdownSettings->AutoEndTasks = GetRegIntFromID(KeyHandle,
124                                                             L"AutoEndTasks",
125                                                             DEFAULT_AUTO_END_TASKS);
126             /* Done */
127             NtClose(KeyHandle);
128         }
129 
130         /* Done */
131         NtClose(CurrentUserKeyHandle);
132     }
133 
134     /* Now open the control key */
135     RtlInitUnicodeString(&RegistryString,
136                          L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
137     InitializeObjectAttributes(&ObjectAttributes,
138                                &RegistryString,
139                                OBJ_CASE_INSENSITIVE,
140                                NULL, NULL);
141     Status = NtOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
142     if (NT_SUCCESS(Status))
143     {
144         /* Read the services timeout */
145         ShutdownSettings->WaitToKillServiceTimeout = GetRegIntFromID(KeyHandle,
146                                                                     L"WaitToKillServiceTimeout",
147                                                                     ShutdownSettings->WaitToKillAppTimeout);
148 
149         /*
150          * Retrieve the process terminate timeout.
151          * See ftp://ftp.microsoft.com/MISC1/BUSSYS/WINNT/KB/Q234/6/06.TXT
152          * and https://web.archive.org/web/20050216235758/http://support.microsoft.com/kb/234606/EN-US/
153          * for more details.
154          *
155          * NOTE: Unused at the moment...
156          */
157         ShutdownSettings->ProcessTerminateTimeout = GetRegIntFromID(KeyHandle,
158                                                                    L"ProcessTerminateTimeout",
159                                                                    DEFAULT_PROCESS_TERMINATE_TIMEOUT);
160         if (ShutdownSettings->ProcessTerminateTimeout < DEFAULT_HUNG_APP_TIMEOUT)
161             ShutdownSettings->ProcessTerminateTimeout = DEFAULT_HUNG_APP_TIMEOUT;
162 
163         /* Done */
164         NtClose(KeyHandle);
165     }
166 }
167 
168 /* ENTRY-POINT ****************************************************************/
169 
170 BOOL
171 WINAPI
172 DllMain(IN HINSTANCE hInstanceDll,
173         IN DWORD dwReason,
174         IN LPVOID lpReserved)
175 {
176     UNREFERENCED_PARAMETER(hInstanceDll);
177     UNREFERENCED_PARAMETER(dwReason);
178     UNREFERENCED_PARAMETER(lpReserved);
179 
180     return TRUE;
181 }
182 
183 /* EOF */
184