ntdll: Fix a few memory leaks in tests.
[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 static ULONG execute_flags = MEM_EXECUTE_OPTION_DISABLE;
41
42 /*
43  *      Process object
44  */
45
46 /******************************************************************************
47  *  NtTerminateProcess                  [NTDLL.@]
48  *
49  *  Native applications must kill themselves when done
50  */
51 NTSTATUS WINAPI NtTerminateProcess( HANDLE handle, LONG exit_code )
52 {
53     NTSTATUS ret;
54     BOOL self;
55     SERVER_START_REQ( terminate_process )
56     {
57         req->handle    = wine_server_obj_handle( handle );
58         req->exit_code = exit_code;
59         ret = wine_server_call( req );
60         self = !ret && reply->self;
61     }
62     SERVER_END_REQ;
63     if (self) exit( exit_code );
64     return ret;
65 }
66
67 /******************************************************************************
68  *  RtlGetCurrentPeb  [NTDLL.@]
69  *
70  */
71 PEB * WINAPI RtlGetCurrentPeb(void)
72 {
73     return NtCurrentTeb()->Peb;
74 }
75
76 /******************************************************************************
77  *  RtlGetNtGlobalFlags  [NTDLL.@]
78  *
79  */
80 ULONG WINAPI RtlGetNtGlobalFlags(void)
81 {
82     return NtCurrentTeb()->Peb->NtGlobalFlag;
83 }
84
85 /***********************************************************************
86  *           __wine_make_process_system   (NTDLL.@)
87  *
88  * Mark the current process as a system process.
89  * Returns the event that is signaled when all non-system processes have exited.
90  */
91 HANDLE CDECL __wine_make_process_system(void)
92 {
93     HANDLE ret = 0;
94     SERVER_START_REQ( make_process_system )
95     {
96         if (!wine_server_call( req )) ret = wine_server_ptr_handle( reply->event );
97     }
98     SERVER_END_REQ;
99     return ret;
100 }
101
102
103 #define UNIMPLEMENTED_INFO_CLASS(c) \
104     case c: \
105         FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
106         ret = STATUS_INVALID_INFO_CLASS; \
107         break
108
109 /******************************************************************************
110 *  NtQueryInformationProcess            [NTDLL.@]
111 *  ZwQueryInformationProcess            [NTDLL.@]
112 *
113 */
114 NTSTATUS WINAPI NtQueryInformationProcess(
115         IN HANDLE ProcessHandle,
116         IN PROCESSINFOCLASS ProcessInformationClass,
117         OUT PVOID ProcessInformation,
118         IN ULONG ProcessInformationLength,
119         OUT PULONG ReturnLength)
120 {
121     NTSTATUS ret = STATUS_SUCCESS;
122     ULONG len = 0;
123
124     TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
125           ProcessHandle,ProcessInformationClass,
126           ProcessInformation,ProcessInformationLength,
127           ReturnLength);
128
129     switch (ProcessInformationClass) 
130     {
131     UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
132     UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
133     UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
134     UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
135     UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
136     UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
137     UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
138     UNIMPLEMENTED_INFO_CLASS(ProcessDefaultHardErrorMode);
139     UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers);
140     UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits);
141     UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch);
142     UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL);
143     UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup);
144     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass);
145     UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information);
146     UNIMPLEMENTED_INFO_CLASS(ProcessAffinityMask);
147     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost);
148     UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
149     UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
150     UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
151     UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
152     UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
153     UNIMPLEMENTED_INFO_CLASS(ProcessDebugFlags);
154     UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
155
156     case ProcessBasicInformation:
157         {
158             PROCESS_BASIC_INFORMATION pbi;
159             const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
160
161             if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
162             {
163                 if (!ProcessInformation)
164                     ret = STATUS_ACCESS_VIOLATION;
165                 else if (!ProcessHandle)
166                     ret = STATUS_INVALID_HANDLE;
167                 else
168                 {
169                     SERVER_START_REQ(get_process_info)
170                     {
171                         req->handle = wine_server_obj_handle( ProcessHandle );
172                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
173                         {
174                             pbi.ExitStatus = reply->exit_code;
175                             pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
176                             pbi.AffinityMask = reply->affinity & affinity_mask;
177                             pbi.BasePriority = reply->priority;
178                             pbi.UniqueProcessId = reply->pid;
179                             pbi.InheritedFromUniqueProcessId = reply->ppid;
180                         }
181                     }
182                     SERVER_END_REQ;
183
184                     memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
185
186                     len = sizeof(PROCESS_BASIC_INFORMATION);
187                 }
188
189                 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
190                     ret = STATUS_INFO_LENGTH_MISMATCH;
191             }
192             else
193             {
194                 len = sizeof(PROCESS_BASIC_INFORMATION);
195                 ret = STATUS_INFO_LENGTH_MISMATCH;
196             }
197         }
198         break;
199     case ProcessIoCounters:
200         {
201             IO_COUNTERS pii;
202
203             if (ProcessInformationLength >= sizeof(IO_COUNTERS))
204             {
205                 if (!ProcessInformation)
206                     ret = STATUS_ACCESS_VIOLATION;
207                 else if (!ProcessHandle)
208                     ret = STATUS_INVALID_HANDLE;
209                 else
210                 {
211                     /* FIXME : real data */
212                     memset(&pii, 0 , sizeof(IO_COUNTERS));
213
214                     memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
215
216                     len = sizeof(IO_COUNTERS);
217                 }
218
219                 if (ProcessInformationLength > sizeof(IO_COUNTERS))
220                     ret = STATUS_INFO_LENGTH_MISMATCH;
221             }
222             else
223             {
224                 len = sizeof(IO_COUNTERS);
225                 ret = STATUS_INFO_LENGTH_MISMATCH;
226             }
227         }
228         break;
229     case ProcessVmCounters:
230         {
231             VM_COUNTERS pvmi;
232
233             /* older Windows versions don't have the PrivatePageCount field */
234             if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
235             {
236                 if (!ProcessInformation)
237                     ret = STATUS_ACCESS_VIOLATION;
238                 else if (!ProcessHandle)
239                     ret = STATUS_INVALID_HANDLE;
240                 else
241                 {
242                     /* FIXME : real data */
243                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
244
245                     len = ProcessInformationLength;
246                     if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
247
248                     memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
249                 }
250
251                 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
252                     ProcessInformationLength != sizeof(VM_COUNTERS))
253                     ret = STATUS_INFO_LENGTH_MISMATCH;
254             }
255             else
256             {
257                 len = sizeof(pvmi);
258                 ret = STATUS_INFO_LENGTH_MISMATCH;
259             }
260         }
261         break;
262     case ProcessTimes:
263         {
264             KERNEL_USER_TIMES pti;
265
266             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
267             {
268                 if (!ProcessInformation)
269                     ret = STATUS_ACCESS_VIOLATION;
270                 else if (!ProcessHandle)
271                     ret = STATUS_INVALID_HANDLE;
272                 else
273                 {
274                     /* FIXME : User- and KernelTime have to be implemented */
275                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
276
277                     SERVER_START_REQ(get_process_info)
278                     {
279                       req->handle = wine_server_obj_handle( ProcessHandle );
280                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
281                       {
282                           pti.CreateTime.QuadPart = reply->start_time;
283                           pti.ExitTime.QuadPart = reply->end_time;
284                       }
285                     }
286                     SERVER_END_REQ;
287
288                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
289                     len = sizeof(KERNEL_USER_TIMES);
290                 }
291
292                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
293                     ret = STATUS_INFO_LENGTH_MISMATCH;
294             }
295             else
296             {
297                 len = sizeof(KERNEL_USER_TIMES);
298                 ret = STATUS_INFO_LENGTH_MISMATCH;
299             }
300         }
301         break;
302     case ProcessDebugPort:
303         /* "These are not the debuggers you are looking for." *
304          * set it to 0 aka "no debugger" to satisfy copy protections */
305         len = 4;
306         if (ProcessInformationLength == len)
307             memset(ProcessInformation, 0, ProcessInformationLength);
308         else
309             ret = STATUS_INFO_LENGTH_MISMATCH;
310         break;
311     case ProcessDebugObjectHandle:
312         /* "These are not the debuggers you are looking for." *
313          * set it to 0 aka "no debugger" to satisfy copy protections */
314         len = sizeof(HANDLE);
315         if (ProcessInformationLength == len)
316         {
317             if (!ProcessInformation)
318                 ret = STATUS_ACCESS_VIOLATION;
319             else if (!ProcessHandle)
320                 ret = STATUS_INVALID_HANDLE;
321             else
322                 memset(ProcessInformation, 0, ProcessInformationLength);
323         }
324         else
325             ret = STATUS_INFO_LENGTH_MISMATCH;
326         break;
327     case ProcessHandleCount:
328         if (ProcessInformationLength >= 4)
329         {
330             if (!ProcessInformation)
331                 ret = STATUS_ACCESS_VIOLATION;
332             else if (!ProcessHandle)
333                 ret = STATUS_INVALID_HANDLE;
334             else
335             {
336                 memset(ProcessInformation, 0, 4);
337                 len = 4;
338             }
339
340             if (ProcessInformationLength > 4)
341                 ret = STATUS_INFO_LENGTH_MISMATCH;
342         }
343         else
344         {
345             len = 4;
346             ret = STATUS_INFO_LENGTH_MISMATCH;
347         }
348         break;
349     case ProcessWow64Information:
350         len = sizeof(DWORD);
351         if (ProcessInformationLength == len)
352         {
353             DWORD val = 0;
354
355             if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
356             else if (server_cpus & (1 << CPU_x86_64))
357             {
358                 SERVER_START_REQ( get_process_info )
359                 {
360                     req->handle = wine_server_obj_handle( ProcessHandle );
361                     if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
362                 }
363                 SERVER_END_REQ;
364             }
365             *(DWORD *)ProcessInformation = val;
366         }
367         else ret = STATUS_INFO_LENGTH_MISMATCH;
368         break;
369     case ProcessImageFileName:
370         /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
371          * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
372         SERVER_START_REQ(get_dll_info)
373         {
374             UNICODE_STRING *image_file_name_str = ProcessInformation;
375
376             req->handle = wine_server_obj_handle( ProcessHandle );
377             req->base_address = 0; /* main module */
378             wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
379                                    ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
380             ret = wine_server_call( req );
381             if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
382
383             len = sizeof(UNICODE_STRING) + reply->filename_len;
384             if (ret == STATUS_SUCCESS)
385             {
386                 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
387                 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
388             }
389         }
390         SERVER_END_REQ;
391         break;
392     case ProcessExecuteFlags:
393         len = sizeof(ULONG);
394         if (ProcessInformationLength == len)
395             *(ULONG *)ProcessInformation = execute_flags;
396         else
397             ret = STATUS_INFO_LENGTH_MISMATCH;
398         break;
399     default:
400         FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
401               ProcessHandle,ProcessInformationClass,
402               ProcessInformation,ProcessInformationLength,
403               ReturnLength);
404         ret = STATUS_INVALID_INFO_CLASS;
405         break;
406     }
407
408     if (ReturnLength) *ReturnLength = len;
409     
410     return ret;
411 }
412
413 /******************************************************************************
414  * NtSetInformationProcess [NTDLL.@]
415  * ZwSetInformationProcess [NTDLL.@]
416  */
417 NTSTATUS WINAPI NtSetInformationProcess(
418         IN HANDLE ProcessHandle,
419         IN PROCESSINFOCLASS ProcessInformationClass,
420         IN PVOID ProcessInformation,
421         IN ULONG ProcessInformationLength)
422 {
423     NTSTATUS ret = STATUS_SUCCESS;
424
425     switch (ProcessInformationClass)
426     {
427     case ProcessAffinityMask:
428         if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
429         if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
430             return STATUS_INVALID_PARAMETER;
431         if (!*(PDWORD_PTR)ProcessInformation)
432             return STATUS_INVALID_PARAMETER;
433         SERVER_START_REQ( set_process_info )
434         {
435             req->handle   = wine_server_obj_handle( ProcessHandle );
436             req->affinity = *(PDWORD_PTR)ProcessInformation;
437             req->mask     = SET_PROCESS_INFO_AFFINITY;
438             ret = wine_server_call( req );
439         }
440         SERVER_END_REQ;
441         break;
442     case ProcessPriorityClass:
443         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
444             return STATUS_INVALID_PARAMETER;
445         else
446         {
447             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
448
449             SERVER_START_REQ( set_process_info )
450             {
451                 req->handle   = wine_server_obj_handle( ProcessHandle );
452                 /* FIXME Foreground isn't used */
453                 req->priority = ppc->PriorityClass;
454                 req->mask     = SET_PROCESS_INFO_PRIORITY;
455                 ret = wine_server_call( req );
456             }
457             SERVER_END_REQ;
458         }
459         break;
460
461     case ProcessExecuteFlags:
462         if (ProcessInformationLength != sizeof(ULONG))
463             return STATUS_INVALID_PARAMETER;
464         else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
465             return STATUS_ACCESS_DENIED;
466         else
467         {
468             BOOL enable;
469             switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
470             {
471             case MEM_EXECUTE_OPTION_ENABLE:
472                 enable = TRUE;
473                 break;
474             case MEM_EXECUTE_OPTION_DISABLE:
475                 enable = FALSE;
476                 break;
477             default:
478                 return STATUS_INVALID_PARAMETER;
479             }
480             execute_flags = *(ULONG *)ProcessInformation;
481             VIRTUAL_SetForceExec( enable );
482         }
483         break;
484
485     default:
486         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
487               ProcessHandle,ProcessInformationClass,ProcessInformation,
488               ProcessInformationLength);
489         ret = STATUS_NOT_IMPLEMENTED;
490         break;
491     }
492     return ret;
493 }
494
495 /******************************************************************************
496  * NtFlushInstructionCache [NTDLL.@]
497  * ZwFlushInstructionCache [NTDLL.@]
498  */
499 NTSTATUS WINAPI NtFlushInstructionCache(
500         IN HANDLE ProcessHandle,
501         IN LPCVOID BaseAddress,
502         IN SIZE_T Size)
503 {
504 #ifdef __i386__
505     TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
506 #else
507     FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
508 #endif
509     return STATUS_SUCCESS;
510 }
511
512 /******************************************************************
513  *              NtOpenProcess [NTDLL.@]
514  *              ZwOpenProcess [NTDLL.@]
515  */
516 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
517                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
518 {
519     NTSTATUS    status;
520
521     SERVER_START_REQ( open_process )
522     {
523         req->pid        = HandleToULong(cid->UniqueProcess);
524         req->access     = access;
525         req->attributes = attr ? attr->Attributes : 0;
526         status = wine_server_call( req );
527         if (!status) *handle = wine_server_ptr_handle( reply->handle );
528     }
529     SERVER_END_REQ;
530     return status;
531 }