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