Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / ntdll / process.c
blobb9b81ca3e76100717082b5a9f6cbdbec94d5b2e9
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(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);
147 case ProcessBasicInformation:
149 PROCESS_BASIC_INFORMATION pbi;
150 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
152 if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
154 if (!ProcessInformation)
155 ret = STATUS_ACCESS_VIOLATION;
156 else if (!ProcessHandle)
157 ret = STATUS_INVALID_HANDLE;
158 else
160 SERVER_START_REQ(get_process_info)
162 req->handle = wine_server_obj_handle( ProcessHandle );
163 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
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;
173 SERVER_END_REQ;
175 memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
177 len = sizeof(PROCESS_BASIC_INFORMATION);
180 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
181 ret = STATUS_INFO_LENGTH_MISMATCH;
183 else
185 len = sizeof(PROCESS_BASIC_INFORMATION);
186 ret = STATUS_INFO_LENGTH_MISMATCH;
189 break;
190 case ProcessIoCounters:
192 IO_COUNTERS pii;
194 if (ProcessInformationLength >= sizeof(IO_COUNTERS))
196 if (!ProcessInformation)
197 ret = STATUS_ACCESS_VIOLATION;
198 else if (!ProcessHandle)
199 ret = STATUS_INVALID_HANDLE;
200 else
202 /* FIXME : real data */
203 memset(&pii, 0 , sizeof(IO_COUNTERS));
205 memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
207 len = sizeof(IO_COUNTERS);
210 if (ProcessInformationLength > sizeof(IO_COUNTERS))
211 ret = STATUS_INFO_LENGTH_MISMATCH;
213 else
215 len = sizeof(IO_COUNTERS);
216 ret = STATUS_INFO_LENGTH_MISMATCH;
219 break;
220 case ProcessVmCounters:
222 VM_COUNTERS pvmi;
224 /* older Windows versions don't have the PrivatePageCount field */
225 if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
227 if (!ProcessInformation)
228 ret = STATUS_ACCESS_VIOLATION;
229 else if (!ProcessHandle)
230 ret = STATUS_INVALID_HANDLE;
231 else
233 /* FIXME : real data */
234 memset(&pvmi, 0 , sizeof(VM_COUNTERS));
236 len = ProcessInformationLength;
237 if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
239 memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
242 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
243 ProcessInformationLength != sizeof(VM_COUNTERS))
244 ret = STATUS_INFO_LENGTH_MISMATCH;
246 else
248 len = sizeof(pvmi);
249 ret = STATUS_INFO_LENGTH_MISMATCH;
252 break;
253 case ProcessTimes:
255 KERNEL_USER_TIMES pti;
257 if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
259 if (!ProcessInformation)
260 ret = STATUS_ACCESS_VIOLATION;
261 else if (!ProcessHandle)
262 ret = STATUS_INVALID_HANDLE;
263 else
265 /* FIXME : User- and KernelTime have to be implemented */
266 memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
268 SERVER_START_REQ(get_process_info)
270 req->handle = wine_server_obj_handle( ProcessHandle );
271 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
273 pti.CreateTime.QuadPart = reply->start_time;
274 pti.ExitTime.QuadPart = reply->end_time;
277 SERVER_END_REQ;
279 memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
280 len = sizeof(KERNEL_USER_TIMES);
283 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
284 ret = STATUS_INFO_LENGTH_MISMATCH;
286 else
288 len = sizeof(KERNEL_USER_TIMES);
289 ret = STATUS_INFO_LENGTH_MISMATCH;
292 break;
293 case ProcessDebugPort:
294 len = sizeof(DWORD_PTR);
295 if (ProcessInformationLength == len)
297 if (!ProcessInformation)
298 ret = STATUS_ACCESS_VIOLATION;
299 else if (!ProcessHandle)
300 ret = STATUS_INVALID_HANDLE;
301 else
303 SERVER_START_REQ(get_process_info)
305 req->handle = wine_server_obj_handle( ProcessHandle );
306 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
308 *(DWORD_PTR *)ProcessInformation = reply->debugger_present ? ~(DWORD_PTR)0 : 0;
311 SERVER_END_REQ;
314 else
315 ret = STATUS_INFO_LENGTH_MISMATCH;
316 break;
317 case ProcessDefaultHardErrorMode:
318 len = sizeof(process_error_mode);
319 if (ProcessInformationLength == len)
320 memcpy(ProcessInformation, &process_error_mode, len);
321 else
322 ret = STATUS_INFO_LENGTH_MISMATCH;
323 break;
324 case ProcessDebugObjectHandle:
325 /* "These are not the debuggers you are looking for." *
326 * set it to 0 aka "no debugger" to satisfy copy protections */
327 len = sizeof(HANDLE);
328 if (ProcessInformationLength == len)
330 if (!ProcessInformation)
331 ret = STATUS_ACCESS_VIOLATION;
332 else if (!ProcessHandle)
333 ret = STATUS_INVALID_HANDLE;
334 else
336 memset(ProcessInformation, 0, ProcessInformationLength);
337 ret = STATUS_PORT_NOT_SET;
340 else
341 ret = STATUS_INFO_LENGTH_MISMATCH;
342 break;
343 case ProcessHandleCount:
344 if (ProcessInformationLength >= 4)
346 if (!ProcessInformation)
347 ret = STATUS_ACCESS_VIOLATION;
348 else if (!ProcessHandle)
349 ret = STATUS_INVALID_HANDLE;
350 else
352 memset(ProcessInformation, 0, 4);
353 len = 4;
356 if (ProcessInformationLength > 4)
357 ret = STATUS_INFO_LENGTH_MISMATCH;
359 else
361 len = 4;
362 ret = STATUS_INFO_LENGTH_MISMATCH;
364 break;
365 case ProcessWow64Information:
366 len = sizeof(DWORD);
367 if (ProcessInformationLength == len)
369 DWORD val = 0;
371 if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
372 else if (server_cpus & (1 << CPU_x86_64))
374 SERVER_START_REQ( get_process_info )
376 req->handle = wine_server_obj_handle( ProcessHandle );
377 if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
379 SERVER_END_REQ;
381 *(DWORD *)ProcessInformation = val;
383 else ret = STATUS_INFO_LENGTH_MISMATCH;
384 break;
385 case ProcessImageFileName:
386 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
387 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
388 SERVER_START_REQ(get_dll_info)
390 UNICODE_STRING *image_file_name_str = ProcessInformation;
392 req->handle = wine_server_obj_handle( ProcessHandle );
393 req->base_address = 0; /* main module */
394 wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
395 ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
396 ret = wine_server_call( req );
397 if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
399 len = sizeof(UNICODE_STRING) + reply->filename_len;
400 if (ret == STATUS_SUCCESS)
402 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
403 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
406 SERVER_END_REQ;
407 break;
408 case ProcessExecuteFlags:
409 len = sizeof(ULONG);
410 if (ProcessInformationLength == len)
411 *(ULONG *)ProcessInformation = execute_flags;
412 else
413 ret = STATUS_INFO_LENGTH_MISMATCH;
414 break;
415 default:
416 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
417 ProcessHandle,ProcessInformationClass,
418 ProcessInformation,ProcessInformationLength,
419 ReturnLength);
420 ret = STATUS_INVALID_INFO_CLASS;
421 break;
424 if (ReturnLength) *ReturnLength = len;
426 return ret;
429 /******************************************************************************
430 * NtSetInformationProcess [NTDLL.@]
431 * ZwSetInformationProcess [NTDLL.@]
433 NTSTATUS WINAPI NtSetInformationProcess(
434 IN HANDLE ProcessHandle,
435 IN PROCESSINFOCLASS ProcessInformationClass,
436 IN PVOID ProcessInformation,
437 IN ULONG ProcessInformationLength)
439 NTSTATUS ret = STATUS_SUCCESS;
441 switch (ProcessInformationClass)
443 case ProcessDefaultHardErrorMode:
444 if (ProcessInformationLength != sizeof(UINT)) return STATUS_INVALID_PARAMETER;
445 process_error_mode = *(UINT *)ProcessInformation;
446 break;
447 case ProcessAffinityMask:
448 if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
449 if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
450 return STATUS_INVALID_PARAMETER;
451 if (!*(PDWORD_PTR)ProcessInformation)
452 return STATUS_INVALID_PARAMETER;
453 SERVER_START_REQ( set_process_info )
455 req->handle = wine_server_obj_handle( ProcessHandle );
456 req->affinity = *(PDWORD_PTR)ProcessInformation;
457 req->mask = SET_PROCESS_INFO_AFFINITY;
458 ret = wine_server_call( req );
460 SERVER_END_REQ;
461 break;
462 case ProcessPriorityClass:
463 if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
464 return STATUS_INVALID_PARAMETER;
465 else
467 PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
469 SERVER_START_REQ( set_process_info )
471 req->handle = wine_server_obj_handle( ProcessHandle );
472 /* FIXME Foreground isn't used */
473 req->priority = ppc->PriorityClass;
474 req->mask = SET_PROCESS_INFO_PRIORITY;
475 ret = wine_server_call( req );
477 SERVER_END_REQ;
479 break;
481 case ProcessExecuteFlags:
482 if (ProcessInformationLength != sizeof(ULONG))
483 return STATUS_INVALID_PARAMETER;
484 else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
485 return STATUS_ACCESS_DENIED;
486 else
488 BOOL enable;
489 switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
491 case MEM_EXECUTE_OPTION_ENABLE:
492 enable = TRUE;
493 break;
494 case MEM_EXECUTE_OPTION_DISABLE:
495 enable = FALSE;
496 break;
497 default:
498 return STATUS_INVALID_PARAMETER;
500 execute_flags = *(ULONG *)ProcessInformation;
501 VIRTUAL_SetForceExec( enable );
503 break;
505 default:
506 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
507 ProcessHandle,ProcessInformationClass,ProcessInformation,
508 ProcessInformationLength);
509 ret = STATUS_NOT_IMPLEMENTED;
510 break;
512 return ret;
515 /******************************************************************************
516 * NtFlushInstructionCache [NTDLL.@]
517 * ZwFlushInstructionCache [NTDLL.@]
519 NTSTATUS WINAPI NtFlushInstructionCache(
520 IN HANDLE ProcessHandle,
521 IN LPCVOID BaseAddress,
522 IN SIZE_T Size)
524 #ifdef __i386__
525 TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
526 #else
527 FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
528 #endif
529 return STATUS_SUCCESS;
532 /******************************************************************
533 * NtOpenProcess [NTDLL.@]
534 * ZwOpenProcess [NTDLL.@]
536 NTSTATUS WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
537 const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
539 NTSTATUS status;
541 SERVER_START_REQ( open_process )
543 req->pid = HandleToULong(cid->UniqueProcess);
544 req->access = access;
545 req->attributes = attr ? attr->Attributes : 0;
546 status = wine_server_call( req );
547 if (!status) *handle = wine_server_ptr_handle( reply->handle );
549 SERVER_END_REQ;
550 return status;