wined3d: Explicitly pass the context to get_drawable_size().
[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 /*
41  *      Process object
42  */
43
44 /******************************************************************************
45  *  NtTerminateProcess                  [NTDLL.@]
46  *
47  *  Native applications must kill themselves when done
48  */
49 NTSTATUS WINAPI NtTerminateProcess( HANDLE handle, LONG exit_code )
50 {
51     NTSTATUS ret;
52     BOOL self;
53     SERVER_START_REQ( terminate_process )
54     {
55         req->handle    = wine_server_obj_handle( handle );
56         req->exit_code = exit_code;
57         ret = wine_server_call( req );
58         self = !ret && reply->self;
59     }
60     SERVER_END_REQ;
61     if (self) exit( exit_code );
62     return ret;
63 }
64
65 /******************************************************************************
66  *  RtlGetCurrentPeb  [NTDLL.@]
67  *
68  */
69 PEB * WINAPI RtlGetCurrentPeb(void)
70 {
71     return NtCurrentTeb()->Peb;
72 }
73
74 /***********************************************************************
75  *           __wine_make_process_system   (NTDLL.@)
76  *
77  * Mark the current process as a system process.
78  * Returns the event that is signaled when all non-system processes have exited.
79  */
80 HANDLE CDECL __wine_make_process_system(void)
81 {
82     HANDLE ret = 0;
83     SERVER_START_REQ( make_process_system )
84     {
85         if (!wine_server_call( req )) ret = wine_server_ptr_handle( reply->event );
86     }
87     SERVER_END_REQ;
88     return ret;
89 }
90
91
92 #define UNIMPLEMENTED_INFO_CLASS(c) \
93     case c: \
94         FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
95         ret = STATUS_INVALID_INFO_CLASS; \
96         break
97
98 /******************************************************************************
99 *  NtQueryInformationProcess            [NTDLL.@]
100 *  ZwQueryInformationProcess            [NTDLL.@]
101 *
102 */
103 NTSTATUS WINAPI NtQueryInformationProcess(
104         IN HANDLE ProcessHandle,
105         IN PROCESSINFOCLASS ProcessInformationClass,
106         OUT PVOID ProcessInformation,
107         IN ULONG ProcessInformationLength,
108         OUT PULONG ReturnLength)
109 {
110     NTSTATUS ret = STATUS_SUCCESS;
111     ULONG len = 0;
112
113     TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
114           ProcessHandle,ProcessInformationClass,
115           ProcessInformation,ProcessInformationLength,
116           ReturnLength);
117
118     switch (ProcessInformationClass) 
119     {
120     UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
121     UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
122     UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
123     UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
124     UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
125     UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
126     UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
127     UNIMPLEMENTED_INFO_CLASS(ProcessDefaultHardErrorMode);
128     UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers);
129     UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits);
130     UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch);
131     UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL);
132     UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup);
133     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass);
134     UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information);
135     UNIMPLEMENTED_INFO_CLASS(ProcessAffinityMask);
136     UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost);
137     UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
138     UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
139     UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
140     UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
141     UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
142     UNIMPLEMENTED_INFO_CLASS(ProcessDebugObjectHandle);
143     UNIMPLEMENTED_INFO_CLASS(ProcessDebugFlags);
144     UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
145
146     case ProcessBasicInformation:
147         {
148             PROCESS_BASIC_INFORMATION pbi;
149             const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
150
151             if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
152             {
153                 if (!ProcessInformation)
154                     ret = STATUS_ACCESS_VIOLATION;
155                 else if (!ProcessHandle)
156                     ret = STATUS_INVALID_HANDLE;
157                 else
158                 {
159                     SERVER_START_REQ(get_process_info)
160                     {
161                         req->handle = wine_server_obj_handle( ProcessHandle );
162                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
163                         {
164                             pbi.ExitStatus = reply->exit_code;
165                             pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
166                             pbi.AffinityMask = reply->affinity & affinity_mask;
167                             pbi.BasePriority = reply->priority;
168                             pbi.UniqueProcessId = reply->pid;
169                             pbi.InheritedFromUniqueProcessId = reply->ppid;
170                         }
171                     }
172                     SERVER_END_REQ;
173
174                     memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
175
176                     len = sizeof(PROCESS_BASIC_INFORMATION);
177                 }
178
179                 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
180                     ret = STATUS_INFO_LENGTH_MISMATCH;
181             }
182             else ret = STATUS_INFO_LENGTH_MISMATCH;
183         }
184         break;
185     case ProcessIoCounters:
186         {
187             IO_COUNTERS pii;
188
189             if (ProcessInformationLength >= sizeof(IO_COUNTERS))
190             {
191                 if (!ProcessInformation)
192                     ret = STATUS_ACCESS_VIOLATION;
193                 else if (!ProcessHandle)
194                     ret = STATUS_INVALID_HANDLE;
195                 else
196                 {
197                     /* FIXME : real data */
198                     memset(&pii, 0 , sizeof(IO_COUNTERS));
199
200                     memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
201
202                     len = sizeof(IO_COUNTERS);
203                 }
204
205                 if (ProcessInformationLength > sizeof(IO_COUNTERS))
206                     ret = STATUS_INFO_LENGTH_MISMATCH;
207             }
208             else ret = STATUS_INFO_LENGTH_MISMATCH;
209         }
210         break;
211     case ProcessVmCounters:
212         {
213             VM_COUNTERS pvmi;
214
215             /* older Windows versions don't have the PrivatePageCount field */
216             if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
217             {
218                 if (!ProcessInformation)
219                     ret = STATUS_ACCESS_VIOLATION;
220                 else if (!ProcessHandle)
221                     ret = STATUS_INVALID_HANDLE;
222                 else
223                 {
224                     /* FIXME : real data */
225                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
226
227                     len = ProcessInformationLength;
228                     if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
229
230                     memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
231                 }
232
233                 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
234                     ProcessInformationLength != sizeof(VM_COUNTERS))
235                     ret = STATUS_INFO_LENGTH_MISMATCH;
236             }
237             else ret = STATUS_INFO_LENGTH_MISMATCH;
238         }
239         break;
240     case ProcessTimes:
241         {
242             KERNEL_USER_TIMES pti;
243
244             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
245             {
246                 if (!ProcessInformation)
247                     ret = STATUS_ACCESS_VIOLATION;
248                 else if (!ProcessHandle)
249                     ret = STATUS_INVALID_HANDLE;
250                 else
251                 {
252                     /* FIXME : User- and KernelTime have to be implemented */
253                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
254
255                     SERVER_START_REQ(get_process_info)
256                     {
257                       req->handle = wine_server_obj_handle( ProcessHandle );
258                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
259                       {
260                           pti.CreateTime.QuadPart = reply->start_time;
261                           pti.ExitTime.QuadPart = reply->end_time;
262                       }
263                     }
264                     SERVER_END_REQ;
265
266                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
267
268                     len = sizeof(KERNEL_USER_TIMES);
269                 }
270
271                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
272                     ret = STATUS_INFO_LENGTH_MISMATCH;
273             }
274             else ret = STATUS_INFO_LENGTH_MISMATCH;
275         }
276         break;
277     case ProcessDebugPort:
278         /* "These are not the debuggers you are looking for." *
279          * set it to 0 aka "no debugger" to satisfy copy protections */
280         if (ProcessInformationLength == 4)
281         {
282             memset(ProcessInformation, 0, ProcessInformationLength);
283             len = 4;
284         }
285         else ret = STATUS_INFO_LENGTH_MISMATCH;
286         break;
287     case ProcessHandleCount:
288         if (ProcessInformationLength >= 4)
289         {
290             if (!ProcessInformation)
291                 ret = STATUS_ACCESS_VIOLATION;
292             else if (!ProcessHandle)
293                 ret = STATUS_INVALID_HANDLE;
294             else
295             {
296                 memset(ProcessInformation, 0, 4);
297
298
299                 len = 4;
300             }
301
302             if (ProcessInformationLength > 4)
303                 ret = STATUS_INFO_LENGTH_MISMATCH;
304          }
305          else ret = STATUS_INFO_LENGTH_MISMATCH;
306          break;
307     case ProcessWow64Information:
308         if (ProcessInformationLength == sizeof(DWORD))
309         {
310             DWORD val = 0;
311
312             if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
313             else if (server_cpus & (1 << CPU_x86_64))
314             {
315                 SERVER_START_REQ( get_process_info )
316                 {
317                     req->handle = wine_server_obj_handle( ProcessHandle );
318                     if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
319                 }
320                 SERVER_END_REQ;
321             }
322             *(DWORD *)ProcessInformation = val;
323             len = sizeof(DWORD);
324         }
325         else ret = STATUS_INFO_LENGTH_MISMATCH;
326         break;
327     case ProcessImageFileName:
328         /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
329          * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
330         SERVER_START_REQ(get_dll_info)
331         {
332             UNICODE_STRING *image_file_name_str = ProcessInformation;
333
334             req->handle = wine_server_obj_handle( ProcessHandle );
335             req->base_address = 0; /* main module */
336             wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
337                                    ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
338             ret = wine_server_call( req );
339             if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
340
341             len = sizeof(UNICODE_STRING) + reply->filename_len;
342             if (ret == STATUS_SUCCESS)
343             {
344                 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
345                 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
346             }
347         }
348         SERVER_END_REQ;
349         break;
350     default:
351         FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
352               ProcessHandle,ProcessInformationClass,
353               ProcessInformation,ProcessInformationLength,
354               ReturnLength);
355         ret = STATUS_INVALID_INFO_CLASS;
356         break;
357     }
358
359     if (ReturnLength) *ReturnLength = len;
360     
361     return ret;
362 }
363
364 /******************************************************************************
365  * NtSetInformationProcess [NTDLL.@]
366  * ZwSetInformationProcess [NTDLL.@]
367  */
368 NTSTATUS WINAPI NtSetInformationProcess(
369         IN HANDLE ProcessHandle,
370         IN PROCESSINFOCLASS ProcessInformationClass,
371         IN PVOID ProcessInformation,
372         IN ULONG ProcessInformationLength)
373 {
374     NTSTATUS ret = STATUS_SUCCESS;
375
376     switch (ProcessInformationClass)
377     {
378     case ProcessAffinityMask:
379         if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
380         if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
381             return STATUS_INVALID_PARAMETER;
382         SERVER_START_REQ( set_process_info )
383         {
384             req->handle   = wine_server_obj_handle( ProcessHandle );
385             req->affinity = *(PDWORD_PTR)ProcessInformation;
386             req->mask     = SET_PROCESS_INFO_AFFINITY;
387             ret = wine_server_call( req );
388         }
389         SERVER_END_REQ;
390         break;
391     case ProcessPriorityClass:
392         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
393             return STATUS_INVALID_PARAMETER;
394         else
395         {
396             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
397
398             SERVER_START_REQ( set_process_info )
399             {
400                 req->handle   = wine_server_obj_handle( ProcessHandle );
401                 /* FIXME Foreground isn't used */
402                 req->priority = ppc->PriorityClass;
403                 req->mask     = SET_PROCESS_INFO_PRIORITY;
404                 ret = wine_server_call( req );
405             }
406             SERVER_END_REQ;
407         }
408         break;
409
410     case ProcessExecuteFlags:
411         if (ProcessInformationLength != sizeof(ULONG))
412             return STATUS_INVALID_PARAMETER;
413         else
414         {
415             BOOL enable;
416             switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
417             {
418             case MEM_EXECUTE_OPTION_ENABLE:
419                 enable = FALSE;
420                 break;
421             case MEM_EXECUTE_OPTION_DISABLE:
422                 enable = TRUE;
423                 break;
424             default:
425                 return STATUS_INVALID_PARAMETER;
426             }
427             VIRTUAL_SetForceExec( enable );
428         }
429         break;
430
431     default:
432         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
433               ProcessHandle,ProcessInformationClass,ProcessInformation,
434               ProcessInformationLength);
435         ret = STATUS_NOT_IMPLEMENTED;
436         break;
437     }
438     return ret;
439 }
440
441 /******************************************************************************
442  * NtFlushInstructionCache [NTDLL.@]
443  * ZwFlushInstructionCache [NTDLL.@]
444  */
445 NTSTATUS WINAPI NtFlushInstructionCache(
446         IN HANDLE ProcessHandle,
447         IN LPCVOID BaseAddress,
448         IN SIZE_T Size)
449 {
450 #ifdef __i386__
451     TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
452 #else
453     FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
454 #endif
455     return STATUS_SUCCESS;
456 }
457
458 /******************************************************************
459  *              NtOpenProcess [NTDLL.@]
460  *              ZwOpenProcess [NTDLL.@]
461  */
462 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
463                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
464 {
465     NTSTATUS    status;
466
467     SERVER_START_REQ( open_process )
468     {
469         req->pid        = HandleToULong(cid->UniqueProcess);
470         req->access     = access;
471         req->attributes = attr ? attr->Attributes : 0;
472         status = wine_server_call( req );
473         if (!status) *handle = wine_server_ptr_handle( reply->handle );
474     }
475     SERVER_END_REQ;
476     return status;
477 }