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.

256 lines
8.9 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. printer.c
  5. Abstract:
  6. Routines to deal with printer/spooler.
  7. Author:
  8. Ted Miller (tedm) 5-Apr-1995
  9. adapted from legacy\dll\printer.c
  10. Revision History:
  11. --*/
  12. #include "setupp.h"
  13. #pragma hdrstop
  14. //
  15. // Name of spooler service.
  16. //
  17. PCWSTR szSpooler = L"Spooler";
  18. BOOL
  19. MiscSpoolerInit(
  20. VOID
  21. )
  22. {
  23. MONITOR_INFO_2 MonitorInfo2;
  24. PROVIDOR_INFO_1 ProviderInfo1;
  25. BOOL b;
  26. WCHAR MonitorName[128];
  27. MonitorInfo2.pName = L"Local Port";
  28. MonitorInfo2.pEnvironment = NULL;
  29. MonitorInfo2.pDLLName = L"localmon.dll";
  30. b = TRUE;
  31. if(!AddMonitor(NULL,2,(PBYTE)&MonitorInfo2)) {
  32. b = FALSE;
  33. SetuplogError(
  34. LogSevWarning,
  35. SETUPLOG_USE_MESSAGEID,
  36. MSG_LOG_MISCSPOOLERINIT, NULL,
  37. SETUPLOG_USE_MESSAGEID,
  38. MSG_LOG_X_RETURNED_WINERR,
  39. L"AddMonitor",
  40. GetLastError(),
  41. NULL,NULL);
  42. }
  43. if(!AddPrintProcessor(NULL,NULL,L"winprint.dll",L"winprint")) {
  44. b = FALSE;
  45. SetuplogError(
  46. LogSevWarning,
  47. SETUPLOG_USE_MESSAGEID,
  48. MSG_LOG_MISCSPOOLERINIT, NULL,
  49. SETUPLOG_USE_MESSAGEID,
  50. MSG_LOG_X_RETURNED_WINERR,
  51. L"AddPrintProcessor",
  52. GetLastError(),
  53. NULL,NULL);
  54. }
  55. ProviderInfo1.pName = L"Internet Print Provider";
  56. ProviderInfo1.pEnvironment = NULL;
  57. ProviderInfo1.pDLLName = L"inetpp.dll";
  58. if ( !AddPrintProvidor(NULL, 1, (LPBYTE)(&ProviderInfo1)) ) {
  59. b = FALSE;
  60. SetuplogError(
  61. LogSevWarning,
  62. SETUPLOG_USE_MESSAGEID,
  63. MSG_LOG_MISCSPOOLERINIT, NULL,
  64. SETUPLOG_USE_MESSAGEID,
  65. MSG_LOG_X_RETURNED_WINERR,
  66. L"AddPrintProvidor",
  67. GetLastError(),
  68. NULL,NULL);
  69. }
  70. return(TRUE);
  71. }
  72. BOOL
  73. StartSpooler(
  74. VOID
  75. )
  76. {
  77. #define SLEEP_TIME 4000
  78. #define LOOP_COUNT 30
  79. SC_HANDLE hSC,hSCService;
  80. BOOL b;
  81. DWORD d;
  82. DWORD dwDesiredAccess;
  83. b = FALSE;
  84. //
  85. // Open a handle to the service controller manager
  86. //
  87. hSC = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  88. d = GetLastError();
  89. if(hSC == NULL) {
  90. SetuplogError(
  91. LogSevWarning,
  92. SETUPLOG_USE_MESSAGEID,
  93. MSG_LOG_STARTSVC_FAIL,
  94. szSpooler, NULL,
  95. SETUPLOG_USE_MESSAGEID,
  96. MSG_LOG_X_RETURNED_WINERR,
  97. szOpenSCManager,
  98. d,
  99. NULL,NULL);
  100. return(FALSE);
  101. }
  102. dwDesiredAccess = SERVICE_START | SERVICE_QUERY_STATUS;
  103. if(hSCService = OpenService(hSC,szSpooler,dwDesiredAccess)) {
  104. SetupDebugPrint1( L"StartSpooler: Just opened service %s", szSpooler);
  105. b = StartService(hSCService,0,NULL);
  106. d = GetLastError();
  107. SetupDebugPrint3( L"StartSpooler: Just started service %s, ret=%d, error=%d", szSpooler, b, d);
  108. if(!b) {
  109. if( d == ERROR_SERVICE_ALREADY_RUNNING) {
  110. //
  111. // Service is already running.
  112. //
  113. b = TRUE;
  114. } else if (d == ERROR_SERVICE_DATABASE_LOCKED) {
  115. LPQUERY_SERVICE_LOCK_STATUS lpqslsBuf;
  116. DWORD dwBytesNeeded;
  117. DWORD loopCount = 0;
  118. lpqslsBuf = (LPQUERY_SERVICE_LOCK_STATUS) MyMalloc( sizeof(QUERY_SERVICE_LOCK_STATUS)+256);
  119. if( lpqslsBuf) {
  120. do {
  121. // Lets query the lock status before we attempt to StartService
  122. b = QueryServiceLockStatus(
  123. hSC,
  124. lpqslsBuf,
  125. sizeof(QUERY_SERVICE_LOCK_STATUS)+256,
  126. &dwBytesNeeded);
  127. d = GetLastError();
  128. if ( !b ) {
  129. // Not a critical error but log it.
  130. SetupDebugPrint2( L"StartSpooler: Could Not QueryServiceLockStatus %s, Error=%d", szSpooler, d);
  131. }
  132. // Now retry StartService
  133. b = StartService(hSCService,0,NULL);
  134. d = GetLastError();
  135. SetupDebugPrint5( L"StartSpooler: Start service %s, database was locked by %s, duration=%d, lockstatus=%d, error=%d", szSpooler, lpqslsBuf->lpLockOwner, lpqslsBuf->dwLockDuration, lpqslsBuf->fIsLocked, d);
  136. if (!b) {
  137. // Could not StartService
  138. if ( loopCount++ == LOOP_COUNT) {
  139. SetupDebugPrint5( L"StartSpooler: Timeout. Start service %s, database was locked by %s, duration=%d, lockstatus=%d, error=%d", szSpooler, lpqslsBuf->lpLockOwner, lpqslsBuf->dwLockDuration, lpqslsBuf->fIsLocked, d);
  140. if( d == ERROR_SERVICE_ALREADY_RUNNING) {
  141. b = TRUE;
  142. } else {
  143. SetuplogError(
  144. LogSevWarning,
  145. SETUPLOG_USE_MESSAGEID,
  146. MSG_LOG_STARTSVC_FAIL,
  147. szSpooler, NULL,
  148. SETUPLOG_USE_MESSAGEID,
  149. MSG_LOG_X_PARAM_RETURNED_WINERR,
  150. szStartService,
  151. WAIT_TIMEOUT,
  152. szSpooler,
  153. NULL,NULL);
  154. SetuplogError(
  155. LogSevWarning,
  156. SETUPLOG_USE_MESSAGEID,
  157. MSG_LOG_STARTSVC_FAIL,
  158. szSpooler, NULL,
  159. SETUPLOG_USE_MESSAGEID,
  160. MSG_LOG_X_PARAM_RETURNED_WINERR,
  161. szStartService,
  162. ERROR_SERVICE_DATABASE_LOCKED,
  163. szSpooler,
  164. NULL,NULL);
  165. }
  166. break;
  167. }
  168. else {
  169. SetupDebugPrint2( L"StartSpooler: Sleeping. service %s, count=%d", szSpooler, loopCount);
  170. Sleep( SLEEP_TIME );
  171. }
  172. }
  173. else {
  174. // StartService succeeded this time.
  175. SetupDebugPrint4( L"StartSpooler: Had to retry start service %s, database was locked by %s, duration=%d, lockstatus=%d", szSpooler, lpqslsBuf->lpLockOwner, lpqslsBuf->dwLockDuration, lpqslsBuf->fIsLocked);
  176. break;
  177. }
  178. } while ( TRUE );
  179. MyFree( lpqslsBuf);
  180. } else {
  181. SetuplogError(
  182. LogSevWarning,
  183. SETUPLOG_USE_MESSAGEID,
  184. MSG_LOG_STARTSVC_FAIL,
  185. szSpooler, NULL,
  186. SETUPLOG_USE_MESSAGEID,
  187. MSG_LOG_X_PARAM_RETURNED_WINERR,
  188. szStartService,
  189. ERROR_NOT_ENOUGH_MEMORY,
  190. szSpooler,
  191. NULL,NULL);
  192. }
  193. } else {
  194. SetuplogError(
  195. LogSevWarning,
  196. SETUPLOG_USE_MESSAGEID,
  197. MSG_LOG_STARTSVC_FAIL,
  198. szSpooler, NULL,
  199. SETUPLOG_USE_MESSAGEID,
  200. MSG_LOG_X_PARAM_RETURNED_WINERR,
  201. szStartService,
  202. d,
  203. szSpooler,
  204. NULL,NULL);
  205. }
  206. }
  207. CloseServiceHandle(hSCService);
  208. } else {
  209. b = FALSE;
  210. SetuplogError(
  211. LogSevWarning,
  212. SETUPLOG_USE_MESSAGEID,
  213. MSG_LOG_STARTSVC_FAIL,
  214. szSpooler, NULL,
  215. SETUPLOG_USE_MESSAGEID,
  216. MSG_LOG_X_PARAM_RETURNED_WINERR,
  217. szOpenService,
  218. GetLastError(),
  219. szSpooler,
  220. NULL,NULL);
  221. }
  222. CloseServiceHandle(hSC);
  223. SetupDebugPrint1( L"StartSpooler: Just closed service handler %s", szSpooler);
  224. return(b);
  225. }