fac8693ad59032290aa9dc1a5bbd8b3b39db9585
[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 LONG ProcessPipeId = 0;
21
22 /* FUNCTIONS ****************************************************************/
23
24 /*
25 * @implemented
26 */
27 BOOL
28 STDCALL
29 CreatePipe(PHANDLE hReadPipe,
30 PHANDLE hWritePipe,
31 LPSECURITY_ATTRIBUTES lpPipeAttributes,
32 DWORD nSize)
33 {
34 WCHAR Buffer[64];
35 UNICODE_STRING PipeName;
36 OBJECT_ATTRIBUTES ObjectAttributes;
37 IO_STATUS_BLOCK StatusBlock;
38 LARGE_INTEGER DefaultTimeout;
39 NTSTATUS Status;
40 HANDLE ReadPipeHandle;
41 HANDLE WritePipeHandle;
42 LONG PipeId;
43 ULONG Attributes;
44 PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
45
46 /* Set the timeout to 120 seconds */
47 DefaultTimeout.QuadPart = -1200000000;
48
49 /* Use default buffer size if desired */
50 if (!nSize) nSize = 0x1000;
51
52 /* Increase the Pipe ID */
53 PipeId = InterlockedIncrement(&ProcessPipeId);
54
55 /* Create the pipe name */
56 swprintf(Buffer,
57 L"\\Device\\NamedPipe\\Win32Pipes.%08x.%08x",
58 NtCurrentTeb()->Cid.UniqueProcess,
59 PipeId);
60 RtlInitUnicodeString(&PipeName, Buffer);
61
62 /* Always use case insensitive */
63 Attributes = OBJ_CASE_INSENSITIVE;
64
65 /* Check if we got attributes */
66 if (lpPipeAttributes)
67 {
68 /* Use the attributes' SD instead */
69 SecurityDescriptor = lpPipeAttributes->lpSecurityDescriptor;
70
71 /* Set OBJ_INHERIT if requested */
72 if (lpPipeAttributes->bInheritHandle) Attributes |= OBJ_INHERIT;
73 }
74
75 /* Initialize the attributes */
76 InitializeObjectAttributes(&ObjectAttributes,
77 &PipeName,
78 Attributes,
79 NULL,
80 SecurityDescriptor);
81
82 /* Create the named pipe */
83 Status = NtCreateNamedPipeFile(&ReadPipeHandle,
84 GENERIC_READ |FILE_WRITE_ATTRIBUTES | SYNCHRONIZE,
85 &ObjectAttributes,
86 &StatusBlock,
87 FILE_SHARE_READ | FILE_SHARE_WRITE,
88 FILE_CREATE,
89 FILE_SYNCHRONOUS_IO_NONALERT,
90 FILE_PIPE_BYTE_STREAM_TYPE,
91 FILE_PIPE_BYTE_STREAM_MODE,
92 FILE_PIPE_QUEUE_OPERATION,
93 1,
94 nSize,
95 nSize,
96 &DefaultTimeout);
97 if (!NT_SUCCESS(Status))
98 {
99 /* Convert error and fail */
100 DPRINT1("Status: %lx\n", Status);
101 SetLastErrorByStatus(Status);
102 return FALSE;
103 }
104
105 /* Now try opening it for write access */
106 Status = NtOpenFile(&WritePipeHandle,
107 FILE_GENERIC_WRITE | SYNCHRONIZE,
108 &ObjectAttributes,
109 &StatusBlock,
110 FILE_SHARE_READ,
111 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
112 if (!NT_SUCCESS(Status))
113 {
114 /* Convert error and fail */
115 DPRINT1("Status: %lx\n", Status);
116 NtClose(ReadPipeHandle);
117 SetLastErrorByStatus(Status);
118 return FALSE;
119 }
120
121 /* Return both handles */
122 *hReadPipe = ReadPipeHandle;
123 *hWritePipe = WritePipeHandle;
124 return TRUE;
125 }
126
127 /* EOF */