- Remove KeAttachProcess and KeDetachProcess prototypes from winddk.h.
[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 static HANDLE WindowsApiPort = NULL;
13 PEPROCESS CsrProcess = NULL;
14
15 NTSTATUS FASTCALL
16 CsrInit(void)
17 {
18 NTSTATUS Status;
19 UNICODE_STRING PortName;
20 ULONG ConnectInfoLength;
21
22 RtlInitUnicodeString(&PortName, L"\\Windows\\ApiPort");
23 ConnectInfoLength = 0;
24 Status = ZwConnectPort(&WindowsApiPort,
25 &PortName,
26 NULL,
27 NULL,
28 NULL,
29 NULL,
30 NULL,
31 &ConnectInfoLength);
32 if (! NT_SUCCESS(Status))
33 {
34 return Status;
35 }
36
37 CsrProcess = PsGetCurrentProcess();
38
39 return STATUS_SUCCESS;
40 }
41
42
43 NTSTATUS FASTCALL
44 CsrNotify(PCSRSS_API_REQUEST Request, PCSRSS_API_REPLY Reply)
45 {
46 NTSTATUS Status;
47 PEPROCESS OldProcess;
48
49 if (NULL == CsrProcess)
50 {
51 return STATUS_INVALID_PORT_HANDLE;
52 }
53
54 Request->Header.DataSize = sizeof(CSRSS_API_REQUEST) - LPC_MESSAGE_BASE_SIZE;
55 Request->Header.MessageSize = sizeof(CSRSS_API_REQUEST);
56
57 /* Switch to the process in which the WindowsApiPort handle is valid */
58 OldProcess = PsGetCurrentProcess();
59 if (CsrProcess != OldProcess)
60 {
61 KeAttachProcess(EPROCESS_TO_KPROCESS(CsrProcess));
62 }
63 Status = ZwRequestWaitReplyPort(WindowsApiPort,
64 &Request->Header,
65 &Reply->Header);
66 if (CsrProcess != OldProcess)
67 {
68 KeDetachProcess();
69 }
70
71 if (NT_SUCCESS(Status))
72 {
73 Status = Reply->Status;
74 }
75
76 return Status;
77 }
78
79 NTSTATUS
80 STDCALL
81 CsrInsertObject(HANDLE ObjectHandle,
82 ACCESS_MASK DesiredAccess,
83 PHANDLE Handle)
84 {
85 NTSTATUS Status;
86 HANDLE CsrProcessHandle;
87 OBJECT_ATTRIBUTES ObjectAttributes;
88 CLIENT_ID Cid;
89
90 /* Put CSR'S CID */
91 Cid.UniqueProcess = CsrProcess->UniqueProcessId;
92 Cid.UniqueThread = 0;
93
94 /* Empty Attributes */
95 InitializeObjectAttributes(&ObjectAttributes,
96 NULL,
97 0,
98 NULL,
99 NULL);
100
101 /* Get a Handle to Csrss */
102 Status = ZwOpenProcess(&CsrProcessHandle,
103 PROCESS_DUP_HANDLE,
104 &ObjectAttributes,
105 &Cid);
106
107 if ((NT_SUCCESS(Status)))
108 {
109 /* Duplicate the Handle */
110 Status = ZwDuplicateObject(NtCurrentProcess(),
111 ObjectHandle,
112 CsrProcessHandle,
113 Handle,
114 DesiredAccess,
115 TRUE,
116 0);
117
118 /* Close our handle to CSRSS */
119 ZwClose(CsrProcessHandle);
120 }
121
122 return Status;
123 }
124
125 NTSTATUS FASTCALL
126 CsrCloseHandle(HANDLE Handle)
127 {
128 NTSTATUS Status;
129 PEPROCESS OldProcess;
130
131 /* Switch to the process in which the handle is valid */
132 OldProcess = PsGetCurrentProcess();
133 if (CsrProcess != OldProcess)
134 {
135 KeAttachProcess(EPROCESS_TO_KPROCESS(CsrProcess));
136 }
137
138 Status = ZwClose(Handle);
139
140 if (CsrProcess != OldProcess)
141 {
142 KeDetachProcess();
143 }
144
145 return Status;
146 }
147
148 /* EOF */