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.

416 lines
12 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. create.c
  5. Abstract
  6. This module contains the code to implement the NtCreateFile,
  7. the NtCreateNamedPipeFile and the NtCreateMailslotFile system
  8. services.
  9. Author:
  10. Darryl E. Havens (darrylh) 14-Apr-1989
  11. Environment:
  12. Kernel mode
  13. Revision History:
  14. --*/
  15. #include "iomgr.h"
  16. #ifdef ALLOC_PRAGMA
  17. #pragma alloc_text(PAGE, NtCreateFile)
  18. #pragma alloc_text(PAGE, NtCreateNamedPipeFile)
  19. #pragma alloc_text(PAGE, NtCreateMailslotFile)
  20. #endif
  21. NTSTATUS
  22. NtCreateFile(
  23. OUT PHANDLE FileHandle,
  24. IN ACCESS_MASK DesiredAccess,
  25. IN POBJECT_ATTRIBUTES ObjectAttributes,
  26. OUT PIO_STATUS_BLOCK IoStatusBlock,
  27. IN PLARGE_INTEGER AllocationSize OPTIONAL,
  28. IN ULONG FileAttributes,
  29. IN ULONG ShareAccess,
  30. IN ULONG CreateDisposition,
  31. IN ULONG CreateOptions,
  32. IN PVOID EaBuffer OPTIONAL,
  33. IN ULONG EaLength
  34. )
  35. /*++
  36. Routine Description:
  37. This service opens or creates a file, or opens a device. It is used to
  38. establish a file handle to the open device/file that can then be used
  39. in subsequent operations to perform I/O operations on. For purposes of
  40. readability, files and devices are treated as "files" throughout the
  41. majority of this module and the system service portion of the I/O system.
  42. The only time a distinction is made is when it is important to determine
  43. which is really being accessed. Then a distinction is also made in the
  44. comments.
  45. Arguments:
  46. FileHandle - A pointer to a variable to receive the handle to the open file.
  47. DesiredAccess - Supplies the types of access that the caller would like to
  48. the file.
  49. ObjectAttributes - Supplies the attributes to be used for file object (name,
  50. SECURITY_DESCRIPTOR, etc.)
  51. IoStatusBlock - Specifies the address of the caller's I/O status block.
  52. AllocationSize - Initial size that should be allocated to the file. This
  53. parameter only has an affect if the file is created. Further, if
  54. not specified, then it is taken to mean zero.
  55. FileAttributes - Specifies the attributes that should be set on the file,
  56. if it is created.
  57. ShareAccess - Supplies the types of share access that the caller would like
  58. to the file.
  59. CreateDisposition - Supplies the method for handling the create/open.
  60. CreateOptions - Caller options for how to perform the create/open.
  61. EaBuffer - Optionally specifies a set of EAs to be applied to the file if
  62. it is created.
  63. EaLength - Supplies the length of the EaBuffer.
  64. Return Value:
  65. The function value is the final status of the create/open operation.
  66. --*/
  67. {
  68. //
  69. // Simply invoke the common I/O file creation routine to do the work.
  70. //
  71. PAGED_CODE();
  72. return IoCreateFile( FileHandle,
  73. DesiredAccess,
  74. ObjectAttributes,
  75. IoStatusBlock,
  76. AllocationSize,
  77. FileAttributes,
  78. ShareAccess,
  79. CreateDisposition,
  80. CreateOptions,
  81. EaBuffer,
  82. EaLength,
  83. CreateFileTypeNone,
  84. (PVOID)NULL,
  85. 0 );
  86. }
  87. NTSTATUS
  88. NtCreateNamedPipeFile(
  89. OUT PHANDLE FileHandle,
  90. IN ULONG DesiredAccess,
  91. IN POBJECT_ATTRIBUTES ObjectAttributes,
  92. OUT PIO_STATUS_BLOCK IoStatusBlock,
  93. IN ULONG ShareAccess,
  94. IN ULONG CreateDisposition,
  95. IN ULONG CreateOptions,
  96. IN ULONG NamedPipeType,
  97. IN ULONG ReadMode,
  98. IN ULONG CompletionMode,
  99. IN ULONG MaximumInstances,
  100. IN ULONG InboundQuota,
  101. IN ULONG OutboundQuota,
  102. IN PLARGE_INTEGER DefaultTimeout OPTIONAL
  103. )
  104. /*++
  105. Routine Description:
  106. Creates and opens the server end handle of the first instance of a
  107. specific named pipe or another instance of an existing named pipe.
  108. Arguments:
  109. FileHandle - Supplies a handle to the file on which the service is being
  110. performed.
  111. DesiredAccess - Supplies the types of access that the caller would like to
  112. the file.
  113. ObjectAttributes - Supplies the attributes to be used for file object
  114. (name, SECURITY_DESCRIPTOR, etc.)
  115. IoStatusBlock - Address of the caller's I/O status block.
  116. ShareAccess - Supplies the types of share access that the caller would
  117. like to the file.
  118. CreateDisposition - Supplies the method for handling the create/open.
  119. CreateOptions - Caller options for how to perform the create/open.
  120. NamedPipeType - Type of named pipe to create (Bitstream or message).
  121. ReadMode - Mode in which to read the pipe (Bitstream or message).
  122. CompletionMode - Specifies how the operation is to be completed.
  123. MaximumInstances - Maximum number of simultaneous instances of the named
  124. pipe.
  125. InboundQuota - Specifies the pool quota that is reserved for writes to the
  126. inbound side of the named pipe.
  127. OutboundQuota - Specifies the pool quota that is reserved for writes to
  128. the inbound side of the named pipe.
  129. DefaultTimeout - Optional pointer to a timeout value that is used if a
  130. timeout value is not specified when waiting for an instance of a named
  131. pipe.
  132. Return Value:
  133. The function value is the final status of the create/open operation.
  134. --*/
  135. {
  136. NAMED_PIPE_CREATE_PARAMETERS namedPipeCreateParameters;
  137. PAGED_CODE();
  138. //
  139. // Check whether or not the DefaultTimeout parameter was specified. If
  140. // so, then capture it in the named pipe create parameter structure.
  141. //
  142. if (ARGUMENT_PRESENT( DefaultTimeout )) {
  143. //
  144. // Indicate that a default timeout period was specified.
  145. //
  146. namedPipeCreateParameters.TimeoutSpecified = TRUE;
  147. //
  148. // A default timeout parameter was specified. Check to see whether
  149. // the caller's mode is kernel and if not capture the parameter inside
  150. // of a try...except clause.
  151. //
  152. if (KeGetPreviousMode() != KernelMode) {
  153. try {
  154. ProbeForReadSmallStructure ( DefaultTimeout,
  155. sizeof( LARGE_INTEGER ),
  156. sizeof( ULONG ) );
  157. namedPipeCreateParameters.DefaultTimeout = *DefaultTimeout;
  158. } except(EXCEPTION_EXECUTE_HANDLER) {
  159. //
  160. // Something went awry attempting to access the parameter.
  161. // Get the reason for the error and return it as the status
  162. // value from this service.
  163. //
  164. return GetExceptionCode();
  165. }
  166. } else {
  167. //
  168. // The caller's mode was kernel so simply store the parameter.
  169. //
  170. namedPipeCreateParameters.DefaultTimeout = *DefaultTimeout;
  171. }
  172. } else {
  173. //
  174. // Indicate that no default timeout period was specified.
  175. //
  176. namedPipeCreateParameters.TimeoutSpecified = FALSE;
  177. }
  178. //
  179. // Store the remainder of the named pipe-specific parameters in the
  180. // structure for use in the call to the common create file routine.
  181. //
  182. namedPipeCreateParameters.NamedPipeType = NamedPipeType;
  183. namedPipeCreateParameters.ReadMode = ReadMode;
  184. namedPipeCreateParameters.CompletionMode = CompletionMode;
  185. namedPipeCreateParameters.MaximumInstances = MaximumInstances;
  186. namedPipeCreateParameters.InboundQuota = InboundQuota;
  187. namedPipeCreateParameters.OutboundQuota = OutboundQuota;
  188. //
  189. // Simply perform the remainder of the service by allowing the common
  190. // file creation code to do the work.
  191. //
  192. return IoCreateFile( FileHandle,
  193. DesiredAccess,
  194. ObjectAttributes,
  195. IoStatusBlock,
  196. (PLARGE_INTEGER) NULL,
  197. 0L,
  198. ShareAccess,
  199. CreateDisposition,
  200. CreateOptions,
  201. (PVOID) NULL,
  202. 0L,
  203. CreateFileTypeNamedPipe,
  204. &namedPipeCreateParameters,
  205. 0 );
  206. }
  207. NTSTATUS
  208. NtCreateMailslotFile(
  209. OUT PHANDLE FileHandle,
  210. IN ULONG DesiredAccess,
  211. IN POBJECT_ATTRIBUTES ObjectAttributes,
  212. OUT PIO_STATUS_BLOCK IoStatusBlock,
  213. ULONG CreateOptions,
  214. IN ULONG MailslotQuota,
  215. IN ULONG MaximumMessageSize,
  216. IN PLARGE_INTEGER ReadTimeout
  217. )
  218. /*++
  219. Routine Description:
  220. Creates and opens the server end handle of a mailslot file.
  221. Arguments:
  222. FileHandle - Supplies a handle to the file on which the service is being
  223. performed.
  224. DesiredAccess - Supplies the types of access that the caller would like to
  225. the file.
  226. ObjectAttributes - Supplies the attributes to be used for file object
  227. (name, SECURITY_DESCRIPTOR, etc.)
  228. IoStatusBlock - Address of the caller's I/O status block.
  229. CreateOptions - Caller options for how to perform the create/open.
  230. MailslotQuota - Specifies the pool quota that is reserved for writes
  231. to this mailslot.
  232. MaximumMessageSize - Specifies the size of the largest message that
  233. can be written to this mailslot.
  234. ReadTimeout - The timeout period for a read operation. This must
  235. be specified as a relative time.
  236. Return Value:
  237. The function value is the final status of the create operation.
  238. --*/
  239. {
  240. MAILSLOT_CREATE_PARAMETERS mailslotCreateParameters;
  241. PAGED_CODE();
  242. //
  243. // Check whether or not the DefaultTimeout parameter was specified. If
  244. // so, then capture it in the mailslot create parameter structure.
  245. //
  246. if (ARGUMENT_PRESENT( ReadTimeout )) {
  247. //
  248. // Indicate that a read timeout period was specified.
  249. //
  250. mailslotCreateParameters.TimeoutSpecified = TRUE;
  251. //
  252. // A read timeout parameter was specified. Check to see whether
  253. // the caller's mode is kernel and if not capture the parameter inside
  254. // of a try...except clause.
  255. //
  256. if (KeGetPreviousMode() != KernelMode) {
  257. try {
  258. ProbeForReadSmallStructure( ReadTimeout,
  259. sizeof( LARGE_INTEGER ),
  260. sizeof( ULONG ) );
  261. mailslotCreateParameters.ReadTimeout = *ReadTimeout;
  262. } except(EXCEPTION_EXECUTE_HANDLER) {
  263. //
  264. // Something went awry attempting to access the parameter.
  265. // Get the reason for the error and return it as the status
  266. // value from this service.
  267. //
  268. return GetExceptionCode();
  269. }
  270. } else {
  271. //
  272. // The caller's mode was kernel so simply store the parameter.
  273. //
  274. mailslotCreateParameters.ReadTimeout = *ReadTimeout;
  275. }
  276. } else {
  277. //
  278. // Indicate that no default timeout period was specified.
  279. //
  280. mailslotCreateParameters.TimeoutSpecified = FALSE;
  281. }
  282. //
  283. // Store the mailslot-specific parameters in the structure for use
  284. // in the call to the common create file routine.
  285. //
  286. mailslotCreateParameters.MailslotQuota = MailslotQuota;
  287. mailslotCreateParameters.MaximumMessageSize = MaximumMessageSize;
  288. //
  289. // Simply perform the remainder of the service by allowing the common
  290. // file creation code to do the work.
  291. //
  292. return IoCreateFile( FileHandle,
  293. DesiredAccess,
  294. ObjectAttributes,
  295. IoStatusBlock,
  296. (PLARGE_INTEGER) NULL,
  297. 0L,
  298. FILE_SHARE_READ | FILE_SHARE_WRITE,
  299. FILE_CREATE,
  300. CreateOptions,
  301. (PVOID) NULL,
  302. 0L,
  303. CreateFileTypeMailslot,
  304. &mailslotCreateParameters,
  305. 0 );
  306. }