ReactOS 0.4.15-dev-8102-g108db8f
init.c File Reference
#include "consrv.h"
#include "api.h"
#include "procinit.h"
#include <debug.h>
Include dependency graph for init.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI ConSrvNewProcess (PCSR_PROCESS SourceProcess, PCSR_PROCESS TargetProcess)
 
NTSTATUS NTAPI ConSrvConnect (IN PCSR_PROCESS CsrProcess, IN OUT PVOID ConnectionInfo, IN OUT PULONG ConnectionInfoLength)
 
VOID NTAPI ConSrvDisconnect (IN PCSR_PROCESS CsrProcess)
 
 CSR_SERVER_DLL_INIT (ConServerDllInitialization)
 

Variables

HINSTANCE ConSrvDllInstance = NULL
 
HANDLE ConSrvHeap = NULL
 
PCSR_API_ROUTINE ConsoleServerApiDispatchTable [ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER]
 
BOOLEAN ConsoleServerApiServerValidTable [ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER]
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 16 of file init.c.

Function Documentation

◆ ConSrvConnect()

NTSTATUS NTAPI ConSrvConnect ( IN PCSR_PROCESS  CsrProcess,
IN OUT PVOID  ConnectionInfo,
IN OUT PULONG  ConnectionInfoLength 
)

Definition at line 401 of file init.c.

