2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS/Win32 Base enviroment Subsystem Server
4 * FILE: subsystems/win/basesrv/proc.c
5 * PURPOSE: Process and Thread Management
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES *******************************************************************/
16 /* PUBLIC SERVER APIS *********************************************************/
18 CSR_API(BaseSrvGetTempFile
)
20 static UINT BaseGetTempFileUnique
= 0;
21 PBASE_GET_TEMP_FILE GetTempFile
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.GetTempFile
;
23 /* Return 16-bits ID */
24 GetTempFile
->UniqueID
= (++BaseGetTempFileUnique
& 0xFFFF);
26 DPRINT("Returning: %u\n", GetTempFile
->UniqueID
);
28 return STATUS_SUCCESS
;
31 CSR_API(BaseSrvCreateProcess
)
34 PBASE_CREATE_PROCESS CreateProcessRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.CreateProcessRequest
;
35 HANDLE ProcessHandle
, ThreadHandle
;
36 PCSR_THREAD CsrThread
;
38 ULONG Flags
= 0, VdmPower
= 0, DebugFlags
= 0;
40 /* Get the current client thread */
41 CsrThread
= CsrGetClientThread();
42 ASSERT(CsrThread
!= NULL
);
44 Process
= CsrThread
->Process
;
46 /* Extract the flags out of the process handle */
47 Flags
= (ULONG_PTR
)CreateProcessRequest
->ProcessHandle
& 3;
48 CreateProcessRequest
->ProcessHandle
= (HANDLE
)((ULONG_PTR
)CreateProcessRequest
->ProcessHandle
& ~3);
50 /* Duplicate the process handle */
51 Status
= NtDuplicateObject(Process
->ProcessHandle
,
52 CreateProcessRequest
->ProcessHandle
,
57 DUPLICATE_SAME_ACCESS
);
58 if (!NT_SUCCESS(Status
))
60 DPRINT1("Failed to duplicate process handle\n");
64 /* Duplicate the thread handle */
65 Status
= NtDuplicateObject(Process
->ProcessHandle
,
66 CreateProcessRequest
->ThreadHandle
,
71 DUPLICATE_SAME_ACCESS
);
72 if (!NT_SUCCESS(Status
))
74 DPRINT1("Failed to duplicate process handle\n");
75 NtClose(ProcessHandle
);
79 /* See if this is a VDM process */
82 /* Request VDM powers */
83 Status
= NtSetInformationProcess(ProcessHandle
,
84 ProcessWx86Information
,
87 if (!NT_SUCCESS(Status
))
89 DPRINT1("Failed to get VDM powers\n");
90 NtClose(ProcessHandle
);
91 NtClose(ThreadHandle
);
96 /* Flags conversion. FIXME: More need conversion */
97 if (CreateProcessRequest
->CreationFlags
& CREATE_NEW_PROCESS_GROUP
)
99 DebugFlags
|= CsrProcessCreateNewGroup
;
101 if ((Flags
& 2) == 0)
103 /* We are launching a console process */
104 DebugFlags
|= CsrProcessIsConsoleApp
;
107 /* FIXME: SxS Stuff */
109 /* Call CSRSRV to create the CSR_PROCESS structure and the first CSR_THREAD */
110 Status
= CsrCreateProcess(ProcessHandle
,
112 &CreateProcessRequest
->ClientId
,
116 if (Status
== STATUS_THREAD_IS_TERMINATING
)
118 DPRINT1("Thread already dead\n");
120 /* Set the special reply value so we don't reply this message back */
121 *ReplyCode
= CsrReplyDeadClient
;
126 /* Check for other failures */
127 if (!NT_SUCCESS(Status
))
129 DPRINT1("Failed to create process/thread structures: %lx\n", Status
);
133 /* FIXME: Should notify user32 */
135 /* FIXME: VDM vodoo */
137 /* Return the result of this operation */
141 CSR_API(BaseSrvCreateThread
)
144 PBASE_CREATE_THREAD CreateThreadRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.CreateThreadRequest
;
145 PCSR_THREAD CurrentThread
;
147 PCSR_PROCESS CsrProcess
;
149 /* Get the current CSR thread */
150 CurrentThread
= CsrGetClientThread();
153 DPRINT1("Server Thread TID: [%lx.%lx]\n",
154 CreateThreadRequest
->ClientId
.UniqueProcess
,
155 CreateThreadRequest
->ClientId
.UniqueThread
);
156 return STATUS_SUCCESS
; // server-to-server
159 /* Get the CSR Process for this request */
160 CsrProcess
= CurrentThread
->Process
;
161 if (CsrProcess
->ClientId
.UniqueProcess
!=
162 CreateThreadRequest
->ClientId
.UniqueProcess
)
164 /* This is a remote thread request -- is it within the server itself? */
165 if (CreateThreadRequest
->ClientId
.UniqueProcess
== NtCurrentTeb()->ClientId
.UniqueProcess
)
167 /* Accept this without any further work */
168 return STATUS_SUCCESS
;
171 /* Get the real CSR Process for the remote thread's process */
172 Status
= CsrLockProcessByClientId(CreateThreadRequest
->ClientId
.UniqueProcess
,
174 if (!NT_SUCCESS(Status
)) return Status
;
177 /* Duplicate the thread handle so we can own it */
178 Status
= NtDuplicateObject(CurrentThread
->Process
->ProcessHandle
,
179 CreateThreadRequest
->ThreadHandle
,
184 DUPLICATE_SAME_ACCESS
);
185 if (NT_SUCCESS(Status
))
187 /* Call CSRSRV to tell it about the new thread */
188 Status
= CsrCreateThread(CsrProcess
,
190 &CreateThreadRequest
->ClientId
,
194 /* Unlock the process and return */
195 if (CsrProcess
!= CurrentThread
->Process
) CsrUnlockProcess(CsrProcess
);
199 CSR_API(BaseSrvExitProcess
)
201 PCSR_THREAD CsrThread
= CsrGetClientThread();
202 ASSERT(CsrThread
!= NULL
);
204 /* Set the special reply value so we don't reply this message back */
205 *ReplyCode
= CsrReplyDeadClient
;
207 /* Remove the CSR_THREADs and CSR_PROCESS */
208 return CsrDestroyProcess(&CsrThread
->ClientId
,
209 (NTSTATUS
)((PBASE_API_MESSAGE
)ApiMessage
)->Data
.ExitProcessRequest
.uExitCode
);
212 CSR_API(BaseSrvGetProcessShutdownParam
)
214 PBASE_GET_PROCESS_SHUTDOWN_PARAMS GetShutdownParametersRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.GetShutdownParametersRequest
;
215 PCSR_THREAD CsrThread
= CsrGetClientThread();
218 GetShutdownParametersRequest
->Level
= CsrThread
->Process
->ShutdownLevel
;
219 GetShutdownParametersRequest
->Flags
= CsrThread
->Process
->ShutdownFlags
;
221 return STATUS_SUCCESS
;
224 CSR_API(BaseSrvSetProcessShutdownParam
)
226 PBASE_SET_PROCESS_SHUTDOWN_PARAMS SetShutdownParametersRequest
= &((PBASE_API_MESSAGE
)ApiMessage
)->Data
.SetShutdownParametersRequest
;
227 PCSR_THREAD CsrThread
= CsrGetClientThread();
230 CsrThread
->Process
->ShutdownLevel
= SetShutdownParametersRequest
->Level
;
231 CsrThread
->Process
->ShutdownFlags
= SetShutdownParametersRequest
->Flags
;
233 return STATUS_SUCCESS
;
236 /* PUBLIC API *****************************************************************/
240 BaseSetProcessCreateNotify(IN BASE_PROCESS_CREATE_NOTIFY_ROUTINE ProcessCreateNotifyProc
)
242 DPRINT("BASESRV: %s(%08lx) called\n", __FUNCTION__
, ProcessCreateNotifyProc
);
243 return STATUS_NOT_IMPLEMENTED
;