Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.6 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation 1998
  4. // All rights reserved
  5. //
  6. // main.cxx
  7. //
  8. //*************************************************************
  9. #include "appmgext.hxx"
  10. static SERVICE_STATUS gServiceStatus;
  11. static SERVICE_STATUS_HANDLE ghServiceHandle;
  12. static HINSTANCE ghInstAppmgmt;
  13. extern "C" void
  14. ServiceHandler(
  15. DWORD OpCode
  16. );
  17. void
  18. UpdateState(
  19. DWORD NewState
  20. );
  21. extern "C" void
  22. ServiceMain(
  23. DWORD argc,
  24. LPWSTR argv[]
  25. )
  26. {
  27. DWORD Status;
  28. BOOL bStatus;
  29. Status = ERROR_SUCCESS;
  30. if ( ! gbInitialized )
  31. Initialize();
  32. InitDebugSupport( DEBUGMODE_SERVICE );
  33. VerboseDebugDump( L"Entering ServiceMain" );
  34. gServiceStatus.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
  35. gServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  36. gServiceStatus.dwControlsAccepted = 0;
  37. gServiceStatus.dwWin32ExitCode = 0;
  38. gServiceStatus.dwServiceSpecificExitCode = 0;
  39. gServiceStatus.dwCheckPoint = 0;
  40. gServiceStatus.dwWaitHint = 10000L;
  41. ghServiceHandle = RegisterServiceCtrlHandler( L"APPMGMT", ServiceHandler );
  42. if ( ! ghServiceHandle )
  43. Status = GetLastError();
  44. if ( ERROR_SUCCESS == Status )
  45. {
  46. //
  47. // SD is null indicating that RPC should pick a default DACL.
  48. // which should be everybody can call and nobody can change this
  49. // permission
  50. //
  51. Status = RpcServerUseProtseqEp(
  52. L"ncalrpc",
  53. RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
  54. L"appmgmt",
  55. NULL );
  56. }
  57. if ( ERROR_SUCCESS == Status )
  58. Status = RpcServerRegisterIf( appmgmt_ServerIfHandle, NULL, NULL );
  59. if ( Status != ERROR_SUCCESS )
  60. {
  61. gServiceStatus.dwWin32ExitCode = Status;
  62. UpdateState( SERVICE_STOPPED );
  63. return;
  64. }
  65. InitializeCriticalSection( &gAppCS );
  66. UpdateState( SERVICE_RUNNING );
  67. }
  68. extern "C" void
  69. ServiceHandler(
  70. DWORD OpCode
  71. )
  72. {
  73. RPC_STATUS status;
  74. switch( OpCode )
  75. {
  76. case SERVICE_CONTROL_STOP:
  77. // Not registered for this control.
  78. break;
  79. case SERVICE_CONTROL_INTERROGATE:
  80. // Service controller wants us to call SetServiceStatus.
  81. UpdateState( gServiceStatus.dwCurrentState );
  82. break ;
  83. case SERVICE_CONTROL_SHUTDOWN:
  84. // Not registered for this control.
  85. break;
  86. // case SERVICE_CONTROL_PAUSE:
  87. // case SERVICE_CONTROL_CONTINUE:
  88. default:
  89. break;
  90. }
  91. return;
  92. }
  93. void
  94. UpdateState(
  95. DWORD NewState
  96. )
  97. {
  98. DWORD Status = ERROR_SUCCESS;
  99. switch ( NewState )
  100. {
  101. case SERVICE_RUNNING:
  102. case SERVICE_STOPPED:
  103. gServiceStatus.dwCheckPoint = 0;
  104. gServiceStatus.dwWaitHint = 0;
  105. break;
  106. case SERVICE_START_PENDING:
  107. case SERVICE_STOP_PENDING:
  108. ++gServiceStatus.dwCheckPoint;
  109. gServiceStatus.dwWaitHint = 30000L;
  110. break;
  111. default:
  112. ASSERT(0);
  113. return;
  114. }
  115. gServiceStatus.dwCurrentState = NewState;
  116. SetServiceStatus( ghServiceHandle, &gServiceStatus );
  117. // We could return a status but how would we recover? Ignore it, the
  118. // worst thing is that services will kill us and there's nothing
  119. // we can about it if this call fails.
  120. }
  121. void * midl_user_allocate( size_t len )
  122. {
  123. return (void *) LocalAlloc( 0, len );
  124. }
  125. void midl_user_free( void * ptr )
  126. {
  127. LocalFree(ptr);
  128. }