404{
405 /**************************************************************************
406 * This function is called whenever a CUI new process is created.
407 **************************************************************************/
408
410 PCONSRV_API_CONNECTINFO ConnectInfo = (PCONSRV_API_CONNECTINFO)ConnectionInfo;
412
413 if ( ConnectionInfo == NULL ||
414 ConnectionInfoLength == NULL ||
415 *ConnectionInfoLength != sizeof(*ConnectInfo) )
416 {
417 DPRINT1("CONSRV: Connection failed - ConnectionInfo = 0x%p ; ConnectionInfoLength = 0x%p (%lu), expected %lu\n",
418 ConnectionInfo,
419 ConnectionInfoLength,
420 ConnectionInfoLength ? *ConnectionInfoLength : (ULONG)-1,
421 sizeof(*ConnectInfo));
422
423 return STATUS_UNSUCCESSFUL;
424 }
425
426 /* Set Control-Dispatcher handler */
427 ProcessData->CtrlRoutine = ConnectInfo->CtrlRoutine;
428
429 /* If we don't need a console, then get out of here */
430 DPRINT("ConnectInfo->IsConsoleApp = %s\n", ConnectInfo->IsConsoleApp ? "True" : "False");
431 if (!ConnectInfo->IsConsoleApp) return STATUS_SUCCESS;
432
433 /* If we don't inherit from an existing console, then create a new one... */
434 if (ConnectInfo->ConsoleStartInfo.ConsoleHandle == NULL)
435 {
436 CONSOLE_INIT_INFO ConsoleInitInfo;
437
438 DPRINT("ConSrvConnect - Allocate a new console\n");
439
440 /* Initialize the console initialization info structure */
441 ConsoleInitInfo.ConsoleStartInfo = &ConnectInfo->ConsoleStartInfo;
442 ConsoleInitInfo.IsWindowVisible = ConnectInfo->IsWindowVisible;
443 ConsoleInitInfo.TitleLength = ConnectInfo->TitleLength;
444 ConsoleInitInfo.ConsoleTitle = ConnectInfo->ConsoleTitle;
445 ConsoleInitInfo.DesktopLength = 0;
446 ConsoleInitInfo.Desktop = NULL;
447 ConsoleInitInfo.AppNameLength = ConnectInfo->AppNameLength;
448 ConsoleInitInfo.AppName = ConnectInfo->AppName;
449 ConsoleInitInfo.CurDirLength = ConnectInfo->CurDirLength;
450 ConsoleInitInfo.CurDir = ConnectInfo->CurDir;
451
452 /*
453 * Contrary to the case of SrvAllocConsole, the desktop string is
454 * allocated in the process' heap, so we need to retrieve it by
455 * using NtReadVirtualMemory.
456 */
457 if (ConnectInfo->DesktopLength)
458 {
459 ConsoleInitInfo.DesktopLength = ConnectInfo->DesktopLength;
460
461 ConsoleInitInfo.Desktop = ConsoleAllocHeap(HEAP_ZERO_MEMORY,
462 ConsoleInitInfo.DesktopLength);
463 if (ConsoleInitInfo.Desktop == NULL)
464 return STATUS_NO_MEMORY;
465
467 ConnectInfo->Desktop,
468 ConsoleInitInfo.Desktop,
469 ConsoleInitInfo.DesktopLength,
470 NULL);
471 if (!NT_SUCCESS(Status))
472 {
473 ConsoleFreeHeap(ConsoleInitInfo.Desktop);
474 return Status;
475 }
476 }
477
478 /*
479 * We are about to create a new console. However when ConSrvNewProcess
480 * was called, we didn't know that we wanted to create a new console and
481 * therefore, we by default inherited the handles table from our parent
482 * process. It's only now that we notice that in fact we do not need
483 * them, because we've created a new console and thus we must use it.
484 *
485 * ConSrvAllocateConsole will free our old handles table
486 * and recreate a new valid one.
487 */
488
489 /* Initialize a new Console owned by the Console Leader Process */
490 Status = ConSrvAllocateConsole(ProcessData,
491 &ConnectInfo->ConsoleStartInfo.InputHandle,
492 &ConnectInfo->ConsoleStartInfo.OutputHandle,
493 &ConnectInfo->ConsoleStartInfo.ErrorHandle,
494 &ConsoleInitInfo);
495
496 /* Free our local desktop string if any */
497 if (ConsoleInitInfo.DesktopLength)
498 ConsoleFreeHeap(ConsoleInitInfo.Desktop);
499
500 /* Check for success */
501 if (!NT_SUCCESS(Status))
502 {
503 DPRINT1("Console allocation failed\n");
504 return Status;
505 }
506 }
507 else /* We inherit it from the parent */
508 {
509 DPRINT("ConSrvConnect - Reuse current (parent's) console\n");
510
511 /* Reuse our current console */
512 Status = ConSrvInheritConsole(ProcessData,
513 ConnectInfo->ConsoleStartInfo.ConsoleHandle,
514 FALSE,
515 NULL, // &ConnectInfo->ConsoleStartInfo.InputHandle,
516 NULL, // &ConnectInfo->ConsoleStartInfo.OutputHandle,
517 NULL, // &ConnectInfo->ConsoleStartInfo.ErrorHandle,
518 &ConnectInfo->ConsoleStartInfo);
519 if (!NT_SUCCESS(Status))
520 {
521 DPRINT1("Console inheritance failed\n");
522 return Status;
523 }
524 }
525
526 /* Set the Property-Dialog handler */
527 ProcessData->PropRoutine = ConnectInfo->PropRoutine;
528
529 return STATUS_SUCCESS;
530}
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
struct _CONSRV_API_CONNECTINFO * PCONSRV_API_CONNECTINFO
#define ConsoleGetPerProcessData(Process)
Definition: consrv.h:37
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
Status
Definition: gdiplustypes.h:25
NTSTATUS NTAPI NtReadVirtualMemory(IN HANDLE ProcessHandle, IN PVOID BaseAddress, OUT PVOID Buffer, IN SIZE_T NumberOfBytesToRead, OUT PSIZE_T NumberOfBytesRead OPTIONAL)
Definition: virtual.c:2816
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
ULONG CurDirLength
Definition: console.h:22
ULONG DesktopLength
Definition: console.h:18
ULONG TitleLength
Definition: console.h:16
PCONSOLE_START_INFO ConsoleStartInfo
Definition: console.h:13
BOOLEAN IsWindowVisible
Definition: console.h:14
PWCHAR ConsoleTitle
Definition: console.h:17
ULONG AppNameLength
Definition: console.h:20
PWCHAR Desktop
Definition: console.h:19
PWCHAR AppName
Definition: console.h:21
LPTHREAD_START_ROUTINE CtrlRoutine
Definition: consrv.h:52
PCSR_PROCESS Process
Definition: consrv.h:43
LPTHREAD_START_ROUTINE PropRoutine
Definition: consrv.h:53
HANDLE ErrorHandle
Definition: conmsg.h:174
HANDLE ConsoleHandle
Definition: conmsg.h:170
HANDLE InputHandle
Definition: conmsg.h:172
HANDLE OutputHandle
Definition: conmsg.h:173
BOOLEAN IsWindowVisible
Definition: conmsg.h:189
WCHAR ConsoleTitle[MAX_PATH+1]
Definition: conmsg.h:198
LPTHREAD_START_ROUTINE PropRoutine
Definition: conmsg.h:194
WCHAR AppName[128]
Definition: conmsg.h:204
WCHAR CurDir[MAX_PATH+1]
Definition: conmsg.h:206
CONSOLE_START_INFO ConsoleStartInfo
Definition: conmsg.h:186
LPTHREAD_START_ROUTINE CtrlRoutine
Definition: conmsg.h:193
HANDLE ProcessHandle
Definition: csrsrv.h:46
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
PKPROCESS CsrProcess
Definition: videoprt.c:39
NTSTATUS ConSrvAllocateConsole(IN OUT PCONSOLE_PROCESS_DATA ProcessData, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle, IN OUT PCONSOLE_INIT_INFO ConsoleInitInfo)
Definition: console.c:968
NTSTATUS ConSrvInheritConsole(IN OUT PCONSOLE_PROCESS_DATA ProcessData, IN HANDLE ConsoleHandle, IN BOOLEAN CreateNewHandleTable, OUT PHANDLE pInputHandle, OUT PHANDLE pOutputHandle, OUT PHANDLE pErrorHandle, IN OUT PCONSOLE_START_INFO ConsoleStartInfo)
Definition: console.c:1096
#define ConsoleAllocHeap(Flags, Size)
Definition: heap.h:14
#define ConsoleFreeHeap(HeapBase)
Definition: heap.h:15

