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