fixed calls to NtDuplicateObject
[reactos.git] / reactos / subsys / win32k / ntuser / csr.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * PURPOSE: Interface to csrss
6 * FILE: subsys/win32k/ntuser/csr.c
7 * PROGRAMER: Ge van Geldorp (ge@gse.nl)
8 */
9
10 #include <w32k.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 static HANDLE WindowsApiPort = NULL;
16 PEPROCESS CsrProcess = NULL;
17
18 NTSTATUS FASTCALL
19 CsrInit(void)
20 {
21 NTSTATUS Status;
22 UNICODE_STRING PortName;
23 ULONG ConnectInfoLength;
24
25 RtlInitUnicodeString(&PortName, L"\\Windows\\ApiPort");
26 ConnectInfoLength = 0;
27 Status = ZwConnectPort(&WindowsApiPort,
28 &PortName,
29 NULL,
30 NULL,
31 NULL,
32 NULL,
33 NULL,
34 &ConnectInfoLength);
35 if (! NT_SUCCESS(Status))
36 {
37 return Status;
38 }
39
40 CsrProcess = PsGetCurrentProcess();
41
42 return STATUS_SUCCESS;
43 }
44
45
46 NTSTATUS FASTCALL
47 co_CsrNotify(PCSR_API_MESSAGE Request)
48 {
49 NTSTATUS Status;
50 PEPROCESS OldProcess;
51
52 if (NULL == CsrProcess)
53 {
54 return STATUS_INVALID_PORT_HANDLE;
55 }
56
57 Request->Header.u1.s1.DataLength = sizeof(CSR_API_MESSAGE) - sizeof(PORT_MESSAGE);
58 Request->Header.u1.s1.TotalLength = sizeof(CSR_API_MESSAGE);
59
60 /* Switch to the process in which the WindowsApiPort handle is valid */
61 OldProcess = PsGetCurrentProcess();
62 if (CsrProcess != OldProcess)
63 {
64 KeAttachProcess(&CsrProcess->Pcb);
65 }
66
67 UserLeaveCo();
68
69 Status = ZwRequestWaitReplyPort(WindowsApiPort,
70 &Request->Header,
71 &Request->Header);
72
73 UserEnterCo();
74
75 if (CsrProcess != OldProcess)
76 {
77 KeDetachProcess();
78 }
79
80 if (NT_SUCCESS(Status))
81 {
82 Status = Request->Status;
83 }
84
85 return Status;
86 }
87
88 NTSTATUS
89 STDCALL
90 CsrInsertObject(HANDLE ObjectHandle,
91 ACCESS_MASK DesiredAccess,
92 PHANDLE Handle)
93 {
94 NTSTATUS Status;
95 HANDLE CsrProcessHandle;
96 OBJECT_ATTRIBUTES ObjectAttributes;
97 CLIENT_ID Cid;
98
99 /* Put CSR'S CID */
100 Cid.UniqueProcess = CsrProcess->UniqueProcessId;
101 Cid.UniqueThread = 0;
102
103 /* Empty Attributes */
104 InitializeObjectAttributes(&ObjectAttributes,
105 NULL,
106 0,
107 NULL,
108 NULL);
109
110 /* Get a Handle to Csrss */
111 Status = ZwOpenProcess(&CsrProcessHandle,
112 PROCESS_DUP_HANDLE,
113 &ObjectAttributes,
114 &Cid);
115
116 if ((NT_SUCCESS(Status)))
117 {
118 /* Duplicate the Handle */
119 Status = ZwDuplicateObject(NtCurrentProcess(),
120 ObjectHandle,
121 CsrProcessHandle,
122 Handle,
123 DesiredAccess,
124 OBJ_INHERIT,
125 0);
126
127 /* Close our handle to CSRSS */
128 ZwClose(CsrProcessHandle);
129 }
130
131 return Status;
132 }
133
134 NTSTATUS FASTCALL
135 CsrCloseHandle(HANDLE Handle)
136 {
137 NTSTATUS Status;
138 PEPROCESS OldProcess;
139
140 /* Switch to the process in which the handle is valid */
141 OldProcess = PsGetCurrentProcess();
142 if (CsrProcess != OldProcess)
143 {
144 KeAttachProcess(&CsrProcess->Pcb);
145 }
146
147 Status = ZwClose(Handle);
148
149 if (CsrProcess != OldProcess)
150 {
151 KeDetachProcess();
152 }
153
154 return Status;
155 }
156
157 /* EOF */