Referenced by CSR_SERVER_DLL_INIT().

◆ ConSrvDisconnect()

VOID NTAPI ConSrvDisconnect ( IN PCSR_PROCESS  CsrProcess)

Definition at line 534 of file init.c.

535{
537
538 /**************************************************************************
539 * This function is called whenever a new process (GUI or CUI) is destroyed.
540 **************************************************************************/
541
542 if ( ProcessData->ConsoleHandle != NULL ||
543 ProcessData->HandleTable != NULL )
544 {
545 DPRINT("ConSrvDisconnect - calling ConSrvRemoveConsole\n");
546 ConSrvRemoveConsole(ProcessData);
547 }
548
550}
NTSYSAPI NTSTATUS NTAPI RtlDeleteCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
HANDLE ConsoleHandle
Definition: consrv.h:45
struct _CONSOLE_IO_HANDLE * HandleTable
Definition: consrv.h:50
RTL_CRITICAL_SECTION HandleTableLock
Definition: consrv.h:48
NTSTATUS ConSrvRemoveConsole(IN OUT PCONSOLE_PROCESS_DATA ProcessData)
Definition: console.c:1227

Referenced by CSR_SERVER_DLL_INIT().

◆ ConSrvNewProcess()

NTSTATUS NTAPI ConSrvNewProcess ( PCSR_PROCESS  SourceProcess,
PCSR_PROCESS  TargetProcess 
)

Definition at line 325 of file init.c.

