Fix HEAD compilation.
[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 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) - LPC_MESSAGE_BASE_SIZE;
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 Status = ZwRequestWaitReplyPort(WindowsApiPort,
67 &Request->Header,
68 &Request->Header);
69 if (CsrProcess != OldProcess)
70 {
71 KeDetachProcess();
72 }
73
74 if (NT_SUCCESS(Status))
75 {
76 Status = Request->Status;
77 }
78
79 return Status;
80 }
81
82 NTSTATUS
83 STDCALL
84 CsrInsertObject(HANDLE ObjectHandle,
85 ACCESS_MASK DesiredAccess,
86 PHANDLE Handle)
87 {
88 NTSTATUS Status;
89 HANDLE CsrProcessHandle;
90 OBJECT_ATTRIBUTES ObjectAttributes;
91 CLIENT_ID Cid;
92
93 /* Put CSR'S CID */
94 Cid.UniqueProcess = CsrProcess->UniqueProcessId;
95 Cid.UniqueThread = 0;
96
97 /* Empty Attributes */
98 InitializeObjectAttributes(&ObjectAttributes,
99 NULL,
100 0,
101 NULL,
102 NULL);
103
104 /* Get a Handle to Csrss */
105 Status = ZwOpenProcess(&CsrProcessHandle,
106 PROCESS_DUP_HANDLE,
107 &ObjectAttributes,
108 &Cid);
109
110 if ((NT_SUCCESS(Status)))
111 {
112 /* Duplicate the Handle */
113 Status = ZwDuplicateObject(NtCurrentProcess(),
114 ObjectHandle,
115 CsrProcessHandle,
116 Handle,
117 DesiredAccess,
118 TRUE,
119 0);
120
121 /* Close our handle to CSRSS */
122 ZwClose(CsrProcessHandle);
123 }
124
125 return Status;
126 }
127
128 NTSTATUS FASTCALL
129 CsrCloseHandle(HANDLE Handle)
130 {
131 NTSTATUS Status;
132 PEPROCESS OldProcess;
133
134 /* Switch to the process in which the handle is valid */
135 OldProcess = PsGetCurrentProcess();
136 if (CsrProcess != OldProcess)
137 {
138 KeAttachProcess(&CsrProcess->Pcb);
139 }
140
141 Status = ZwClose(Handle);
142
143 if (CsrProcess != OldProcess)
144 {
145 KeDetachProcess();
146 }
147
148 return Status;
149 }
150
151 /* EOF */