efabbd288085968e91fe92a610ca763213943f67
[reactos.git] / reactos / lib / 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
15 #define NDEBUG
16 #include "../include/debug.h"
17
18 /* GLOBALS ******************************************************************/
19
20 ULONG ProcessPipeId = 0;
21
22 /* FUNCTIONS ****************************************************************/
23
24 /*
25 * @implemented
26 */
27 BOOL STDCALL CreatePipe(PHANDLE hReadPipe,
28 PHANDLE hWritePipe,
29 LPSECURITY_ATTRIBUTES lpPipeAttributes,
30 DWORD nSize)
31 {
32 WCHAR Buffer[64];
33 UNICODE_STRING PipeName;
34 OBJECT_ATTRIBUTES ObjectAttributes;
35 IO_STATUS_BLOCK StatusBlock;
36 LARGE_INTEGER DefaultTimeout;
37 NTSTATUS Status;
38 HANDLE ReadPipeHandle;
39 HANDLE WritePipeHandle;
40 ULONG PipeId;
41 ULONG Attributes;
42 PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
43
44 DefaultTimeout.QuadPart = 300000000; /* 30 seconds */
45
46 PipeId = (ULONG)InterlockedIncrement((LONG*)&ProcessPipeId);
47 swprintf(Buffer,
48 L"\\Device\\NamedPipe\\Win32Pipes.%08x.%08x",
49 NtCurrentTeb()->Cid.UniqueProcess,
50 PipeId);
51 RtlInitUnicodeString (&PipeName,
52 Buffer);
53
54 Attributes = OBJ_CASE_INSENSITIVE;
55 if (lpPipeAttributes)
56 {
57 SecurityDescriptor = lpPipeAttributes->lpSecurityDescriptor;
58 if (lpPipeAttributes->bInheritHandle)
59 Attributes |= OBJ_INHERIT;
60 }
61
62 /* use default buffer size if desired */
63 if (nSize == 0) nSize = 0x1000;
64
65 InitializeObjectAttributes(&ObjectAttributes,
66 &PipeName,
67 Attributes,
68 NULL,
69 SecurityDescriptor);
70
71 Status = NtCreateNamedPipeFile(&ReadPipeHandle,
72 FILE_GENERIC_READ | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE,
73 &ObjectAttributes,
74 &StatusBlock,
75 FILE_SHARE_READ | FILE_SHARE_WRITE,
76 FILE_CREATE,
77 FILE_SYNCHRONOUS_IO_NONALERT,
78 FALSE,
79 FALSE,
80 FALSE,
81 1,
82 nSize,
83 nSize,
84 &DefaultTimeout);
85 if (!NT_SUCCESS(Status))
86 {
87 SetLastErrorByStatus(Status);
88 return FALSE;
89 }
90
91 Status = NtOpenFile(&WritePipeHandle,
92 FILE_GENERIC_WRITE | SYNCHRONIZE,
93 &ObjectAttributes,
94 &StatusBlock,
95 FILE_SHARE_READ | FILE_SHARE_WRITE,
96 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
97 if (!NT_SUCCESS(Status))
98 {
99 NtClose(ReadPipeHandle);
100 SetLastErrorByStatus(Status);
101 return FALSE;
102 }
103
104 *hReadPipe = ReadPipeHandle;
105 *hWritePipe = WritePipeHandle;
106
107 return TRUE;
108 }
109
110 /* EOF */