push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / dlls / ntdll / process.c
blobaaf8315513d1a8b37c1ad8501c638f4cedc1f76d
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 * RtlGetNtGlobalFlags [NTDLL.@]
80 ULONG WINAPI RtlGetNtGlobalFlags(void)
82 return NtCurrentTeb()->Peb->NtGlobalFlag;
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;
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
109 /******************************************************************************
110 * NtQueryInformationProcess [NTDLL.@]
111 * ZwQueryInformationProcess [NTDLL.@]
114 NTSTATUS WINAPI NtQueryInformationProcess(
115 IN HANDLE ProcessHandle,
116 IN PROCESSINFOCLASS ProcessInformationClass,
117 OUT PVOID ProcessInformation,
118 IN ULONG ProcessInformationLength,
119 OUT PULONG ReturnLength)
121 NTSTATUS ret = STATUS_SUCCESS;
122 ULONG len = 0;
124 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
125 ProcessHandle,ProcessInformationClass,
126 ProcessInformation,ProcessInformationLength,
127 ReturnLength);
129 switch (ProcessInformationClass)
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);
156 case ProcessBasicInformation:
158 PROCESS_BASIC_INFORMATION pbi;
159 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
161 if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
163 if (!ProcessInformation)
164 ret = STATUS_ACCESS_VIOLATION;
165 else if (!ProcessHandle)
166 ret = STATUS_INVALID_HANDLE;
167 else
169 SERVER_START_REQ(get_process_info)
171 req->handle = wine_server_obj_handle( ProcessHandle );
172 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
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;
182 SERVER_END_REQ;
184 memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
186 len = sizeof(PROCESS_BASIC_INFORMATION);
189 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
190 ret = STATUS_INFO_LENGTH_MISMATCH;
192 else
194 len = sizeof(PROCESS_BASIC_INFORMATION);
195 ret = STATUS_INFO_LENGTH_MISMATCH;
198 break;
199 case ProcessIoCounters:
201 IO_COUNTERS pii;
203 if (ProcessInformationLength >= sizeof(IO_COUNTERS))
205 if (!ProcessInformation)
206 ret = STATUS_ACCESS_VIOLATION;
207 else if (!ProcessHandle)
208 ret = STATUS_INVALID_HANDLE;
209 else
211 /* FIXME : real data */
212 memset(&pii, 0 , sizeof(IO_COUNTERS));
214 memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
216 len = sizeof(IO_COUNTERS);
219 if (ProcessInformationLength > sizeof(IO_COUNTERS))
220 ret = STATUS_INFO_LENGTH_MISMATCH;
222 else
224 len = sizeof(IO_COUNTERS);
225 ret = STATUS_INFO_LENGTH_MISMATCH;
228 break;
229 case ProcessVmCounters:
231 VM_COUNTERS pvmi;
233 /* older Windows versions don't have the PrivatePageCount field */
234 if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
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(&pvmi, 0 , sizeof(VM_COUNTERS));
245 len = ProcessInformationLength;
246 if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
248 memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
251 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
252 ProcessInformationLength != sizeof(VM_COUNTERS))
253 ret = STATUS_INFO_LENGTH_MISMATCH;
255 else
257 len = sizeof(pvmi);
258 ret = STATUS_INFO_LENGTH_MISMATCH;
261 break;
262 case ProcessTimes:
264 KERNEL_USER_TIMES pti;
266 if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
268 if (!ProcessInformation)
269 ret = STATUS_ACCESS_VIOLATION;
270 else if (!ProcessHandle)
271 ret = STATUS_INVALID_HANDLE;
272 else
274 /* FIXME : User- and KernelTime have to be implemented */
275 memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
277 SERVER_START_REQ(get_process_info)
279 req->handle = wine_server_obj_handle( ProcessHandle );
280 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
282 pti.CreateTime.QuadPart = reply->start_time;
283 pti.ExitTime.QuadPart = reply->end_time;
286 SERVER_END_REQ;
288 memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
289 len = sizeof(KERNEL_USER_TIMES);
292 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
293 ret = STATUS_INFO_LENGTH_MISMATCH;
295 else
297 len = sizeof(KERNEL_USER_TIMES);
298 ret = STATUS_INFO_LENGTH_MISMATCH;
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)
317 if (!ProcessInformation)
318 ret = STATUS_ACCESS_VIOLATION;
319 else if (!ProcessHandle)
320 ret = STATUS_INVALID_HANDLE;
321 else
322 memset(ProcessInformation, 0, ProcessInformationLength);
324 else
325 ret = STATUS_INFO_LENGTH_MISMATCH;
326 break;
327 case ProcessHandleCount:
328 if (ProcessInformationLength >= 4)
330 if (!ProcessInformation)
331 ret = STATUS_ACCESS_VIOLATION;
332 else if (!ProcessHandle)
333 ret = STATUS_INVALID_HANDLE;
334 else
336 memset(ProcessInformation, 0, 4);
337 len = 4;
340 if (ProcessInformationLength > 4)
341 ret = STATUS_INFO_LENGTH_MISMATCH;
343 else
345 len = 4;
346 ret = STATUS_INFO_LENGTH_MISMATCH;
348 break;
349 case ProcessWow64Information:
350 len = sizeof(DWORD);
351 if (ProcessInformationLength == len)
353 DWORD val = 0;
355 if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
356 else if (server_cpus & (1 << CPU_x86_64))
358 SERVER_START_REQ( get_process_info )
360 req->handle = wine_server_obj_handle( ProcessHandle );
361 if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
363 SERVER_END_REQ;
365 *(DWORD *)ProcessInformation = val;
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)
374 UNICODE_STRING *image_file_name_str = ProcessInformation;
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;
383 len = sizeof(UNICODE_STRING) + reply->filename_len;
384 if (ret == STATUS_SUCCESS)
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);
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;
408 if (ReturnLength) *ReturnLength = len;
410 return ret;
413 /******************************************************************************
414 * NtSetInformationProcess [NTDLL.@]
415 * ZwSetInformationProcess [NTDLL.@]
417 NTSTATUS WINAPI NtSetInformationProcess(
418 IN HANDLE ProcessHandle,
419 IN PROCESSINFOCLASS ProcessInformationClass,
420 IN PVOID ProcessInformation,
421 IN ULONG ProcessInformationLength)
423 NTSTATUS ret = STATUS_SUCCESS;
425 switch (ProcessInformationClass)
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 )
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 );
440 SERVER_END_REQ;
441 break;
442 case ProcessPriorityClass:
443 if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
444 return STATUS_INVALID_PARAMETER;
445 else
447 PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
449 SERVER_START_REQ( set_process_info )
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 );
457 SERVER_END_REQ;
459 break;
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
468 BOOL enable;
469 switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
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;
480 execute_flags = *(ULONG *)ProcessInformation;
481 VIRTUAL_SetForceExec( enable );
483 break;
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;
492 return ret;
495 /******************************************************************************
496 * NtFlushInstructionCache [NTDLL.@]
497 * ZwFlushInstructionCache [NTDLL.@]
499 NTSTATUS WINAPI NtFlushInstructionCache(
500 IN HANDLE ProcessHandle,
501 IN LPCVOID BaseAddress,
502 IN SIZE_T Size)
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;
512 /******************************************************************
513 * NtOpenProcess [NTDLL.@]
514 * ZwOpenProcess [NTDLL.@]
516 NTSTATUS WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
517 const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
519 NTSTATUS status;
521 SERVER_START_REQ( open_process )
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 );
529 SERVER_END_REQ;
530 return status;