setupapi: Add pSetupFree|Malloc|Realloc to spec file and forward them.
[wine.git] / dlls / ntdll / process.c
bloba39f5d0719e1b6529d0c86feeada616a7ebd8a1d
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 <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40 static ULONG execute_flags = MEM_EXECUTE_OPTION_DISABLE;
43 * Process object
46 /******************************************************************************
47 * NtTerminateProcess [NTDLL.@]
49 * Native applications must kill themselves when done
51 NTSTATUS WINAPI NtTerminateProcess( HANDLE handle, LONG exit_code )
53 NTSTATUS ret;
54 BOOL self;
55 SERVER_START_REQ( terminate_process )
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;
62 SERVER_END_REQ;
63 if (self) exit( exit_code );
64 return ret;
67 /******************************************************************************
68 * RtlGetCurrentPeb [NTDLL.@]
71 PEB * WINAPI RtlGetCurrentPeb(void)
73 return NtCurrentTeb()->Peb;
76 /***********************************************************************
77 * __wine_make_process_system (NTDLL.@)
79 * Mark the current process as a system process.
80 * Returns the event that is signaled when all non-system processes have exited.
82 HANDLE CDECL __wine_make_process_system(void)
84 HANDLE ret = 0;
85 SERVER_START_REQ( make_process_system )
87 if (!wine_server_call( req )) ret = wine_server_ptr_handle( reply->event );
89 SERVER_END_REQ;
90 return ret;
93 static UINT process_error_mode;
95 #define UNIMPLEMENTED_INFO_CLASS(c) \
96 case c: \
97 FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
98 ret = STATUS_INVALID_INFO_CLASS; \
99 break
101 /******************************************************************************
102 * NtQueryInformationProcess [NTDLL.@]
103 * ZwQueryInformationProcess [NTDLL.@]
106 NTSTATUS WINAPI NtQueryInformationProcess(
107 IN HANDLE ProcessHandle,
108 IN PROCESSINFOCLASS ProcessInformationClass,
109 OUT PVOID ProcessInformation,
110 IN ULONG ProcessInformationLength,
111 OUT PULONG ReturnLength)
113 NTSTATUS ret = STATUS_SUCCESS;
114 ULONG len = 0;
116 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
117 ProcessHandle,ProcessInformationClass,
118 ProcessInformation,ProcessInformationLength,
119 ReturnLength);
121 switch (ProcessInformationClass)
123 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
124 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
125 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
126 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
127 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
128 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
129 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
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(ProcessPriorityBoost);
138 UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
139 UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
140 UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
141 UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
142 UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
143 UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
145 case ProcessBasicInformation:
147 PROCESS_BASIC_INFORMATION pbi;
148 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
150 if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
152 if (!ProcessInformation)
153 ret = STATUS_ACCESS_VIOLATION;
154 else if (!ProcessHandle)
155 ret = STATUS_INVALID_HANDLE;
156 else
158 SERVER_START_REQ(get_process_info)
160 req->handle = wine_server_obj_handle( ProcessHandle );
161 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
163 pbi.ExitStatus = reply->exit_code;
164 pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
165 pbi.AffinityMask = reply->affinity & affinity_mask;
166 pbi.BasePriority = reply->priority;
167 pbi.UniqueProcessId = reply->pid;
168 pbi.InheritedFromUniqueProcessId = reply->ppid;
171 SERVER_END_REQ;
173 memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
175 len = sizeof(PROCESS_BASIC_INFORMATION);
178 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
179 ret = STATUS_INFO_LENGTH_MISMATCH;
181 else
183 len = sizeof(PROCESS_BASIC_INFORMATION);
184 ret = STATUS_INFO_LENGTH_MISMATCH;
187 break;
188 case ProcessIoCounters:
190 IO_COUNTERS pii;
192 if (ProcessInformationLength >= sizeof(IO_COUNTERS))
194 if (!ProcessInformation)
195 ret = STATUS_ACCESS_VIOLATION;
196 else if (!ProcessHandle)
197 ret = STATUS_INVALID_HANDLE;
198 else
200 /* FIXME : real data */
201 memset(&pii, 0 , sizeof(IO_COUNTERS));
203 memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
205 len = sizeof(IO_COUNTERS);
208 if (ProcessInformationLength > sizeof(IO_COUNTERS))
209 ret = STATUS_INFO_LENGTH_MISMATCH;
211 else
213 len = sizeof(IO_COUNTERS);
214 ret = STATUS_INFO_LENGTH_MISMATCH;
217 break;
218 case ProcessVmCounters:
220 VM_COUNTERS pvmi;
222 /* older Windows versions don't have the PrivatePageCount field */
223 if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
225 if (!ProcessInformation)
226 ret = STATUS_ACCESS_VIOLATION;
227 else if (!ProcessHandle)
228 ret = STATUS_INVALID_HANDLE;
229 else
231 /* FIXME : real data */
232 memset(&pvmi, 0 , sizeof(VM_COUNTERS));
234 len = ProcessInformationLength;
235 if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
237 memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
240 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
241 ProcessInformationLength != sizeof(VM_COUNTERS))
242 ret = STATUS_INFO_LENGTH_MISMATCH;
244 else
246 len = sizeof(pvmi);
247 ret = STATUS_INFO_LENGTH_MISMATCH;
250 break;
251 case ProcessTimes:
253 KERNEL_USER_TIMES pti;
255 if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
257 if (!ProcessInformation)
258 ret = STATUS_ACCESS_VIOLATION;
259 else if (!ProcessHandle)
260 ret = STATUS_INVALID_HANDLE;
261 else
263 /* FIXME : User- and KernelTime have to be implemented */
264 memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
266 SERVER_START_REQ(get_process_info)
268 req->handle = wine_server_obj_handle( ProcessHandle );
269 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
271 pti.CreateTime.QuadPart = reply->start_time;
272 pti.ExitTime.QuadPart = reply->end_time;
275 SERVER_END_REQ;
277 memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
278 len = sizeof(KERNEL_USER_TIMES);
281 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
282 ret = STATUS_INFO_LENGTH_MISMATCH;
284 else
286 len = sizeof(KERNEL_USER_TIMES);
287 ret = STATUS_INFO_LENGTH_MISMATCH;
290 break;
291 case ProcessDebugPort:
292 len = sizeof(DWORD_PTR);
293 if (ProcessInformationLength == len)
295 if (!ProcessInformation)
296 ret = STATUS_ACCESS_VIOLATION;
297 else if (!ProcessHandle)
298 ret = STATUS_INVALID_HANDLE;
299 else
301 SERVER_START_REQ(get_process_info)
303 req->handle = wine_server_obj_handle( ProcessHandle );
304 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
306 *(DWORD_PTR *)ProcessInformation = reply->debugger_present ? ~(DWORD_PTR)0 : 0;
309 SERVER_END_REQ;
312 else
313 ret = STATUS_INFO_LENGTH_MISMATCH;
314 break;
315 case ProcessDebugFlags:
316 len = sizeof(DWORD);
317 if (ProcessInformationLength == len)
319 if (!ProcessInformation)
320 ret = STATUS_ACCESS_VIOLATION;
321 else if (!ProcessHandle)
322 ret = STATUS_INVALID_HANDLE;
323 else
325 SERVER_START_REQ(get_process_info)
327 req->handle = wine_server_obj_handle( ProcessHandle );
328 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
330 *(DWORD *)ProcessInformation = !reply->debugger_present;
333 SERVER_END_REQ;
336 else
337 ret = STATUS_INFO_LENGTH_MISMATCH;
338 break;
339 case ProcessDefaultHardErrorMode:
340 len = sizeof(process_error_mode);
341 if (ProcessInformationLength == len)
342 memcpy(ProcessInformation, &process_error_mode, len);
343 else
344 ret = STATUS_INFO_LENGTH_MISMATCH;
345 break;
346 case ProcessDebugObjectHandle:
347 /* "These are not the debuggers you are looking for." *
348 * set it to 0 aka "no debugger" to satisfy copy protections */
349 len = sizeof(HANDLE);
350 if (ProcessInformationLength == len)
352 if (!ProcessInformation)
353 ret = STATUS_ACCESS_VIOLATION;
354 else if (!ProcessHandle)
355 ret = STATUS_INVALID_HANDLE;
356 else
358 memset(ProcessInformation, 0, ProcessInformationLength);
359 ret = STATUS_PORT_NOT_SET;
362 else
363 ret = STATUS_INFO_LENGTH_MISMATCH;
364 break;
365 case ProcessHandleCount:
366 if (ProcessInformationLength >= 4)
368 if (!ProcessInformation)
369 ret = STATUS_ACCESS_VIOLATION;
370 else if (!ProcessHandle)
371 ret = STATUS_INVALID_HANDLE;
372 else
374 memset(ProcessInformation, 0, 4);
375 len = 4;
378 if (ProcessInformationLength > 4)
379 ret = STATUS_INFO_LENGTH_MISMATCH;
381 else
383 len = 4;
384 ret = STATUS_INFO_LENGTH_MISMATCH;
386 break;
388 case ProcessAffinityMask:
389 len = sizeof(ULONG_PTR);
390 if (ProcessInformationLength == len)
392 const ULONG_PTR system_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
394 SERVER_START_REQ(get_process_info)
396 req->handle = wine_server_obj_handle( ProcessHandle );
397 if (!(ret = wine_server_call( req )))
398 *(ULONG_PTR *)ProcessInformation = reply->affinity & system_mask;
400 SERVER_END_REQ;
402 else ret = STATUS_INFO_LENGTH_MISMATCH;
403 break;
405 case ProcessWow64Information:
406 len = sizeof(DWORD);
407 if (ProcessInformationLength == len)
409 DWORD val = 0;
411 if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
412 else if (server_cpus & (1 << CPU_x86_64))
414 SERVER_START_REQ( get_process_info )
416 req->handle = wine_server_obj_handle( ProcessHandle );
417 if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
419 SERVER_END_REQ;
421 *(DWORD *)ProcessInformation = val;
423 else ret = STATUS_INFO_LENGTH_MISMATCH;
424 break;
425 case ProcessImageFileName:
426 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
427 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
428 SERVER_START_REQ(get_dll_info)
430 UNICODE_STRING *image_file_name_str = ProcessInformation;
432 req->handle = wine_server_obj_handle( ProcessHandle );
433 req->base_address = 0; /* main module */
434 wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
435 ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
436 ret = wine_server_call( req );
437 if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
439 len = sizeof(UNICODE_STRING) + reply->filename_len;
440 if (ret == STATUS_SUCCESS)
442 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
443 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
446 SERVER_END_REQ;
447 break;
448 case ProcessExecuteFlags:
449 len = sizeof(ULONG);
450 if (ProcessInformationLength == len)
451 *(ULONG *)ProcessInformation = execute_flags;
452 else
453 ret = STATUS_INFO_LENGTH_MISMATCH;
454 break;
455 default:
456 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
457 ProcessHandle,ProcessInformationClass,
458 ProcessInformation,ProcessInformationLength,
459 ReturnLength);
460 ret = STATUS_INVALID_INFO_CLASS;
461 break;
464 if (ReturnLength) *ReturnLength = len;
466 return ret;
469 /******************************************************************************
470 * NtSetInformationProcess [NTDLL.@]
471 * ZwSetInformationProcess [NTDLL.@]
473 NTSTATUS WINAPI NtSetInformationProcess(
474 IN HANDLE ProcessHandle,
475 IN PROCESSINFOCLASS ProcessInformationClass,
476 IN PVOID ProcessInformation,
477 IN ULONG ProcessInformationLength)
479 NTSTATUS ret = STATUS_SUCCESS;
481 switch (ProcessInformationClass)
483 case ProcessDefaultHardErrorMode:
484 if (ProcessInformationLength != sizeof(UINT)) return STATUS_INVALID_PARAMETER;
485 process_error_mode = *(UINT *)ProcessInformation;
486 break;
487 case ProcessAffinityMask:
488 if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
489 if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
490 return STATUS_INVALID_PARAMETER;
491 if (!*(PDWORD_PTR)ProcessInformation)
492 return STATUS_INVALID_PARAMETER;
493 SERVER_START_REQ( set_process_info )
495 req->handle = wine_server_obj_handle( ProcessHandle );
496 req->affinity = *(PDWORD_PTR)ProcessInformation;
497 req->mask = SET_PROCESS_INFO_AFFINITY;
498 ret = wine_server_call( req );
500 SERVER_END_REQ;
501 break;
502 case ProcessPriorityClass:
503 if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
504 return STATUS_INVALID_PARAMETER;
505 else
507 PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
509 SERVER_START_REQ( set_process_info )
511 req->handle = wine_server_obj_handle( ProcessHandle );
512 /* FIXME Foreground isn't used */
513 req->priority = ppc->PriorityClass;
514 req->mask = SET_PROCESS_INFO_PRIORITY;
515 ret = wine_server_call( req );
517 SERVER_END_REQ;
519 break;
521 case ProcessExecuteFlags:
522 if (ProcessInformationLength != sizeof(ULONG))
523 return STATUS_INVALID_PARAMETER;
524 else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
525 return STATUS_ACCESS_DENIED;
526 else
528 BOOL enable;
529 switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
531 case MEM_EXECUTE_OPTION_ENABLE:
532 enable = TRUE;
533 break;
534 case MEM_EXECUTE_OPTION_DISABLE:
535 enable = FALSE;
536 break;
537 default:
538 return STATUS_INVALID_PARAMETER;
540 execute_flags = *(ULONG *)ProcessInformation;
541 VIRTUAL_SetForceExec( enable );
543 break;
545 default:
546 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
547 ProcessHandle,ProcessInformationClass,ProcessInformation,
548 ProcessInformationLength);
549 ret = STATUS_NOT_IMPLEMENTED;
550 break;
552 return ret;
555 /******************************************************************************
556 * NtFlushInstructionCache [NTDLL.@]
557 * ZwFlushInstructionCache [NTDLL.@]
559 NTSTATUS WINAPI NtFlushInstructionCache(
560 IN HANDLE ProcessHandle,
561 IN LPCVOID BaseAddress,
562 IN SIZE_T Size)
564 #ifdef __i386__
565 TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
566 #else
567 FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
568 #endif
569 return STATUS_SUCCESS;
572 /******************************************************************
573 * NtOpenProcess [NTDLL.@]
574 * ZwOpenProcess [NTDLL.@]
576 NTSTATUS WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
577 const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
579 NTSTATUS status;
581 SERVER_START_REQ( open_process )
583 req->pid = HandleToULong(cid->UniqueProcess);
584 req->access = access;
585 req->attributes = attr ? attr->Attributes : 0;
586 status = wine_server_call( req );
587 if (!status) *handle = wine_server_ptr_handle( reply->handle );
589 SERVER_END_REQ;
590 return status;