Release 1.5.29.
[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_DISABLE;
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 && handle) 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 static UINT process_error_mode;
94
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
100
101 /******************************************************************************
102 *  NtQueryInformationProcess            [NTDLL.@]
103 *  ZwQueryInformationProcess            [NTDLL.@]
104 *
105 */
106 NTSTATUS WINAPI NtQueryInformationProcess(
107         IN HANDLE ProcessHandle,
108         IN PROCESSINFOCLASS ProcessInformationClass,
109         OUT PVOID ProcessInformation,
110         IN ULONG ProcessInformationLength,
111         OUT PULONG ReturnLength)
112 {
113     NTSTATUS ret = STATUS_SUCCESS;
114     ULONG len = 0;
115
116     TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
117           ProcessHandle,ProcessInformationClass,
118           ProcessInformation,ProcessInformationLength,
119           ReturnLength);
120
121     switch (ProcessInformationClass) 
122     {
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);
144
145     case ProcessBasicInformation:
146         {
147             PROCESS_BASIC_INFORMATION pbi;
148             const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
149
150             if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
151             {
152                 if (!ProcessInformation)
153                     ret = STATUS_ACCESS_VIOLATION;
154                 else if (!ProcessHandle)
155                     ret = STATUS_INVALID_HANDLE;
156                 else
157                 {
158                     SERVER_START_REQ(get_process_info)
159                     {
160                         req->handle = wine_server_obj_handle( ProcessHandle );
161                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
162                         {
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;
169                         }
170                     }
171                     SERVER_END_REQ;
172
173                     memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
174
175                     len = sizeof(PROCESS_BASIC_INFORMATION);
176                 }
177
178                 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
179                     ret = STATUS_INFO_LENGTH_MISMATCH;
180             }
181             else
182             {
183                 len = sizeof(PROCESS_BASIC_INFORMATION);
184                 ret = STATUS_INFO_LENGTH_MISMATCH;
185             }
186         }
187         break;
188     case ProcessIoCounters:
189         {
190             IO_COUNTERS pii;
191
192             if (ProcessInformationLength >= sizeof(IO_COUNTERS))
193             {
194                 if (!ProcessInformation)
195                     ret = STATUS_ACCESS_VIOLATION;
196                 else if (!ProcessHandle)
197                     ret = STATUS_INVALID_HANDLE;
198                 else
199                 {
200                     /* FIXME : real data */
201                     memset(&pii, 0 , sizeof(IO_COUNTERS));
202
203                     memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
204
205                     len = sizeof(IO_COUNTERS);
206                 }
207
208                 if (ProcessInformationLength > sizeof(IO_COUNTERS))
209                     ret = STATUS_INFO_LENGTH_MISMATCH;
210             }
211             else
212             {
213                 len = sizeof(IO_COUNTERS);
214                 ret = STATUS_INFO_LENGTH_MISMATCH;
215             }
216         }
217         break;
218     case ProcessVmCounters:
219         {
220             VM_COUNTERS pvmi;
221
222             /* older Windows versions don't have the PrivatePageCount field */
223             if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
224             {
225                 if (!ProcessInformation)
226                     ret = STATUS_ACCESS_VIOLATION;
227                 else if (!ProcessHandle)
228                     ret = STATUS_INVALID_HANDLE;
229                 else
230                 {
231                     /* FIXME : real data */
232                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
233
234                     len = ProcessInformationLength;
235                     if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
236
237                     memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
238                 }
239
240                 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
241                     ProcessInformationLength != sizeof(VM_COUNTERS))
242                     ret = STATUS_INFO_LENGTH_MISMATCH;
243             }
244             else
245             {
246                 len = sizeof(pvmi);
247                 ret = STATUS_INFO_LENGTH_MISMATCH;
248             }
249         }
250         break;
251     case ProcessTimes:
252         {
253             KERNEL_USER_TIMES pti;
254
255             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
256             {
257                 if (!ProcessInformation)
258                     ret = STATUS_ACCESS_VIOLATION;
259                 else if (!ProcessHandle)
260                     ret = STATUS_INVALID_HANDLE;
261                 else
262                 {
263                     /* FIXME : User- and KernelTime have to be implemented */
264                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
265
266                     SERVER_START_REQ(get_process_info)
267                     {
268                       req->handle = wine_server_obj_handle( ProcessHandle );
269                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
270                       {
271                           pti.CreateTime.QuadPart = reply->start_time;
272                           pti.ExitTime.QuadPart = reply->end_time;
273                       }
274                     }
275                     SERVER_END_REQ;
276
277                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
278                     len = sizeof(KERNEL_USER_TIMES);
279                 }
280
281                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
282                     ret = STATUS_INFO_LENGTH_MISMATCH;
283             }
284             else
285             {
286                 len = sizeof(KERNEL_USER_TIMES);
287                 ret = STATUS_INFO_LENGTH_MISMATCH;
288             }
289         }
290         break;
291     case ProcessDebugPort:
292         len = sizeof(DWORD_PTR);
293         if (ProcessInformationLength == len)
294         {
295             if (!ProcessInformation)
296                 ret = STATUS_ACCESS_VIOLATION;
297             else if (!ProcessHandle)
298                 ret = STATUS_INVALID_HANDLE;
299             else
300             {
301                 SERVER_START_REQ(get_process_info)
302                 {
303                     req->handle = wine_server_obj_handle( ProcessHandle );
304                     if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
305                     {
306                         *(DWORD_PTR *)ProcessInformation = reply->debugger_present ? ~(DWORD_PTR)0 : 0;
307                     }
308                 }
309                 SERVER_END_REQ;
310             }
311         }
312         else
313             ret = STATUS_INFO_LENGTH_MISMATCH;
314         break;
315     case ProcessDebugFlags:
316         len = sizeof(DWORD);
317         if (ProcessInformationLength == len)
318         {
319             if (!ProcessInformation)
320                 ret = STATUS_ACCESS_VIOLATION;
321             else if (!ProcessHandle)
322                 ret = STATUS_INVALID_HANDLE;
323             else
324             {
325                 SERVER_START_REQ(get_process_info)
326                 {
327                     req->handle = wine_server_obj_handle( ProcessHandle );
328                     if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
329                     {
330                         *(DWORD *)ProcessInformation = !reply->debugger_present;
331                     }
332                 }
333                 SERVER_END_REQ;
334             }
335         }
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)
351         {
352             if (!ProcessInformation)
353                 ret = STATUS_ACCESS_VIOLATION;
354             else if (!ProcessHandle)
355                 ret = STATUS_INVALID_HANDLE;
356             else
357             {
358                 memset(ProcessInformation, 0, ProcessInformationLength);
359                 ret = STATUS_PORT_NOT_SET;
360             }
361         }
362         else
363             ret = STATUS_INFO_LENGTH_MISMATCH;
364         break;
365     case ProcessHandleCount:
366         if (ProcessInformationLength >= 4)
367         {
368             if (!ProcessInformation)
369                 ret = STATUS_ACCESS_VIOLATION;
370             else if (!ProcessHandle)
371                 ret = STATUS_INVALID_HANDLE;
372             else
373             {
374                 memset(ProcessInformation, 0, 4);
375                 len = 4;
376             }
377
378             if (ProcessInformationLength > 4)
379                 ret = STATUS_INFO_LENGTH_MISMATCH;
380         }
381         else
382         {
383             len = 4;
384             ret = STATUS_INFO_LENGTH_MISMATCH;
385         }
386         break;
387
388     case ProcessAffinityMask:
389         len = sizeof(ULONG_PTR);
390         if (ProcessInformationLength == len)
391         {
392             const ULONG_PTR system_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
393
394             SERVER_START_REQ(get_process_info)
395             {
396                 req->handle = wine_server_obj_handle( ProcessHandle );
397                 if (!(ret = wine_server_call( req )))
398                     *(ULONG_PTR *)ProcessInformation = reply->affinity & system_mask;
399             }
400             SERVER_END_REQ;
401         }
402         else ret = STATUS_INFO_LENGTH_MISMATCH;
403         break;
404
405     case ProcessWow64Information:
406         len = sizeof(DWORD);
407         if (ProcessInformationLength == len)
408         {
409             DWORD val = 0;
410
411             if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
412             else if (server_cpus & (1 << CPU_x86_64))
413             {
414                 SERVER_START_REQ( get_process_info )
415                 {
416                     req->handle = wine_server_obj_handle( ProcessHandle );
417                     if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
418                 }
419                 SERVER_END_REQ;
420             }
421             *(DWORD *)ProcessInformation = val;
422         }
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)
429         {
430             UNICODE_STRING *image_file_name_str = ProcessInformation;
431
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;
438
439             len = sizeof(UNICODE_STRING) + reply->filename_len;
440             if (ret == STATUS_SUCCESS)
441             {
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);
444             }
445         }
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;
462     }
463
464     if (ReturnLength) *ReturnLength = len;
465     
466     return ret;
467 }
468
469 /******************************************************************************
470  * NtSetInformationProcess [NTDLL.@]
471  * ZwSetInformationProcess [NTDLL.@]
472  */
473 NTSTATUS WINAPI NtSetInformationProcess(
474         IN HANDLE ProcessHandle,
475         IN PROCESSINFOCLASS ProcessInformationClass,
476         IN PVOID ProcessInformation,
477         IN ULONG ProcessInformationLength)
478 {
479     NTSTATUS ret = STATUS_SUCCESS;
480
481     switch (ProcessInformationClass)
482     {
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 )
494         {
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 );
499         }
500         SERVER_END_REQ;
501         break;
502     case ProcessPriorityClass:
503         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
504             return STATUS_INVALID_PARAMETER;
505         else
506         {
507             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
508
509             SERVER_START_REQ( set_process_info )
510             {
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 );
516             }
517             SERVER_END_REQ;
518         }
519         break;
520
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
527         {
528             BOOL enable;
529             switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
530             {
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;
539             }
540             execute_flags = *(ULONG *)ProcessInformation;
541             VIRTUAL_SetForceExec( enable );
542         }
543         break;
544
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;
551     }
552     return ret;
553 }
554
555 /******************************************************************************
556  * NtFlushInstructionCache [NTDLL.@]
557  * ZwFlushInstructionCache [NTDLL.@]
558  */
559 NTSTATUS WINAPI NtFlushInstructionCache(
560         IN HANDLE ProcessHandle,
561         IN LPCVOID BaseAddress,
562         IN SIZE_T Size)
563 {
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;
570 }
571
572 /******************************************************************
573  *              NtOpenProcess [NTDLL.@]
574  *              ZwOpenProcess [NTDLL.@]
575  */
576 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
577                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
578 {
579     NTSTATUS    status;
580
581     SERVER_START_REQ( open_process )
582     {
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 );
588     }
589     SERVER_END_REQ;
590     return status;
591 }