Reactos
at master 1765 lines 60 kB view raw
1/* 2 * Kernel synchronization objects 3 * 4 * Copyright 1998 Alexandre Julliard 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 19 */ 20 21#include <stdarg.h> 22#include <string.h> 23#include <stdio.h> 24 25#include "ntstatus.h" 26#define WIN32_NO_STATUS 27#include "windef.h" 28#include "winbase.h" 29#include "wincon.h" 30#include "winerror.h" 31#include "winnls.h" 32#include "winternl.h" 33#include "winioctl.h" 34#include "ddk/wdm.h" 35 36#include "kernelbase.h" 37#include "wine/asm.h" 38#include "wine/exception.h" 39#include "wine/debug.h" 40 41WINE_DEFAULT_DEBUG_CHANNEL(sync); 42 43static const struct _KUSER_SHARED_DATA *user_shared_data = (struct _KUSER_SHARED_DATA *)0x7ffe0000; 44 45/* check if current version is NT or Win95 */ 46static inline BOOL is_version_nt(void) 47{ 48 return !(GetVersion() & 0x80000000); 49} 50 51/* helper for kernel32->ntdll timeout format conversion */ 52static inline LARGE_INTEGER *get_nt_timeout( LARGE_INTEGER *time, DWORD timeout ) 53{ 54 if (timeout == INFINITE) return NULL; 55 time->QuadPart = (ULONGLONG)timeout * -10000; 56 return time; 57} 58 59 60/*********************************************************************** 61 * BaseGetNamedObjectDirectory (kernelbase.@) 62 */ 63NTSTATUS WINAPI BaseGetNamedObjectDirectory( HANDLE *dir ) 64{ 65 static HANDLE handle; 66 WCHAR buffer[64]; 67 UNICODE_STRING str; 68 OBJECT_ATTRIBUTES attr; 69 NTSTATUS status = STATUS_SUCCESS; 70 71 if (!handle) 72 { 73 HANDLE dir; 74 75 swprintf( buffer, ARRAY_SIZE(buffer), L"\\Sessions\\%u\\BaseNamedObjects", 76 NtCurrentTeb()->Peb->SessionId ); 77 RtlInitUnicodeString( &str, buffer ); 78 InitializeObjectAttributes(&attr, &str, 0, 0, NULL); 79 status = NtOpenDirectoryObject( &dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE, &attr ); 80 if (!status && InterlockedCompareExchangePointer( &handle, dir, 0 ) != 0) 81 { 82 /* someone beat us here... */ 83 CloseHandle( dir ); 84 } 85 } 86 *dir = handle; 87 return status; 88} 89 90static void get_create_object_attributes( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *nameW, 91 SECURITY_ATTRIBUTES *sa, const WCHAR *name ) 92{ 93 attr->Length = sizeof(*attr); 94 attr->RootDirectory = 0; 95 attr->ObjectName = NULL; 96 attr->Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0); 97 attr->SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL; 98 attr->SecurityQualityOfService = NULL; 99 if (name) 100 { 101 RtlInitUnicodeString( nameW, name ); 102 attr->ObjectName = nameW; 103 BaseGetNamedObjectDirectory( &attr->RootDirectory ); 104 } 105} 106 107static BOOL get_open_object_attributes( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *nameW, 108 BOOL inherit, const WCHAR *name ) 109{ 110 HANDLE dir; 111 112 if (!name) 113 { 114 SetLastError( ERROR_INVALID_PARAMETER ); 115 return FALSE; 116 } 117 RtlInitUnicodeString( nameW, name ); 118 BaseGetNamedObjectDirectory( &dir ); 119 InitializeObjectAttributes( attr, nameW, inherit ? OBJ_INHERIT : 0, dir, NULL ); 120 return TRUE; 121} 122 123 124/*********************************************************************** 125 * Time functions 126 ***********************************************************************/ 127 128 129/********************************************************************* 130 * GetSystemTimes (kernelbase.@) 131 */ 132BOOL WINAPI DECLSPEC_HOTPATCH GetSystemTimes( FILETIME *idle, FILETIME *kernel, FILETIME *user ) 133{ 134 LARGE_INTEGER idle_time, kernel_time, user_time; 135 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *info; 136 ULONG ret_size; 137 DWORD i, cpus = NtCurrentTeb()->Peb->NumberOfProcessors; 138 139 if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) * cpus ))) 140 { 141 SetLastError( ERROR_OUTOFMEMORY ); 142 return FALSE; 143 } 144 if (!set_ntstatus( NtQuerySystemInformation( SystemProcessorPerformanceInformation, info, 145 sizeof(*info) * cpus, &ret_size ))) 146 { 147 HeapFree( GetProcessHeap(), 0, info ); 148 return FALSE; 149 } 150 idle_time.QuadPart = 0; 151 kernel_time.QuadPart = 0; 152 user_time.QuadPart = 0; 153 for (i = 0; i < cpus; i++) 154 { 155 idle_time.QuadPart += info[i].IdleTime.QuadPart; 156 kernel_time.QuadPart += info[i].KernelTime.QuadPart; 157 user_time.QuadPart += info[i].UserTime.QuadPart; 158 } 159 if (idle) 160 { 161 idle->dwLowDateTime = idle_time.u.LowPart; 162 idle->dwHighDateTime = idle_time.u.HighPart; 163 } 164 if (kernel) 165 { 166 kernel->dwLowDateTime = kernel_time.u.LowPart; 167 kernel->dwHighDateTime = kernel_time.u.HighPart; 168 } 169 if (user) 170 { 171 user->dwLowDateTime = user_time.u.LowPart; 172 user->dwHighDateTime = user_time.u.HighPart; 173 } 174 HeapFree( GetProcessHeap(), 0, info ); 175 return TRUE; 176} 177 178 179/****************************************************************************** 180 * GetTickCount (kernelbase.@) 181 */ 182ULONG WINAPI DECLSPEC_HOTPATCH GetTickCount(void) 183{ 184 /* note: we ignore TickCountMultiplier */ 185 return user_shared_data->TickCount.LowPart; 186} 187 188 189/****************************************************************************** 190 * GetTickCount64 (kernelbase.@) 191 */ 192ULONGLONG WINAPI DECLSPEC_HOTPATCH GetTickCount64(void) 193{ 194 ULONG high, low; 195 196 do 197 { 198 high = user_shared_data->TickCount.High1Time; 199 low = user_shared_data->TickCount.LowPart; 200 } 201 while (high != user_shared_data->TickCount.High2Time); 202 /* note: we ignore TickCountMultiplier */ 203 return (ULONGLONG)high << 32 | low; 204} 205 206 207/****************************************************************************** 208 * QueryInterruptTime (kernelbase.@) 209 */ 210void WINAPI DECLSPEC_HOTPATCH QueryInterruptTime( ULONGLONG *time ) 211{ 212 ULONG high, low; 213 214 do 215 { 216 high = user_shared_data->InterruptTime.High1Time; 217 low = user_shared_data->InterruptTime.LowPart; 218 } 219 while (high != user_shared_data->InterruptTime.High2Time); 220 *time = (ULONGLONG)high << 32 | low; 221} 222 223 224/****************************************************************************** 225 * QueryInterruptTimePrecise (kernelbase.@) 226 */ 227void WINAPI DECLSPEC_HOTPATCH QueryInterruptTimePrecise( ULONGLONG *time ) 228{ 229 static int once; 230 if (!once++) FIXME( "(%p) semi-stub\n", time ); 231 232 QueryInterruptTime( time ); 233} 234 235 236/*********************************************************************** 237 * QueryUnbiasedInterruptTimePrecise (kernelbase.@) 238 */ 239void WINAPI DECLSPEC_HOTPATCH QueryUnbiasedInterruptTimePrecise( ULONGLONG *time ) 240{ 241 static int once; 242 if (!once++) FIXME( "(%p): semi-stub.\n", time ); 243 244 RtlQueryUnbiasedInterruptTime( time ); 245} 246 247 248/*********************************************************************** 249 * QueryIdleProcessorCycleTime (kernelbase.@) 250 */ 251BOOL WINAPI QueryIdleProcessorCycleTime( ULONG *size, ULONG64 *times ) 252{ 253 ULONG ret_size; 254 NTSTATUS status = NtQuerySystemInformation( SystemProcessorIdleCycleTimeInformation, times, *size, &ret_size ); 255 256 if (!*size || !status) *size = ret_size; 257 return TRUE; 258} 259 260 261/*********************************************************************** 262 * QueryIdleProcessorCycleTimeEx (kernelbase.@) 263 */ 264BOOL WINAPI QueryIdleProcessorCycleTimeEx( USHORT group_id, ULONG *size, ULONG64 *times ) 265{ 266 ULONG ret_size; 267 NTSTATUS status = NtQuerySystemInformationEx( SystemProcessorIdleCycleTimeInformation, &group_id, sizeof(group_id), 268 times, *size, &ret_size ); 269 if (!*size || !status) *size = ret_size; 270 return TRUE; 271} 272 273 274/*********************************************************************** 275 * Waits 276 ***********************************************************************/ 277 278 279static HANDLE normalize_std_handle( HANDLE handle ) 280{ 281 if ((handle == (HANDLE)STD_INPUT_HANDLE) || 282 (handle == (HANDLE)STD_OUTPUT_HANDLE) || 283 (handle == (HANDLE)STD_ERROR_HANDLE)) 284 return GetStdHandle( HandleToULong(handle) ); 285 286 return handle; 287} 288 289 290/*********************************************************************** 291 * RegisterWaitForSingleObjectEx (kernelbase.@) 292 */ 293HANDLE WINAPI DECLSPEC_HOTPATCH RegisterWaitForSingleObjectEx( HANDLE handle, WAITORTIMERCALLBACK callback, 294 PVOID context, ULONG timeout, ULONG flags ) 295{ 296 HANDLE ret; 297 298 TRACE( "%p %p %p %ld %ld\n", handle, callback, context, timeout, flags ); 299 300 handle = normalize_std_handle( handle ); 301 if (!set_ntstatus( RtlRegisterWait( &ret, handle, callback, context, timeout, flags ))) return NULL; 302 return ret; 303} 304 305 306/*********************************************************************** 307 * SignalObjectAndWait (kernelbase.@) 308 */ 309DWORD WINAPI DECLSPEC_HOTPATCH SignalObjectAndWait( HANDLE signal, HANDLE wait, 310 DWORD timeout, BOOL alertable ) 311{ 312 NTSTATUS status; 313 LARGE_INTEGER time; 314 315 TRACE( "%p %p %ld %d\n", signal, wait, timeout, alertable ); 316 317 status = NtSignalAndWaitForSingleObject( signal, wait, alertable, get_nt_timeout( &time, timeout ) ); 318 if (HIWORD(status)) 319 { 320 SetLastError( RtlNtStatusToDosError(status) ); 321 status = WAIT_FAILED; 322 } 323 return status; 324} 325 326 327/*********************************************************************** 328 * Sleep (kernelbase.@) 329 */ 330void WINAPI DECLSPEC_HOTPATCH Sleep( DWORD timeout ) 331{ 332 LARGE_INTEGER time; 333 334 NtDelayExecution( FALSE, get_nt_timeout( &time, timeout ) ); 335} 336 337 338/****************************************************************************** 339 * SleepEx (kernelbase.@) 340 */ 341DWORD WINAPI DECLSPEC_HOTPATCH SleepEx( DWORD timeout, BOOL alertable ) 342{ 343 NTSTATUS status; 344 LARGE_INTEGER time; 345 346 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) ); 347 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION; 348 return 0; 349} 350 351 352/*********************************************************************** 353 * UnregisterWaitEx (kernelbase.@) 354 */ 355BOOL WINAPI DECLSPEC_HOTPATCH UnregisterWaitEx( HANDLE handle, HANDLE event ) 356{ 357 return set_ntstatus( RtlDeregisterWaitEx( handle, event )); 358} 359 360 361/*********************************************************************** 362 * WaitForSingleObject (kernelbase.@) 363 */ 364DWORD WINAPI DECLSPEC_HOTPATCH WaitForSingleObject( HANDLE handle, DWORD timeout ) 365{ 366 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE ); 367} 368 369 370/*********************************************************************** 371 * WaitForSingleObjectEx (kernelbase.@) 372 */ 373DWORD WINAPI DECLSPEC_HOTPATCH WaitForSingleObjectEx( HANDLE handle, DWORD timeout, BOOL alertable ) 374{ 375 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable ); 376} 377 378 379/*********************************************************************** 380 * WaitForMultipleObjects (kernelbase.@) 381 */ 382DWORD WINAPI DECLSPEC_HOTPATCH WaitForMultipleObjects( DWORD count, const HANDLE *handles, 383 BOOL wait_all, DWORD timeout ) 384{ 385 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE ); 386} 387 388 389/*********************************************************************** 390 * WaitForMultipleObjectsEx (kernelbase.@) 391 */ 392DWORD WINAPI DECLSPEC_HOTPATCH WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles, 393 BOOL wait_all, DWORD timeout, BOOL alertable ) 394{ 395 NTSTATUS status; 396 HANDLE hloc[MAXIMUM_WAIT_OBJECTS]; 397 LARGE_INTEGER time; 398 unsigned int i; 399 400 if (count > MAXIMUM_WAIT_OBJECTS) 401 { 402 SetLastError(ERROR_INVALID_PARAMETER); 403 return WAIT_FAILED; 404 } 405 for (i = 0; i < count; i++) hloc[i] = normalize_std_handle( handles[i] ); 406 407 status = NtWaitForMultipleObjects( count, hloc, !wait_all, alertable, 408 get_nt_timeout( &time, timeout ) ); 409 if (HIWORD(status)) /* is it an error code? */ 410 { 411 SetLastError( RtlNtStatusToDosError(status) ); 412 status = WAIT_FAILED; 413 } 414 return status; 415} 416 417 418/****************************************************************************** 419 * WaitForDebugEvent (kernelbase.@) 420 */ 421BOOL WINAPI DECLSPEC_HOTPATCH WaitForDebugEvent( DEBUG_EVENT *event, DWORD timeout ) 422{ 423 NTSTATUS status; 424 LARGE_INTEGER time; 425 DBGUI_WAIT_STATE_CHANGE state; 426 427 for (;;) 428 { 429 status = DbgUiWaitStateChange( &state, get_nt_timeout( &time, timeout ) ); 430 switch (status) 431 { 432 case STATUS_SUCCESS: 433 /* continue on wide print exceptions to force resending an ANSI one. */ 434 if (state.NewState == DbgExceptionStateChange) 435 { 436 DBGKM_EXCEPTION *info = &state.StateInfo.Exception; 437 DWORD code = info->ExceptionRecord.ExceptionCode; 438 if (code == DBG_PRINTEXCEPTION_WIDE_C && info->ExceptionRecord.NumberParameters >= 2) 439 { 440 DbgUiContinue( &state.AppClientId, DBG_EXCEPTION_NOT_HANDLED ); 441 break; 442 } 443 } 444 DbgUiConvertStateChangeStructure( &state, event ); 445 return TRUE; 446 case STATUS_USER_APC: 447 continue; 448 case STATUS_TIMEOUT: 449 SetLastError( ERROR_SEM_TIMEOUT ); 450 return FALSE; 451 default: 452 return set_ntstatus( status ); 453 } 454 } 455} 456 457/****************************************************************************** 458 * WaitForDebugEventEx (kernelbase.@) 459 */ 460BOOL WINAPI DECLSPEC_HOTPATCH WaitForDebugEventEx( DEBUG_EVENT *event, DWORD timeout ) 461{ 462 NTSTATUS status; 463 LARGE_INTEGER time; 464 DBGUI_WAIT_STATE_CHANGE state; 465 466 for (;;) 467 { 468 status = DbgUiWaitStateChange( &state, get_nt_timeout( &time, timeout ) ); 469 switch (status) 470 { 471 case STATUS_SUCCESS: 472 DbgUiConvertStateChangeStructure( &state, event ); 473 return TRUE; 474 case STATUS_USER_APC: 475 continue; 476 case STATUS_TIMEOUT: 477 SetLastError( ERROR_SEM_TIMEOUT ); 478 return FALSE; 479 default: 480 return set_ntstatus( status ); 481 } 482 } 483} 484 485 486/*********************************************************************** 487 * WaitOnAddress (kernelbase.@) 488 */ 489BOOL WINAPI DECLSPEC_HOTPATCH WaitOnAddress( volatile void *addr, void *cmp, SIZE_T size, DWORD timeout ) 490{ 491 LARGE_INTEGER to; 492 493 if (timeout != INFINITE) 494 { 495 to.QuadPart = -(LONGLONG)timeout * 10000; 496 return set_ntstatus( RtlWaitOnAddress( (const void *)addr, cmp, size, &to )); 497 } 498 return set_ntstatus( RtlWaitOnAddress( (const void *)addr, cmp, size, NULL )); 499} 500 501 502/*********************************************************************** 503 * Events 504 ***********************************************************************/ 505 506 507/*********************************************************************** 508 * CreateEventA (kernelbase.@) 509 */ 510HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset, 511 BOOL initial_state, LPCSTR name ) 512{ 513 DWORD flags = 0; 514 515 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET; 516 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET; 517 return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS ); 518} 519 520 521/*********************************************************************** 522 * CreateEventW (kernelbase.@) 523 */ 524HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset, 525 BOOL initial_state, LPCWSTR name ) 526{ 527 DWORD flags = 0; 528 529 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET; 530 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET; 531 return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS ); 532} 533 534 535/*********************************************************************** 536 * CreateEventExA (kernelbase.@) 537 */ 538HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, 539 DWORD flags, DWORD access ) 540{ 541 WCHAR buffer[MAX_PATH]; 542 543 if (!name) return CreateEventExW( sa, NULL, flags, access ); 544 545 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH )) 546 { 547 SetLastError( ERROR_FILENAME_EXCED_RANGE ); 548 return 0; 549 } 550 return CreateEventExW( sa, buffer, flags, access ); 551} 552 553 554/*********************************************************************** 555 * CreateEventExW (kernelbase.@) 556 */ 557HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, 558 DWORD flags, DWORD access ) 559{ 560 HANDLE ret = 0; 561 UNICODE_STRING nameW; 562 OBJECT_ATTRIBUTES attr; 563 NTSTATUS status; 564 565 /* one buggy program needs this 566 * ("Van Dale Groot woordenboek der Nederlandse taal") 567 */ 568 __TRY 569 { 570 get_create_object_attributes( &attr, &nameW, sa, name ); 571 } 572 __EXCEPT_PAGE_FAULT 573 { 574 SetLastError( ERROR_INVALID_PARAMETER); 575 return 0; 576 } 577 __ENDTRY 578 579 status = NtCreateEvent( &ret, access, &attr, 580 (flags & CREATE_EVENT_MANUAL_RESET) ? NotificationEvent : SynchronizationEvent, 581 (flags & CREATE_EVENT_INITIAL_SET) != 0 ); 582 if (status == STATUS_OBJECT_NAME_EXISTS) 583 SetLastError( ERROR_ALREADY_EXISTS ); 584 else 585 SetLastError( RtlNtStatusToDosError(status) ); 586 return ret; 587} 588 589 590/*********************************************************************** 591 * OpenEventA (kernelbase.@) 592 */ 593HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventA( DWORD access, BOOL inherit, LPCSTR name ) 594{ 595 WCHAR buffer[MAX_PATH]; 596 597 if (!name) return OpenEventW( access, inherit, NULL ); 598 599 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH )) 600 { 601 SetLastError( ERROR_FILENAME_EXCED_RANGE ); 602 return 0; 603 } 604 return OpenEventW( access, inherit, buffer ); 605} 606 607 608/*********************************************************************** 609 * OpenEventW (kernelbase.@) 610 */ 611HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventW( DWORD access, BOOL inherit, LPCWSTR name ) 612{ 613 HANDLE ret; 614 UNICODE_STRING nameW; 615 OBJECT_ATTRIBUTES attr; 616 617 if (!is_version_nt()) access = EVENT_ALL_ACCESS; 618 619 if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0; 620 621 if (!set_ntstatus( NtOpenEvent( &ret, access, &attr ))) return 0; 622 return ret; 623} 624 625/*********************************************************************** 626 * PulseEvent (kernelbase.@) 627 */ 628BOOL WINAPI DECLSPEC_HOTPATCH PulseEvent( HANDLE handle ) 629{ 630 return set_ntstatus( NtPulseEvent( handle, NULL )); 631} 632 633 634/*********************************************************************** 635 * SetEvent (kernelbase.@) 636 */ 637BOOL WINAPI DECLSPEC_HOTPATCH SetEvent( HANDLE handle ) 638{ 639 return set_ntstatus( NtSetEvent( handle, NULL )); 640} 641 642 643/*********************************************************************** 644 * ResetEvent (kernelbase.@) 645 */ 646BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent( HANDLE handle ) 647{ 648 return set_ntstatus( NtResetEvent( handle, NULL )); 649} 650 651 652/*********************************************************************** 653 * Mutexes 654 ***********************************************************************/ 655 656 657/*********************************************************************** 658 * CreateMutexA (kernelbase.@) 659 */ 660HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name ) 661{ 662 return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS ); 663} 664 665 666/*********************************************************************** 667 * CreateMutexW (kernelbase.@) 668 */ 669HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name ) 670{ 671 return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS ); 672} 673 674 675/*********************************************************************** 676 * CreateMutexExA (kernelbase.@) 677 */ 678HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, 679 DWORD flags, DWORD access ) 680{ 681 ANSI_STRING nameA; 682 NTSTATUS status; 683 684 if (!name) return CreateMutexExW( sa, NULL, flags, access ); 685 686 RtlInitAnsiString( &nameA, name ); 687 status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString, &nameA, FALSE ); 688 if (status != STATUS_SUCCESS) 689 { 690 SetLastError( ERROR_FILENAME_EXCED_RANGE ); 691 return 0; 692 } 693 return CreateMutexExW( sa, NtCurrentTeb()->StaticUnicodeString.Buffer, flags, access ); 694} 695 696 697/*********************************************************************** 698 * CreateMutexExW (kernelbase.@) 699 */ 700HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, 701 DWORD flags, DWORD access ) 702{ 703 HANDLE ret = 0; 704 UNICODE_STRING nameW; 705 OBJECT_ATTRIBUTES attr; 706 NTSTATUS status; 707 708 get_create_object_attributes( &attr, &nameW, sa, name ); 709 710 status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 ); 711 if (status == STATUS_OBJECT_NAME_EXISTS) 712 SetLastError( ERROR_ALREADY_EXISTS ); 713 else 714 SetLastError( RtlNtStatusToDosError(status) ); 715 return ret; 716} 717 718 719/*********************************************************************** 720 * OpenMutexW (kernelbase.@) 721 */ 722HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name ) 723{ 724 HANDLE ret; 725 UNICODE_STRING nameW; 726 OBJECT_ATTRIBUTES attr; 727 728 if (!is_version_nt()) access = MUTEX_ALL_ACCESS; 729 730 if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0; 731 732 if (!set_ntstatus( NtOpenMutant( &ret, access, &attr ))) return 0; 733 return ret; 734} 735 736 737/*********************************************************************** 738 * ReleaseMutex (kernelbase.@) 739 */ 740BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex( HANDLE handle ) 741{ 742 return set_ntstatus( NtReleaseMutant( handle, NULL )); 743} 744 745 746/*********************************************************************** 747 * Semaphores 748 ***********************************************************************/ 749 750 751/*********************************************************************** 752 * CreateSemaphoreW (kernelbase.@) 753 */ 754HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial, 755 LONG max, LPCWSTR name ) 756{ 757 return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS ); 758} 759 760 761/*********************************************************************** 762 * CreateSemaphoreExW (kernelbase.@) 763 */ 764HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, 765 LPCWSTR name, DWORD flags, DWORD access ) 766{ 767 HANDLE ret = 0; 768 UNICODE_STRING nameW; 769 OBJECT_ATTRIBUTES attr; 770 NTSTATUS status; 771 772 get_create_object_attributes( &attr, &nameW, sa, name ); 773 774 status = NtCreateSemaphore( &ret, access, &attr, initial, max ); 775 if (status == STATUS_OBJECT_NAME_EXISTS) 776 SetLastError( ERROR_ALREADY_EXISTS ); 777 else 778 SetLastError( RtlNtStatusToDosError(status) ); 779 return ret; 780} 781 782 783/*********************************************************************** 784 * OpenSemaphoreW (kernelbase.@) 785 */ 786HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name ) 787{ 788 HANDLE ret; 789 UNICODE_STRING nameW; 790 OBJECT_ATTRIBUTES attr; 791 792 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS; 793 794 if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0; 795 796 if (!set_ntstatus( NtOpenSemaphore( &ret, access, &attr ))) return 0; 797 return ret; 798} 799 800 801/*********************************************************************** 802 * ReleaseSemaphore (kernelbase.@) 803 */ 804BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous ) 805{ 806 return set_ntstatus( NtReleaseSemaphore( handle, count, (PULONG)previous )); 807} 808 809 810/*********************************************************************** 811 * Waitable timers 812 ***********************************************************************/ 813 814 815/*********************************************************************** 816 * CreateWaitableTimerW (kernelbase.@) 817 */ 818HANDLE WINAPI DECLSPEC_HOTPATCH CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name ) 819{ 820 return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0, 821 TIMER_ALL_ACCESS ); 822} 823 824 825/*********************************************************************** 826 * CreateWaitableTimerExW (kernelbase.@) 827 */ 828HANDLE WINAPI DECLSPEC_HOTPATCH CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, 829 DWORD flags, DWORD access ) 830{ 831 HANDLE handle; 832 NTSTATUS status; 833 UNICODE_STRING nameW; 834 OBJECT_ATTRIBUTES attr; 835 836 get_create_object_attributes( &attr, &nameW, sa, name ); 837 838 status = NtCreateTimer( &handle, access, &attr, 839 (flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer ); 840 if (status == STATUS_OBJECT_NAME_EXISTS) 841 SetLastError( ERROR_ALREADY_EXISTS ); 842 else 843 SetLastError( RtlNtStatusToDosError(status) ); 844 return handle; 845} 846 847 848/*********************************************************************** 849 * OpenWaitableTimerW (kernelbase.@) 850 */ 851HANDLE WINAPI DECLSPEC_HOTPATCH OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name ) 852{ 853 HANDLE handle; 854 UNICODE_STRING nameW; 855 OBJECT_ATTRIBUTES attr; 856 857 if (!is_version_nt()) access = TIMER_ALL_ACCESS; 858 859 if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0; 860 861 if (!set_ntstatus( NtOpenTimer( &handle, access, &attr ))) return 0; 862 return handle; 863} 864 865 866/*********************************************************************** 867 * SetWaitableTimer (kernelbase.@) 868 */ 869BOOL WINAPI DECLSPEC_HOTPATCH SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period, 870 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume ) 871{ 872 NTSTATUS status = NtSetTimer( handle, when, (PTIMER_APC_ROUTINE)callback, 873 arg, resume, period, NULL ); 874 return set_ntstatus( status ) || status == STATUS_TIMER_RESUME_IGNORED; 875} 876 877 878/*********************************************************************** 879 * SetWaitableTimerEx (kernelbase.@) 880 */ 881BOOL WINAPI DECLSPEC_HOTPATCH SetWaitableTimerEx( HANDLE handle, const LARGE_INTEGER *when, LONG period, 882 PTIMERAPCROUTINE callback, LPVOID arg, 883 REASON_CONTEXT *context, ULONG tolerabledelay ) 884{ 885 static int once; 886 if (!once++) FIXME( "(%p, %p, %ld, %p, %p, %p, %ld) semi-stub\n", 887 handle, when, period, callback, arg, context, tolerabledelay ); 888 889 return SetWaitableTimer( handle, when, period, callback, arg, FALSE ); 890} 891 892 893/*********************************************************************** 894 * CancelWaitableTimer (kernelbase.@) 895 */ 896BOOL WINAPI DECLSPEC_HOTPATCH CancelWaitableTimer( HANDLE handle ) 897{ 898 return set_ntstatus( NtCancelTimer( handle, NULL )); 899} 900 901 902/*********************************************************************** 903 * Timer queues 904 ***********************************************************************/ 905 906 907/*********************************************************************** 908 * CreateTimerQueue (kernelbase.@) 909 */ 910HANDLE WINAPI DECLSPEC_HOTPATCH CreateTimerQueue(void) 911{ 912 HANDLE q; 913 914 if (!set_ntstatus( RtlCreateTimerQueue( &q ))) return NULL; 915 return q; 916} 917 918 919/*********************************************************************** 920 * CreateTimerQueueTimer (kernelbase.@) 921 */ 922BOOL WINAPI DECLSPEC_HOTPATCH CreateTimerQueueTimer( PHANDLE timer, HANDLE queue, 923 WAITORTIMERCALLBACK callback, PVOID arg, 924 DWORD when, DWORD period, ULONG flags ) 925{ 926 return set_ntstatus( RtlCreateTimer( queue, timer, callback, arg, when, period, flags )); 927} 928 929 930/*********************************************************************** 931 * ChangeTimerQueueTimer (kernelbase.@) 932 */ 933BOOL WINAPI DECLSPEC_HOTPATCH ChangeTimerQueueTimer( HANDLE queue, HANDLE timer, 934 ULONG when, ULONG period ) 935{ 936 return set_ntstatus( RtlUpdateTimer( queue, timer, when, period )); 937} 938 939 940/*********************************************************************** 941 * DeleteTimerQueueEx (kernelbase.@) 942 */ 943BOOL WINAPI DECLSPEC_HOTPATCH DeleteTimerQueueEx( HANDLE queue, HANDLE event ) 944{ 945 return set_ntstatus( RtlDeleteTimerQueueEx( queue, event )); 946} 947 948 949/*********************************************************************** 950 * DeleteTimerQueueTimer (kernelbase.@) 951 */ 952BOOL WINAPI DECLSPEC_HOTPATCH DeleteTimerQueueTimer( HANDLE queue, HANDLE timer, HANDLE event ) 953{ 954 return set_ntstatus( RtlDeleteTimer( queue, timer, event )); 955} 956 957 958/*********************************************************************** 959 * Critical sections 960 ***********************************************************************/ 961 962 963/*********************************************************************** 964 * InitializeCriticalSectionAndSpinCount (kernelbase.@) 965 */ 966BOOL WINAPI DECLSPEC_HOTPATCH InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD count ) 967{ 968 return !RtlInitializeCriticalSectionAndSpinCount( crit, count ); 969} 970 971/*********************************************************************** 972 * InitializeCriticalSectionEx (kernelbase.@) 973 */ 974BOOL WINAPI DECLSPEC_HOTPATCH InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, 975 DWORD flags ) 976{ 977 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags ); 978 if (ret) RtlRaiseStatus( ret ); 979 return !ret; 980} 981 982 983/*********************************************************************** 984 * File mappings 985 ***********************************************************************/ 986 987/*********************************************************************** 988 * CreateFileMappingW (kernelbase.@) 989 */ 990HANDLE WINAPI DECLSPEC_HOTPATCH CreateFileMappingW( HANDLE file, LPSECURITY_ATTRIBUTES sa, DWORD protect, 991 DWORD size_high, DWORD size_low, LPCWSTR name ) 992{ 993 static const int sec_flags = (SEC_FILE | SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | 994 SEC_NOCACHE | SEC_WRITECOMBINE | SEC_LARGE_PAGES); 995 HANDLE ret; 996 NTSTATUS status; 997 DWORD access, sec_type; 998 LARGE_INTEGER size; 999 UNICODE_STRING nameW; 1000 OBJECT_ATTRIBUTES attr; 1001 1002 sec_type = protect & sec_flags; 1003 protect &= ~sec_flags; 1004 if (!sec_type) sec_type = SEC_COMMIT; 1005 1006 /* Win9x compatibility */ 1007 if (!protect && !is_version_nt()) protect = PAGE_READONLY; 1008 1009 switch(protect) 1010 { 1011 case PAGE_READONLY: 1012 case PAGE_WRITECOPY: 1013 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ; 1014 break; 1015 case PAGE_READWRITE: 1016 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE; 1017 break; 1018 case PAGE_EXECUTE_READ: 1019 case PAGE_EXECUTE_WRITECOPY: 1020 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_EXECUTE; 1021 break; 1022 case PAGE_EXECUTE_READWRITE: 1023 access = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE; 1024 break; 1025 default: 1026 SetLastError( ERROR_INVALID_PARAMETER ); 1027 return 0; 1028 } 1029 1030 size.u.LowPart = size_low; 1031 size.u.HighPart = size_high; 1032 1033 if (file == INVALID_HANDLE_VALUE) 1034 { 1035 file = 0; 1036 if (!size.QuadPart) 1037 { 1038 SetLastError( ERROR_INVALID_PARAMETER ); 1039 return 0; 1040 } 1041 } 1042 1043 get_create_object_attributes( &attr, &nameW, sa, name ); 1044 1045 status = NtCreateSection( &ret, access, &attr, &size, protect, sec_type, file ); 1046 if (status == STATUS_OBJECT_NAME_EXISTS) 1047 SetLastError( ERROR_ALREADY_EXISTS ); 1048 else 1049 SetLastError( RtlNtStatusToDosError(status) ); 1050 return ret; 1051} 1052 1053 1054/*********************************************************************** 1055 * CreateFileMappingFromApp (kernelbase.@) 1056 */ 1057HANDLE WINAPI DECLSPEC_HOTPATCH CreateFileMappingFromApp( HANDLE file, LPSECURITY_ATTRIBUTES sa, ULONG protect, 1058 ULONG64 size, LPCWSTR name ) 1059{ 1060 return CreateFileMappingW( file, sa, protect, size << 32, size, name ); 1061} 1062 1063/*********************************************************************** 1064 * OpenFileMappingW (kernelbase.@) 1065 */ 1066HANDLE WINAPI DECLSPEC_HOTPATCH OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name ) 1067{ 1068 OBJECT_ATTRIBUTES attr; 1069 UNICODE_STRING nameW; 1070 HANDLE ret; 1071 1072 if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0; 1073 1074 if (access == FILE_MAP_COPY) access = SECTION_MAP_READ; 1075 1076 if (!is_version_nt()) 1077 { 1078 /* win9x doesn't do access checks, so try with full access first */ 1079 if (!NtOpenSection( &ret, access | SECTION_MAP_READ | SECTION_MAP_WRITE, &attr )) return ret; 1080 } 1081 1082 if (!set_ntstatus( NtOpenSection( &ret, access, &attr ))) return 0; 1083 return ret; 1084} 1085 1086 1087/*********************************************************************** 1088 * OpenFileMappingFromApp (kernelbase.@) 1089 */ 1090HANDLE WINAPI DECLSPEC_HOTPATCH OpenFileMappingFromApp( ULONG access, BOOL inherit, LPCWSTR name ) 1091{ 1092 OBJECT_ATTRIBUTES attr; 1093 UNICODE_STRING nameW; 1094 HANDLE ret; 1095 1096 if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0; 1097 1098 if (access == FILE_MAP_COPY) access = SECTION_MAP_READ; 1099 1100 if (!set_ntstatus( NtOpenSection( &ret, access, &attr ))) return 0; 1101 return ret; 1102} 1103 1104 1105/*********************************************************************** 1106 * Condition variables 1107 ***********************************************************************/ 1108 1109 1110/*********************************************************************** 1111 * SleepConditionVariableCS (kernelbase.@) 1112 */ 1113BOOL WINAPI DECLSPEC_HOTPATCH SleepConditionVariableCS( CONDITION_VARIABLE *variable, 1114 CRITICAL_SECTION *crit, DWORD timeout ) 1115{ 1116 LARGE_INTEGER time; 1117 1118 return set_ntstatus( RtlSleepConditionVariableCS( variable, crit, get_nt_timeout( &time, timeout ))); 1119} 1120 1121 1122/*********************************************************************** 1123 * SleepConditionVariableSRW (kernelbase.@) 1124 */ 1125BOOL WINAPI DECLSPEC_HOTPATCH SleepConditionVariableSRW( RTL_CONDITION_VARIABLE *variable, 1126 RTL_SRWLOCK *lock, DWORD timeout, ULONG flags ) 1127{ 1128 LARGE_INTEGER time; 1129 1130 return set_ntstatus( RtlSleepConditionVariableSRW( variable, lock, 1131 get_nt_timeout( &time, timeout ), flags )); 1132} 1133 1134 1135/*********************************************************************** 1136 * I/O completions 1137 ***********************************************************************/ 1138 1139 1140/****************************************************************************** 1141 * CreateIoCompletionPort (kernelbase.@) 1142 */ 1143HANDLE WINAPI DECLSPEC_HOTPATCH CreateIoCompletionPort( HANDLE handle, HANDLE port, 1144 ULONG_PTR key, DWORD threads ) 1145{ 1146 FILE_COMPLETION_INFORMATION info; 1147 IO_STATUS_BLOCK iosb; 1148 HANDLE ret = port; 1149 1150 TRACE( "(%p, %p, %08Ix, %08lx)\n", handle, port, key, threads ); 1151 1152 if (!port) 1153 { 1154 if (!set_ntstatus( NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, threads ))) 1155 return 0; 1156 } 1157 else if (handle == INVALID_HANDLE_VALUE) 1158 { 1159 SetLastError( ERROR_INVALID_PARAMETER ); 1160 return 0; 1161 } 1162 1163 if (handle != INVALID_HANDLE_VALUE) 1164 { 1165 info.CompletionPort = ret; 1166 info.CompletionKey = key; 1167 if (!set_ntstatus( NtSetInformationFile( handle, &iosb, &info, sizeof(info), FileCompletionInformation ))) 1168 { 1169 if (!port) CloseHandle( ret ); 1170 return 0; 1171 } 1172 } 1173 return ret; 1174} 1175 1176 1177/****************************************************************************** 1178 * GetQueuedCompletionStatus (kernelbase.@) 1179 */ 1180BOOL WINAPI DECLSPEC_HOTPATCH GetQueuedCompletionStatus( HANDLE port, LPDWORD count, PULONG_PTR key, 1181 LPOVERLAPPED *overlapped, DWORD timeout ) 1182{ 1183 NTSTATUS status; 1184 IO_STATUS_BLOCK iosb; 1185 LARGE_INTEGER wait_time; 1186 1187 TRACE( "(%p,%p,%p,%p,%ld)\n", port, count, key, overlapped, timeout ); 1188 1189 *overlapped = NULL; 1190 status = NtRemoveIoCompletion( port, key, (PULONG_PTR)overlapped, &iosb, 1191 get_nt_timeout( &wait_time, timeout ) ); 1192 if (status == STATUS_SUCCESS) 1193 { 1194 *count = iosb.Information; 1195 if (iosb.Status >= 0) return TRUE; 1196 SetLastError( RtlNtStatusToDosError(iosb.Status) ); 1197 return FALSE; 1198 } 1199 1200 if (status == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT ); 1201 else if (status == STATUS_ABANDONED) SetLastError( ERROR_ABANDONED_WAIT_0 ); 1202 else SetLastError( RtlNtStatusToDosError(status) ); 1203 return FALSE; 1204} 1205 1206/****************************************************************************** 1207 * GetQueuedCompletionStatusEx (kernelbase.@) 1208 */ 1209BOOL WINAPI DECLSPEC_HOTPATCH GetQueuedCompletionStatusEx( HANDLE port, OVERLAPPED_ENTRY *entries, 1210 ULONG count, ULONG *written, 1211 DWORD timeout, BOOL alertable ) 1212{ 1213 LARGE_INTEGER time; 1214 NTSTATUS ret; 1215 1216 TRACE( "%p %p %lu %p %lu %u\n", port, entries, count, written, timeout, alertable ); 1217 1218 ret = NtRemoveIoCompletionEx( port, (FILE_IO_COMPLETION_INFORMATION *)entries, count, 1219 written, get_nt_timeout( &time, timeout ), alertable ); 1220 if (ret == STATUS_SUCCESS) return TRUE; 1221 else if (ret == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT ); 1222 else if (ret == STATUS_USER_APC) SetLastError( WAIT_IO_COMPLETION ); 1223 else if (ret == STATUS_ABANDONED) SetLastError( ERROR_ABANDONED_WAIT_0 ); 1224 else SetLastError( RtlNtStatusToDosError(ret) ); 1225 return FALSE; 1226} 1227 1228 1229/****************************************************************************** 1230 * PostQueuedCompletionStatus (kernelbase.@) 1231 */ 1232BOOL WINAPI DECLSPEC_HOTPATCH PostQueuedCompletionStatus( HANDLE port, DWORD count, 1233 ULONG_PTR key, LPOVERLAPPED overlapped ) 1234{ 1235 TRACE( "%p %ld %08Ix %p\n", port, count, key, overlapped ); 1236 1237 return set_ntstatus( NtSetIoCompletion( port, key, (ULONG_PTR)overlapped, STATUS_SUCCESS, count )); 1238} 1239 1240 1241/*********************************************************************** 1242 * Named pipes 1243 ***********************************************************************/ 1244 1245 1246/*********************************************************************** 1247 * CallNamedPipeW (kernelbase.@) 1248 */ 1249BOOL WINAPI DECLSPEC_HOTPATCH CallNamedPipeW( LPCWSTR name, LPVOID input, DWORD in_size, 1250 LPVOID output, DWORD out_size, 1251 LPDWORD read_size, DWORD timeout ) 1252{ 1253 HANDLE pipe; 1254 BOOL ret; 1255 DWORD mode; 1256 1257 TRACE( "%s %p %ld %p %ld %p %ld\n", debugstr_w(name), 1258 input, in_size, output, out_size, read_size, timeout ); 1259 1260 pipe = CreateFileW( name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); 1261 if (pipe == INVALID_HANDLE_VALUE) 1262 { 1263 if (!WaitNamedPipeW( name, timeout )) return FALSE; 1264 pipe = CreateFileW( name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); 1265 if (pipe == INVALID_HANDLE_VALUE) return FALSE; 1266 } 1267 1268 mode = PIPE_READMODE_MESSAGE; 1269 ret = SetNamedPipeHandleState( pipe, &mode, NULL, NULL ); 1270 if (ret) ret = TransactNamedPipe( pipe, input, in_size, output, out_size, read_size, NULL ); 1271 CloseHandle( pipe ); 1272 return ret; 1273} 1274 1275 1276/*********************************************************************** 1277 * ConnectNamedPipe (kernelbase.@) 1278 */ 1279BOOL WINAPI DECLSPEC_HOTPATCH ConnectNamedPipe( HANDLE pipe, LPOVERLAPPED overlapped ) 1280{ 1281 NTSTATUS status; 1282 IO_STATUS_BLOCK status_block; 1283 LPVOID cvalue = NULL; 1284 1285 TRACE( "(%p,%p)\n", pipe, overlapped ); 1286 1287 if (overlapped) 1288 { 1289 overlapped->Internal = STATUS_PENDING; 1290 overlapped->InternalHigh = 0; 1291 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped; 1292 } 1293 1294 status = NtFsControlFile( pipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue, 1295 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block, 1296 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0 ); 1297 if (status == STATUS_PENDING && !overlapped) 1298 { 1299 WaitForSingleObject( pipe, INFINITE ); 1300 status = status_block.Status; 1301 } 1302 return set_ntstatus( status ); 1303} 1304 1305/*********************************************************************** 1306 * CreateNamedPipeW (kernelbase.@) 1307 */ 1308HANDLE WINAPI DECLSPEC_HOTPATCH CreateNamedPipeW( LPCWSTR name, DWORD open_mode, DWORD pipe_mode, 1309 DWORD instances, DWORD out_buff, DWORD in_buff, 1310 DWORD timeout, LPSECURITY_ATTRIBUTES sa ) 1311{ 1312 HANDLE handle; 1313 UNICODE_STRING nt_name; 1314 OBJECT_ATTRIBUTES attr; 1315 DWORD access, options, sharing; 1316 BOOLEAN pipe_type, read_mode, non_block; 1317 NTSTATUS status; 1318 IO_STATUS_BLOCK iosb; 1319 LARGE_INTEGER time; 1320 1321 TRACE( "(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n", debugstr_w(name), 1322 open_mode, pipe_mode, instances, out_buff, in_buff, timeout, sa ); 1323 1324 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL )) 1325 { 1326 SetLastError( ERROR_PATH_NOT_FOUND ); 1327 return INVALID_HANDLE_VALUE; 1328 } 1329 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ) 1330 { 1331 SetLastError( ERROR_FILENAME_EXCED_RANGE ); 1332 RtlFreeUnicodeString( &nt_name ); 1333 return INVALID_HANDLE_VALUE; 1334 } 1335 1336 attr.Length = sizeof(attr); 1337 attr.RootDirectory = 0; 1338 attr.ObjectName = &nt_name; 1339 attr.Attributes = OBJ_CASE_INSENSITIVE | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0); 1340 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL; 1341 attr.SecurityQualityOfService = NULL; 1342 1343 switch (open_mode & 3) 1344 { 1345 case PIPE_ACCESS_INBOUND: 1346 sharing = FILE_SHARE_WRITE; 1347 access = GENERIC_READ; 1348 break; 1349 case PIPE_ACCESS_OUTBOUND: 1350 sharing = FILE_SHARE_READ; 1351 access = GENERIC_WRITE; 1352 break; 1353 case PIPE_ACCESS_DUPLEX: 1354 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; 1355 access = GENERIC_READ | GENERIC_WRITE; 1356 break; 1357 default: 1358 SetLastError( ERROR_INVALID_PARAMETER ); 1359 return INVALID_HANDLE_VALUE; 1360 } 1361 access |= SYNCHRONIZE; 1362 options = 0; 1363 if (open_mode & WRITE_DAC) access |= WRITE_DAC; 1364 if (open_mode & WRITE_OWNER) access |= WRITE_OWNER; 1365 if (open_mode & ACCESS_SYSTEM_SECURITY) access |= ACCESS_SYSTEM_SECURITY; 1366 if (open_mode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH; 1367 if (!(open_mode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT; 1368 pipe_type = (pipe_mode & PIPE_TYPE_MESSAGE) != 0; 1369 read_mode = (pipe_mode & PIPE_READMODE_MESSAGE) != 0; 1370 non_block = (pipe_mode & PIPE_NOWAIT) != 0; 1371 if (instances >= PIPE_UNLIMITED_INSTANCES) instances = ~0U; 1372 1373 time.QuadPart = (ULONGLONG)timeout * -10000; 1374 status = NtCreateNamedPipeFile( &handle, access, &attr, &iosb, sharing, 1375 FILE_OPEN_IF, options, pipe_type, 1376 read_mode, non_block, instances, in_buff, out_buff, &time ); 1377 RtlFreeUnicodeString( &nt_name ); 1378 if (!set_ntstatus( status )) return INVALID_HANDLE_VALUE; 1379 SetLastError( iosb.Information == FILE_CREATED ? ERROR_SUCCESS : ERROR_ALREADY_EXISTS ); 1380 return handle; 1381} 1382 1383 1384/****************************************************************** 1385 * CreatePipe (kernelbase.@) 1386 */ 1387BOOL WINAPI DECLSPEC_HOTPATCH CreatePipe( HANDLE *read_pipe, HANDLE *write_pipe, 1388 SECURITY_ATTRIBUTES *sa, DWORD size ) 1389{ 1390 static unsigned int index; 1391 WCHAR name[64]; 1392 UNICODE_STRING nt_name; 1393 OBJECT_ATTRIBUTES attr; 1394 IO_STATUS_BLOCK iosb; 1395 LARGE_INTEGER timeout; 1396 1397 *read_pipe = *write_pipe = INVALID_HANDLE_VALUE; 1398 1399 attr.Length = sizeof(attr); 1400 attr.RootDirectory = 0; 1401 attr.ObjectName = &nt_name; 1402 attr.Attributes = OBJ_CASE_INSENSITIVE | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0); 1403 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL; 1404 attr.SecurityQualityOfService = NULL; 1405 1406 if (!size) size = 4096; 1407 1408 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000; 1409 1410 /* generate a unique pipe name (system wide) */ 1411 for (;;) 1412 { 1413 swprintf( name, ARRAY_SIZE(name), L"\\??\\pipe\\Win32.Pipes.%08lu.%08u", 1414 GetCurrentProcessId(), ++index ); 1415 RtlInitUnicodeString( &nt_name, name ); 1416 if (!NtCreateNamedPipeFile( read_pipe, GENERIC_READ | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE, 1417 &attr, &iosb, FILE_SHARE_WRITE, FILE_OPEN_IF, 1418 FILE_SYNCHRONOUS_IO_NONALERT, 1419 FALSE, FALSE, FALSE, 1, size, size, &timeout )) 1420 break; 1421 } 1422 if (!set_ntstatus( NtOpenFile( write_pipe, GENERIC_WRITE | FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, 1423 &iosb, 0, FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE ))) 1424 { 1425 NtClose( *read_pipe ); 1426 return FALSE; 1427 } 1428 return TRUE; 1429} 1430 1431 1432/*********************************************************************** 1433 * DisconnectNamedPipe (kernelbase.@) 1434 */ 1435BOOL WINAPI DECLSPEC_HOTPATCH DisconnectNamedPipe( HANDLE pipe ) 1436{ 1437 IO_STATUS_BLOCK io_block; 1438 1439 TRACE( "(%p)\n", pipe ); 1440 return set_ntstatus( NtFsControlFile( pipe, 0, NULL, NULL, &io_block, 1441 FSCTL_PIPE_DISCONNECT, NULL, 0, NULL, 0 )); 1442} 1443 1444 1445/*********************************************************************** 1446 * GetNamedPipeHandleStateW (kernelbase.@) 1447 */ 1448BOOL WINAPI DECLSPEC_HOTPATCH GetNamedPipeHandleStateW( HANDLE pipe, DWORD *state, DWORD *instances, 1449 DWORD *max_count, DWORD *timeout, 1450 WCHAR *user, DWORD size ) 1451{ 1452 IO_STATUS_BLOCK io; 1453 1454 FIXME( "%p %p %p %p %p %p %ld: semi-stub\n", pipe, state, instances, max_count, timeout, user, size ); 1455 1456 if (max_count) *max_count = 0; 1457 if (timeout) *timeout = 0; 1458 if (user && size && !GetEnvironmentVariableW( L"WINEUSERNAME", user, size )) user[0] = 0; 1459 1460 if (state) 1461 { 1462 FILE_PIPE_INFORMATION info; 1463 1464 if (!set_ntstatus( NtQueryInformationFile( pipe, &io, &info, sizeof(info), FilePipeInformation ))) 1465 return FALSE; 1466 1467 *state = (info.ReadMode ? PIPE_READMODE_MESSAGE : PIPE_READMODE_BYTE) | 1468 (info.CompletionMode ? PIPE_NOWAIT : PIPE_WAIT); 1469 } 1470 if (instances) 1471 { 1472 FILE_PIPE_LOCAL_INFORMATION info; 1473 1474 if (!set_ntstatus( NtQueryInformationFile( pipe, &io, &info, sizeof(info), 1475 FilePipeLocalInformation))) 1476 return FALSE; 1477 *instances = info.CurrentInstances; 1478 } 1479 return TRUE; 1480} 1481 1482 1483/*********************************************************************** 1484 * GetNamedPipeInfo (kernelbase.@) 1485 */ 1486BOOL WINAPI DECLSPEC_HOTPATCH GetNamedPipeInfo( HANDLE pipe, LPDWORD flags, LPDWORD out_size, 1487 LPDWORD in_size, LPDWORD instances ) 1488{ 1489 FILE_PIPE_LOCAL_INFORMATION info; 1490 IO_STATUS_BLOCK iosb; 1491 1492 if (!set_ntstatus( NtQueryInformationFile( pipe, &iosb, &info, sizeof(info), FilePipeLocalInformation ))) 1493 return FALSE; 1494 1495 if (flags) 1496 { 1497 *flags = (info.NamedPipeEnd & FILE_PIPE_SERVER_END) ? PIPE_SERVER_END : PIPE_CLIENT_END; 1498 *flags |= (info.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ? PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE; 1499 } 1500 if (out_size) *out_size = info.OutboundQuota; 1501 if (in_size) *in_size = info.InboundQuota; 1502 if (instances) *instances = info.MaximumInstances; 1503 return TRUE; 1504} 1505 1506 1507/*********************************************************************** 1508 * PeekNamedPipe (kernelbase.@) 1509 */ 1510BOOL WINAPI DECLSPEC_HOTPATCH PeekNamedPipe( HANDLE pipe, LPVOID out_buffer, DWORD size, 1511 LPDWORD read_size, LPDWORD avail, LPDWORD message ) 1512{ 1513 FILE_PIPE_PEEK_BUFFER local_buffer; 1514 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer; 1515 IO_STATUS_BLOCK io; 1516 NTSTATUS status; 1517 1518 if (size && !(buffer = HeapAlloc( GetProcessHeap(), 0, 1519 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[size] )))) 1520 { 1521 SetLastError( ERROR_NOT_ENOUGH_MEMORY ); 1522 return FALSE; 1523 } 1524 1525 status = NtFsControlFile( pipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0, 1526 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[size] ) ); 1527 if (status == STATUS_BUFFER_OVERFLOW) status = STATUS_SUCCESS; 1528 if (!status) 1529 { 1530 ULONG count = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data ); 1531 if (avail) *avail = buffer->ReadDataAvailable; 1532 if (read_size) *read_size = count; 1533 if (message) *message = buffer->MessageLength - count; 1534 if (out_buffer) memcpy( out_buffer, buffer->Data, count ); 1535 } 1536 else SetLastError( RtlNtStatusToDosError(status) ); 1537 1538 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer ); 1539 return !status; 1540} 1541 1542 1543/*********************************************************************** 1544 * SetNamedPipeHandleState (kernelbase.@) 1545 */ 1546BOOL WINAPI DECLSPEC_HOTPATCH SetNamedPipeHandleState( HANDLE pipe, LPDWORD mode, 1547 LPDWORD count, LPDWORD timeout ) 1548{ 1549 FILE_PIPE_INFORMATION info; 1550 IO_STATUS_BLOCK iosb; 1551 NTSTATUS status = STATUS_SUCCESS; 1552 1553 TRACE( "%p %p/%ld %p %p\n", pipe, mode, mode ? *mode : 0, count, timeout ); 1554 if (count || timeout) FIXME( "Unsupported arguments\n" ); 1555 1556 if (mode) 1557 { 1558 if (*mode & ~(PIPE_READMODE_MESSAGE | PIPE_NOWAIT)) status = STATUS_INVALID_PARAMETER; 1559 else 1560 { 1561 info.CompletionMode = (*mode & PIPE_NOWAIT) ? 1562 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION; 1563 info.ReadMode = (*mode & PIPE_READMODE_MESSAGE) ? 1564 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE; 1565 status = NtSetInformationFile( pipe, &iosb, &info, sizeof(info), FilePipeInformation ); 1566 } 1567 } 1568 return set_ntstatus( status ); 1569} 1570 1571/*********************************************************************** 1572 * TransactNamedPipe (kernelbase.@) 1573 */ 1574BOOL WINAPI DECLSPEC_HOTPATCH TransactNamedPipe( HANDLE handle, LPVOID write_buf, DWORD write_size, 1575 LPVOID read_buf, DWORD read_size, LPDWORD bytes_read, 1576 LPOVERLAPPED overlapped) 1577{ 1578 IO_STATUS_BLOCK default_iosb, *iosb = &default_iosb; 1579 HANDLE event = NULL; 1580 void *cvalue = NULL; 1581 NTSTATUS status; 1582 1583 TRACE( "%p %p %lu %p %lu %p %p\n", handle, 1584 write_buf, write_size, read_buf, read_size, bytes_read, overlapped ); 1585 1586 if (overlapped) 1587 { 1588 event = overlapped->hEvent; 1589 iosb = (IO_STATUS_BLOCK *)overlapped; 1590 if (((ULONG_PTR)event & 1) == 0) cvalue = overlapped; 1591 } 1592 else 1593 { 1594 iosb->Information = 0; 1595 } 1596 1597 status = NtFsControlFile( handle, event, NULL, cvalue, iosb, FSCTL_PIPE_TRANSCEIVE, 1598 write_buf, write_size, read_buf, read_size ); 1599 if (status == STATUS_PENDING && !overlapped) 1600 { 1601 WaitForSingleObject(handle, INFINITE); 1602 status = iosb->Status; 1603 } 1604 1605 if (bytes_read) *bytes_read = overlapped && status ? 0 : iosb->Information; 1606 return set_ntstatus( status ); 1607} 1608 1609 1610/*********************************************************************** 1611 * WaitNamedPipeW (kernelbase.@) 1612 */ 1613BOOL WINAPI DECLSPEC_HOTPATCH WaitNamedPipeW( LPCWSTR name, DWORD timeout ) 1614{ 1615 static const int prefix_len = sizeof(L"\\??\\PIPE\\") - sizeof(WCHAR); 1616 NTSTATUS status; 1617 UNICODE_STRING nt_name, pipe_dev_name; 1618 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait; 1619 IO_STATUS_BLOCK iosb; 1620 OBJECT_ATTRIBUTES attr; 1621 ULONG wait_size; 1622 HANDLE pipe_dev; 1623 1624 TRACE( "%s 0x%08lx\n", debugstr_w(name), timeout ); 1625 1626 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL )) return FALSE; 1627 1628 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) || 1629 nt_name.Length < prefix_len || 1630 wcsnicmp( nt_name.Buffer, L"\\??\\PIPE\\", prefix_len / sizeof(WCHAR) )) 1631 { 1632 RtlFreeUnicodeString( &nt_name ); 1633 SetLastError( ERROR_PATH_NOT_FOUND ); 1634 return FALSE; 1635 } 1636 1637 wait_size = offsetof( FILE_PIPE_WAIT_FOR_BUFFER, Name[(nt_name.Length - prefix_len) / sizeof(WCHAR)] ); 1638 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, wait_size))) 1639 { 1640 RtlFreeUnicodeString( &nt_name ); 1641 SetLastError( ERROR_OUTOFMEMORY ); 1642 return FALSE; 1643 } 1644 1645 pipe_dev_name.Buffer = nt_name.Buffer; 1646 pipe_dev_name.Length = prefix_len; 1647 pipe_dev_name.MaximumLength = prefix_len; 1648 InitializeObjectAttributes( &attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL ); 1649 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, 1650 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE, 1651 FILE_SYNCHRONOUS_IO_NONALERT); 1652 if (status != STATUS_SUCCESS) 1653 { 1654 HeapFree( GetProcessHeap(), 0, pipe_wait ); 1655 RtlFreeUnicodeString( &nt_name ); 1656 SetLastError( ERROR_PATH_NOT_FOUND ); 1657 return FALSE; 1658 } 1659 1660 pipe_wait->TimeoutSpecified = !(timeout == NMPWAIT_USE_DEFAULT_WAIT); 1661 if (timeout == NMPWAIT_WAIT_FOREVER) 1662 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff; 1663 else 1664 pipe_wait->Timeout.QuadPart = (ULONGLONG)timeout * -10000; 1665 pipe_wait->NameLength = nt_name.Length - prefix_len; 1666 memcpy( pipe_wait->Name, nt_name.Buffer + prefix_len/sizeof(WCHAR), pipe_wait->NameLength ); 1667 RtlFreeUnicodeString( &nt_name ); 1668 1669 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT, 1670 pipe_wait, wait_size, NULL, 0 ); 1671 1672 HeapFree( GetProcessHeap(), 0, pipe_wait ); 1673 NtClose( pipe_dev ); 1674 return set_ntstatus( status ); 1675} 1676 1677 1678 1679/*********************************************************************** 1680 * Interlocked functions 1681 ***********************************************************************/ 1682 1683 1684/*********************************************************************** 1685 * InitOnceBeginInitialize (kernelbase.@) 1686 */ 1687BOOL WINAPI DECLSPEC_HOTPATCH InitOnceBeginInitialize( INIT_ONCE *once, DWORD flags, 1688 BOOL *pending, void **context ) 1689{ 1690 NTSTATUS status = RtlRunOnceBeginInitialize( once, flags, context ); 1691 if (status >= 0) *pending = (status == STATUS_PENDING); 1692 else SetLastError( RtlNtStatusToDosError(status) ); 1693 return status >= 0; 1694} 1695 1696 1697/*********************************************************************** 1698 * InitOnceComplete (kernelbase.@) 1699 */ 1700BOOL WINAPI DECLSPEC_HOTPATCH InitOnceComplete( INIT_ONCE *once, DWORD flags, void *context ) 1701{ 1702 return set_ntstatus( RtlRunOnceComplete( once, flags, context )); 1703} 1704 1705 1706/*********************************************************************** 1707 * InitOnceExecuteOnce (kernelbase.@) 1708 */ 1709BOOL WINAPI DECLSPEC_HOTPATCH InitOnceExecuteOnce( INIT_ONCE *once, PINIT_ONCE_FN func, 1710 void *param, void **context ) 1711{ 1712 return !RtlRunOnceExecuteOnce( once, (PRTL_RUN_ONCE_INIT_FN)func, param, context ); 1713} 1714 1715#ifdef __i386__ 1716 1717/*********************************************************************** 1718 * InterlockedCompareExchange (kernelbase.@) 1719 */ 1720__ASM_STDCALL_FUNC(InterlockedCompareExchange, 12, 1721 "movl 12(%esp),%eax\n\t" 1722 "movl 8(%esp),%ecx\n\t" 1723 "movl 4(%esp),%edx\n\t" 1724 "lock; cmpxchgl %ecx,(%edx)\n\t" 1725 "ret $12") 1726 1727/*********************************************************************** 1728 * InterlockedExchange (kernelbase.@) 1729 */ 1730__ASM_STDCALL_FUNC(InterlockedExchange, 8, 1731 "movl 8(%esp),%eax\n\t" 1732 "movl 4(%esp),%edx\n\t" 1733 "lock; xchgl %eax,(%edx)\n\t" 1734 "ret $8") 1735 1736/*********************************************************************** 1737 * InterlockedExchangeAdd (kernelbase.@) 1738 */ 1739__ASM_STDCALL_FUNC(InterlockedExchangeAdd, 8, 1740 "movl 8(%esp),%eax\n\t" 1741 "movl 4(%esp),%edx\n\t" 1742 "lock; xaddl %eax,(%edx)\n\t" 1743 "ret $8") 1744 1745/*********************************************************************** 1746 * InterlockedIncrement (kernelbase.@) 1747 */ 1748__ASM_STDCALL_FUNC(InterlockedIncrement, 4, 1749 "movl 4(%esp),%edx\n\t" 1750 "movl $1,%eax\n\t" 1751 "lock; xaddl %eax,(%edx)\n\t" 1752 "incl %eax\n\t" 1753 "ret $4") 1754 1755/*********************************************************************** 1756 * InterlockedDecrement (kernelbase.@) 1757 */ 1758__ASM_STDCALL_FUNC(InterlockedDecrement, 4, 1759 "movl 4(%esp),%edx\n\t" 1760 "movl $-1,%eax\n\t" 1761 "lock; xaddl %eax,(%edx)\n\t" 1762 "decl %eax\n\t" 1763 "ret $4") 1764 1765#endif /* __i386__ */