ReactOS 0.4.15-dev-7958-gcd0bb1a
npipe.c File Reference
#include <k32.h>
#include <debug.h>
Include dependency graph for npipe.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

 DEBUG_CHANNEL (kernel32file)
 
static BOOL NpGetUserNamep (HANDLE hNamedPipe, LPWSTR lpUserName, DWORD nMaxUserNameSize)
 
BOOL WINAPI CreatePipe (PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpPipeAttributes, DWORD nSize)
 
HANDLE WINAPI CreateNamedPipeA (LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
 
HANDLE WINAPI CreateNamedPipeW (LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
 
BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
 
BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
 
BOOL WINAPI ConnectNamedPipe (IN HANDLE hNamedPipe, IN LPOVERLAPPED lpOverlapped)
 
BOOL WINAPI SetNamedPipeHandleState (HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout)
 
BOOL WINAPI CallNamedPipeA (LPCSTR lpNamedPipeName, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesRead, DWORD nTimeOut)
 
BOOL WINAPI CallNamedPipeW (LPCWSTR lpNamedPipeName, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesRead, DWORD nTimeOut)
 
BOOL WINAPI DisconnectNamedPipe (HANDLE hNamedPipe)
 
BOOL WINAPI GetNamedPipeHandleStateW (HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout, LPWSTR lpUserName, DWORD nMaxUserNameSize)
 
BOOL WINAPI GetNamedPipeHandleStateA (HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout, LPSTR lpUserName, DWORD nMaxUserNameSize)
 
BOOL WINAPI GetNamedPipeInfo (HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutBufferSize, LPDWORD lpInBufferSize, LPDWORD lpMaxInstances)
 
BOOL WINAPI PeekNamedPipe (HANDLE hNamedPipe, LPVOID lpBuffer, DWORD nBufferSize, LPDWORD lpBytesRead, LPDWORD lpTotalBytesAvail, LPDWORD lpBytesLeftThisMessage)
 
BOOL WINAPI TransactNamedPipe (IN HANDLE hNamedPipe, IN LPVOID lpInBuffer, IN DWORD nInBufferSize, OUT LPVOID lpOutBuffer, IN DWORD nOutBufferSize, OUT LPDWORD lpBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
 

Variables

LONG ProcessPipeId
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file npipe.c.

Function Documentation

◆ CallNamedPipeA()

BOOL WINAPI CallNamedPipeA ( LPCSTR  lpNamedPipeName,
LPVOID  lpInBuffer,
DWORD  nInBufferSize,
LPVOID  lpOutBuffer,
DWORD  nOutBufferSize,
LPDWORD  lpBytesRead,
DWORD  nTimeOut 
)

Definition at line 860 of file npipe.c.

867{
868 PUNICODE_STRING PipeName = &NtCurrentTeb()->StaticUnicodeString;
869 ANSI_STRING AnsiPipe;
870
871 /* Initialize the string as ANSI_STRING and convert to Unicode */
872 RtlInitAnsiString(&AnsiPipe, (LPSTR)lpNamedPipeName);
873 RtlAnsiStringToUnicodeString(PipeName, &AnsiPipe, FALSE);
874
875 /* Call the Unicode function */
876 return CallNamedPipeW(PipeName->Buffer,
877 lpInBuffer,
878 nInBufferSize,
879 lpOutBuffer,
880 nOutBufferSize,
881 lpBytesRead,
882 nTimeOut);
883}
#define FALSE
Definition: types.h:117
#define NtCurrentTeb
BOOL WINAPI CallNamedPipeW(LPCWSTR lpNamedPipeName, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesRead, DWORD nTimeOut)
Definition: npipe.c:891
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
char * LPSTR
Definition: xmlstorage.h:182

◆ CallNamedPipeW()

BOOL WINAPI CallNamedPipeW ( LPCWSTR  lpNamedPipeName,
LPVOID  lpInBuffer,
DWORD  nInBufferSize,
LPVOID  lpOutBuffer,
DWORD  nOutBufferSize,
LPDWORD  lpBytesRead,
DWORD  nTimeOut 
)

Definition at line 891 of file npipe.c.

898{
899 HANDLE hPipe;
900 BOOL bRetry = TRUE;
901 BOOL bError;
902 DWORD dwPipeMode;
903
904 while (TRUE)
905 {
906 /* Try creating it */
907 hPipe = CreateFileW(lpNamedPipeName,
910 NULL,
913 NULL);
914
915 /* Success, break out */
916 if (hPipe != INVALID_HANDLE_VALUE)
917 break;
918
919 /* Already tried twice, give up */
920 if (bRetry == FALSE)
921 return FALSE;
922
923 /* Wait on it */
924 WaitNamedPipeW(lpNamedPipeName, nTimeOut);
925
926 /* Get ready to try again */
927 bRetry = FALSE;
928 }
929
930 /* Set the pipe mode */
931 dwPipeMode = PIPE_READMODE_MESSAGE | PIPE_WAIT;
932 bError = SetNamedPipeHandleState(hPipe, &dwPipeMode, NULL, NULL);
933 if (!bError)
934 {
935 /* Couldn't change state, fail */
936 CloseHandle(hPipe);
937 return FALSE;
938 }
939
940 /* Do the transact */
941 bError = TransactNamedPipe(hPipe,
942 lpInBuffer,
943 nInBufferSize,
944 lpOutBuffer,
945 nOutBufferSize,
946 lpBytesRead,
947 NULL);
948
949 /* Close the handle */
950 CloseHandle(hPipe);
951
952 return bError;
953}
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GENERIC_READ
Definition: compat.h:135
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI TransactNamedPipe(IN HANDLE hNamedPipe, IN LPVOID lpInBuffer, IN DWORD nInBufferSize, OUT LPVOID lpOutBuffer, IN DWORD nOutBufferSize, OUT LPDWORD lpBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: npipe.c:1315
BOOL WINAPI SetNamedPipeHandleState(HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout)
Definition: npipe.c:774
BOOL WINAPI WaitNamedPipeW(LPCWSTR lpNamedPipeName, DWORD nTimeOut)
Definition: npipe.c:458
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
#define PIPE_WAIT
Definition: winbase.h:171
#define PIPE_READMODE_MESSAGE
Definition: winbase.h:170

Referenced by CallNamedPipeA().

◆ ConnectNamedPipe()

BOOL WINAPI ConnectNamedPipe ( IN HANDLE  hNamedPipe,
IN LPOVERLAPPED  lpOverlapped 
)

Definition at line 701 of file npipe.c.

703{
705
706 if (lpOverlapped != NULL)
707 {
709
710 lpOverlapped->Internal = STATUS_PENDING;
711 ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
712
713 Status = NtFsControlFile(hNamedPipe,
714 lpOverlapped->hEvent,
715 NULL,
719 NULL,
720 0,
721 NULL,
722 0);
723
724 /* return FALSE in case of failure and pending operations! */
726 {
728 return FALSE;
729 }
730 }
731 else
732 {
734
735 Status = NtFsControlFile(hNamedPipe,
736 NULL,
737 NULL,
738 NULL,
739 &Iosb,
741 NULL,
742 0,
743 NULL,
744 0);
745
746 /* wait in case operation is pending */
747 if (Status == STATUS_PENDING)
748 {
749 Status = NtWaitForSingleObject(hNamedPipe,
750 FALSE,
751 NULL);
752 if (NT_SUCCESS(Status))
753 {
754 Status = Iosb.Status;
755 }
756 }
757
758 if (!NT_SUCCESS(Status))
759 {
761 return FALSE;
762 }
763 }
764
765 return TRUE;
766}
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
return Iosb
Definition: create.c:4402
#define ULONG_PTR
Definition: config.h:101
Status
Definition: gdiplustypes.h:25
#define FSCTL_PIPE_LISTEN
Definition: pipe.c:63
_In_ HANDLE _In_ DWORD _In_ DWORD _Inout_opt_ LPOVERLAPPED lpOverlapped
Definition: mswsock.h:93
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE _In_opt_ PVOID ApcContext
Definition: iofuncs.h:727
NTSYSAPI NTSTATUS NTAPI NtWaitForSingleObject(IN HANDLE hObject, IN BOOLEAN bAlertable, IN PLARGE_INTEGER Timeout)
NTSYSAPI NTSTATUS NTAPI NtFsControlFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG DeviceIoControlCode, IN PVOID InBuffer OPTIONAL, IN ULONG InBufferLength, OUT PVOID OutBuffer OPTIONAL, IN ULONG OutBufferLength)
#define STATUS_PENDING
Definition: ntstatus.h:82
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166

Referenced by _RunRemoteTest(), create_overlapped_pipe(), custom_start_server(), InstallDevice(), local_server_thread(), main(), overlapped_server(), pipe_thread(), pipeserver(), PipeThreadProc(), ScmWaitForServiceConnect(), serverThreadMain1(), serverThreadMain2(), serverThreadMain3(), serverThreadMain4(), serverThreadMain5(), test_CreateNamedPipe(), test_ImpersonateNamedPipeClient(), test_overlapped_error(), test_readfileex_pending(), and test_write_watch().

◆ CreateNamedPipeA()

HANDLE WINAPI CreateNamedPipeA ( LPCSTR  lpName,
DWORD  dwOpenMode,
DWORD  dwPipeMode,
DWORD  nMaxInstances,
DWORD  nOutBufferSize,
DWORD  nInBufferSize,
DWORD  nDefaultTimeOut,
LPSECURITY_ATTRIBUTES  lpSecurityAttributes 
)

◆ CreateNamedPipeW()

HANDLE WINAPI CreateNamedPipeW ( LPCWSTR  lpName,
DWORD  dwOpenMode,
DWORD  dwPipeMode,
DWORD  nMaxInstances,
DWORD  nOutBufferSize,
DWORD  nInBufferSize,
DWORD  nDefaultTimeOut,
LPSECURITY_ATTRIBUTES  lpSecurityAttributes 
)

Definition at line 246 of file npipe.c.

254{
255 UNICODE_STRING NamedPipeName;
256 BOOL Result;
262 ULONG WriteModeMessage;
263 ULONG ReadModeMessage;
264 ULONG NonBlocking;
267 LARGE_INTEGER DefaultTimeOut;
269
270 /* Check for valid instances */
271 if (nMaxInstances == 0 || nMaxInstances > PIPE_UNLIMITED_INSTANCES)
272 {
273 /* Fail */
276 }
277
278 /* Convert to NT syntax */
279 if (nMaxInstances == PIPE_UNLIMITED_INSTANCES)
280 nMaxInstances = -1;
281
282 /* Convert the name */
284 &NamedPipeName,
285 NULL,
286 NULL);
287 if (!Result)
288 {
289 /* Conversion failed */
292 }
293
294 TRACE("Pipe name: %wZ\n", &NamedPipeName);
295 TRACE("Pipe name: %S\n", NamedPipeName.Buffer);
296
297 /* Always case insensitive, check if we got extra attributes */
299 if (lpSecurityAttributes)
300 {
301 /* We did; get the security descriptor */
302 SecurityDescriptor = lpSecurityAttributes->lpSecurityDescriptor;
303
304 /* And check if this is pipe's handle will beinheritable */
305 if (lpSecurityAttributes->bInheritHandle)
307 }
308
309 /* Now we can initialize the object attributes */
311 &NamedPipeName,
313 NULL,
315
316 /* Setup the default Desired Access */
317 DesiredAccess = SYNCHRONIZE | (dwOpenMode & (WRITE_DAC |
320
321 /* Convert to NT Create Flags */
322 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH)
323 {
325 }
326
327 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED))
328 {
330 }
331
332 /* Handle all open modes */
333 if (dwOpenMode & PIPE_ACCESS_OUTBOUND)
334 {
337 }
338
339 if (dwOpenMode & PIPE_ACCESS_INBOUND)
340 {
343 }
344
345 /* Handle the type flags */
346 if (dwPipeMode & PIPE_TYPE_MESSAGE)
347 {
348 WriteModeMessage = FILE_PIPE_MESSAGE_TYPE;
349 }
350 else
351 {
352 WriteModeMessage = FILE_PIPE_BYTE_STREAM_TYPE;
353 }
354
355 /* Handle the mode flags */
356 if (dwPipeMode & PIPE_READMODE_MESSAGE)
357 {
358 ReadModeMessage = FILE_PIPE_MESSAGE_MODE;
359 }
360 else
361 {
362 ReadModeMessage = FILE_PIPE_BYTE_STREAM_MODE;
363 }
364
365 /* Handle the blocking mode */
366 if (dwPipeMode & PIPE_NOWAIT)
367 {
368 NonBlocking = FILE_PIPE_COMPLETE_OPERATION;
369 }
370 else
371 {
372 NonBlocking = FILE_PIPE_QUEUE_OPERATION;
373 }
374
375 /* Check if we have a timeout */
376 if (nDefaultTimeOut)
377 {
378 /* Convert the time to NT format */
379 DefaultTimeOut.QuadPart = nDefaultTimeOut * -10000LL;
380 }
381 else
382 {
383 /* Use default timeout of 50 ms */
384 DefaultTimeOut.QuadPart = -500000;
385 }
386
387 /* Now create the pipe */
391 &Iosb,
395 WriteModeMessage,
396 ReadModeMessage,
397 NonBlocking,
398 nMaxInstances,
399 nInBufferSize,
400 nOutBufferSize,
401 &DefaultTimeOut);
402
403 /* Normalize special error codes */
406 {
408 }
409
410 /* Free the name */
411 RtlFreeHeap(RtlGetProcessHeap(),
412 0,
413 NamedPipeName.Buffer);
414
415 /* Check status */
416 if (!NT_SUCCESS(Status))
417 {
418 /* Failed to create it */
419 WARN("NtCreateNamedPipe failed (Status %x)!\n", Status);
422 }
423
424 /* Return the handle */
425 return PipeHandle;
426}
#define WARN(fmt,...)
Definition: debug.h:112
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:608
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
static HANDLE PipeHandle
Definition: dhcpcsvc.c:22
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define SetLastError(x)
Definition: compat.h:752
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK _In_ USHORT ShareAccess
Definition: create.c:4147
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define FILE_WRITE_THROUGH
Definition: from_kernel.h:26
#define FILE_OPEN_IF
Definition: from_kernel.h:56
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define OBJ_INHERIT
Definition: winternl.h:225
#define FILE_FLAG_OVERLAPPED
Definition: disk.h:46
#define FILE_FLAG_WRITE_THROUGH
Definition: disk.h:47
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define FILE_PIPE_MESSAGE_TYPE
Definition: iotypes.h:76
#define FILE_PIPE_BYTE_STREAM_TYPE
Definition: iotypes.h:75
#define FILE_PIPE_BYTE_STREAM_MODE
Definition: iotypes.h:77
#define FILE_PIPE_COMPLETE_OPERATION
Definition: iotypes.h:80
#define FILE_PIPE_MESSAGE_MODE
Definition: iotypes.h:78
#define FILE_PIPE_QUEUE_OPERATION
Definition: iotypes.h:79
NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(_In_opt_z_ PCWSTR DosPathName, _Out_ PUNICODE_STRING NtPathName, _Out_opt_ PCWSTR *NtFileNamePart, _Out_opt_ PRTL_RELATIVE_NAME_U DirectoryInfo)
#define SYNCHRONIZE
Definition: nt_native.h:61
#define WRITE_DAC
Definition: nt_native.h:59
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define ACCESS_SYSTEM_SECURITY
Definition: nt_native.h:77
#define WRITE_OWNER
Definition: nt_native.h:60
NTSTATUS NTAPI NtCreateNamedPipeFile(OUT PHANDLE FileHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK IoStatusBlock, IN ULONG ShareAccess, IN ULONG CreateDisposition, IN ULONG CreateOptions, IN ULONG NamedPipeType, IN ULONG ReadMode, IN ULONG CompletionMode, IN ULONG MaximumInstances, IN ULONG InboundQuota, IN ULONG OutboundQuota, IN PLARGE_INTEGER DefaultTimeout)
Definition: file.c:3858
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
#define TRACE(s)
Definition: solgame.cpp:4
LPVOID lpSecurityDescriptor
Definition: compat.h:193
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
#define STATUS_OBJECT_NAME_INVALID
Definition: udferr_usr.h:148
LONGLONG QuadPart
Definition: typedefs.h:114
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658
_Must_inspect_result_ _In_opt_ WDFKEY _In_ PCUNICODE_STRING _In_ ACCESS_MASK _In_ ULONG CreateOptions
Definition: wdfregistry.h:118
#define PIPE_ACCESS_INBOUND
Definition: winbase.h:165
#define PIPE_ACCESS_OUTBOUND
Definition: winbase.h:166
#define PIPE_NOWAIT
Definition: winbase.h:172
#define PIPE_TYPE_MESSAGE
Definition: winbase.h:168
#define PIPE_UNLIMITED_INSTANCES
Definition: winbase.h:175
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:106
_In_ USHORT _In_ ULONG _In_ PSOCKADDR _In_ PSOCKADDR _Reserved_ ULONG _In_opt_ PVOID _In_opt_ const WSK_CLIENT_CONNECTION_DISPATCH _In_opt_ PEPROCESS _In_opt_ PETHREAD _In_opt_ PSECURITY_DESCRIPTOR SecurityDescriptor
Definition: wsk.h:191
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409

