Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. pipe.c
  5. Abstract:
  6. This module contains the Win32 Anonymous Pipe API
  7. Author:
  8. Steve Wood (stevewo) 24-Sep-1990
  9. Revision History:
  10. --*/
  11. #include "basedll.h"
  12. ULONG PipeSerialNumber;
  13. BOOL
  14. APIENTRY
  15. CreatePipe(
  16. OUT LPHANDLE lpReadPipe,
  17. OUT LPHANDLE lpWritePipe,
  18. IN LPSECURITY_ATTRIBUTES lpPipeAttributes,
  19. IN DWORD nSize
  20. )
  21. /*++
  22. Routine Description:
  23. The CreatePipe API is used to create an anonymous pipe I/O device.
  24. Two handles to the device are created. One handle is opened for
  25. reading and the other is opened for writing. These handles may be
  26. used in subsequent calls to ReadFile and WriteFile to transmit data
  27. through the pipe.
  28. Arguments:
  29. lpReadPipe - Returns a handle to the read side of the pipe. Data
  30. may be read from the pipe by specifying this handle value in a
  31. subsequent call to ReadFile.
  32. lpWritePipe - Returns a handle to the write side of the pipe. Data
  33. may be written to the pipe by specifying this handle value in a
  34. subsequent call to WriteFile.
  35. lpPipeAttributes - An optional parameter that may be used to specify
  36. the attributes of the new pipe. If the parameter is not
  37. specified, then the pipe is created without a security
  38. descriptor, and the resulting handles are not inherited on
  39. process creation. Otherwise, the optional security attributes
  40. are used on the pipe, and the inherit handles flag effects both
  41. pipe handles.
  42. nSize - Supplies the requested buffer size for the pipe. This is
  43. only a suggestion and is used by the operating system to
  44. calculate an appropriate buffering mechanism. A value of zero
  45. indicates that the system is to choose the default buffering
  46. scheme.
  47. Return Value:
  48. TRUE - The operation was successful.
  49. FALSE/NULL - The operation failed. Extended error status is available
  50. using GetLastError.
  51. --*/
  52. {
  53. UCHAR PipeNameBuffer[ MAX_PATH ];
  54. ANSI_STRING PipeName;
  55. OBJECT_ATTRIBUTES ObjectAttributes;
  56. HANDLE ReadPipeHandle, WritePipeHandle;
  57. NTSTATUS Status;
  58. IO_STATUS_BLOCK Iosb;
  59. LARGE_INTEGER Timeout;
  60. ULONG Attributes;
  61. PVOID SecurityDescriptor;
  62. PUNICODE_STRING Unicode;
  63. //
  64. // Set the default timeout to 120 seconds
  65. //
  66. Timeout.QuadPart = - 10 * 1000 * 1000 * 120;
  67. if (nSize == 0) {
  68. nSize = 4096;
  69. }
  70. sprintf( PipeNameBuffer,
  71. WIN32_SS_PIPE_FORMAT_STRING,
  72. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess),
  73. InterlockedIncrement(&PipeSerialNumber)
  74. );
  75. RtlInitAnsiString( &PipeName, PipeNameBuffer );
  76. Unicode = &NtCurrentTeb()->StaticUnicodeString;
  77. Status = RtlAnsiStringToUnicodeString(Unicode,&PipeName,FALSE);
  78. if ( !NT_SUCCESS(Status) ) {
  79. BaseSetLastNTError(Status);
  80. return FALSE;
  81. }
  82. if ( ARGUMENT_PRESENT(lpPipeAttributes) ) {
  83. Attributes =
  84. lpPipeAttributes->bInheritHandle ? (OBJ_INHERIT | OBJ_CASE_INSENSITIVE) : (OBJ_CASE_INSENSITIVE);
  85. SecurityDescriptor = lpPipeAttributes->lpSecurityDescriptor;
  86. }
  87. else {
  88. Attributes = OBJ_CASE_INSENSITIVE;
  89. SecurityDescriptor = NULL;
  90. }
  91. InitializeObjectAttributes( &ObjectAttributes,
  92. Unicode,
  93. Attributes,
  94. NULL,
  95. SecurityDescriptor
  96. );
  97. Status = NtCreateNamedPipeFile( &ReadPipeHandle,
  98. GENERIC_READ | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE,
  99. &ObjectAttributes,
  100. &Iosb,
  101. FILE_SHARE_READ | FILE_SHARE_WRITE, // Gary ?
  102. FILE_CREATE,
  103. FILE_SYNCHRONOUS_IO_NONALERT,
  104. FILE_PIPE_BYTE_STREAM_TYPE,
  105. FILE_PIPE_BYTE_STREAM_MODE,
  106. FILE_PIPE_QUEUE_OPERATION,
  107. 1,
  108. nSize,
  109. nSize,
  110. (PLARGE_INTEGER) &Timeout
  111. );
  112. if (!NT_SUCCESS( Status )) {
  113. BaseSetLastNTError( Status );
  114. return( FALSE );
  115. }
  116. Status = NtOpenFile( &WritePipeHandle,
  117. GENERIC_WRITE | SYNCHRONIZE,
  118. &ObjectAttributes,
  119. &Iosb,
  120. FILE_SHARE_READ | FILE_SHARE_WRITE,
  121. FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE
  122. );
  123. if (!NT_SUCCESS( Status )) {
  124. NtClose( ReadPipeHandle );
  125. BaseSetLastNTError( Status );
  126. return( FALSE );
  127. }
  128. *lpReadPipe = ReadPipeHandle;
  129. *lpWritePipe = WritePipeHandle;
  130. return( TRUE );
  131. }