ntdll: Use module for path to activation context.
[wine] / dlls / ntdll / sync.c
1 /*
2  *      Process synchronisation
3  *
4  * Copyright 1996, 1997, 1998 Marcus Meissner
5  * Copyright 1997, 1999 Alexandre Julliard
6  * Copyright 1999, 2000 Juergen Schmied
7  * Copyright 2003 Eric Pouech
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 "config.h"
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <signal.h>
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_POLL_H
33 #include <poll.h>
34 #endif
35 #ifdef HAVE_SYS_POLL_H
36 # include <sys/poll.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #ifdef HAVE_SCHED_H
42 # include <sched.h>
43 #endif
44 #include <string.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <time.h>
49
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
52
53 #include "ntstatus.h"
54 #define WIN32_NO_STATUS
55 #include "windef.h"
56 #include "winternl.h"
57 #include "wine/server.h"
58 #include "wine/debug.h"
59 #include "ntdll_misc.h"
60
61 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
62
63 /* creates a struct security_descriptor and contained information in one contiguous piece of memory */
64 NTSTATUS NTDLL_create_struct_sd(PSECURITY_DESCRIPTOR nt_sd, struct security_descriptor **server_sd,
65                                 data_size_t *server_sd_len)
66 {
67     unsigned int len;
68     PSID owner, group;
69     ACL *dacl, *sacl;
70     BOOLEAN owner_present, group_present, dacl_present, sacl_present;
71     BOOLEAN defaulted;
72     NTSTATUS status;
73     unsigned char *ptr;
74
75     if (!nt_sd)
76     {
77         *server_sd = NULL;
78         *server_sd_len = 0;
79         return STATUS_SUCCESS;
80     }
81
82     len = sizeof(struct security_descriptor);
83
84     status = RtlGetOwnerSecurityDescriptor(nt_sd, &owner, &owner_present);
85     if (status != STATUS_SUCCESS) return status;
86     status = RtlGetGroupSecurityDescriptor(nt_sd, &group, &group_present);
87     if (status != STATUS_SUCCESS) return status;
88     status = RtlGetSaclSecurityDescriptor(nt_sd, &sacl_present, &sacl, &defaulted);
89     if (status != STATUS_SUCCESS) return status;
90     status = RtlGetDaclSecurityDescriptor(nt_sd, &dacl_present, &dacl, &defaulted);
91     if (status != STATUS_SUCCESS) return status;
92
93     if (owner_present)
94         len += RtlLengthSid(owner);
95     if (group_present)
96         len += RtlLengthSid(group);
97     if (sacl_present && sacl)
98         len += sacl->AclSize;
99     if (dacl_present && dacl)
100         len += dacl->AclSize;
101
102     /* fix alignment for the Unicode name that follows the structure */
103     len = (len + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
104     *server_sd = RtlAllocateHeap(GetProcessHeap(), 0, len);
105     if (!*server_sd) return STATUS_NO_MEMORY;
106
107     (*server_sd)->control = ((SECURITY_DESCRIPTOR *)nt_sd)->Control & ~SE_SELF_RELATIVE;
108     (*server_sd)->owner_len = owner_present ? RtlLengthSid(owner) : 0;
109     (*server_sd)->group_len = group_present ? RtlLengthSid(group) : 0;
110     (*server_sd)->sacl_len = (sacl_present && sacl) ? sacl->AclSize : 0;
111     (*server_sd)->dacl_len = (dacl_present && dacl) ? dacl->AclSize : 0;
112
113     ptr = (unsigned char *)(*server_sd + 1);
114     memcpy(ptr, owner, (*server_sd)->owner_len);
115     ptr += (*server_sd)->owner_len;
116     memcpy(ptr, group, (*server_sd)->group_len);
117     ptr += (*server_sd)->group_len;
118     memcpy(ptr, sacl, (*server_sd)->sacl_len);
119     ptr += (*server_sd)->sacl_len;
120     memcpy(ptr, dacl, (*server_sd)->dacl_len);
121
122     *server_sd_len = len;
123
124     return STATUS_SUCCESS;
125 }
126
127 /* frees a struct security_descriptor allocated by NTDLL_create_struct_sd */
128 void NTDLL_free_struct_sd(struct security_descriptor *server_sd)
129 {
130     RtlFreeHeap(GetProcessHeap(), 0, server_sd);
131 }
132
133 /*
134  *      Semaphores
135  */
136
137 /******************************************************************************
138  *  NtCreateSemaphore (NTDLL.@)
139  */
140 NTSTATUS WINAPI NtCreateSemaphore( OUT PHANDLE SemaphoreHandle,
141                                    IN ACCESS_MASK access,
142                                    IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
143                                    IN LONG InitialCount,
144                                    IN LONG MaximumCount )
145 {
146     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
147     NTSTATUS ret;
148     struct object_attributes objattr;
149     struct security_descriptor *sd = NULL;
150
151     if (MaximumCount <= 0 || InitialCount < 0 || InitialCount > MaximumCount)
152         return STATUS_INVALID_PARAMETER;
153     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
154
155     objattr.rootdir =  attr ? attr->RootDirectory : 0;
156     objattr.sd_len = 0;
157     objattr.name_len = len;
158     if (attr)
159     {
160         ret = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
161         if (ret != STATUS_SUCCESS) return ret;
162     }
163
164     SERVER_START_REQ( create_semaphore )
165     {
166         req->access  = access;
167         req->attributes = (attr) ? attr->Attributes : 0;
168         req->initial = InitialCount;
169         req->max     = MaximumCount;
170         wine_server_add_data( req, &objattr, sizeof(objattr) );
171         if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
172         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
173         ret = wine_server_call( req );
174         *SemaphoreHandle = reply->handle;
175     }
176     SERVER_END_REQ;
177
178     NTDLL_free_struct_sd( sd );
179
180     return ret;
181 }
182
183 /******************************************************************************
184  *  NtOpenSemaphore (NTDLL.@)
185  */
186 NTSTATUS WINAPI NtOpenSemaphore( OUT PHANDLE SemaphoreHandle,
187                                  IN ACCESS_MASK access,
188                                  IN const OBJECT_ATTRIBUTES *attr )
189 {
190     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
191     NTSTATUS ret;
192
193     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
194
195     SERVER_START_REQ( open_semaphore )
196     {
197         req->access  = access;
198         req->attributes = (attr) ? attr->Attributes : 0;
199         req->rootdir = attr ? attr->RootDirectory : 0;
200         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
201         ret = wine_server_call( req );
202         *SemaphoreHandle = reply->handle;
203     }
204     SERVER_END_REQ;
205     return ret;
206 }
207
208 /******************************************************************************
209  *  NtQuerySemaphore (NTDLL.@)
210  */
211 NTSTATUS WINAPI NtQuerySemaphore(
212         HANDLE SemaphoreHandle,
213         SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass,
214         PVOID SemaphoreInformation,
215         ULONG Length,
216         PULONG ReturnLength)
217 {
218         FIXME("(%p,%d,%p,0x%08x,%p) stub!\n",
219         SemaphoreHandle, SemaphoreInformationClass, SemaphoreInformation, Length, ReturnLength);
220         return STATUS_SUCCESS;
221 }
222
223 /******************************************************************************
224  *  NtReleaseSemaphore (NTDLL.@)
225  */
226 NTSTATUS WINAPI NtReleaseSemaphore( HANDLE handle, ULONG count, PULONG previous )
227 {
228     NTSTATUS ret;
229     SERVER_START_REQ( release_semaphore )
230     {
231         req->handle = handle;
232         req->count  = count;
233         if (!(ret = wine_server_call( req )))
234         {
235             if (previous) *previous = reply->prev_count;
236         }
237     }
238     SERVER_END_REQ;
239     return ret;
240 }
241
242 /*
243  *      Events
244  */
245
246 /**************************************************************************
247  * NtCreateEvent (NTDLL.@)
248  * ZwCreateEvent (NTDLL.@)
249  */
250 NTSTATUS WINAPI NtCreateEvent(
251         OUT PHANDLE EventHandle,
252         IN ACCESS_MASK DesiredAccess,
253         IN const OBJECT_ATTRIBUTES *attr,
254         IN BOOLEAN ManualReset,
255         IN BOOLEAN InitialState)
256 {
257     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
258     NTSTATUS ret;
259     struct security_descriptor *sd = NULL;
260     struct object_attributes objattr;
261
262     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
263
264     objattr.rootdir = attr ? attr->RootDirectory : 0;
265     objattr.sd_len = 0;
266     objattr.name_len = len;
267     if (attr)
268     {
269         ret = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
270         if (ret != STATUS_SUCCESS) return ret;
271     }
272
273     SERVER_START_REQ( create_event )
274     {
275         req->access = DesiredAccess;
276         req->attributes = (attr) ? attr->Attributes : 0;
277         req->manual_reset = ManualReset;
278         req->initial_state = InitialState;
279         wine_server_add_data( req, &objattr, sizeof(objattr) );
280         if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
281         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
282         ret = wine_server_call( req );
283         *EventHandle = reply->handle;
284     }
285     SERVER_END_REQ;
286
287     NTDLL_free_struct_sd( sd );
288
289     return ret;
290 }
291
292 /******************************************************************************
293  *  NtOpenEvent (NTDLL.@)
294  *  ZwOpenEvent (NTDLL.@)
295  */
296 NTSTATUS WINAPI NtOpenEvent(
297         OUT PHANDLE EventHandle,
298         IN ACCESS_MASK DesiredAccess,
299         IN const OBJECT_ATTRIBUTES *attr )
300 {
301     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
302     NTSTATUS ret;
303
304     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
305
306     SERVER_START_REQ( open_event )
307     {
308         req->access  = DesiredAccess;
309         req->attributes = (attr) ? attr->Attributes : 0;
310         req->rootdir = attr ? attr->RootDirectory : 0;
311         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
312         ret = wine_server_call( req );
313         *EventHandle = reply->handle;
314     }
315     SERVER_END_REQ;
316     return ret;
317 }
318
319
320 /******************************************************************************
321  *  NtSetEvent (NTDLL.@)
322  *  ZwSetEvent (NTDLL.@)
323  */
324 NTSTATUS WINAPI NtSetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
325 {
326     NTSTATUS ret;
327
328     /* FIXME: set NumberOfThreadsReleased */
329
330     SERVER_START_REQ( event_op )
331     {
332         req->handle = handle;
333         req->op     = SET_EVENT;
334         ret = wine_server_call( req );
335     }
336     SERVER_END_REQ;
337     return ret;
338 }
339
340 /******************************************************************************
341  *  NtResetEvent (NTDLL.@)
342  */
343 NTSTATUS WINAPI NtResetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
344 {
345     NTSTATUS ret;
346
347     /* resetting an event can't release any thread... */
348     if (NumberOfThreadsReleased) *NumberOfThreadsReleased = 0;
349
350     SERVER_START_REQ( event_op )
351     {
352         req->handle = handle;
353         req->op     = RESET_EVENT;
354         ret = wine_server_call( req );
355     }
356     SERVER_END_REQ;
357     return ret;
358 }
359
360 /******************************************************************************
361  *  NtClearEvent (NTDLL.@)
362  *
363  * FIXME
364  *   same as NtResetEvent ???
365  */
366 NTSTATUS WINAPI NtClearEvent ( HANDLE handle )
367 {
368     return NtResetEvent( handle, NULL );
369 }
370
371 /******************************************************************************
372  *  NtPulseEvent (NTDLL.@)
373  *
374  * FIXME
375  *   PulseCount
376  */
377 NTSTATUS WINAPI NtPulseEvent( HANDLE handle, PULONG PulseCount )
378 {
379     NTSTATUS ret;
380
381     if (PulseCount)
382       FIXME("(%p,%d)\n", handle, *PulseCount);
383
384     SERVER_START_REQ( event_op )
385     {
386         req->handle = handle;
387         req->op     = PULSE_EVENT;
388         ret = wine_server_call( req );
389     }
390     SERVER_END_REQ;
391     return ret;
392 }
393
394 /******************************************************************************
395  *  NtQueryEvent (NTDLL.@)
396  */
397 NTSTATUS WINAPI NtQueryEvent (
398         IN  HANDLE EventHandle,
399         IN  EVENT_INFORMATION_CLASS EventInformationClass,
400         OUT PVOID EventInformation,
401         IN  ULONG EventInformationLength,
402         OUT PULONG  ReturnLength)
403 {
404         FIXME("(%p)\n", EventHandle);
405         return STATUS_SUCCESS;
406 }
407
408 /*
409  *      Mutants (known as Mutexes in Kernel32)
410  */
411
412 /******************************************************************************
413  *              NtCreateMutant                          [NTDLL.@]
414  *              ZwCreateMutant                          [NTDLL.@]
415  */
416 NTSTATUS WINAPI NtCreateMutant(OUT HANDLE* MutantHandle,
417                                IN ACCESS_MASK access,
418                                IN const OBJECT_ATTRIBUTES* attr OPTIONAL,
419                                IN BOOLEAN InitialOwner)
420 {
421     NTSTATUS status;
422     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
423     struct security_descriptor *sd = NULL;
424     struct object_attributes objattr;
425
426     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
427
428     objattr.rootdir = attr ? attr->RootDirectory : 0;
429     objattr.sd_len = 0;
430     objattr.name_len = len;
431     if (attr)
432     {
433         status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
434         if (status != STATUS_SUCCESS) return status;
435     }
436
437     SERVER_START_REQ( create_mutex )
438     {
439         req->access  = access;
440         req->attributes = (attr) ? attr->Attributes : 0;
441         req->owned   = InitialOwner;
442         wine_server_add_data( req, &objattr, sizeof(objattr) );
443         if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
444         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
445         status = wine_server_call( req );
446         *MutantHandle = reply->handle;
447     }
448     SERVER_END_REQ;
449
450     NTDLL_free_struct_sd( sd );
451
452     return status;
453 }
454
455 /**************************************************************************
456  *              NtOpenMutant                            [NTDLL.@]
457  *              ZwOpenMutant                            [NTDLL.@]
458  */
459 NTSTATUS WINAPI NtOpenMutant(OUT HANDLE* MutantHandle, 
460                              IN ACCESS_MASK access, 
461                              IN const OBJECT_ATTRIBUTES* attr )
462 {
463     NTSTATUS    status;
464     DWORD       len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
465
466     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
467
468     SERVER_START_REQ( open_mutex )
469     {
470         req->access  = access;
471         req->attributes = (attr) ? attr->Attributes : 0;
472         req->rootdir = attr ? attr->RootDirectory : 0;
473         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
474         status = wine_server_call( req );
475         *MutantHandle = reply->handle;
476     }
477     SERVER_END_REQ;
478     return status;
479 }
480
481 /**************************************************************************
482  *              NtReleaseMutant                         [NTDLL.@]
483  *              ZwReleaseMutant                         [NTDLL.@]
484  */
485 NTSTATUS WINAPI NtReleaseMutant( IN HANDLE handle, OUT PLONG prev_count OPTIONAL)
486 {
487     NTSTATUS    status;
488
489     SERVER_START_REQ( release_mutex )
490     {
491         req->handle = handle;
492         status = wine_server_call( req );
493         if (prev_count) *prev_count = reply->prev_count;
494     }
495     SERVER_END_REQ;
496     return status;
497 }
498
499 /******************************************************************
500  *              NtQueryMutant                   [NTDLL.@]
501  *              ZwQueryMutant                   [NTDLL.@]
502  */
503 NTSTATUS WINAPI NtQueryMutant(IN HANDLE handle, 
504                               IN MUTANT_INFORMATION_CLASS MutantInformationClass, 
505                               OUT PVOID MutantInformation, 
506                               IN ULONG MutantInformationLength, 
507                               OUT PULONG ResultLength OPTIONAL )
508 {
509     FIXME("(%p %u %p %u %p): stub!\n", 
510           handle, MutantInformationClass, MutantInformation, MutantInformationLength, ResultLength);
511     return STATUS_NOT_IMPLEMENTED;
512 }
513
514 /*
515  *      Timers
516  */
517
518 /**************************************************************************
519  *              NtCreateTimer                           [NTDLL.@]
520  *              ZwCreateTimer                           [NTDLL.@]
521  */
522 NTSTATUS WINAPI NtCreateTimer(OUT HANDLE *handle,
523                               IN ACCESS_MASK access,
524                               IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
525                               IN TIMER_TYPE timer_type)
526 {
527     DWORD       len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
528     NTSTATUS    status;
529
530     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
531
532     if (timer_type != NotificationTimer && timer_type != SynchronizationTimer)
533         return STATUS_INVALID_PARAMETER;
534
535     SERVER_START_REQ( create_timer )
536     {
537         req->access  = access;
538         req->attributes = (attr) ? attr->Attributes : 0;
539         req->rootdir = attr ? attr->RootDirectory : 0;
540         req->manual  = (timer_type == NotificationTimer) ? TRUE : FALSE;
541         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
542         status = wine_server_call( req );
543         *handle = reply->handle;
544     }
545     SERVER_END_REQ;
546     return status;
547
548 }
549
550 /**************************************************************************
551  *              NtOpenTimer                             [NTDLL.@]
552  *              ZwOpenTimer                             [NTDLL.@]
553  */
554 NTSTATUS WINAPI NtOpenTimer(OUT PHANDLE handle,
555                             IN ACCESS_MASK access,
556                             IN const OBJECT_ATTRIBUTES* attr )
557 {
558     DWORD       len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
559     NTSTATUS    status;
560
561     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
562
563     SERVER_START_REQ( open_timer )
564     {
565         req->access  = access;
566         req->attributes = (attr) ? attr->Attributes : 0;
567         req->rootdir = attr ? attr->RootDirectory : 0;
568         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
569         status = wine_server_call( req );
570         *handle = reply->handle;
571     }
572     SERVER_END_REQ;
573     return status;
574 }
575
576 /**************************************************************************
577  *              NtSetTimer                              [NTDLL.@]
578  *              ZwSetTimer                              [NTDLL.@]
579  */
580 NTSTATUS WINAPI NtSetTimer(IN HANDLE handle,
581                            IN const LARGE_INTEGER* when,
582                            IN PTIMER_APC_ROUTINE callback,
583                            IN PVOID callback_arg,
584                            IN BOOLEAN resume,
585                            IN ULONG period OPTIONAL,
586                            OUT PBOOLEAN state OPTIONAL)
587 {
588     NTSTATUS    status = STATUS_SUCCESS;
589
590     TRACE("(%p,%p,%p,%p,%08x,0x%08x,%p) stub\n",
591           handle, when, callback, callback_arg, resume, period, state);
592
593     SERVER_START_REQ( set_timer )
594     {
595         req->handle   = handle;
596         req->period   = period;
597         req->expire   = when->QuadPart;
598         req->callback = callback;
599         req->arg      = callback_arg;
600         status = wine_server_call( req );
601         if (state) *state = reply->signaled;
602     }
603     SERVER_END_REQ;
604
605     /* set error but can still succeed */
606     if (resume && status == STATUS_SUCCESS) return STATUS_TIMER_RESUME_IGNORED;
607     return status;
608 }
609
610 /**************************************************************************
611  *              NtCancelTimer                           [NTDLL.@]
612  *              ZwCancelTimer                           [NTDLL.@]
613  */
614 NTSTATUS WINAPI NtCancelTimer(IN HANDLE handle, OUT BOOLEAN* state)
615 {
616     NTSTATUS    status;
617
618     SERVER_START_REQ( cancel_timer )
619     {
620         req->handle = handle;
621         status = wine_server_call( req );
622         if (state) *state = reply->signaled;
623     }
624     SERVER_END_REQ;
625     return status;
626 }
627
628 /******************************************************************************
629  *  NtQueryTimer (NTDLL.@)
630  *
631  * Retrieves information about a timer.
632  *
633  * PARAMS
634  *  TimerHandle           [I] The timer to retrieve information about.
635  *  TimerInformationClass [I] The type of information to retrieve.
636  *  TimerInformation      [O] Pointer to buffer to store information in.
637  *  Length                [I] The length of the buffer pointed to by TimerInformation.
638  *  ReturnLength          [O] Optional. The size of buffer actually used.
639  *
640  * RETURNS
641  *  Success: STATUS_SUCCESS
642  *  Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
643  *           size for the class specified.
644  *           STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
645  *           STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
646  *           to the timer.
647  */
648 NTSTATUS WINAPI NtQueryTimer(
649     HANDLE TimerHandle,
650     TIMER_INFORMATION_CLASS TimerInformationClass,
651     PVOID TimerInformation,
652     ULONG Length,
653     PULONG ReturnLength)
654 {
655     TIMER_BASIC_INFORMATION * basic_info = (TIMER_BASIC_INFORMATION *)TimerInformation;
656     NTSTATUS status;
657     LARGE_INTEGER now;
658
659     TRACE("(%p,%d,%p,0x%08x,%p)\n", TimerHandle, TimerInformationClass,
660        TimerInformation, Length, ReturnLength);
661
662     switch (TimerInformationClass)
663     {
664     case TimerBasicInformation:
665         if (Length < sizeof(TIMER_BASIC_INFORMATION))
666             return STATUS_INFO_LENGTH_MISMATCH;
667
668         SERVER_START_REQ(get_timer_info)
669         {
670             req->handle = TimerHandle;
671             status = wine_server_call(req);
672
673             /* convert server time to absolute NTDLL time */
674             basic_info->RemainingTime.QuadPart = reply->when;
675             basic_info->TimerState = reply->signaled;
676         }
677         SERVER_END_REQ;
678
679         /* convert from absolute into relative time */
680         NtQuerySystemTime(&now);
681         if (now.QuadPart > basic_info->RemainingTime.QuadPart)
682             basic_info->RemainingTime.QuadPart = 0;
683         else
684             basic_info->RemainingTime.QuadPart -= now.QuadPart;
685
686         if (ReturnLength) *ReturnLength = sizeof(TIMER_BASIC_INFORMATION);
687
688         return status;
689     }
690
691     FIXME("Unhandled class %d\n", TimerInformationClass);
692     return STATUS_INVALID_INFO_CLASS;
693 }
694
695
696 /******************************************************************************
697  * NtQueryTimerResolution [NTDLL.@]
698  */
699 NTSTATUS WINAPI NtQueryTimerResolution(OUT ULONG* min_resolution,
700                                        OUT ULONG* max_resolution,
701                                        OUT ULONG* current_resolution)
702 {
703     FIXME("(%p,%p,%p), stub!\n",
704           min_resolution, max_resolution, current_resolution);
705
706     return STATUS_NOT_IMPLEMENTED;
707 }
708
709 /******************************************************************************
710  * NtSetTimerResolution [NTDLL.@]
711  */
712 NTSTATUS WINAPI NtSetTimerResolution(IN ULONG resolution,
713                                      IN BOOLEAN set_resolution,
714                                      OUT ULONG* current_resolution )
715 {
716     FIXME("(%u,%u,%p), stub!\n",
717           resolution, set_resolution, current_resolution);
718
719     return STATUS_NOT_IMPLEMENTED;
720 }
721
722
723 /***********************************************************************
724  *              wait_reply
725  *
726  * Wait for a reply on the waiting pipe of the current thread.
727  */
728 static int wait_reply( void *cookie )
729 {
730     int signaled;
731     struct wake_up_reply reply;
732     for (;;)
733     {
734         int ret;
735         ret = read( ntdll_get_thread_data()->wait_fd[0], &reply, sizeof(reply) );
736         if (ret == sizeof(reply))
737         {
738             if (!reply.cookie) break;  /* thread got killed */
739             if (reply.cookie == cookie) return reply.signaled;
740             /* we stole another reply, wait for the real one */
741             signaled = wait_reply( cookie );
742             /* and now put the wrong one back in the pipe */
743             for (;;)
744             {
745                 ret = write( ntdll_get_thread_data()->wait_fd[1], &reply, sizeof(reply) );
746                 if (ret == sizeof(reply)) break;
747                 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
748                 if (errno == EINTR) continue;
749                 server_protocol_perror("wakeup write");
750             }
751             return signaled;
752         }
753         if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
754         if (errno == EINTR) continue;
755         server_protocol_perror("wakeup read");
756     }
757     /* the server closed the connection; time to die... */
758     server_abort_thread(0);
759 }
760
761
762 /***********************************************************************
763  *              invoke_apc
764  *
765  * Invoke a single APC. Return TRUE if a user APC has been run.
766  */
767 static BOOL invoke_apc( const apc_call_t *call, apc_result_t *result )
768 {
769     BOOL user_apc = FALSE;
770
771     memset( result, 0, sizeof(*result) );
772
773     switch (call->type)
774     {
775     case APC_NONE:
776         break;
777     case APC_USER:
778         call->user.func( call->user.args[0], call->user.args[1], call->user.args[2] );
779         user_apc = TRUE;
780         break;
781     case APC_TIMER:
782         call->timer.func( call->timer.arg, (DWORD)call->timer.time, (DWORD)(call->timer.time >> 32) );
783         user_apc = TRUE;
784         break;
785     case APC_ASYNC_IO:
786         result->type = call->type;
787         result->async_io.status = call->async_io.func( call->async_io.user,
788                                                        call->async_io.sb,
789                                                        call->async_io.status,
790                                                        &result->async_io.total );
791         break;
792     case APC_VIRTUAL_ALLOC:
793         result->type = call->type;
794         result->virtual_alloc.addr = call->virtual_alloc.addr;
795         result->virtual_alloc.size = call->virtual_alloc.size;
796         result->virtual_alloc.status = NtAllocateVirtualMemory( NtCurrentProcess(),
797                                                                 &result->virtual_alloc.addr,
798                                                                 call->virtual_alloc.zero_bits,
799                                                                 &result->virtual_alloc.size,
800                                                                 call->virtual_alloc.op_type,
801                                                                 call->virtual_alloc.prot );
802         break;
803     case APC_VIRTUAL_FREE:
804         result->type = call->type;
805         result->virtual_free.addr = call->virtual_free.addr;
806         result->virtual_free.size = call->virtual_free.size;
807         result->virtual_free.status = NtFreeVirtualMemory( NtCurrentProcess(),
808                                                            &result->virtual_free.addr,
809                                                            &result->virtual_free.size,
810                                                            call->virtual_free.op_type );
811         break;
812     case APC_VIRTUAL_QUERY:
813     {
814         MEMORY_BASIC_INFORMATION info;
815         result->type = call->type;
816         result->virtual_query.status = NtQueryVirtualMemory( NtCurrentProcess(),
817                                                              call->virtual_query.addr,
818                                                              MemoryBasicInformation, &info,
819                                                              sizeof(info), NULL );
820         if (result->virtual_query.status == STATUS_SUCCESS)
821         {
822             result->virtual_query.base       = info.BaseAddress;
823             result->virtual_query.alloc_base = info.AllocationBase;
824             result->virtual_query.size       = info.RegionSize;
825             result->virtual_query.state      = info.State;
826             result->virtual_query.prot       = info.Protect;
827             result->virtual_query.alloc_prot = info.AllocationProtect;
828             result->virtual_query.alloc_type = info.Type;
829         }
830         break;
831     }
832     case APC_VIRTUAL_PROTECT:
833         result->type = call->type;
834         result->virtual_protect.addr = call->virtual_protect.addr;
835         result->virtual_protect.size = call->virtual_protect.size;
836         result->virtual_protect.status = NtProtectVirtualMemory( NtCurrentProcess(),
837                                                                  &result->virtual_protect.addr,
838                                                                  &result->virtual_protect.size,
839                                                                  call->virtual_protect.prot,
840                                                                  &result->virtual_protect.prot );
841         break;
842     case APC_VIRTUAL_FLUSH:
843         result->type = call->type;
844         result->virtual_flush.addr = call->virtual_flush.addr;
845         result->virtual_flush.size = call->virtual_flush.size;
846         result->virtual_flush.status = NtFlushVirtualMemory( NtCurrentProcess(),
847                                                              &result->virtual_flush.addr,
848                                                              &result->virtual_flush.size, 0 );
849         break;
850     case APC_VIRTUAL_LOCK:
851         result->type = call->type;
852         result->virtual_lock.addr = call->virtual_lock.addr;
853         result->virtual_lock.size = call->virtual_lock.size;
854         result->virtual_lock.status = NtLockVirtualMemory( NtCurrentProcess(),
855                                                            &result->virtual_lock.addr,
856                                                            &result->virtual_lock.size, 0 );
857         break;
858     case APC_VIRTUAL_UNLOCK:
859         result->type = call->type;
860         result->virtual_unlock.addr = call->virtual_unlock.addr;
861         result->virtual_unlock.size = call->virtual_unlock.size;
862         result->virtual_unlock.status = NtUnlockVirtualMemory( NtCurrentProcess(),
863                                                                &result->virtual_unlock.addr,
864                                                                &result->virtual_unlock.size, 0 );
865         break;
866     case APC_MAP_VIEW:
867     {
868         LARGE_INTEGER offset;
869         result->type = call->type;
870         result->map_view.addr   = call->map_view.addr;
871         result->map_view.size   = call->map_view.size;
872         offset.QuadPart         = call->map_view.offset;
873         result->map_view.status = NtMapViewOfSection( call->map_view.handle, NtCurrentProcess(),
874                                                       &result->map_view.addr, call->map_view.zero_bits,
875                                                       0, &offset, &result->map_view.size, ViewShare,
876                                                       call->map_view.alloc_type, call->map_view.prot );
877         NtClose( call->map_view.handle );
878         break;
879     }
880     case APC_UNMAP_VIEW:
881         result->type = call->type;
882         result->unmap_view.status = NtUnmapViewOfSection( NtCurrentProcess(), call->unmap_view.addr );
883         break;
884     case APC_CREATE_THREAD:
885     {
886         CLIENT_ID id;
887         result->type = call->type;
888         result->create_thread.status = RtlCreateUserThread( NtCurrentProcess(), NULL,
889                                                             call->create_thread.suspend, NULL,
890                                                             call->create_thread.reserve,
891                                                             call->create_thread.commit,
892                                                             call->create_thread.func,
893                                                             call->create_thread.arg,
894                                                             &result->create_thread.handle, &id );
895         result->create_thread.tid = HandleToULong(id.UniqueThread);
896         break;
897     }
898     default:
899         server_protocol_error( "get_apc_request: bad type %d\n", call->type );
900         break;
901     }
902     return user_apc;
903 }
904
905 /***********************************************************************
906  *           NTDLL_queue_process_apc
907  */
908 NTSTATUS NTDLL_queue_process_apc( HANDLE process, const apc_call_t *call, apc_result_t *result )
909 {
910     for (;;)
911     {
912         NTSTATUS ret;
913         HANDLE handle = 0;
914         BOOL self = FALSE;
915
916         SERVER_START_REQ( queue_apc )
917         {
918             req->process = process;
919             req->call = *call;
920             if (!(ret = wine_server_call( req )))
921             {
922                 handle = reply->handle;
923                 self = reply->self;
924             }
925         }
926         SERVER_END_REQ;
927         if (ret != STATUS_SUCCESS) return ret;
928
929         if (self)
930         {
931             invoke_apc( call, result );
932         }
933         else
934         {
935             NtWaitForSingleObject( handle, FALSE, NULL );
936
937             SERVER_START_REQ( get_apc_result )
938             {
939                 req->handle = handle;
940                 if (!(ret = wine_server_call( req ))) *result = reply->result;
941             }
942             SERVER_END_REQ;
943
944             if (!ret && result->type == APC_NONE) continue;  /* APC didn't run, try again */
945             if (ret) NtClose( handle );
946         }
947         return ret;
948     }
949 }
950
951
952 /***********************************************************************
953  *              NTDLL_wait_for_multiple_objects
954  *
955  * Implementation of NtWaitForMultipleObjects
956  */
957 NTSTATUS NTDLL_wait_for_multiple_objects( UINT count, const HANDLE *handles, UINT flags,
958                                           const LARGE_INTEGER *timeout, HANDLE signal_object )
959 {
960     NTSTATUS ret;
961     int cookie;
962     BOOL user_apc = FALSE;
963     obj_handle_t apc_handle = 0;
964     apc_call_t call;
965     apc_result_t result;
966     timeout_t abs_timeout = timeout ? timeout->QuadPart : TIMEOUT_INFINITE;
967
968     memset( &result, 0, sizeof(result) );
969
970     for (;;)
971     {
972         SERVER_START_REQ( select )
973         {
974             req->flags    = flags;
975             req->cookie   = &cookie;
976             req->signal   = signal_object;
977             req->prev_apc = apc_handle;
978             req->timeout  = abs_timeout;
979             wine_server_add_data( req, &result, sizeof(result) );
980             wine_server_add_data( req, handles, count * sizeof(HANDLE) );
981             ret = wine_server_call( req );
982             abs_timeout = reply->timeout;
983             apc_handle  = reply->apc_handle;
984             call        = reply->call;
985         }
986         SERVER_END_REQ;
987         if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
988         if (ret != STATUS_USER_APC) break;
989         if (invoke_apc( &call, &result ))
990         {
991             /* if we ran a user apc we have to check once more if an object got signaled,
992              * but we don't want to wait */
993             abs_timeout = 0;
994             user_apc = TRUE;
995         }
996         signal_object = 0;  /* don't signal it multiple times */
997     }
998
999     if (ret == STATUS_TIMEOUT && user_apc) ret = STATUS_USER_APC;
1000
1001     /* A test on Windows 2000 shows that Windows always yields during
1002        a wait, but a wait that is hit by an event gets a priority
1003        boost as well.  This seems to model that behavior the closest.  */
1004     if (ret == STATUS_TIMEOUT) NtYieldExecution();
1005
1006     return ret;
1007 }
1008
1009
1010 /* wait operations */
1011
1012 /******************************************************************
1013  *              NtWaitForMultipleObjects (NTDLL.@)
1014  */
1015 NTSTATUS WINAPI NtWaitForMultipleObjects( DWORD count, const HANDLE *handles,
1016                                           BOOLEAN wait_all, BOOLEAN alertable,
1017                                           const LARGE_INTEGER *timeout )
1018 {
1019     UINT flags = SELECT_INTERRUPTIBLE;
1020
1021     if (!count || count > MAXIMUM_WAIT_OBJECTS) return STATUS_INVALID_PARAMETER_1;
1022
1023     if (wait_all) flags |= SELECT_ALL;
1024     if (alertable) flags |= SELECT_ALERTABLE;
1025     return NTDLL_wait_for_multiple_objects( count, handles, flags, timeout, 0 );
1026 }
1027
1028
1029 /******************************************************************
1030  *              NtWaitForSingleObject (NTDLL.@)
1031  */
1032 NTSTATUS WINAPI NtWaitForSingleObject(HANDLE handle, BOOLEAN alertable, const LARGE_INTEGER *timeout )
1033 {
1034     return NtWaitForMultipleObjects( 1, &handle, FALSE, alertable, timeout );
1035 }
1036
1037
1038 /******************************************************************
1039  *              NtSignalAndWaitForSingleObject (NTDLL.@)
1040  */
1041 NTSTATUS WINAPI NtSignalAndWaitForSingleObject( HANDLE hSignalObject, HANDLE hWaitObject,
1042                                                 BOOLEAN alertable, const LARGE_INTEGER *timeout )
1043 {
1044     UINT flags = SELECT_INTERRUPTIBLE;
1045
1046     if (!hSignalObject) return STATUS_INVALID_HANDLE;
1047     if (alertable) flags |= SELECT_ALERTABLE;
1048     return NTDLL_wait_for_multiple_objects( 1, &hWaitObject, flags, timeout, hSignalObject );
1049 }
1050
1051
1052 /******************************************************************
1053  *              NtYieldExecution (NTDLL.@)
1054  */
1055 NTSTATUS WINAPI NtYieldExecution(void)
1056 {
1057 #ifdef HAVE_SCHED_YIELD
1058     sched_yield();
1059     return STATUS_SUCCESS;
1060 #else
1061     return STATUS_NO_YIELD_PERFORMED;
1062 #endif
1063 }
1064
1065
1066 /******************************************************************
1067  *              NtDelayExecution (NTDLL.@)
1068  */
1069 NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeout )
1070 {
1071     /* if alertable, we need to query the server */
1072     if (alertable)
1073         return NTDLL_wait_for_multiple_objects( 0, NULL, SELECT_INTERRUPTIBLE | SELECT_ALERTABLE,
1074                                                 timeout, 0 );
1075
1076     if (!timeout || timeout->QuadPart == TIMEOUT_INFINITE)  /* sleep forever */
1077     {
1078         for (;;) select( 0, NULL, NULL, NULL, NULL );
1079     }
1080     else
1081     {
1082         LARGE_INTEGER now;
1083         timeout_t when, diff;
1084
1085         if ((when = timeout->QuadPart) < 0)
1086         {
1087             NtQuerySystemTime( &now );
1088             when = now.QuadPart - when;
1089         }
1090
1091         /* Note that we yield after establishing the desired timeout */
1092         NtYieldExecution();
1093         if (!when) return STATUS_SUCCESS;
1094
1095         for (;;)
1096         {
1097             struct timeval tv;
1098             NtQuerySystemTime( &now );
1099             diff = (when - now.QuadPart + 9) / 10;
1100             if (diff <= 0) break;
1101             tv.tv_sec  = diff / 1000000;
1102             tv.tv_usec = diff % 1000000;
1103             if (select( 0, NULL, NULL, NULL, &tv ) != -1) break;
1104         }
1105     }
1106     return STATUS_SUCCESS;
1107 }
1108
1109 /******************************************************************
1110  *              NtCreateIoCompletion (NTDLL.@)
1111  *              ZwCreateIoCompletion (NTDLL.@)
1112  *
1113  * Creates I/O completion object.
1114  *
1115  * PARAMS
1116  *      CompletionPort            [O] created completion object handle will be placed there
1117  *      DesiredAccess             [I] desired access to a handle (combination of IO_COMPLETION_*)
1118  *      ObjectAttributes          [I] completion object attributes
1119  *      NumberOfConcurrentThreads [I] desired number of concurrent active worker threads
1120  *
1121  */
1122 NTSTATUS WINAPI NtCreateIoCompletion( PHANDLE CompletionPort, ACCESS_MASK DesiredAccess,
1123                                       POBJECT_ATTRIBUTES ObjectAttributes, ULONG NumberOfConcurrentThreads )
1124 {
1125     NTSTATUS status;
1126
1127     TRACE("(%p, %x, %p, %d)\n", CompletionPort, DesiredAccess,
1128           ObjectAttributes, NumberOfConcurrentThreads);
1129
1130     if (!CompletionPort)
1131         return STATUS_INVALID_PARAMETER;
1132
1133     SERVER_START_REQ( create_completion )
1134     {
1135         req->access     = DesiredAccess;
1136         req->attributes = ObjectAttributes ? ObjectAttributes->Attributes : 0;
1137         req->rootdir    = ObjectAttributes ? ObjectAttributes->RootDirectory : NULL;
1138         req->concurrent = NumberOfConcurrentThreads;
1139         if (ObjectAttributes && ObjectAttributes->ObjectName)
1140             wine_server_add_data( req, ObjectAttributes->ObjectName->Buffer,
1141                                        ObjectAttributes->ObjectName->Length );
1142         if (!(status = wine_server_call( req )))
1143             *CompletionPort = reply->handle;
1144     }
1145     SERVER_END_REQ;
1146     return status;
1147 }
1148
1149 /******************************************************************
1150  *              NtSetIoCompletion (NTDLL.@)
1151  *              ZwSetIoCompletion (NTDLL.@)
1152  *
1153  * Inserts completion message into queue
1154  *
1155  * PARAMS
1156  *      CompletionPort           [I] HANDLE to completion object
1157  *      CompletionKey            [I] completion key
1158  *      CompletionValue          [I] completion value (usually pointer to OVERLAPPED)
1159  *      Status                   [I] operation status
1160  *      NumberOfBytesTransferred [I] number of bytes transferred
1161  */
1162 NTSTATUS WINAPI NtSetIoCompletion( HANDLE CompletionPort, ULONG_PTR CompletionKey,
1163                                    ULONG_PTR CompletionValue, NTSTATUS Status,
1164                                    ULONG NumberOfBytesTransferred )
1165 {
1166     NTSTATUS status;
1167
1168     TRACE("(%p, %lx, %lx, %x, %d)\n", CompletionPort, CompletionKey,
1169           CompletionValue, Status, NumberOfBytesTransferred);
1170
1171     SERVER_START_REQ( add_completion )
1172     {
1173         req->handle      = CompletionPort;
1174         req->ckey        = CompletionKey;
1175         req->cvalue      = CompletionValue;
1176         req->status      = Status;
1177         req->information = NumberOfBytesTransferred;
1178         status = wine_server_call( req );
1179     }
1180     SERVER_END_REQ;
1181     return status;
1182 }
1183
1184 /******************************************************************
1185  *              NtRemoveIoCompletion (NTDLL.@)
1186  *              ZwRemoveIoCompletion (NTDLL.@)
1187  *
1188  * (Wait for and) retrieve first completion message from completion object's queue
1189  *
1190  * PARAMS
1191  *      CompletionPort  [I] HANDLE to I/O completion object
1192  *      CompletionKey   [O] completion key
1193  *      CompletionValue [O] Completion value given in NtSetIoCompletion or in async operation
1194  *      iosb            [O] IO_STATUS_BLOCK of completed asynchronous operation
1195  *      WaitTime        [I] optional wait time in NTDLL format
1196  *
1197  */
1198 NTSTATUS WINAPI NtRemoveIoCompletion( HANDLE CompletionPort, PULONG_PTR CompletionKey,
1199                                       PULONG_PTR CompletionValue, PIO_STATUS_BLOCK iosb,
1200                                       PLARGE_INTEGER WaitTime )
1201 {
1202     NTSTATUS status;
1203
1204     TRACE("(%p, %p, %p, %p, %p)\n", CompletionPort, CompletionKey,
1205           CompletionValue, iosb, WaitTime);
1206
1207     for(;;)
1208     {
1209         SERVER_START_REQ( remove_completion )
1210         {
1211             req->handle = CompletionPort;
1212             if (!(status = wine_server_call( req )))
1213             {
1214                 *CompletionKey    = reply->ckey;
1215                 *CompletionValue  = reply->cvalue;
1216                 iosb->Information = reply->information;
1217                 iosb->u.Status    = reply->status;
1218             }
1219         }
1220         SERVER_END_REQ;
1221         if (status != STATUS_PENDING) break;
1222
1223         status = NtWaitForSingleObject( CompletionPort, FALSE, WaitTime );
1224         if (status != WAIT_OBJECT_0) break;
1225     }
1226     return status;
1227 }
1228
1229 /******************************************************************
1230  *              NtOpenIoCompletion (NTDLL.@)
1231  *              ZwOpenIoCompletion (NTDLL.@)
1232  *
1233  * Opens I/O completion object
1234  *
1235  * PARAMS
1236  *      CompletionPort     [O] completion object handle will be placed there
1237  *      DesiredAccess      [I] desired access to a handle (combination of IO_COMPLETION_*)
1238  *      ObjectAttributes   [I] completion object name
1239  *
1240  */
1241 NTSTATUS WINAPI NtOpenIoCompletion( PHANDLE CompletionPort, ACCESS_MASK DesiredAccess,
1242                                     POBJECT_ATTRIBUTES ObjectAttributes )
1243 {
1244     NTSTATUS status;
1245
1246     TRACE("(%p, 0x%x, %p)\n", CompletionPort, DesiredAccess, ObjectAttributes);
1247
1248     if (!CompletionPort || !ObjectAttributes || !ObjectAttributes->ObjectName)
1249         return STATUS_INVALID_PARAMETER;
1250
1251     SERVER_START_REQ( open_completion )
1252     {
1253         req->access     = DesiredAccess;
1254         req->rootdir    = ObjectAttributes->RootDirectory;
1255         wine_server_add_data( req, ObjectAttributes->ObjectName->Buffer,
1256                                    ObjectAttributes->ObjectName->Length );
1257         if (!(status = wine_server_call( req )))
1258             *CompletionPort = reply->handle;
1259     }
1260     SERVER_END_REQ;
1261     return status;
1262 }
1263
1264 /******************************************************************
1265  *              NtQueryIoCompletion (NTDLL.@)
1266  *              ZwQueryIoCompletion (NTDLL.@)
1267  *
1268  * Requests information about given I/O completion object
1269  *
1270  * PARAMS
1271  *      CompletionPort        [I] HANDLE to completion port to request
1272  *      InformationClass      [I] information class
1273  *      CompletionInformation [O] user-provided buffer for data
1274  *      BufferLength          [I] buffer length
1275  *      RequiredLength        [O] required buffer length
1276  *
1277  */
1278 NTSTATUS WINAPI NtQueryIoCompletion( HANDLE CompletionPort, IO_COMPLETION_INFORMATION_CLASS InformationClass,
1279                                      PVOID CompletionInformation, ULONG BufferLength, PULONG RequiredLength )
1280 {
1281     NTSTATUS status;
1282
1283     TRACE("(%p, %d, %p, 0x%x, %p)\n", CompletionPort, InformationClass, CompletionInformation,
1284           BufferLength, RequiredLength);
1285
1286     if (!CompletionInformation) return STATUS_INVALID_PARAMETER;
1287     switch( InformationClass )
1288     {
1289         case IoCompletionBasicInformation:
1290             {
1291                 ULONG *info = (ULONG *)CompletionInformation;
1292
1293                 if (RequiredLength) *RequiredLength = sizeof(*info);
1294                 if (BufferLength != sizeof(*info))
1295                     status = STATUS_INFO_LENGTH_MISMATCH;
1296                 else
1297                 {
1298                     SERVER_START_REQ( query_completion )
1299                     {
1300                         req->handle = CompletionPort;
1301                         if (!(status = wine_server_call( req )))
1302                             *info = reply->depth;
1303                     }
1304                     SERVER_END_REQ;
1305                 }
1306             }
1307             break;
1308         default:
1309             status = STATUS_INVALID_PARAMETER;
1310             break;
1311     }
1312     return status;
1313 }
1314
1315 NTSTATUS NTDLL_AddCompletion( HANDLE hFile, ULONG_PTR CompletionValue, NTSTATUS CompletionStatus, ULONG_PTR Information )
1316 {
1317     NTSTATUS status;
1318
1319     SERVER_START_REQ( add_fd_completion )
1320     {
1321         req->handle      = hFile;
1322         req->cvalue      = CompletionValue;
1323         req->status      = CompletionStatus;
1324         req->information = Information;
1325         status = wine_server_call( req );
1326     }
1327     SERVER_END_REQ;
1328     return status;
1329 }