crypt32/tests: Add a trailing '\n' to ok() calls.
[wine.git] / dlls / ntdll / process.c
blobadff826b5110d2ac0ce2e713b8f0812dca174371
1 /*
2 * NT basis DLL
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)
7 * Copyright 1996-1998 Marcus Meissner
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.
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.
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
24 #include "config.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "wine/debug.h"
38 #include "windef.h"
39 #include "winternl.h"
40 #include "ntdll_misc.h"
41 #include "wine/server.h"
43 #ifdef HAVE_MACH_MACH_H
44 #include <mach/mach.h>
45 #endif
47 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
49 static ULONG execute_flags = MEM_EXECUTE_OPTION_DISABLE;
52 * Process object
55 /******************************************************************************
56 * NtTerminateProcess [NTDLL.@]
58 * Native applications must kill themselves when done
60 NTSTATUS WINAPI NtTerminateProcess( HANDLE handle, LONG exit_code )
62 NTSTATUS ret;
63 BOOL self;
64 SERVER_START_REQ( terminate_process )
66 req->handle = wine_server_obj_handle( handle );
67 req->exit_code = exit_code;
68 ret = wine_server_call( req );
69 self = !ret && reply->self;
71 SERVER_END_REQ;
72 if (self && handle) _exit( exit_code );
73 return ret;
76 /******************************************************************************
77 * RtlGetCurrentPeb [NTDLL.@]
80 PEB * WINAPI RtlGetCurrentPeb(void)
82 return NtCurrentTeb()->Peb;
85 /***********************************************************************
86 * __wine_make_process_system (NTDLL.@)
88 * Mark the current process as a system process.
89 * Returns the event that is signaled when all non-system processes have exited.
91 HANDLE CDECL __wine_make_process_system(void)
93 HANDLE ret = 0;
94 SERVER_START_REQ( make_process_system )
96 if (!wine_server_call( req )) ret = wine_server_ptr_handle( reply->event );
98 SERVER_END_REQ;
99 return ret;
102 static UINT process_error_mode;
104 #define UNIMPLEMENTED_INFO_CLASS(c) \
105 case c: \
106 FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
107 ret = STATUS_INVALID_INFO_CLASS; \
108 break
110 ULONG_PTR get_system_affinity_mask(void)
112 ULONG num_cpus = NtCurrentTeb()->Peb->NumberOfProcessors;
113 if (num_cpus >= sizeof(ULONG_PTR) * 8) return ~(ULONG_PTR)0;
114 return ((ULONG_PTR)1 << num_cpus) - 1;
117 #if defined(HAVE_MACH_MACH_H)
119 static void fill_VM_COUNTERS(VM_COUNTERS* pvmi)
121 #if defined(MACH_TASK_BASIC_INFO)
122 struct mach_task_basic_info info;
123 mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
124 if(task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &infoCount) == KERN_SUCCESS)
126 pvmi->VirtualSize = info.resident_size + info.virtual_size;
127 pvmi->PagefileUsage = info.virtual_size;
128 pvmi->WorkingSetSize = info.resident_size;
129 pvmi->PeakWorkingSetSize = info.resident_size_max;
131 #endif
134 #else
136 static void fill_VM_COUNTERS(VM_COUNTERS* pvmi)
138 /* FIXME : real data */
141 #endif
143 /******************************************************************************
144 * NtQueryInformationProcess [NTDLL.@]
145 * ZwQueryInformationProcess [NTDLL.@]
148 NTSTATUS WINAPI NtQueryInformationProcess(
149 IN HANDLE ProcessHandle,
150 IN PROCESSINFOCLASS ProcessInformationClass,
151 OUT PVOID ProcessInformation,
152 IN ULONG ProcessInformationLength,
153 OUT PULONG ReturnLength)
155 NTSTATUS ret = STATUS_SUCCESS;
156 ULONG len = 0;
158 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
159 ProcessHandle,ProcessInformationClass,
160 ProcessInformation,ProcessInformationLength,
161 ReturnLength);
163 switch (ProcessInformationClass)
165 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
166 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
167 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
168 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
169 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
170 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
171 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
172 UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers);
173 UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits);
174 UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch);
175 UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL);
176 UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup);
177 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass);
178 UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information);
179 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost);
180 UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
181 UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
182 UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
183 UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
184 UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
185 UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
187 case ProcessBasicInformation:
189 PROCESS_BASIC_INFORMATION pbi;
190 const ULONG_PTR affinity_mask = get_system_affinity_mask();
192 if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
194 if (!ProcessInformation)
195 ret = STATUS_ACCESS_VIOLATION;
196 else if (!ProcessHandle)
197 ret = STATUS_INVALID_HANDLE;
198 else
200 SERVER_START_REQ(get_process_info)
202 req->handle = wine_server_obj_handle( ProcessHandle );
203 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
205 pbi.ExitStatus = reply->exit_code;
206 pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
207 pbi.AffinityMask = reply->affinity & affinity_mask;
208 pbi.BasePriority = reply->priority;
209 pbi.UniqueProcessId = reply->pid;
210 pbi.InheritedFromUniqueProcessId = reply->ppid;
213 SERVER_END_REQ;
215 memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
217 len = sizeof(PROCESS_BASIC_INFORMATION);
220 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
221 ret = STATUS_INFO_LENGTH_MISMATCH;
223 else
225 len = sizeof(PROCESS_BASIC_INFORMATION);
226 ret = STATUS_INFO_LENGTH_MISMATCH;
229 break;
230 case ProcessIoCounters:
232 IO_COUNTERS pii;
234 if (ProcessInformationLength >= sizeof(IO_COUNTERS))
236 if (!ProcessInformation)
237 ret = STATUS_ACCESS_VIOLATION;
238 else if (!ProcessHandle)
239 ret = STATUS_INVALID_HANDLE;
240 else
242 /* FIXME : real data */
243 memset(&pii, 0 , sizeof(IO_COUNTERS));
245 memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
247 len = sizeof(IO_COUNTERS);
250 if (ProcessInformationLength > sizeof(IO_COUNTERS))
251 ret = STATUS_INFO_LENGTH_MISMATCH;
253 else
255 len = sizeof(IO_COUNTERS);
256 ret = STATUS_INFO_LENGTH_MISMATCH;
259 break;
260 case ProcessVmCounters:
262 VM_COUNTERS pvmi;
264 /* older Windows versions don't have the PrivatePageCount field */
265 if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
267 if (!ProcessInformation)
268 ret = STATUS_ACCESS_VIOLATION;
269 else if (!ProcessHandle)
270 ret = STATUS_INVALID_HANDLE;
271 else
273 memset(&pvmi, 0 , sizeof(VM_COUNTERS));
274 fill_VM_COUNTERS(&pvmi);
276 len = ProcessInformationLength;
277 if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
279 memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
282 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
283 ProcessInformationLength != sizeof(VM_COUNTERS))
284 ret = STATUS_INFO_LENGTH_MISMATCH;
286 else
288 len = sizeof(pvmi);
289 ret = STATUS_INFO_LENGTH_MISMATCH;
292 break;
293 case ProcessTimes:
295 KERNEL_USER_TIMES pti;
297 if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
299 if (!ProcessInformation)
300 ret = STATUS_ACCESS_VIOLATION;
301 else if (!ProcessHandle)
302 ret = STATUS_INVALID_HANDLE;
303 else
305 /* FIXME : User- and KernelTime have to be implemented */
306 memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
308 SERVER_START_REQ(get_process_info)
310 req->handle = wine_server_obj_handle( ProcessHandle );
311 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
313 pti.CreateTime.QuadPart = reply->start_time;
314 pti.ExitTime.QuadPart = reply->end_time;
317 SERVER_END_REQ;
319 memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
320 len = sizeof(KERNEL_USER_TIMES);
323 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
324 ret = STATUS_INFO_LENGTH_MISMATCH;
326 else
328 len = sizeof(KERNEL_USER_TIMES);
329 ret = STATUS_INFO_LENGTH_MISMATCH;
332 break;
333 case ProcessDebugPort:
334 len = sizeof(DWORD_PTR);
335 if (ProcessInformationLength == len)
337 if (!ProcessInformation)
338 ret = STATUS_ACCESS_VIOLATION;
339 else if (!ProcessHandle)
340 ret = STATUS_INVALID_HANDLE;
341 else
343 SERVER_START_REQ(get_process_info)
345 req->handle = wine_server_obj_handle( ProcessHandle );
346 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
348 *(DWORD_PTR *)ProcessInformation = reply->debugger_present ? ~(DWORD_PTR)0 : 0;
351 SERVER_END_REQ;
354 else
355 ret = STATUS_INFO_LENGTH_MISMATCH;
356 break;
357 case ProcessDebugFlags:
358 len = sizeof(DWORD);
359 if (ProcessInformationLength == len)
361 if (!ProcessInformation)
362 ret = STATUS_ACCESS_VIOLATION;
363 else if (!ProcessHandle)
364 ret = STATUS_INVALID_HANDLE;
365 else
367 SERVER_START_REQ(get_process_info)
369 req->handle = wine_server_obj_handle( ProcessHandle );
370 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
372 *(DWORD *)ProcessInformation = reply->debug_children;
375 SERVER_END_REQ;
378 else
379 ret = STATUS_INFO_LENGTH_MISMATCH;
380 break;
381 case ProcessDefaultHardErrorMode:
382 len = sizeof(process_error_mode);
383 if (ProcessInformationLength == len)
384 memcpy(ProcessInformation, &process_error_mode, len);
385 else
386 ret = STATUS_INFO_LENGTH_MISMATCH;
387 break;
388 case ProcessDebugObjectHandle:
389 /* "These are not the debuggers you are looking for." *
390 * set it to 0 aka "no debugger" to satisfy copy protections */
391 len = sizeof(HANDLE);
392 if (ProcessInformationLength == len)
394 if (!ProcessInformation)
395 ret = STATUS_ACCESS_VIOLATION;
396 else if (!ProcessHandle)
397 ret = STATUS_INVALID_HANDLE;
398 else
400 memset(ProcessInformation, 0, ProcessInformationLength);
401 ret = STATUS_PORT_NOT_SET;
404 else
405 ret = STATUS_INFO_LENGTH_MISMATCH;
406 break;
407 case ProcessHandleCount:
408 if (ProcessInformationLength >= 4)
410 if (!ProcessInformation)
411 ret = STATUS_ACCESS_VIOLATION;
412 else if (!ProcessHandle)
413 ret = STATUS_INVALID_HANDLE;
414 else
416 memset(ProcessInformation, 0, 4);
417 len = 4;
420 if (ProcessInformationLength > 4)
421 ret = STATUS_INFO_LENGTH_MISMATCH;
423 else
425 len = 4;
426 ret = STATUS_INFO_LENGTH_MISMATCH;
428 break;
430 case ProcessAffinityMask:
431 len = sizeof(ULONG_PTR);
432 if (ProcessInformationLength == len)
434 const ULONG_PTR system_mask = get_system_affinity_mask();
436 SERVER_START_REQ(get_process_info)
438 req->handle = wine_server_obj_handle( ProcessHandle );
439 if (!(ret = wine_server_call( req )))
440 *(ULONG_PTR *)ProcessInformation = reply->affinity & system_mask;
442 SERVER_END_REQ;
444 else ret = STATUS_INFO_LENGTH_MISMATCH;
445 break;
447 case ProcessWow64Information:
448 len = sizeof(ULONG_PTR);
449 if (ProcessInformationLength != len) ret = STATUS_INFO_LENGTH_MISMATCH;
450 else if (!ProcessInformation) ret = STATUS_ACCESS_VIOLATION;
451 else if(!ProcessHandle) ret = STATUS_INVALID_HANDLE;
452 else
454 ULONG_PTR val = 0;
456 if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
457 else if (server_cpus & ((1 << CPU_x86_64) | (1 << CPU_ARM64)))
459 SERVER_START_REQ( get_process_info )
461 req->handle = wine_server_obj_handle( ProcessHandle );
462 if (!(ret = wine_server_call( req )))
463 val = (reply->cpu != CPU_x86_64 && reply->cpu != CPU_ARM64);
465 SERVER_END_REQ;
467 *(ULONG_PTR *)ProcessInformation = val;
469 break;
470 case ProcessImageFileName:
471 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
472 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
473 SERVER_START_REQ(get_dll_info)
475 UNICODE_STRING *image_file_name_str = ProcessInformation;
477 req->handle = wine_server_obj_handle( ProcessHandle );
478 req->base_address = 0; /* main module */
479 wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
480 ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
481 ret = wine_server_call( req );
482 if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
484 len = sizeof(UNICODE_STRING) + reply->filename_len;
485 if (ret == STATUS_SUCCESS)
487 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
488 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
491 SERVER_END_REQ;
492 break;
493 case ProcessExecuteFlags:
494 len = sizeof(ULONG);
495 if (ProcessInformationLength == len)
496 *(ULONG *)ProcessInformation = execute_flags;
497 else
498 ret = STATUS_INFO_LENGTH_MISMATCH;
499 break;
500 default:
501 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
502 ProcessHandle,ProcessInformationClass,
503 ProcessInformation,ProcessInformationLength,
504 ReturnLength);
505 ret = STATUS_INVALID_INFO_CLASS;
506 break;
509 if (ReturnLength) *ReturnLength = len;
511 return ret;
514 /******************************************************************************
515 * NtSetInformationProcess [NTDLL.@]
516 * ZwSetInformationProcess [NTDLL.@]
518 NTSTATUS WINAPI NtSetInformationProcess(
519 IN HANDLE ProcessHandle,
520 IN PROCESSINFOCLASS ProcessInformationClass,
521 IN PVOID ProcessInformation,
522 IN ULONG ProcessInformationLength)
524 NTSTATUS ret = STATUS_SUCCESS;
526 switch (ProcessInformationClass)
528 case ProcessDefaultHardErrorMode:
529 if (ProcessInformationLength != sizeof(UINT)) return STATUS_INVALID_PARAMETER;
530 process_error_mode = *(UINT *)ProcessInformation;
531 break;
532 case ProcessAffinityMask:
534 const ULONG_PTR system_mask = get_system_affinity_mask();
536 if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
537 if (*(PDWORD_PTR)ProcessInformation & ~system_mask)
538 return STATUS_INVALID_PARAMETER;
539 if (!*(PDWORD_PTR)ProcessInformation)
540 return STATUS_INVALID_PARAMETER;
541 SERVER_START_REQ( set_process_info )
543 req->handle = wine_server_obj_handle( ProcessHandle );
544 req->affinity = *(PDWORD_PTR)ProcessInformation;
545 req->mask = SET_PROCESS_INFO_AFFINITY;
546 ret = wine_server_call( req );
548 SERVER_END_REQ;
549 break;
551 case ProcessPriorityClass:
552 if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
553 return STATUS_INVALID_PARAMETER;
554 else
556 PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
558 SERVER_START_REQ( set_process_info )
560 req->handle = wine_server_obj_handle( ProcessHandle );
561 /* FIXME Foreground isn't used */
562 req->priority = ppc->PriorityClass;
563 req->mask = SET_PROCESS_INFO_PRIORITY;
564 ret = wine_server_call( req );
566 SERVER_END_REQ;
568 break;
570 case ProcessExecuteFlags:
571 if (ProcessInformationLength != sizeof(ULONG))
572 return STATUS_INVALID_PARAMETER;
573 else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
574 return STATUS_ACCESS_DENIED;
575 else
577 BOOL enable;
578 switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
580 case MEM_EXECUTE_OPTION_ENABLE:
581 enable = TRUE;
582 break;
583 case MEM_EXECUTE_OPTION_DISABLE:
584 enable = FALSE;
585 break;
586 default:
587 return STATUS_INVALID_PARAMETER;
589 execute_flags = *(ULONG *)ProcessInformation;
590 VIRTUAL_SetForceExec( enable );
592 break;
594 default:
595 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
596 ProcessHandle,ProcessInformationClass,ProcessInformation,
597 ProcessInformationLength);
598 ret = STATUS_NOT_IMPLEMENTED;
599 break;
601 return ret;
604 /******************************************************************************
605 * NtFlushInstructionCache [NTDLL.@]
606 * ZwFlushInstructionCache [NTDLL.@]
608 NTSTATUS WINAPI NtFlushInstructionCache(
609 IN HANDLE ProcessHandle,
610 IN LPCVOID BaseAddress,
611 IN SIZE_T Size)
613 static int once;
614 if (!once++)
616 #if defined(__x86_64__) || defined(__i386__)
617 TRACE("%p %p %ld - no-op on x86 and x86_64\n", ProcessHandle, BaseAddress, Size );
618 #else
619 FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
620 #endif
622 return STATUS_SUCCESS;
625 /******************************************************************
626 * NtOpenProcess [NTDLL.@]
627 * ZwOpenProcess [NTDLL.@]
629 NTSTATUS WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
630 const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
632 NTSTATUS status;
634 SERVER_START_REQ( open_process )
636 req->pid = HandleToULong(cid->UniqueProcess);
637 req->access = access;
638 req->attributes = attr ? attr->Attributes : 0;
639 status = wine_server_call( req );
640 if (!status) *handle = wine_server_ptr_handle( reply->handle );
642 SERVER_END_REQ;
643 return status;
646 /******************************************************************************
647 * NtResumeProcess
648 * ZwResumeProcess
650 NTSTATUS WINAPI NtResumeProcess( HANDLE handle )
652 FIXME("stub: %p\n", handle);
653 return STATUS_NOT_IMPLEMENTED;
656 /******************************************************************************
657 * NtSuspendProcess
658 * ZwSuspendProcess
660 NTSTATUS WINAPI NtSuspendProcess( HANDLE handle )
662 FIXME("stub: %p\n", handle);
663 return STATUS_NOT_IMPLEMENTED;