Referenced by _RunRemoteTest(), CPipe::CPipe(), custom_start_server(), InstallDevice(), local_server_thread(), PipeThreadProc(), RPC_StartLocalServer(), ScmCreateNewControlPipe(), and test_runner().

◆ CreatePipe()

BOOL WINAPI CreatePipe ( PHANDLE  hReadPipe,
PHANDLE  hWritePipe,
LPSECURITY_ATTRIBUTES  lpPipeAttributes,
DWORD  nSize 
)

Definition at line 117 of file npipe.c.

121{
122 WCHAR Buffer[64];
123 UNICODE_STRING PipeName;
125 IO_STATUS_BLOCK StatusBlock;
126 LARGE_INTEGER DefaultTimeout;
128 HANDLE ReadPipeHandle;
129 HANDLE WritePipeHandle;
130 LONG PipeId;
133
134 /* Set the timeout to 120 seconds */
135 DefaultTimeout.QuadPart = -1200000000;
136
137 /* Use default buffer size if desired */
138 if (!nSize) nSize = 0x1000;
139
140 /* Increase the Pipe ID */
142
143 /* Create the pipe name */
145 L"\\Device\\NamedPipe\\Win32Pipes.%p.%08x",
147 PipeId);
148 RtlInitUnicodeString(&PipeName, Buffer);
149
150 /* Always use case insensitive */
152
153 /* Check if we got attributes */
154 if (lpPipeAttributes)
155 {
156 /* Use the attributes' SD instead */
157 SecurityDescriptor = lpPipeAttributes->lpSecurityDescriptor;
158
159 /* Set OBJ_INHERIT if requested */
160 if (lpPipeAttributes->bInheritHandle) Attributes |= OBJ_INHERIT;
161 }
162
163 /* Initialize the attributes */
165 &PipeName,
167 NULL,
169
170 /* Create the named pipe */
171 Status = NtCreateNamedPipeFile(&ReadPipeHandle,
174 &StatusBlock,
181 1,
182 nSize,
183 nSize,
184 &DefaultTimeout);
185 if (!NT_SUCCESS(Status))
186 {
187 /* Convert error and fail */
188 WARN("Status: %lx\n", Status);
190 return FALSE;
191 }
192
193 /* Now try opening it for write access */
194 Status = NtOpenFile(&WritePipeHandle,
197 &StatusBlock,
200 if (!NT_SUCCESS(Status))
201 {
202 /* Convert error and fail */
203 WARN("Status: %lx\n", Status);
204 NtClose(ReadPipeHandle);
206 return FALSE;
207 }
208
209 /* Return both handles */
210 *hReadPipe = ReadPipeHandle;
211 *hWritePipe = WritePipeHandle;
212 return TRUE;
213}
#define InterlockedIncrement
Definition: armddk.h:53
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
Definition: bufpool.h:45
#define swprintf
Definition: precomp.h:40
#define FILE_CREATE
Definition: from_kernel.h:55
LONG ProcessPipeId
Definition: npipe.c:19
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3952
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define FILE_WRITE_ATTRIBUTES
Definition: nt_native.h:649
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define FILE_GENERIC_WRITE
Definition: nt_native.h:660
#define L(x)
Definition: ntvdm.h:50
long LONG
Definition: pedump.c:60
HANDLE UniqueProcess
Definition: compat.h:825
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2084
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1151
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by _pipe(), _tpopen(), CreateClientProcess(), CreateHiddenConsoleProcess(), ExecutePipeline(), main(), PrepareForRedirect(), read_reg_output_(), RunShell(), test__open_osfhandle(), test_Console(), and test_CreatePipe().

