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