msxml3: Support loading windows file paths.
[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_ENABLE;
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(ProcessDebugObjectHandle);
145     UNIMPLEMENTED_INFO_CLASS(ProcessDebugFlags);
146     UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
147
148     case ProcessBasicInformation:
149         {
150             PROCESS_BASIC_INFORMATION pbi;
151             const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
152
153             if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
154             {
155                 if (!ProcessInformation)
156                     ret = STATUS_ACCESS_VIOLATION;
157                 else if (!ProcessHandle)
158                     ret = STATUS_INVALID_HANDLE;
159                 else
160                 {
161                     SERVER_START_REQ(get_process_info)
162                     {
163                         req->handle = wine_server_obj_handle( ProcessHandle );
164                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
165                         {
166                             pbi.ExitStatus = reply->exit_code;
167                             pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
168                             pbi.AffinityMask = reply->affinity & affinity_mask;
169                             pbi.BasePriority = reply->priority;
170                             pbi.UniqueProcessId = reply->pid;
171                             pbi.InheritedFromUniqueProcessId = reply->ppid;
172                         }
173                     }
174                     SERVER_END_REQ;
175
176                     memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
177
178                     len = sizeof(PROCESS_BASIC_INFORMATION);
179                 }
180
181                 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
182                     ret = STATUS_INFO_LENGTH_MISMATCH;
183             }
184             else ret = STATUS_INFO_LENGTH_MISMATCH;
185         }
186         break;
187     case ProcessIoCounters:
188         {
189             IO_COUNTERS pii;
190
191             if (ProcessInformationLength >= sizeof(IO_COUNTERS))
192             {
193                 if (!ProcessInformation)
194                     ret = STATUS_ACCESS_VIOLATION;
195                 else if (!ProcessHandle)
196                     ret = STATUS_INVALID_HANDLE;
197                 else
198                 {
199                     /* FIXME : real data */
200                     memset(&pii, 0 , sizeof(IO_COUNTERS));
201
202                     memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
203
204                     len = sizeof(IO_COUNTERS);
205                 }
206
207                 if (ProcessInformationLength > sizeof(IO_COUNTERS))
208                     ret = STATUS_INFO_LENGTH_MISMATCH;
209             }
210             else ret = STATUS_INFO_LENGTH_MISMATCH;
211         }
212         break;
213     case ProcessVmCounters:
214         {
215             VM_COUNTERS pvmi;
216
217             /* older Windows versions don't have the PrivatePageCount field */
218             if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
219             {
220                 if (!ProcessInformation)
221                     ret = STATUS_ACCESS_VIOLATION;
222                 else if (!ProcessHandle)
223                     ret = STATUS_INVALID_HANDLE;
224                 else
225                 {
226                     /* FIXME : real data */
227                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
228
229                     len = ProcessInformationLength;
230                     if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
231
232                     memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
233                 }
234
235                 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
236                     ProcessInformationLength != sizeof(VM_COUNTERS))
237                     ret = STATUS_INFO_LENGTH_MISMATCH;
238             }
239             else ret = STATUS_INFO_LENGTH_MISMATCH;
240         }
241         break;
242     case ProcessTimes:
243         {
244             KERNEL_USER_TIMES pti;
245
246             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
247             {
248                 if (!ProcessInformation)
249                     ret = STATUS_ACCESS_VIOLATION;
250                 else if (!ProcessHandle)
251                     ret = STATUS_INVALID_HANDLE;
252                 else
253                 {
254                     /* FIXME : User- and KernelTime have to be implemented */
255                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
256
257                     SERVER_START_REQ(get_process_info)
258                     {
259                       req->handle = wine_server_obj_handle( ProcessHandle );
260                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
261                       {
262                           pti.CreateTime.QuadPart = reply->start_time;
263                           pti.ExitTime.QuadPart = reply->end_time;
264                       }
265                     }
266                     SERVER_END_REQ;
267
268                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
269
270                     len = sizeof(KERNEL_USER_TIMES);
271                 }
272
273                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
274                     ret = STATUS_INFO_LENGTH_MISMATCH;
275             }
276             else ret = STATUS_INFO_LENGTH_MISMATCH;
277         }
278         break;
279     case ProcessDebugPort:
280         /* "These are not the debuggers you are looking for." *
281          * set it to 0 aka "no debugger" to satisfy copy protections */
282         if (ProcessInformationLength == 4)
283         {
284             memset(ProcessInformation, 0, ProcessInformationLength);
285             len = 4;
286         }
287         else ret = STATUS_INFO_LENGTH_MISMATCH;
288         break;
289     case ProcessHandleCount:
290         if (ProcessInformationLength >= 4)
291         {
292             if (!ProcessInformation)
293                 ret = STATUS_ACCESS_VIOLATION;
294             else if (!ProcessHandle)
295                 ret = STATUS_INVALID_HANDLE;
296             else
297             {
298                 memset(ProcessInformation, 0, 4);
299
300
301                 len = 4;
302             }
303
304             if (ProcessInformationLength > 4)
305                 ret = STATUS_INFO_LENGTH_MISMATCH;
306          }
307          else ret = STATUS_INFO_LENGTH_MISMATCH;
308          break;
309     case ProcessWow64Information:
310         if (ProcessInformationLength == sizeof(DWORD))
311         {
312             DWORD val = 0;
313
314             if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
315             else if (server_cpus & (1 << CPU_x86_64))
316             {
317                 SERVER_START_REQ( get_process_info )
318                 {
319                     req->handle = wine_server_obj_handle( ProcessHandle );
320                     if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
321                 }
322                 SERVER_END_REQ;
323             }
324             *(DWORD *)ProcessInformation = val;
325             len = sizeof(DWORD);
326         }
327         else ret = STATUS_INFO_LENGTH_MISMATCH;
328         break;
329     case ProcessImageFileName:
330         /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
331          * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
332         SERVER_START_REQ(get_dll_info)
333         {
334             UNICODE_STRING *image_file_name_str = ProcessInformation;
335
336             req->handle = wine_server_obj_handle( ProcessHandle );
337             req->base_address = 0; /* main module */
338             wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
339                                    ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
340             ret = wine_server_call( req );
341             if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
342
343             len = sizeof(UNICODE_STRING) + reply->filename_len;
344             if (ret == STATUS_SUCCESS)
345             {
346                 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
347                 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
348             }
349         }
350         SERVER_END_REQ;
351         break;
352     case ProcessExecuteFlags:
353         if (ProcessInformationLength == sizeof(ULONG))
354         {
355             *(ULONG *)ProcessInformation = execute_flags;
356             len = sizeof(ULONG);
357         }
358         else ret = STATUS_INFO_LENGTH_MISMATCH;
359         break;
360     default:
361         FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
362               ProcessHandle,ProcessInformationClass,
363               ProcessInformation,ProcessInformationLength,
364               ReturnLength);
365         ret = STATUS_INVALID_INFO_CLASS;
366         break;
367     }
368
369     if (ReturnLength) *ReturnLength = len;
370     
371     return ret;
372 }
373
374 /******************************************************************************
375  * NtSetInformationProcess [NTDLL.@]
376  * ZwSetInformationProcess [NTDLL.@]
377  */
378 NTSTATUS WINAPI NtSetInformationProcess(
379         IN HANDLE ProcessHandle,
380         IN PROCESSINFOCLASS ProcessInformationClass,
381         IN PVOID ProcessInformation,
382         IN ULONG ProcessInformationLength)
383 {
384     NTSTATUS ret = STATUS_SUCCESS;
385
386     switch (ProcessInformationClass)
387     {
388     case ProcessAffinityMask:
389         if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
390         if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
391             return STATUS_INVALID_PARAMETER;
392         SERVER_START_REQ( set_process_info )
393         {
394             req->handle   = wine_server_obj_handle( ProcessHandle );
395             req->affinity = *(PDWORD_PTR)ProcessInformation;
396             req->mask     = SET_PROCESS_INFO_AFFINITY;
397             ret = wine_server_call( req );
398         }
399         SERVER_END_REQ;
400         break;
401     case ProcessPriorityClass:
402         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
403             return STATUS_INVALID_PARAMETER;
404         else
405         {
406             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
407
408             SERVER_START_REQ( set_process_info )
409             {
410                 req->handle   = wine_server_obj_handle( ProcessHandle );
411                 /* FIXME Foreground isn't used */
412                 req->priority = ppc->PriorityClass;
413                 req->mask     = SET_PROCESS_INFO_PRIORITY;
414                 ret = wine_server_call( req );
415             }
416             SERVER_END_REQ;
417         }
418         break;
419
420     case ProcessExecuteFlags:
421         if (ProcessInformationLength != sizeof(ULONG))
422             return STATUS_INVALID_PARAMETER;
423         else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
424             return STATUS_ACCESS_DENIED;
425         else
426         {
427             BOOL enable;
428             switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
429             {
430             case MEM_EXECUTE_OPTION_ENABLE:
431                 enable = TRUE;
432                 break;
433             case MEM_EXECUTE_OPTION_DISABLE:
434                 enable = FALSE;
435                 break;
436             default:
437                 return STATUS_INVALID_PARAMETER;
438             }
439             execute_flags = *(ULONG *)ProcessInformation;
440             VIRTUAL_SetForceExec( enable );
441         }
442         break;
443
444     default:
445         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
446               ProcessHandle,ProcessInformationClass,ProcessInformation,
447               ProcessInformationLength);
448         ret = STATUS_NOT_IMPLEMENTED;
449         break;
450     }
451     return ret;
452 }
453
454 /******************************************************************************
455  * NtFlushInstructionCache [NTDLL.@]
456  * ZwFlushInstructionCache [NTDLL.@]
457  */
458 NTSTATUS WINAPI NtFlushInstructionCache(
459         IN HANDLE ProcessHandle,
460         IN LPCVOID BaseAddress,
461         IN SIZE_T Size)
462 {
463 #ifdef __i386__
464     TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
465 #else
466     FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
467 #endif
468     return STATUS_SUCCESS;
469 }
470
471 /******************************************************************
472  *              NtOpenProcess [NTDLL.@]
473  *              ZwOpenProcess [NTDLL.@]
474  */
475 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
476                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
477 {
478     NTSTATUS    status;
479
480     SERVER_START_REQ( open_process )
481     {
482         req->pid        = HandleToULong(cid->UniqueProcess);
483         req->access     = access;
484         req->attributes = attr ? attr->Attributes : 0;
485         status = wine_server_call( req );
486         if (!status) *handle = wine_server_ptr_handle( reply->handle );
487     }
488     SERVER_END_REQ;
489     return status;
490 }