◆ DEBUG_CHANNEL()

DEBUG_CHANNEL ( kernel32file  )

◆ DisconnectNamedPipe()

BOOL WINAPI DisconnectNamedPipe ( HANDLE  hNamedPipe)

Definition at line 961 of file npipe.c.

962{
965
966 /* Send the FSCTL to the driver */
967 Status = NtFsControlFile(hNamedPipe,
968 NULL,
969 NULL,
970 NULL,
971 &Iosb,
973 NULL,
974 0,
975 NULL,
976 0);
977 if (Status == STATUS_PENDING)
978 {
979 /* Wait on NPFS to finish and get updated status */
980 Status = NtWaitForSingleObject(hNamedPipe, FALSE, NULL);
981 if (NT_SUCCESS(Status))
982 Status = Iosb.Status;
983 }
984
985 /* Check for error */
986 if (!NT_SUCCESS(Status))
987 {
988 /* Fail */
990 return FALSE;
991 }
992
993 return TRUE;
994}
#define FSCTL_PIPE_DISCONNECT
Definition: winioctl.h:194

Referenced by InstanceThread(), local_server_thread(), main(), overlapped_server(), pipe_thread(), pipeserver(), PipeThreadProc(), serverThreadMain1(), serverThreadMain2(), serverThreadMain3(), serverThreadMain4(), serverThreadMain5(), test_alertable(), test_CreateNamedPipe(), test_DisconnectNamedPipe(), test_file_info(), test_overlapped_transport(), test_ReadFile(), and test_security_info().

