2 * subsystems/win32/csrss/csrsrv/api/process.c
4 * "\windows\ApiPort" port process management functions
6 * ReactOS Operating System
9 /* INCLUDES ******************************************************************/
16 extern NTSTATUS
CallProcessCreated(PCSR_PROCESS
, PCSR_PROCESS
);
18 /* GLOBALS *******************************************************************/
20 /* FUNCTIONS *****************************************************************/
22 /**********************************************************************
24 *********************************************************************/
27 *** Some APIs from here will go to basesrv.dll, some others to winsrv.dll.
28 *** Furthermore, this structure uses the old definition of APIs list.
29 *** The new one is in fact three arrays, one of APIs pointers, one other of
30 *** corresponding indexes, and the third one of names (not very efficient...).
33 CSR_API(BaseSrvCreateProcess
)
36 HANDLE ProcessHandle
, ThreadHandle
;
37 PCSR_THREAD CsrThread
;
38 PCSR_PROCESS Process
, NewProcess
;
39 ULONG Flags
, VdmPower
= 0, DebugFlags
= 0;
41 /* Get the current client thread */
42 CsrThread
= CsrGetClientThread();
43 ASSERT(CsrThread
!= NULL
);
45 Process
= CsrThread
->Process
;
47 /* Extract the flags out of the process handle */
48 Flags
= (ULONG_PTR
)ApiMessage
->Data
.CreateProcessRequest
.ProcessHandle
& 3;
49 ApiMessage
->Data
.CreateProcessRequest
.ProcessHandle
= (HANDLE
)((ULONG_PTR
)ApiMessage
->Data
.CreateProcessRequest
.ProcessHandle
& ~3);
51 /* Duplicate the process handle */
52 Status
= NtDuplicateObject(Process
->ProcessHandle
,
53 ApiMessage
->Data
.CreateProcessRequest
.ProcessHandle
,
58 DUPLICATE_SAME_ACCESS
);
59 if (!NT_SUCCESS(Status
))
61 DPRINT1("Failed to duplicate process handle\n");
65 /* Duplicate the thread handle */
66 Status
= NtDuplicateObject(Process
->ProcessHandle
,
67 ApiMessage
->Data
.CreateProcessRequest
.ThreadHandle
,
72 DUPLICATE_SAME_ACCESS
);
73 if (!NT_SUCCESS(Status
))
75 DPRINT1("Failed to duplicate process handle\n");
76 NtClose(ProcessHandle
);
80 /* See if this is a VDM process */
83 /* Request VDM powers */
84 Status
= NtSetInformationProcess(ProcessHandle
,
85 ProcessWx86Information
,
88 if (!NT_SUCCESS(Status
))
90 DPRINT1("Failed to get VDM powers\n");
91 NtClose(ProcessHandle
);
92 NtClose(ThreadHandle
);
97 /* Convert some flags. FIXME: More need conversion */
98 if (ApiMessage
->Data
.CreateProcessRequest
.CreationFlags
& CREATE_NEW_PROCESS_GROUP
)
100 DebugFlags
|= CsrProcessCreateNewGroup
;
103 /* FIXME: SxS Stuff */
105 /* Call CSRSRV to create the CSR_PROCESS structure and the first CSR_THREAD */
106 Status
= CsrCreateProcess(ProcessHandle
,
108 &ApiMessage
->Data
.CreateProcessRequest
.ClientId
,
112 if (Status
== STATUS_THREAD_IS_TERMINATING
)
114 DPRINT1("Thread already dead\n");
118 /* Check for other failures */
119 if (!NT_SUCCESS(Status
))
121 DPRINT1("Failed to create process/thread structures: %lx\n", Status
);
125 /* FIXME: Should notify user32 */
127 /* FIXME: VDM vodoo */
129 /* ReactOS Compatibility */
130 Status
= CsrLockProcessByClientId(ApiMessage
->Data
.CreateProcessRequest
.ClientId
.UniqueProcess
, &NewProcess
);
131 ASSERT(Status
== STATUS_SUCCESS
);
132 if (!(ApiMessage
->Data
.CreateProcessRequest
.CreationFlags
& (CREATE_NEW_CONSOLE
| DETACHED_PROCESS
)))
134 NewProcess
->ParentConsole
= Process
->Console
;
135 NewProcess
->bInheritHandles
= ApiMessage
->Data
.CreateProcessRequest
.bInheritHandles
;
137 RtlInitializeCriticalSection(&NewProcess
->HandleTableLock
);
138 CallProcessCreated(Process
, NewProcess
);
139 CsrUnlockProcess(NewProcess
);
141 /* Return the result of this operation */
145 CSR_API(BaseSrvCreateThread
)
147 PCSR_THREAD CurrentThread
;
150 PCSR_PROCESS CsrProcess
;
152 /* Get the current CSR thread */
153 CurrentThread
= CsrGetClientThread();
156 DPRINT1("Server Thread TID: [%lx.%lx]\n",
157 ApiMessage
->Data
.CreateThreadRequest
.ClientId
.UniqueProcess
,
158 ApiMessage
->Data
.CreateThreadRequest
.ClientId
.UniqueThread
);
159 return STATUS_SUCCESS
; // server-to-server
162 /* Get the CSR Process for this request */
163 CsrProcess
= CurrentThread
->Process
;
164 if (CsrProcess
->ClientId
.UniqueProcess
!=
165 ApiMessage
->Data
.CreateThreadRequest
.ClientId
.UniqueProcess
)
167 /* This is a remote thread request -- is it within the server itself? */
168 if (ApiMessage
->Data
.CreateThreadRequest
.ClientId
.UniqueProcess
== NtCurrentTeb()->ClientId
.UniqueProcess
)
170 /* Accept this without any further work */
171 return STATUS_SUCCESS
;
174 /* Get the real CSR Process for the remote thread's process */
175 Status
= CsrLockProcessByClientId(ApiMessage
->Data
.CreateThreadRequest
.ClientId
.UniqueProcess
,
177 if (!NT_SUCCESS(Status
)) return Status
;
180 /* Duplicate the thread handle so we can own it */
181 Status
= NtDuplicateObject(CurrentThread
->Process
->ProcessHandle
,
182 ApiMessage
->Data
.CreateThreadRequest
.ThreadHandle
,
187 DUPLICATE_SAME_ACCESS
);
188 if (NT_SUCCESS(Status
))
190 /* Call CSRSRV to tell it about the new thread */
191 Status
= CsrCreateThread(CsrProcess
,
193 &ApiMessage
->Data
.CreateThreadRequest
.ClientId
);
196 /* Unlock the process and return */
197 if (CsrProcess
!= CurrentThread
->Process
) CsrUnlockProcess(CsrProcess
);
201 CSR_API(BaseSrvExitProcess
)
203 PCSR_THREAD CsrThread
= CsrGetClientThread();
204 ASSERT(CsrThread
!= NULL
);
206 /* Set magic flag so we don't reply this message back */
207 ApiMessage
->ApiNumber
= 0xBABE;
209 /* Remove the CSR_THREADs and CSR_PROCESS */
210 return CsrDestroyProcess(&CsrThread
->ClientId
,
211 (NTSTATUS
)ApiMessage
->Data
.TerminateProcessRequest
.uExitCode
);
214 CSR_API(BaseSrvGetProcessShutdownParam
)
216 PCSR_THREAD CsrThread
= CsrGetClientThread();
219 ApiMessage
->Data
.GetShutdownParametersRequest
.Level
= CsrThread
->Process
->ShutdownLevel
;
220 ApiMessage
->Data
.GetShutdownParametersRequest
.Flags
= CsrThread
->Process
->ShutdownFlags
;
222 return STATUS_SUCCESS
;
225 CSR_API(BaseSrvSetProcessShutdownParam
)
227 PCSR_THREAD CsrThread
= CsrGetClientThread();
230 CsrThread
->Process
->ShutdownLevel
= ApiMessage
->Data
.SetShutdownParametersRequest
.Level
;
231 CsrThread
->Process
->ShutdownFlags
= ApiMessage
->Data
.SetShutdownParametersRequest
.Flags
;
233 return STATUS_SUCCESS
;