fa0e7e08db1efd20ba37467630dc42ea640e993d
[reactos.git] / reactos / dll / win32 / kernel32 / file / pipe.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/create.c
6 * PURPOSE: Directory functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <k32.h>
14 #define NDEBUG
15 #include <debug.h>
16
17 /* GLOBALS ******************************************************************/
18
19 LONG ProcessPipeId = 0;
20
21 /* FUNCTIONS ****************************************************************/
22
23 /*
24 * @implemented
25 */
26 BOOL
27 WINAPI
28 CreatePipe(PHANDLE hReadPipe,
29 PHANDLE hWritePipe,
30 LPSECURITY_ATTRIBUTES lpPipeAttributes,
31 DWORD nSize)
32 {
33 WCHAR Buffer[64];
34 UNICODE_STRING PipeName;
35 OBJECT_ATTRIBUTES ObjectAttributes;
36 IO_STATUS_BLOCK StatusBlock;
37 LARGE_INTEGER DefaultTimeout;
38 NTSTATUS Status;
39 HANDLE ReadPipeHandle;
40 HANDLE WritePipeHandle;
41 LONG PipeId;
42 ULONG Attributes;
43 PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
44
45 /* Set the timeout to 120 seconds */
46 DefaultTimeout.QuadPart = -1200000000;
47
48 /* Use default buffer size if desired */
49 if (!nSize) nSize = 0x1000;
50
51 /* Increase the Pipe ID */
52 PipeId = InterlockedIncrement(&ProcessPipeId);
53
54 /* Create the pipe name */
55 swprintf(Buffer,
56 L"\\Device\\NamedPipe\\Win32Pipes.%08x.%08x",
57 NtCurrentTeb()->ClientId.UniqueProcess,
58 PipeId);
59 RtlInitUnicodeString(&PipeName, Buffer);
60
61 /* Always use case insensitive */
62 Attributes = OBJ_CASE_INSENSITIVE;
63
64 /* Check if we got attributes */
65 if (lpPipeAttributes)
66 {
67 /* Use the attributes' SD instead */
68 SecurityDescriptor = lpPipeAttributes->lpSecurityDescriptor;
69
70 /* Set OBJ_INHERIT if requested */
71 if (lpPipeAttributes->bInheritHandle) Attributes |= OBJ_INHERIT;
72 }
73
74 /* Initialize the attributes */
75 InitializeObjectAttributes(&ObjectAttributes,
76 &PipeName,
77 Attributes,
78 NULL,
79 SecurityDescriptor);
80
81 /* Create the named pipe */
82 Status = NtCreateNamedPipeFile(&ReadPipeHandle,
83 GENERIC_READ |FILE_WRITE_ATTRIBUTES | SYNCHRONIZE,
84 &ObjectAttributes,
85 &StatusBlock,
86 FILE_SHARE_READ | FILE_SHARE_WRITE,
87 FILE_CREATE,
88 FILE_SYNCHRONOUS_IO_NONALERT,
89 FILE_PIPE_BYTE_STREAM_TYPE,
90 FILE_PIPE_BYTE_STREAM_MODE,
91 FILE_PIPE_QUEUE_OPERATION,
92 1,
93 nSize,
94 nSize,
95 &DefaultTimeout);
96 if (!NT_SUCCESS(Status))
97 {
98 /* Convert error and fail */
99 WARN("Status: %lx\n", Status);
100 SetLastErrorByStatus(Status);
101 return FALSE;
102 }
103
104 /* Now try opening it for write access */
105 Status = NtOpenFile(&WritePipeHandle,
106 FILE_GENERIC_WRITE | SYNCHRONIZE,
107 &ObjectAttributes,
108 &StatusBlock,
109 FILE_SHARE_READ,
110 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
111 if (!NT_SUCCESS(Status))
112 {
113 /* Convert error and fail */
114 WARN("Status: %lx\n", Status);
115 NtClose(ReadPipeHandle);
116 SetLastErrorByStatus(Status);
117 return FALSE;
118 }
119
120 /* Return both handles */
121 *hReadPipe = ReadPipeHandle;
122 *hWritePipe = WritePipeHandle;
123 return TRUE;
124 }
125
126 /* EOF */