◆ GetNamedPipeHandleStateA()

BOOL WINAPI GetNamedPipeHandleStateA ( HANDLE  hNamedPipe,
LPDWORD  lpState,
LPDWORD  lpCurInstances,
LPDWORD  lpMaxCollectionCount,
LPDWORD  lpCollectDataTimeout,
LPSTR  lpUserName,
DWORD  nMaxUserNameSize 
)

Definition at line 1103 of file npipe.c.

1110{
1111 UNICODE_STRING UserNameW = { 0, 0, NULL };
1112 ANSI_STRING UserNameA;
1113 BOOL Ret;
1114
1115 if (lpUserName != NULL)
1116 {
1117 UserNameW.MaximumLength = (USHORT)nMaxUserNameSize * sizeof(WCHAR);
1118 UserNameW.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, UserNameW.MaximumLength);
1119 if (UserNameW.Buffer == NULL)
1120 {
1122 return FALSE;
1123 }
1124
1125 UserNameA.Buffer = lpUserName;
1126 UserNameA.Length = 0;
1127 UserNameA.MaximumLength = (USHORT)nMaxUserNameSize;
1128 }
1129
1130 Ret = GetNamedPipeHandleStateW(hNamedPipe,
1131 lpState,
1132 lpCurInstances,
1133 lpMaxCollectionCount,
1134 lpCollectDataTimeout,
1135 UserNameW.Buffer,
1136 nMaxUserNameSize);
1137 if (Ret && lpUserName != NULL)
1138 {
1140
1141 RtlInitUnicodeString(&UserNameW, UserNameW.Buffer);
1142 Status = RtlUnicodeStringToAnsiString(&UserNameA, &UserNameW, FALSE);
1143 if (!NT_SUCCESS(Status))
1144 {
1146 Ret = FALSE;
1147 }
1148 }
1149
1150 if (UserNameW.Buffer != NULL)
1151 {
1152 RtlFreeHeap(RtlGetProcessHeap(), 0, UserNameW.Buffer);
1153 }
1154
1155 return Ret;
1156}
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:590
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
BOOL WINAPI GetNamedPipeHandleStateW(HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout, LPWSTR lpUserName, DWORD nMaxUserNameSize)
Definition: npipe.c:1002
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
unsigned short USHORT
Definition: pedump.c:61
USHORT MaximumLength
Definition: env_spec_w32.h:377
USHORT MaximumLength
Definition: env_spec_w32.h:370

Referenced by test_CloseHandle(), and test_NamedPipeHandleState().

◆ GetNamedPipeHandleStateW()

BOOL WINAPI GetNamedPipeHandleStateW ( HANDLE  hNamedPipe,
LPDWORD  lpState,
LPDWORD  lpCurInstances,
LPDWORD  lpMaxCollectionCount,
LPDWORD  lpCollectDataTimeout,
LPWSTR  lpUserName,
DWORD  nMaxUserNameSize 
)

Definition at line 1002 of file npipe.c.

