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