abeba30de1870ada91e717b364e9cdbe947428b3
[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)
64 nSize = 0x1000;
65
66 InitializeObjectAttributes(&ObjectAttributes,
67 &PipeName,
68 Attributes,
69 NULL,
70 SecurityDescriptor);
71
72 Status = NtCreateNamedPipeFile(&ReadPipeHandle,
73 FILE_GENERIC_READ |FILE_WRITE_ATTRIBUTES | SYNCHRONIZE,
74 &ObjectAttributes,
75 &StatusBlock,
76 FILE_SHARE_READ | FILE_SHARE_WRITE,
77 FILE_CREATE,
78 FILE_SYNCHRONOUS_IO_NONALERT,
79 FALSE,
80 FALSE,
81 FALSE,
82 1,
83 nSize,
84 nSize,
85 &DefaultTimeout);
86 if (!NT_SUCCESS(Status))
87 {
88 SetLastErrorByStatus(Status);
89 return FALSE;
90 }
91
92 Status = NtOpenFile(&WritePipeHandle,
93 FILE_GENERIC_WRITE | SYNCHRONIZE,
94 &ObjectAttributes,
95 &StatusBlock,
96 FILE_SHARE_READ | FILE_SHARE_WRITE,
97 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
98 if (!NT_SUCCESS(Status))
99 {
100 NtClose(ReadPipeHandle);
101 SetLastErrorByStatus(Status);
102 return FALSE;
103 }
104
105 *hReadPipe = ReadPipeHandle;
106 *hWritePipe = WritePipeHandle;
107
108 return TRUE;
109 }
110
111 /* EOF */