1009{
1010 IO_STATUS_BLOCK StatusBlock;
1012
1013 if (lpState != NULL)
1014 {
1016
1017 Status = NtQueryInformationFile(hNamedPipe,
1018 &StatusBlock,
1019 &PipeInfo,
1020 sizeof(PipeInfo),
1022 if (!NT_SUCCESS(Status))
1023 {
1025 return FALSE;
1026 }
1027
1028 *lpState = ((PipeInfo.CompletionMode != FILE_PIPE_QUEUE_OPERATION) ? PIPE_NOWAIT : PIPE_WAIT);
1030 }
1031
1032 if (lpCurInstances != NULL)
1033 {
1035
1036 Status = NtQueryInformationFile(hNamedPipe,
1037 &StatusBlock,
1038 &LocalInfo,
1039 sizeof(LocalInfo),
1041 if (!NT_SUCCESS(Status))
1042 {
1044 return FALSE;
1045 }
1046
1047 *lpCurInstances = min(LocalInfo.CurrentInstances, PIPE_UNLIMITED_INSTANCES);
1048 }
1049
1050 if (lpMaxCollectionCount != NULL || lpCollectDataTimeout != NULL)
1051 {
1053
1054 Status = NtQueryInformationFile(hNamedPipe,
1055 &StatusBlock,
1056 &RemoteInfo,
1057 sizeof(RemoteInfo),
1059 if (!NT_SUCCESS(Status))
1060 {
1062 return FALSE;
1063 }
1064
1065 if (lpMaxCollectionCount != NULL)
1066 {
1067 *lpMaxCollectionCount = RemoteInfo.MaximumCollectionCount;
1068 }
1069
1070 if (lpCollectDataTimeout != NULL)
1071 {
1072 LARGE_INTEGER CollectDataTime;
1073
1074 /* Convert time and return it */
1075 RemoteInfo.CollectDataTime.QuadPart *= -1;
1076 CollectDataTime = RtlExtendedLargeIntegerDivide(RemoteInfo.CollectDataTime, 10000, NULL);
1077 /* In case of overflow, just return MAX - 1 */
1078 if (CollectDataTime.HighPart != 0)
1079 {
1080 *lpCollectDataTimeout = -2;
1081 }
1082 else
1083 {
1084 *lpCollectDataTimeout = CollectDataTime.LowPart;
1085 }
1086 }
1087 }
1088
1089 if (lpUserName != NULL)
1090 {
1091 return NpGetUserNamep(hNamedPipe, lpUserName, nMaxUserNameSize);
1092 }
1093
1094 return TRUE;
1095}
@ FilePipeLocalInformation
Definition: from_kernel.h:85
@ FilePipeRemoteInformation
Definition: from_kernel.h:86
@ FilePipeInformation
Definition: from_kernel.h:84
NTSYSAPI LONGLONG WINAPI RtlExtendedLargeIntegerDivide(LONGLONG, INT, INT *)
#define min(a, b)
Definition: monoChain.cc:55
static BOOL NpGetUserNamep(HANDLE hNamedPipe, LPWSTR lpUserName, DWORD nMaxUserNameSize)
Definition: npipe.c:28
NTSYSAPI NTSTATUS NTAPI NtQueryInformationFile(IN HANDLE hFile, OUT PIO_STATUS_BLOCK pIoStatusBlock, OUT PVOID FileInformationBuffer, IN ULONG FileInformationBufferLength, IN FILE_INFORMATION_CLASS FileInfoClass)
LARGE_INTEGER CollectDataTime
Definition: iotypes.h:5916
ULONG LowPart
Definition: typedefs.h:106
_In_ WDFUSBINTERFACE _In_ UCHAR _Out_opt_ PWDF_USB_PIPE_INFORMATION PipeInfo
Definition: wdfusb.h:2543
#define PIPE_READMODE_BYTE
Definition: winbase.h:169

Referenced by GetNamedPipeHandleStateA().

◆ GetNamedPipeInfo()

BOOL WINAPI GetNamedPipeInfo ( HANDLE  hNamedPipe,
LPDWORD  lpFlags,
LPDWORD  lpOutBufferSize,
LPDWORD  lpInBufferSize,
LPDWORD  lpMaxInstances 
)

Definition at line 1164 of file npipe.c.

1169{
1170 FILE_PIPE_LOCAL_INFORMATION PipeLocalInformation;
1171 IO_STATUS_BLOCK StatusBlock;
1173
1174 Status = NtQueryInformationFile(hNamedPipe,
1175 &StatusBlock,
1176 &PipeLocalInformation,
1177 sizeof(PipeLocalInformation),
1179 if (!NT_SUCCESS(Status))
1180 {
1182 return FALSE;
1183 }
1184
1185 if (lpFlags != NULL)
1186 {
1187 *lpFlags = (PipeLocalInformation.NamedPipeEnd == FILE_PIPE_SERVER_END) ? PIPE_SERVER_END : PIPE_CLIENT_END;
1188 *lpFlags |= (PipeLocalInformation.NamedPipeType == FILE_PIPE_MESSAGE_TYPE) ? PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1189 }
1190
1191 if (lpOutBufferSize != NULL)
1192 *lpOutBufferSize = PipeLocalInformation.OutboundQuota;
1193
1194 if (lpInBufferSize != NULL)
1195 *lpInBufferSize = PipeLocalInformation.InboundQuota;
1196
1197 if (lpMaxInstances != NULL)
1198 {
1199 if (PipeLocalInformation.MaximumInstances >= 255)
1200 *lpMaxInstances = PIPE_UNLIMITED_INSTANCES;
1201 else
1202 *lpMaxInstances = PipeLocalInformation.MaximumInstances;
1203 }
1204
1205 return TRUE;
1206}
#define FILE_PIPE_SERVER_END
Definition: iotypes.h:85
#define PIPE_CLIENT_END
Definition: winbase.h:173
#define PIPE_SERVER_END
Definition: winbase.h:174
#define PIPE_TYPE_BYTE
Definition: winbase.h:167

Referenced by _test_pipe_info().

◆ NpGetUserNamep()

static BOOL NpGetUserNamep ( HANDLE  hNamedPipe,
LPWSTR  lpUserName,
DWORD  nMaxUserNameSize 
)
static

Definition at line 28 of file npipe.c.

31{
32 BOOL Ret;
33 HANDLE hToken;
34 HMODULE hAdvapi;
36 BOOL (WINAPI *pRevertToSelf)(void);
37 BOOL (WINAPI *pGetUserNameW)(LPWSTR lpBuffer, LPDWORD lpnSize);
38 BOOL (WINAPI *pImpersonateNamedPipeClient)(HANDLE hNamedPipe);
39
40 /* Open advapi, we'll funcs from it */
41 hAdvapi = LoadLibraryW(L"advapi32.dll");
42 if (hAdvapi == NULL)
43 {
44 return FALSE;
45 }
46
47 /* Import the three required functions */
48 pRevertToSelf = (BOOL (WINAPI *)(void))GetProcAddress(hAdvapi, "RevertToSelf");
49 pGetUserNameW = (BOOL (WINAPI *)(LPWSTR, LPDWORD))GetProcAddress(hAdvapi, "GetUserNameW");
50 pImpersonateNamedPipeClient = (BOOL (WINAPI *)(HANDLE))GetProcAddress(hAdvapi, "ImpersonateNamedPipeClient");
51 /* If any couldn't be found, fail */
52 if (pRevertToSelf == NULL || pGetUserNameW == NULL || pImpersonateNamedPipeClient == NULL)
53 {
54 FreeLibrary(hAdvapi);
55 return FALSE;
56 }
57
58 /* Now, open the thread token for impersonation */
60 /* Try to impersonate the pipe client */
61 if (pImpersonateNamedPipeClient(hNamedPipe))
62 {
64
65 /* It worked, get the user name */
66 lpnSize = nMaxUserNameSize;
67 Ret = pGetUserNameW(lpUserName, &lpnSize);
68 /* Failed to get the thread token? Revert to self */
69 if (!NT_SUCCESS(Status))
70 {
71 pRevertToSelf();
72
73 FreeLibrary(hAdvapi);
74 return Ret;
75 }
76
77 /* Restore the thread token */
79 &hToken, sizeof(hToken));
80 /* We cannot fail closing the thread token! */
81 if (!CloseHandle(hToken))
82 {
84 }
85
86 /* Set last error if it failed */
87 if (!NT_SUCCESS(Status))
88 {
90 }
91 }
92 else
93 {
94 /* If opening the thread token succeed, close it */
95 if (NT_SUCCESS(Status))
96 {
97 /* We cannot fail closing it! */
98 if (!CloseHandle(hToken))
99 {
100 ASSERT(FALSE);
101 }
102 }
103
104 Ret = FALSE;
105 }
106
107 FreeLibrary(hAdvapi);
108 return Ret;
109}
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
#define GetProcAddress(x, y)
Definition: compat.h:753
@ ThreadImpersonationToken
Definition: compat.h:940
#define FreeLibrary(x)
Definition: compat.h:748
#define LoadLibraryW(x)
Definition: compat.h:747
#define ASSERT(a)
Definition: mode.c:44
#define BOOL
Definition: nt_native.h:43
#define LPDWORD
Definition: nt_native.h:46
NTSTATUS NTAPI NtSetInformationThread(IN HANDLE ThreadHandle, IN THREADINFOCLASS ThreadInformationClass, IN PVOID ThreadInformation, IN ULONG ThreadInformationLength)
Definition: query.c:2018
NTSTATUS NTAPI NtOpenThreadToken(_In_ HANDLE ThreadHandle, _In_ ACCESS_MASK DesiredAccess, _In_ BOOLEAN OpenAsSelf, _Out_ PHANDLE TokenHandle)
Opens a token that is tied to a thread handle.
Definition: token.c:2474
static LPSTR PULONG lpnSize
Definition: secur32.c:50
PVOID HANDLE
Definition: typedefs.h:73
uint32_t * LPDWORD
Definition: typedefs.h:59
#define WINAPI
Definition: msvc.h:6
#define TOKEN_IMPERSONATE
Definition: setypes.h:927
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define NtCurrentThread()

Referenced by GetNamedPipeHandleStateW().

◆ PeekNamedPipe()

BOOL WINAPI PeekNamedPipe ( HANDLE  hNamedPipe,
LPVOID  lpBuffer,
DWORD  nBufferSize,
LPDWORD  lpBytesRead,
LPDWORD  lpTotalBytesAvail,
LPDWORD  lpBytesLeftThisMessage 
)

Definition at line 1214 of file npipe.c.

1220{
1226
1227 /* Calculate the buffer space that we'll need and allocate it */
1229 Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, BufferSize);
1230 if (Buffer == NULL)
1231 {
1233 return FALSE;
1234 }
1235
1236 /* Tell the driver to seek */
1237 Status = NtFsControlFile(hNamedPipe,
1238 NULL,
1239 NULL,
1240 NULL,
1241 &Iosb,
1243 NULL,
1244 0,
1245 Buffer,
1246 BufferSize);
1247 if (Status == STATUS_PENDING)
1248 {
1249 /* Wait for npfs to be done, and update the status */
1250 Status = NtWaitForSingleObject(hNamedPipe, FALSE, NULL);
1251 if (NT_SUCCESS(Status))
1252 Status = Iosb.Status;
1253 }
1254
1255 /* Overflow is success for us */
1258
1259 /* If we failed */
1260 if (!NT_SUCCESS(Status))
1261 {
1262 /* Free the buffer and return failure */
1263 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
1265 return FALSE;
1266 }
1267
1268 /* Check if caller requested bytes available */
1269 if (lpTotalBytesAvail)
1270 {
1271 /* Return bytes available */
1272 *lpTotalBytesAvail = Buffer->ReadDataAvailable;
1273 }
1274
1275 /* Calculate the bytes returned, minus our structure overhead */
1276 BytesRead = (ULONG)(Iosb.Information -
1278 ASSERT(BytesRead <= nBufferSize);
1279
1280 /* Check if caller requested bytes read */
1281 if (lpBytesRead)
1282 {
1283 /* Return the bytes read */
1284 *lpBytesRead = BytesRead;
1285 }
1286
1287 /* Check if caller requested bytes left */
1288 if (lpBytesLeftThisMessage)
1289 {
1290 /* Calculate total minus what we returned and our structure overhead */
1291 *lpBytesLeftThisMessage = Buffer->MessageLength - BytesRead;
1292 }
1293
1294 /* Check if the caller wanted to see the actual data */
1295 if (lpBuffer)
1296 {
1297 /* Give him what he wants */
1299 Buffer->Data,
1300 BytesRead);
1301 }
1302
1303 /* Free the buffer */
1304 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
1305
1306 return TRUE;
1307}
#define BufferSize
Definition: mmc.h:75
#define FSCTL_PIPE_PEEK
Definition: winioctl.h:196
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesRead
Definition: wdfiotarget.h:870
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254

Referenced by _test_peek_pipe(), DoTestEntry(), CPipe::Peek(), ReadFromPipeThread(), serverThreadMain4(), test_CloseHandle(), test_CreateNamedPipe(), and test_DisconnectNamedPipe().

◆ SetNamedPipeHandleState()

BOOL WINAPI SetNamedPipeHandleState ( HANDLE  hNamedPipe,
LPDWORD  lpMode,
LPDWORD  lpMaxCollectionCount,
LPDWORD  lpCollectDataTimeout 
)

Definition at line 774 of file npipe.c.

778{
781
782 /* Check if the Mode is being changed */
783 if (lpMode)
784 {
786
787 /* Set the Completion Mode */
788 Settings.CompletionMode = (*lpMode & PIPE_NOWAIT) ?
790
791 /* Set the Read Mode */
792 Settings.ReadMode = (*lpMode & PIPE_READMODE_MESSAGE) ?
794
795 /* Send the changes to the Driver */
796 Status = NtSetInformationFile(hNamedPipe,
797 &Iosb,
798 &Settings,
799 sizeof(Settings),
801 if (!NT_SUCCESS(Status))
802 {
804 return FALSE;
805 }
806 }
807
808 /* Check if the Collection count or Timeout are being changed */
809 if (lpMaxCollectionCount || lpCollectDataTimeout)
810 {
811 FILE_PIPE_REMOTE_INFORMATION RemoteSettings;
812
813 /* Setting one without the other would delete it, so we read old one */
814 if (!lpMaxCollectionCount || !lpCollectDataTimeout)
815 {
816 Status = NtQueryInformationFile(hNamedPipe,
817 &Iosb,
818 &RemoteSettings,
819 sizeof(RemoteSettings),
821 if (!NT_SUCCESS(Status))
822 {
824 return FALSE;
825 }
826 }
827
828 /* Now set the new settings */
829 RemoteSettings.MaximumCollectionCount = (lpMaxCollectionCount) ?
830 *lpMaxCollectionCount :
831 RemoteSettings.MaximumCollectionCount;
832 if (lpCollectDataTimeout)
833 {
834 /* Convert it to Quad */
835 RemoteSettings.CollectDataTime.QuadPart = *lpCollectDataTimeout * -10000LL;
836 }
837
838 /* Tell the driver to change them */
839 Status = NtSetInformationFile(hNamedPipe,
840 &Iosb,
841 &RemoteSettings,
842 sizeof(RemoteSettings),
844 if (!NT_SUCCESS(Status))
845 {
847 return FALSE;
848 }
849 }
850
851 return TRUE;
852}
if(dx< 0)
Definition: linetemp.h:194
NTSYSAPI NTSTATUS NTAPI NtSetInformationFile(IN HANDLE hFile, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID FileInformationBuffer, IN ULONG FileInformationBufferLength, IN FILE_INFORMATION_CLASS FileInfoClass)
Definition: iofunc.c:3096
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_POWER_POLICY_IDLE_SETTINGS Settings
Definition: wdfdevice.h:2595

Referenced by CallNamedPipeW(), create_overlapped_pipe(), create_pipe_pair(), DhcpCApiInitialize(), main(), rpcrt4_conn_open_pipe(), ScConnectControlPipe(), test_CloseHandle(), test_CreateNamedPipe(), and test_NamedPipeHandleState().

◆ TransactNamedPipe()

BOOL WINAPI TransactNamedPipe ( IN HANDLE  hNamedPipe,
IN LPVOID  lpInBuffer,
IN DWORD  nInBufferSize,
OUT LPVOID  lpOutBuffer,
IN DWORD  nOutBufferSize,
OUT LPDWORD lpBytesRead  OPTIONAL,
IN LPOVERLAPPED lpOverlapped  OPTIONAL 
)

Definition at line 1315 of file npipe.c.

1322{
1324
1325 if (lpBytesRead != NULL)
1326 {
1327 *lpBytesRead = 0;
1328 }
1329
1330 if (lpOverlapped != NULL)
1331 {
1333
1334 ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
1335 lpOverlapped->Internal = STATUS_PENDING;
1336
1337 Status = NtFsControlFile(hNamedPipe,
1338 lpOverlapped->hEvent,
1339 NULL,
1340 ApcContext,
1343 lpInBuffer,
1344 nInBufferSize,
1345 lpOutBuffer,
1346 nOutBufferSize);
1348 {
1350 return FALSE;
1351 }
1352
1353 if (lpBytesRead != NULL)
1354 {
1355 *lpBytesRead = lpOverlapped->InternalHigh;
1356 }
1357 }
1358 else
1359 {
1361
1362 Status = NtFsControlFile(hNamedPipe,
1363 NULL,
1364 NULL,
1365 NULL,
1366 &Iosb,
1368 lpInBuffer,
1369 nInBufferSize,
1370 lpOutBuffer,
1371 nOutBufferSize);
1372 if (Status == STATUS_PENDING)
1373 {
1374 Status = NtWaitForSingleObject(hNamedPipe,
1375 FALSE,
1376 NULL);
1377 if (NT_SUCCESS(Status))
1378 Status = Iosb.Status;
1379 }
1380
1381 if (NT_SUCCESS(Status))
1382 {
1383 /* lpNumberOfBytesRead must not be NULL here, in fact Win doesn't
1384 check that case either and crashes (only after the operation
1385 completed) */
1386 *lpBytesRead = Iosb.Information;
1387 }
1388 else
1389 {
1391 return FALSE;
1392 }
1393 }
1394
1395 return TRUE;
1396}
#define FSCTL_PIPE_TRANSCEIVE
Definition: winioctl.h:198

Referenced by CallNamedPipeW(), DhcpLeaseIpAddress(), DhcpQueryHWInfo(), DhcpReleaseIpAddressLease(), DhcpRenewIpAddressLease(), DhcpRosGetAdapterInfo(), and DhcpStaticRefreshParams().

◆ WaitNamedPipeA()

BOOL WINAPI WaitNamedPipeA ( LPCSTR  lpNamedPipeName,
DWORD  nTimeOut 
)

Definition at line 433 of file npipe.c.

435{
436 BOOL r = FALSE;
437 UNICODE_STRING NameU;
438
439 /* Convert the name to Unicode */
440 if (Basep8BitStringToDynamicUnicodeString(&NameU, lpNamedPipeName))
441 {
442 /* Call the Unicode API */
443 r = WaitNamedPipeW(NameU.Buffer, nTimeOut);
444
445 /* Free the Unicode string */
446 RtlFreeUnicodeString(&NameU);
447 }
448
449 /* Return result */
450 return r;
451}
BOOLEAN WINAPI Basep8BitStringToDynamicUnicodeString(OUT PUNICODE_STRING UnicodeString, IN LPCSTR String)
Definition: utils.c:225
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)

Referenced by is_pipe_listening(), rpcrt4_conn_open_pipe(), service_process(), and test_CreateNamedPipe().

◆ WaitNamedPipeW()

BOOL WINAPI WaitNamedPipeW ( LPCWSTR  lpNamedPipeName,
DWORD  nTimeOut 
)

Definition at line 458 of file npipe.c.

460{
461 UNICODE_STRING NamedPipeName, NewName, DevicePath, PipePrefix;
462 ULONG NameLength;
463 ULONG i;
464 PWCHAR p;
465 ULONG Type;
470 ULONG WaitPipeInfoSize;
471 PVOID DevicePathBuffer;
472 PFILE_PIPE_WAIT_FOR_BUFFER WaitPipeInfo;
473
474 /* Start by making a unicode string of the name */
475 TRACE("Sent path: %S\n", lpNamedPipeName);
476 if (!RtlCreateUnicodeString(&NamedPipeName, lpNamedPipeName))
477 {
479 return FALSE;
480 }
481 NameLength = NamedPipeName.Length / sizeof(WCHAR);
482
483 /* All slashes must become backslashes */
484 for (i = 0; i < NameLength; i++)
485 {
486 /* Check and convert */
487 if (NamedPipeName.Buffer[i] == L'/') NamedPipeName.Buffer[i] = L'\\';
488 }
489
490 DevicePathBuffer = NULL;
491
492 /* Find the path type of the name we were given */
493 NewName = NamedPipeName;
494 Type = RtlDetermineDosPathNameType_U(lpNamedPipeName);
495
496 /* Check if this was a device path, ie : "\\.\pipe\name" */
498 {
499 /* Make sure it's a valid prefix */
500 RtlInitUnicodeString(&PipePrefix, L"\\\\.\\pipe\\");
501 if (!RtlPrefixUnicodeString(&PipePrefix, &NewName, TRUE))
502 {
503 /* The name is invalid */
504 WARN("Invalid name!\n");
505 RtlFreeUnicodeString(&NamedPipeName);
507 return FALSE;
508 }
509
510 /* Move past it */
511 NewName.Buffer += PipePrefix.Length / sizeof(WCHAR);
512 NewName.Length -= PipePrefix.Length;
513 NewName.MaximumLength -= PipePrefix.Length;
514
515 /* Initialize the Dos Devices name */
516 TRACE("NewName: %wZ\n", &NewName);
517 RtlInitUnicodeString(&DevicePath, L"\\DosDevices\\pipe\\");
518 }
519 else if (Type == RtlPathTypeUncAbsolute)
520 {
521 PWSTR PipeName;
522
523 /* The path is \\server\\pipe\name; find the pipename itself */
524 p = &NewName.Buffer[2];
525
526 /* First loop to get past the server name */
527 do
528 {
529 /* Check if this is a backslash */
530 if (*p == L'\\') break;
531
532 /* Check next */
533 p++;
534 } while (*p);
535
536 /* Now make sure the full name contains "pipe\" */
537 if ((*p) && !(_wcsnicmp(p + 1, L"pipe\\", sizeof("pipe\\") - sizeof(ANSI_NULL))))
538 {
539 /* Get to the pipe name itself now */
540 p += sizeof("pipe\\") - sizeof(ANSI_NULL);
541 }
542 else
543 {
544 /* The name is invalid */
545 WARN("Invalid name!\n");
546 RtlFreeUnicodeString(&NamedPipeName);
548 return FALSE;
549 }
550
551 /* Skip first backslash */
552 NewName.Buffer++;
553 /* And skip pipe for copying name */
554 PipeName = p + ((sizeof(L"pipe\\") - sizeof(UNICODE_NULL)) / sizeof(WCHAR));
555 /* Update the string */
558
559 /* DevicePath will contain the pipename + the DosDevice prefix */
560 DevicePath.MaximumLength = (USHORT)((ULONG_PTR)PipeName - (ULONG_PTR)NewName.Buffer) + sizeof(L"\\DosDevices\\UNC\\");
561
562 /* Allocate the buffer for DevicePath */
563 DevicePathBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, DevicePath.MaximumLength);
564 if (DevicePathBuffer == NULL)
565 {
566 RtlFreeUnicodeString(&NamedPipeName);
568 return FALSE;
569 }
570
571 /* Copy the prefix first */
572 DevicePath.Buffer = DevicePathBuffer;
573 RtlCopyMemory(DevicePathBuffer, L"\\DosDevices\\UNC\\", sizeof(L"\\DosDevices\\UNC\\") - sizeof(UNICODE_NULL));
574 DevicePath.Length = sizeof(L"\\DosDevices\\UNC\\") - sizeof(UNICODE_NULL);
575 /* And append the rest */
577 /* And fix pipe name without its prefix */
578 RtlInitUnicodeString(&NewName, PipeName + 1);
579 }
580 else
581 {
582 WARN("Invalid path type\n");
583 RtlFreeUnicodeString(&NamedPipeName);
585 return FALSE;
586 }
587
588 /* Now calculate the total length of the structure and allocate it */
589 WaitPipeInfoSize = FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[0]) +
591 WaitPipeInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, WaitPipeInfoSize);
592 if (WaitPipeInfo == NULL)
593 {
594 if (DevicePathBuffer != NULL)
595 {
596 RtlFreeHeap(RtlGetProcessHeap(), 0, DevicePathBuffer);
597 }
598
599 RtlFreeUnicodeString(&NamedPipeName);
601 return FALSE;
602 }
603
604 /* Initialize the object attributes */
605 TRACE("Opening: %wZ\n", &DevicePath);
607 &DevicePath,
609 NULL,
610 NULL);
611
612 /* Open the path */
619
620 if (DevicePathBuffer != NULL)
621 {
622 RtlFreeHeap(RtlGetProcessHeap(), 0, DevicePathBuffer);
623 }
624
625 if (!NT_SUCCESS(Status))
626 {
627 /* Fail; couldn't open */
628 WARN("Status: %lx\n", Status);
629 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo);
630 RtlFreeUnicodeString(&NamedPipeName);
632 return FALSE;
633 }
634
635 /* Check what timeout we got */
636 if (nTimeOut == NMPWAIT_USE_DEFAULT_WAIT)
637 {
638 /* Don't use a timeout */
639 WaitPipeInfo->TimeoutSpecified = FALSE;
640 }
641 else
642 {
643 /* Check if we should wait forever */
644 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
645 {
646 /* Set the max */
647 WaitPipeInfo->Timeout.LowPart = 0;
648 WaitPipeInfo->Timeout.HighPart = 0x80000000;
649 }
650 else
651 {
652 /* Convert to NT format */
653 WaitPipeInfo->Timeout.QuadPart = nTimeOut * -10000LL;
654 }
655
656 /* In both cases, we do have a timeout */
657 WaitPipeInfo->TimeoutSpecified = TRUE;
658 }
659
660 /* Set the length and copy the name */
661 WaitPipeInfo->NameLength = NewName.Length;
663
664 /* Get rid of the full name */
665 RtlFreeUnicodeString(&NamedPipeName);
666
667 /* Let NPFS know of our request */
669 NULL,
670 NULL,
671 NULL,
674 WaitPipeInfo,
675 WaitPipeInfoSize,
676 NULL,
677 0);
678
679 /* Free our pipe info data and close the handle */
680 RtlFreeHeap(RtlGetProcessHeap(), 0, WaitPipeInfo);
682
683 /* Check the status */
684 if (!NT_SUCCESS(Status))
685 {
686 /* Failure to wait on the pipe */
687 WARN("Status: %lx\n", Status);
689 return FALSE;
690 }
691
692 /* Success */
693 return TRUE;
694}
Type
Definition: Type.h:7
NTSYSAPI BOOLEAN NTAPI RtlCreateUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
NTSYSAPI RTL_PATH_TYPE NTAPI RtlDetermineDosPathNameType_U(_In_ PCWSTR Path)
@ RtlPathTypeUncAbsolute
Definition: rtltypes.h:472
@ RtlPathTypeLocalDevice
Definition: rtltypes.h:477
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
#define FILE_READ_ATTRIBUTES
Definition: nt_native.h:647
NTSYSAPI BOOLEAN NTAPI RtlPrefixUnicodeString(IN PUNICODE_STRING String1, IN PUNICODE_STRING String2, IN BOOLEAN CaseInSensitive)
#define UNICODE_NULL
#define ANSI_NULL
#define STATUS_OBJECT_PATH_SYNTAX_BAD
Definition: ntstatus.h:295
#define FSCTL_PIPE_WAIT
Definition: winioctl.h:199
_Check_return_ _CRTIMP int __cdecl _wcsnicmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
LARGE_INTEGER Timeout
Definition: winioctl.h:457
uint16_t * PWSTR
Definition: typedefs.h:56
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint16_t * PWCHAR
Definition: typedefs.h:56
#define NMPWAIT_WAIT_FOREVER
Definition: winbase.h:133
#define NMPWAIT_USE_DEFAULT_WAIT
Definition: winbase.h:134
_In_ PUNICODE_STRING NewName
Definition: zwfuncs.h:1203

Referenced by CallNamedPipeW(), DhcpCApiInitialize(), RPC_GetLocalClassObject(), ScConnectControlPipe(), service_process(), and WaitNamedPipeA().

Variable Documentation

◆ ProcessPipeId

LONG ProcessPipeId

Definition at line 19 of file npipe.c.

Referenced by CreatePipe().