327{
328 /**************************************************************************
329 * This function is called whenever a new process (GUI or CUI) is created.
330 *
331 * Copy the parent's handles table here if both the parent and the child
332 * processes are CUI. If we must actually create our proper console (and
333 * thus do not inherit from the console handles of the parent's), then we
334 * will clean this table in the next ConSrvConnect call. Why we are doing
335 * this? It's because here, we still don't know whether or not we must create
336 * a new console instead of inherit it from the parent, and, because in
337 * ConSrvConnect we don't have any reference to the parent process anymore.
338 **************************************************************************/
339
341 PCONSOLE_PROCESS_DATA TargetProcessData;
342
343 /* An empty target process is invalid */
344 if (!TargetProcess) return STATUS_INVALID_PARAMETER;
345
346 TargetProcessData = ConsoleGetPerProcessData(TargetProcess);
347
348 /* Initialize the new (target) process */
349 RtlZeroMemory(TargetProcessData, sizeof(*TargetProcessData));
350 TargetProcessData->Process = TargetProcess;
351 TargetProcessData->ConsoleHandle = NULL;
352 TargetProcessData->ConsoleApp = FALSE;
353
354 /*
355 * The handles table gets initialized either when inheriting from
356 * another console process, or when creating a new console.
357 */
358 TargetProcessData->HandleTableSize = 0;
359 TargetProcessData->HandleTable = NULL;
360
362
363 /* Do nothing if the source process is NULL */
364 if (!SourceProcess) return STATUS_SUCCESS;
365
366 // SourceProcessData = ConsoleGetPerProcessData(SourceProcess);
367
368 /*
369 * If the child process is a console application and the parent process is
370 * either a console application or just has a valid console (with a valid
371 * handles table: this can happen if it is a GUI application having called
372 * AllocConsole), then try to inherit handles from the parent process.
373 */
374 if (TargetProcess->Flags & CsrProcessIsConsoleApp /* && SourceProcessData->ConsoleHandle != NULL */)
375 {
376 PCONSOLE_PROCESS_DATA SourceProcessData = ConsoleGetPerProcessData(SourceProcess);
377 PCONSRV_CONSOLE SourceConsole;
378
379 /* Validate and lock the parent's console */
380 if (ConSrvValidateConsole(&SourceConsole,
381 SourceProcessData->ConsoleHandle,
383 {
384 /* Inherit the parent's handles table */
385 Status = ConSrvInheritHandlesTable(SourceProcessData, TargetProcessData);
386 if (!NT_SUCCESS(Status))
387 {
388 DPRINT1("Inheriting handles table failed\n");
389 }
390
391 /* Unlock the parent's console */
392 LeaveCriticalSection(&SourceConsole->Lock);
393 }
394 }
395
396 return Status;
397}
@ CsrProcessIsConsoleApp
Definition: csrsrv.h:94
#define TRUE
Definition: types.h:120
NTSYSAPI NTSTATUS NTAPI RtlInitializeCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
ULONG HandleTableSize
Definition: consrv.h:49
BOOLEAN ConsoleApp
Definition: consrv.h:46
ULONG Flags
Definition: csrsrv.h:48
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
BOOLEAN NTAPI ConSrvValidateConsole(OUT PCONSRV_CONSOLE *Console, IN HANDLE ConsoleHandle, IN CONSOLE_STATE ExpectedState, IN BOOLEAN LockConsole)
Definition: console.c:247
NTSTATUS ConSrvInheritHandlesTable(IN PCONSOLE_PROCESS_DATA SourceProcessData, IN PCONSOLE_PROCESS_DATA TargetProcessData)
Definition: handle.c:120
@ CONSOLE_RUNNING
Definition: conio.h:283
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)

Referenced by CSR_SERVER_DLL_INIT().

◆ CSR_SERVER_DLL_INIT()

CSR_SERVER_DLL_INIT ( ConServerDllInitialization  )

Definition at line 552 of file init.c.

553{
554 /* Initialize the memory */
555 ConSrvHeap = RtlGetProcessHeap();
556/*
557 // We can use our own heap instead of the CSR heap to investigate heap corruptions :)
558 ConSrvHeap = RtlCreateHeap(HEAP_GROWABLE |
559 HEAP_PROTECTION_ENABLED |
560 HEAP_FREE_CHECKING_ENABLED |
561 HEAP_TAIL_CHECKING_ENABLED |
562 HEAP_VALIDATE_ALL_ENABLED,
563 NULL, 0, 0, NULL, NULL);
564 if (!ConSrvHeap) return STATUS_NO_MEMORY;
565*/
566
568
569 /* Setup the DLL Object */
570 LoadedServerDll->ApiBase = CONSRV_FIRST_API_NUMBER;
571 LoadedServerDll->HighestApiSupported = ConsolepMaxApiNumber;
572 LoadedServerDll->DispatchTable = ConsoleServerApiDispatchTable;
573 LoadedServerDll->ValidTable = ConsoleServerApiServerValidTable;
574#ifdef CSR_DBG
575 LoadedServerDll->NameTable = ConsoleServerApiNameTable;
576#endif
577 LoadedServerDll->SizeOfProcessData = sizeof(CONSOLE_PROCESS_DATA);
578 LoadedServerDll->ConnectCallback = ConSrvConnect;
579 LoadedServerDll->DisconnectCallback = ConSrvDisconnect;
580 LoadedServerDll->NewProcessCallback = ConSrvNewProcess;
581 // LoadedServerDll->HardErrorCallback = ConSrvHardError;
582 LoadedServerDll->ShutdownProcessCallback = ConsoleClientShutdown;
583
584 ConSrvDllInstance = LoadedServerDll->ServerHandle;
585
586 /* All done */
587 return STATUS_SUCCESS;
588}
#define CONSRV_FIRST_API_NUMBER
Definition: conmsg.h:16
@ ConsolepMaxApiNumber
Definition: conmsg.h:114
ULONG NTAPI ConsoleClientShutdown(IN PCSR_PROCESS CsrProcess, IN ULONG Flags, IN BOOLEAN FirstPhase)
Definition: shutdown.c:72
struct _CONSOLE_PROCESS_DATA CONSOLE_PROCESS_DATA
VOID NTAPI ConSrvInitConsoleSupport(VOID)
Definition: console.c:347
NTSTATUS NTAPI ConSrvNewProcess(PCSR_PROCESS SourceProcess, PCSR_PROCESS TargetProcess)
Definition: init.c:325
BOOLEAN ConsoleServerApiServerValidTable[ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER]
Definition: init.c:123
HINSTANCE ConSrvDllInstance
Definition: init.c:21
VOID NTAPI ConSrvDisconnect(IN PCSR_PROCESS CsrProcess)
Definition: init.c:534
NTSTATUS NTAPI ConSrvConnect(IN PCSR_PROCESS CsrProcess, IN OUT PVOID ConnectionInfo, IN OUT PULONG ConnectionInfoLength)
Definition: init.c:401
PCSR_API_ROUTINE ConsoleServerApiDispatchTable[ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER]
Definition: init.c:27
HANDLE ConSrvHeap
Definition: init.c:24

Variable Documentation

◆ ConsoleServerApiDispatchTable

Definition at line 27 of file init.c.

Referenced by CSR_SERVER_DLL_INIT().

◆ ConsoleServerApiServerValidTable

BOOLEAN ConsoleServerApiServerValidTable[ConsolepMaxApiNumber - CONSRV_FIRST_API_NUMBER]

Definition at line 123 of file init.c.

Referenced by CSR_SERVER_DLL_INIT().

◆ ConSrvDllInstance

◆ ConSrvHeap

HANDLE ConSrvHeap = NULL

Definition at line 24 of file init.c.

Referenced by CSR_SERVER_DLL_INIT().