Source code of Windows XP (NT5)
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.

262 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. srvinit.c
  5. Abstract:
  6. This is the main initialization module for the POSIX Subsystem Server
  7. Author:
  8. Mark Lucovsky (markl) 08-Mar-1989
  9. Revision History:
  10. Ellen Aycock-Wright (ellena) 15-Jul-1989 Updated
  11. --*/
  12. #include <time.h>
  13. #include <wchar.h>
  14. #include "psxsrv.h"
  15. void RemoveJunkDirectories(void);
  16. NTSTATUS
  17. PsxServerInitialization(VOID)
  18. {
  19. NTSTATUS Status;
  20. BOOLEAN b;
  21. //
  22. // Use the process heap for memory allocation.
  23. //
  24. PsxHeap = RtlProcessHeap();
  25. //
  26. // Initialize the Session List
  27. //
  28. Status = PsxInitializeNtSessionList();
  29. ASSERT( NT_SUCCESS( Status ) );
  30. //
  31. // Initialize the Process List
  32. //
  33. Status = PsxInitializeProcessStructure();
  34. ASSERT( NT_SUCCESS( Status ) );
  35. //
  36. // Initialize the I/O related functions
  37. //
  38. Status = PsxInitializeIO();
  39. ASSERT(NT_SUCCESS(Status));
  40. //
  41. // Initialize the POSIX Server Console Session Port, the listen thread
  42. // and one request thread.
  43. //
  44. Status = InitConnectingTerminalList();
  45. if (!NT_SUCCESS(Status)) {
  46. return Status;
  47. }
  48. Status = PsxInitializeConsolePort();
  49. ASSERT( NT_SUCCESS( Status ) );
  50. //
  51. // Initialize the POSIX Server API Port, the listen thread and one or more
  52. // request threads.
  53. //
  54. PsxNumberApiRequestThreads = 1;
  55. Status = PsxApiPortInitialize();
  56. ASSERT(NT_SUCCESS(Status));
  57. //
  58. // Initialize the Posix Server Sid cache.
  59. //
  60. InitSidList();
  61. //
  62. // Enable the backup privilege.
  63. //
  64. Status = RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE, TRUE, FALSE, &b);
  65. if (!NT_SUCCESS(Status)) {
  66. KdPrint(("PSXSS: AdjustPrivilege: 0x%x\n", Status));
  67. }
  68. RemoveJunkDirectories();
  69. return Status;
  70. }
  71. NTSTATUS
  72. PsxInitializeIO(VOID)
  73. /*++
  74. Routine Description:
  75. This function initializes all of the Io related functions in PSX.
  76. Arguments:
  77. None.
  78. Return Value:
  79. Status.
  80. --*/
  81. {
  82. LONG i;
  83. NTSTATUS st;
  84. //
  85. // Initialize SystemOpenFileLock and IoNodeHashTableLock
  86. //
  87. st = RtlInitializeCriticalSection(&SystemOpenFileLock);
  88. ASSERT(NT_SUCCESS(st));
  89. st = RtlInitializeCriticalSection(&IoNodeHashTableLock);
  90. ASSERT(NT_SUCCESS(st));
  91. //
  92. // Initialize the IoNodeHashTable
  93. //
  94. for (i = 0; i < IONODEHASHSIZE; i++ ) {
  95. InitializeListHead(&IoNodeHashTable[i]);
  96. }
  97. return (st);
  98. }
  99. void
  100. RemoveJunkDirectories(void)
  101. {
  102. OBJECT_ATTRIBUTES Obj;
  103. NTSTATUS Status;
  104. HANDLE hDir;
  105. UNICODE_STRING U, U2, HardDisk_U;
  106. wchar_t wc;
  107. UCHAR Buf[sizeof(FILE_NAMES_INFORMATION) +
  108. NAME_MAX * sizeof(WCHAR)];
  109. PFILE_NAMES_INFORMATION pNamesInfo = (PVOID)Buf;
  110. FILE_DISPOSITION_INFORMATION Disp;
  111. IO_STATUS_BLOCK Iosb;
  112. HANDLE hFile;
  113. wchar_t buf[512];
  114. wchar_t buf2[512];
  115. RtlInitUnicodeString(&HardDisk_U, L"\\Device\\Harddisk");
  116. //
  117. // If there are "psxjunk" directories in the root of any filesystems,
  118. // we remove them and any files that may happen to be in them...
  119. //
  120. for (wc = L'A'; wc <= L'Z'; wc++) {
  121. swprintf(buf, L"\\DosDevices\\%wc:", wc);
  122. U.Buffer = buf;
  123. U.Length = wcslen(buf) * sizeof(wchar_t);
  124. U.MaximumLength = sizeof(buf);
  125. InitializeObjectAttributes(&Obj, &U, 0, NULL, NULL);
  126. Status = NtOpenSymbolicLinkObject(&hDir, SYMBOLIC_LINK_QUERY,
  127. &Obj);
  128. if (!NT_SUCCESS(Status)) {
  129. continue;
  130. }
  131. U2.Buffer = buf2;
  132. U2.Length = 0;
  133. U2.MaximumLength = sizeof(buf2);
  134. Status = NtQuerySymbolicLinkObject(hDir, &U2, NULL);
  135. NtClose(hDir);
  136. if (!NT_SUCCESS(Status)) {
  137. KdPrint(("PSXSS: NtQuerySymLink: 0x%x\n", Status));
  138. continue;
  139. }
  140. if (!RtlPrefixUnicodeString(&HardDisk_U, &U2, TRUE)) {
  141. // Symlink does not point to hard disk.
  142. continue;
  143. }
  144. Status = RtlAppendUnicodeToString(&U, L"\\");
  145. ASSERT(NT_SUCCESS(Status));
  146. Status = RtlAppendUnicodeToString(&U, PSX_JUNK_DIR);
  147. ASSERT(NT_SUCCESS(Status));
  148. InitializeObjectAttributes(&Obj, &U, 0, NULL, NULL);
  149. Status = NtOpenFile(&hDir, SYNCHRONIZE | DELETE | GENERIC_READ,
  150. &Obj, &Iosb, SHARE_ALL,
  151. FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
  152. if (!NT_SUCCESS(Status)) {
  153. continue;
  154. }
  155. //
  156. // Delete all files in the directory.
  157. //
  158. for (;;) {
  159. Status = NtQueryDirectoryFile(hDir, NULL, NULL, NULL, &Iosb,
  160. &Buf, sizeof(Buf), FileNamesInformation, TRUE, NULL,
  161. FALSE);
  162. if (STATUS_NO_MORE_FILES == Status) {
  163. break;
  164. }
  165. if (!NT_SUCCESS(Status)) {
  166. KdPrint(("NtQueryDirectoryFile: 0x%x\n", Status));
  167. break;
  168. }
  169. U.Length = U.MaximumLength = (USHORT)pNamesInfo->FileNameLength;
  170. U.Buffer = pNamesInfo->FileName;
  171. InitializeObjectAttributes(&Obj, &U, 0, hDir, NULL);
  172. Status = NtOpenFile(&hFile, SYNCHRONIZE | DELETE,
  173. &Obj, &Iosb, SHARE_ALL,
  174. FILE_SYNCHRONOUS_IO_NONALERT);
  175. if (!NT_SUCCESS(Status)) {
  176. KdPrint(("NtOpenFile: %wZ\n", &U));
  177. KdPrint(("NtOpenFile: 0x%x\n", Status));
  178. continue;
  179. }
  180. Disp.DeleteFile = TRUE;
  181. Status = NtSetInformationFile(hFile, &Iosb, &Disp,
  182. sizeof(Disp), FileDispositionInformation);
  183. if (!NT_SUCCESS(Status)) {
  184. KdPrint(("NtSetInfoFile: 0x%x\n", Status));
  185. }
  186. NtClose(hFile);
  187. }
  188. Disp.DeleteFile = TRUE;
  189. Status = NtSetInformationFile(hDir, &Iosb, &Disp,
  190. sizeof(Disp), FileDispositionInformation);
  191. NtClose(hDir);
  192. }
  193. }