ntdll: Add tests for the data and name returned by NtQueryValueKey.
[wine] / dlls / ntdll / process.c
1 /*
2  * NT basis DLL
3  *
4  * This file contains the Nt* API functions of NTDLL.DLL.
5  * In the original ntdll.dll they all seem to just call int 0x2e (down to the NTOSKRNL)
6  *
7  * Copyright 1996-1998 Marcus Meissner
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "wine/debug.h"
33 #include "windef.h"
34 #include "winternl.h"
35 #include "ntdll_misc.h"
36 #include "wine/server.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
39
40 /*
41  *      Process object
42  */
43
44 /******************************************************************************
45  *  NtTerminateProcess                  [NTDLL.@]
46  *
47  *  Native applications must kill themselves when done
48  */
49 NTSTATUS WINAPI NtTerminateProcess( HANDLE handle, LONG exit_code )
50 {
51     NTSTATUS ret;
52     BOOL self;
53     SERVER_START_REQ( terminate_process )
54     {
55         req->handle    = handle;
56         req->exit_code = exit_code;
57         ret = wine_server_call( req );
58         self = !ret && reply->self;
59     }
60     SERVER_END_REQ;
61     if (self) exit( exit_code );
62     return ret;
63 }
64
65 /******************************************************************************
66  *  RtlGetCurrentPeb  [NTDLL.@]
67  *
68  */
69 PEB * WINAPI RtlGetCurrentPeb(void)
70 {
71     return NtCurrentTeb()->Peb;
72 }
73
74 /***********************************************************************
75  *           __wine_make_process_system   (NTDLL.@)
76  *
77  * Mark the current process as a system process.
78  * Returns the event that is signaled when all non-system processes have exited.
79  */
80 HANDLE __wine_make_process_system(void)
81 {
82     HANDLE ret = 0;
83     SERVER_START_REQ( make_process_system )
84     {
85         if (!wine_server_call( req )) ret = reply->event;
86     }
87     SERVER_END_REQ;
88     return ret;
89 }
90
91
92 #define UNIMPLEMENTED_INFO_CLASS(c) \
93     case c: \
94         FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
95         ret = STATUS_INVALID_INFO_CLASS; \
96         break
97
98 /******************************************************************************
99 *  NtQueryInformationProcess            [NTDLL.@]
100 *  ZwQueryInformationProcess            [NTDLL.@]
101 *
102 */
103 NTSTATUS WINAPI NtQueryInformationProcess(
104         IN HANDLE ProcessHandle,
105         IN PROCESSINFOCLASS ProcessInformationClass,
106         OUT PVOID ProcessInformation,
107         IN ULONG ProcessInformationLength,
108         OUT PULONG ReturnLength)
109 {
110     NTSTATUS ret = STATUS_SUCCESS;
111     ULONG len = 0;
112
113     TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
114           ProcessHandle,ProcessInformationClass,
115           ProcessInformation,ProcessInformationLength,
116           ReturnLength);
117
118     switch (ProcessInformationClass) 
119     {
120     UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
121     UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
122     UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
123     UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
124     UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
125     UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
126     UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
127     UNIMPLEMENTED_INFO_CLASS(ProcessDefaultHardErrorMode);
128     UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers);
129     UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits);
130     UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch);
131     UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL);
132     UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup);
133     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass);
134     UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information);
135     UNIMPLEMENTED_INFO_CLASS(ProcessAffinityMask);
136     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost);
137     UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
138     UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
139     UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
140     UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
141     UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
142     UNIMPLEMENTED_INFO_CLASS(ProcessDebugObjectHandle);
143     UNIMPLEMENTED_INFO_CLASS(ProcessDebugFlags);
144     UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
145
146     case ProcessBasicInformation:
147         {
148             PROCESS_BASIC_INFORMATION pbi;
149
150             if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
151             {
152                 if (!ProcessInformation)
153                     ret = STATUS_ACCESS_VIOLATION;
154                 else if (!ProcessHandle)
155                     ret = STATUS_INVALID_HANDLE;
156                 else
157                 {
158                     SERVER_START_REQ(get_process_info)
159                     {
160                         req->handle = ProcessHandle;
161                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
162                         {
163                             pbi.ExitStatus = reply->exit_code;
164                             pbi.PebBaseAddress = reply->peb;
165                             pbi.AffinityMask = reply->affinity;
166                             pbi.BasePriority = reply->priority;
167                             pbi.UniqueProcessId = reply->pid;
168                             pbi.InheritedFromUniqueProcessId = reply->ppid;
169                         }
170                     }
171                     SERVER_END_REQ;
172
173                     memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
174
175                     len = sizeof(PROCESS_BASIC_INFORMATION);
176                 }
177
178                 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
179                     ret = STATUS_INFO_LENGTH_MISMATCH;
180             }
181             else ret = STATUS_INFO_LENGTH_MISMATCH;
182         }
183         break;
184     case ProcessIoCounters:
185         {
186             IO_COUNTERS pii;
187
188             if (ProcessInformationLength >= sizeof(IO_COUNTERS))
189             {
190                 if (!ProcessInformation)
191                     ret = STATUS_ACCESS_VIOLATION;
192                 else if (!ProcessHandle)
193                     ret = STATUS_INVALID_HANDLE;
194                 else
195                 {
196                     /* FIXME : real data */
197                     memset(&pii, 0 , sizeof(IO_COUNTERS));
198
199                     memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
200
201                     len = sizeof(IO_COUNTERS);
202                 }
203
204                 if (ProcessInformationLength > sizeof(IO_COUNTERS))
205                     ret = STATUS_INFO_LENGTH_MISMATCH;
206             }
207             else ret = STATUS_INFO_LENGTH_MISMATCH;
208         }
209         break;
210     case ProcessVmCounters:
211         {
212             VM_COUNTERS pvmi;
213
214             if (ProcessInformationLength >= sizeof(VM_COUNTERS))
215             {
216                 if (!ProcessInformation)
217                     ret = STATUS_ACCESS_VIOLATION;
218                 else if (!ProcessHandle)
219                     ret = STATUS_INVALID_HANDLE;
220                 else
221                 {
222                     /* FIXME : real data */
223                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
224
225                     memcpy(ProcessInformation, &pvmi, sizeof(VM_COUNTERS));
226
227                     len = sizeof(VM_COUNTERS);
228                 }
229
230                 if (ProcessInformationLength > sizeof(VM_COUNTERS))
231                     ret = STATUS_INFO_LENGTH_MISMATCH;
232             }
233             else ret = STATUS_INFO_LENGTH_MISMATCH;
234         }
235         break;
236     case ProcessTimes:
237         {
238             KERNEL_USER_TIMES pti;
239
240             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
241             {
242                 if (!ProcessInformation)
243                     ret = STATUS_ACCESS_VIOLATION;
244                 else if (!ProcessHandle)
245                     ret = STATUS_INVALID_HANDLE;
246                 else
247                 {
248                     /* FIXME : User- and KernelTime have to be implemented */
249                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
250
251                     SERVER_START_REQ(get_process_info)
252                     {
253                       req->handle = ProcessHandle;
254                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
255                       {
256                           pti.CreateTime.QuadPart = reply->start_time;
257                           pti.ExitTime.QuadPart = reply->end_time;
258                       }
259                     }
260                     SERVER_END_REQ;
261
262                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
263
264                     len = sizeof(KERNEL_USER_TIMES);
265                 }
266
267                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
268                     ret = STATUS_INFO_LENGTH_MISMATCH;
269             }
270             else ret = STATUS_INFO_LENGTH_MISMATCH;
271         }
272         break;
273     case ProcessDebugPort:
274         /* "These are not the debuggers you are looking for." *
275          * set it to 0 aka "no debugger" to satisfy copy protections */
276         if (ProcessInformationLength == 4)
277         {
278             memset(ProcessInformation, 0, ProcessInformationLength);
279             len = 4;
280         }
281         else ret = STATUS_INFO_LENGTH_MISMATCH;
282         break;
283     case ProcessHandleCount:
284         if (ProcessInformationLength >= 4)
285         {
286             if (!ProcessInformation)
287                 ret = STATUS_ACCESS_VIOLATION;
288             else if (!ProcessHandle)
289                 ret = STATUS_INVALID_HANDLE;
290             else
291             {
292                 memset(ProcessInformation, 0, 4);
293
294
295                 len = 4;
296             }
297
298             if (ProcessInformationLength > 4)
299                 ret = STATUS_INFO_LENGTH_MISMATCH;
300          }
301          else ret = STATUS_INFO_LENGTH_MISMATCH;
302          break;
303     case ProcessWow64Information:
304         if (ProcessInformationLength == 4)
305         {
306             memset(ProcessInformation, 0, ProcessInformationLength);
307             len = 4;
308         }
309         else ret = STATUS_INFO_LENGTH_MISMATCH;
310         break;
311     case ProcessImageFileName:
312         SERVER_START_REQ(get_dll_info)
313         {
314             UNICODE_STRING *image_file_name_str = ProcessInformation;
315
316             req->handle = ProcessHandle;
317             req->base_address = NULL; /* main module */
318             wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
319                                    ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
320             ret = wine_server_call( req );
321             if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
322
323             len = sizeof(UNICODE_STRING) + reply->filename_len;
324             if (ret == STATUS_SUCCESS)
325             {
326                 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
327                 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
328             }
329         }
330         SERVER_END_REQ;
331         break;
332     default:
333         FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
334               ProcessHandle,ProcessInformationClass,
335               ProcessInformation,ProcessInformationLength,
336               ReturnLength);
337         ret = STATUS_INVALID_INFO_CLASS;
338         break;
339     }
340
341     if (ReturnLength) *ReturnLength = len;
342     
343     return ret;
344 }
345
346 /******************************************************************************
347  * NtSetInformationProcess [NTDLL.@]
348  * ZwSetInformationProcess [NTDLL.@]
349  */
350 NTSTATUS WINAPI NtSetInformationProcess(
351         IN HANDLE ProcessHandle,
352         IN PROCESSINFOCLASS ProcessInformationClass,
353         IN PVOID ProcessInformation,
354         IN ULONG ProcessInformationLength)
355 {
356     NTSTATUS ret = STATUS_SUCCESS;
357
358     switch (ProcessInformationClass)
359     {
360     case ProcessAffinityMask:
361         if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
362         SERVER_START_REQ( set_process_info )
363         {
364             req->handle   = ProcessHandle;
365             req->affinity = *(PDWORD_PTR)ProcessInformation;
366             req->mask     = SET_PROCESS_INFO_AFFINITY;
367             ret = wine_server_call( req );
368         }
369         SERVER_END_REQ;
370         break;
371     case ProcessPriorityClass:
372         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
373             return STATUS_INVALID_PARAMETER;
374         else
375         {
376             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
377
378             SERVER_START_REQ( set_process_info )
379             {
380                 req->handle   = ProcessHandle;
381                 /* FIXME Foreground isn't used */
382                 req->priority = ppc->PriorityClass;
383                 req->mask     = SET_PROCESS_INFO_PRIORITY;
384                 ret = wine_server_call( req );
385             }
386             SERVER_END_REQ;
387         }
388         break;
389     default:
390         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
391               ProcessHandle,ProcessInformationClass,ProcessInformation,
392               ProcessInformationLength);
393         ret = STATUS_NOT_IMPLEMENTED;
394         break;
395     }
396     return ret;
397 }
398
399 /******************************************************************************
400  * NtFlushInstructionCache [NTDLL.@]
401  * ZwFlushInstructionCache [NTDLL.@]
402  */
403 NTSTATUS WINAPI NtFlushInstructionCache(
404         IN HANDLE ProcessHandle,
405         IN LPCVOID BaseAddress,
406         IN SIZE_T Size)
407 {
408 #ifdef __i386__
409     TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
410 #else
411     FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
412 #endif
413     return STATUS_SUCCESS;
414 }
415
416 /******************************************************************
417  *              NtOpenProcess [NTDLL.@]
418  *              ZwOpenProcess [NTDLL.@]
419  */
420 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
421                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
422 {
423     NTSTATUS    status;
424
425     SERVER_START_REQ( open_process )
426     {
427         req->pid        = HandleToULong(cid->UniqueProcess);
428         req->access     = access;
429         req->attributes = attr ? attr->Attributes : 0;
430         status = wine_server_call( req );
431         if (!status) *handle = reply->handle;
432     }
433     SERVER_END_REQ;
434     return status;
435 }