riched20: Constify some variables.
[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    = 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 __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 = 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(ProcessImageFileName);
141     UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
142     UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
143     UNIMPLEMENTED_INFO_CLASS(ProcessDebugObjectHandle);
144     UNIMPLEMENTED_INFO_CLASS(ProcessDebugFlags);
145     UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
146
147     case ProcessBasicInformation:
148         {
149             PROCESS_BASIC_INFORMATION pbi;
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 = ProcessHandle;
162                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
163                         {
164                             pbi.ExitStatus = reply->exit_code;
165                             pbi.PebBaseAddress = reply->peb;
166                             pbi.AffinityMask = reply->affinity;
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             if (ProcessInformationLength >= sizeof(VM_COUNTERS))
216             {
217                 if (!ProcessInformation)
218                     ret = STATUS_ACCESS_VIOLATION;
219                 else if (!ProcessHandle)
220                     ret = STATUS_INVALID_HANDLE;
221                 else
222                 {
223                     /* FIXME : real data */
224                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
225
226                     memcpy(ProcessInformation, &pvmi, sizeof(VM_COUNTERS));
227
228                     len = sizeof(VM_COUNTERS);
229                 }
230
231                 if (ProcessInformationLength > sizeof(VM_COUNTERS))
232                     ret = STATUS_INFO_LENGTH_MISMATCH;
233             }
234             else ret = STATUS_INFO_LENGTH_MISMATCH;
235         }
236         break;
237     case ProcessTimes:
238         {
239             KERNEL_USER_TIMES pti;
240
241             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
242             {
243                 if (!ProcessInformation)
244                     ret = STATUS_ACCESS_VIOLATION;
245                 else if (!ProcessHandle)
246                     ret = STATUS_INVALID_HANDLE;
247                 else
248                 {
249                     /* FIXME : User- and KernelTime have to be implemented */
250                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
251
252                     SERVER_START_REQ(get_process_info)
253                     {
254                       req->handle = ProcessHandle;
255                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
256                       {
257                           pti.CreateTime.QuadPart = reply->start_time;
258                           pti.ExitTime.QuadPart = reply->end_time;
259                       }
260                     }
261                     SERVER_END_REQ;
262
263                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
264
265                     len = sizeof(KERNEL_USER_TIMES);
266                 }
267
268                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
269                     ret = STATUS_INFO_LENGTH_MISMATCH;
270             }
271             else ret = STATUS_INFO_LENGTH_MISMATCH;
272         }
273         break;
274     case ProcessDebugPort:
275         /* "These are not the debuggers you are looking for." *
276          * set it to 0 aka "no debugger" to satisfy copy protections */
277         if (ProcessInformationLength == 4)
278         {
279             memset(ProcessInformation, 0, ProcessInformationLength);
280             len = 4;
281         }
282         else ret = STATUS_INFO_LENGTH_MISMATCH;
283         break;
284     case ProcessHandleCount:
285         if (ProcessInformationLength >= 4)
286         {
287             if (!ProcessInformation)
288                 ret = STATUS_ACCESS_VIOLATION;
289             else if (!ProcessHandle)
290                 ret = STATUS_INVALID_HANDLE;
291             else
292             {
293                 memset(ProcessInformation, 0, 4);
294
295
296                 len = 4;
297             }
298
299             if (ProcessInformationLength > 4)
300                 ret = STATUS_INFO_LENGTH_MISMATCH;
301          }
302          else ret = STATUS_INFO_LENGTH_MISMATCH;
303          break;
304     case ProcessWow64Information:
305         if (ProcessInformationLength == 4)
306         {
307             memset(ProcessInformation, 0, ProcessInformationLength);
308             len = 4;
309         }
310         else ret = STATUS_INFO_LENGTH_MISMATCH;
311         break;
312     default:
313         FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
314               ProcessHandle,ProcessInformationClass,
315               ProcessInformation,ProcessInformationLength,
316               ReturnLength);
317         ret = STATUS_INVALID_INFO_CLASS;
318         break;
319     }
320
321     if (ReturnLength) *ReturnLength = len;
322     
323     return ret;
324 }
325
326 /******************************************************************************
327  * NtSetInformationProcess [NTDLL.@]
328  * ZwSetInformationProcess [NTDLL.@]
329  */
330 NTSTATUS WINAPI NtSetInformationProcess(
331         IN HANDLE ProcessHandle,
332         IN PROCESSINFOCLASS ProcessInformationClass,
333         IN PVOID ProcessInformation,
334         IN ULONG ProcessInformationLength)
335 {
336     NTSTATUS ret = STATUS_SUCCESS;
337
338     switch (ProcessInformationClass)
339     {
340     case ProcessAffinityMask:
341         if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
342         SERVER_START_REQ( set_process_info )
343         {
344             req->handle   = ProcessHandle;
345             req->affinity = *(PDWORD_PTR)ProcessInformation;
346             req->mask     = SET_PROCESS_INFO_AFFINITY;
347             ret = wine_server_call( req );
348         }
349         SERVER_END_REQ;
350         break;
351     case ProcessPriorityClass:
352         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
353             return STATUS_INVALID_PARAMETER;
354         else
355         {
356             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
357
358             SERVER_START_REQ( set_process_info )
359             {
360                 req->handle   = ProcessHandle;
361                 /* FIXME Foreground isn't used */
362                 req->priority = ppc->PriorityClass;
363                 req->mask     = SET_PROCESS_INFO_PRIORITY;
364                 ret = wine_server_call( req );
365             }
366             SERVER_END_REQ;
367         }
368         break;
369     default:
370         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
371               ProcessHandle,ProcessInformationClass,ProcessInformation,
372               ProcessInformationLength);
373         ret = STATUS_NOT_IMPLEMENTED;
374         break;
375     }
376     return ret;
377 }
378
379 /******************************************************************************
380  * NtFlushInstructionCache [NTDLL.@]
381  * ZwFlushInstructionCache [NTDLL.@]
382  */
383 NTSTATUS WINAPI NtFlushInstructionCache(
384         IN HANDLE ProcessHandle,
385         IN LPCVOID BaseAddress,
386         IN SIZE_T Size)
387 {
388 #ifdef __i386__
389     TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
390 #else
391     FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
392 #endif
393     return STATUS_SUCCESS;
394 }
395
396 /******************************************************************
397  *              NtOpenProcess [NTDLL.@]
398  *              ZwOpenProcess [NTDLL.@]
399  */
400 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
401                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
402 {
403     NTSTATUS    status;
404
405     SERVER_START_REQ( open_process )
406     {
407         req->pid        = HandleToULong(cid->UniqueProcess);
408         req->access     = access;
409         req->attributes = attr ? attr->Attributes : 0;
410         status = wine_server_call( req );
411         if (!status) *handle = reply->handle;
412     }
413     SERVER_END_REQ;
414     return status;
415 }