00001 /*++ 00002 00003 Copyright (c) 1989 Microsoft Corporation 00004 00005 Module Name: 00006 00007 create.c 00008 00009 Abstract 00010 00011 This module contains the code to implement the NtCreateFile, 00012 the NtCreateNamedPipeFile and the NtCreateMailslotFile system 00013 services. 00014 00015 Author: 00016 00017 Darryl E. Havens (darrylh) 14-Apr-1989 00018 00019 Environment: 00020 00021 Kernel mode 00022 00023 Revision History: 00024 00025 00026 --*/ 00027 00028 #include "iop.h" 00029 00030 #ifdef ALLOC_PRAGMA 00031 #pragma alloc_text(PAGE, NtCreateFile) 00032 #pragma alloc_text(PAGE, NtCreateNamedPipeFile) 00033 #pragma alloc_text(PAGE, NtCreateMailslotFile) 00034 #endif 00035 00036 NTSTATUS 00037 NtCreateFile( 00038 OUT PHANDLE FileHandle, 00039 IN ACCESS_MASK DesiredAccess, 00040 IN POBJECT_ATTRIBUTES ObjectAttributes, 00041 OUT PIO_STATUS_BLOCK IoStatusBlock, 00042 IN PLARGE_INTEGER AllocationSize OPTIONAL, 00043 IN ULONG FileAttributes, 00044 IN ULONG ShareAccess, 00045 IN ULONG CreateDisposition, 00046 IN ULONG CreateOptions, 00047 IN PVOID EaBuffer OPTIONAL, 00048 IN ULONG EaLength 00049 ) 00050 00051 /*++ 00052 00053 Routine Description: 00054 00055 This service opens or creates a file, or opens a device. It is used to 00056 establish a file handle to the open device/file that can then be used 00057 in subsequent operations to perform I/O operations on. For purposes of 00058 readability, files and devices are treated as "files" throughout the 00059 majority of this module and the system service portion of the I/O system. 00060 The only time a distinction is made is when it is important to determine 00061 which is really being accessed. Then a distinction is also made in the 00062 comments. 00063 00064 Arguments: 00065 00066 FileHandle - A pointer to a variable to receive the handle to the open file. 00067 00068 DesiredAccess - Supplies the types of access that the caller would like to 00069 the file. 00070 00071 ObjectAttributes - Supplies the attributes to be used for file object (name, 00072 SECURITY_DESCRIPTOR, etc.) 00073 00074 IoStatusBlock - Specifies the address of the caller's I/O status block. 00075 00076 AllocationSize - Initial size that should be allocated to the file. This 00077 parameter only has an affect if the file is created. Further, if 00078 not specified, then it is taken to mean zero. 00079 00080 FileAttributes - Specifies the attributes that should be set on the file, 00081 if it is created. 00082 00083 ShareAccess - Supplies the types of share access that the caller would like 00084 to the file. 00085 00086 CreateDisposition - Supplies the method for handling the create/open. 00087 00088 CreateOptions - Caller options for how to perform the create/open. 00089 00090 EaBuffer - Optionally specifies a set of EAs to be applied to the file if 00091 it is created. 00092 00093 EaLength - Supplies the length of the EaBuffer. 00094 00095 Return Value: 00096 00097 The function value is the final status of the create/open operation. 00098 00099 --*/ 00100 00101 { 00102 // 00103 // Simply invoke the common I/O file creation routine to do the work. 00104 // 00105 00106 PAGED_CODE(); 00107 00108 return IoCreateFile( FileHandle, 00109 DesiredAccess, 00110 ObjectAttributes, 00111 IoStatusBlock, 00112 AllocationSize, 00113 FileAttributes, 00114 ShareAccess, 00115 CreateDisposition, 00116 CreateOptions, 00117 EaBuffer, 00118 EaLength, 00119 CreateFileTypeNone, 00120 (PVOID)NULL, 00121 0 ); 00122 } 00123 00124 NTSTATUS 00125 NtCreateNamedPipeFile( 00126 OUT PHANDLE FileHandle, 00127 IN ULONG DesiredAccess, 00128 IN POBJECT_ATTRIBUTES ObjectAttributes, 00129 OUT PIO_STATUS_BLOCK IoStatusBlock, 00130 IN ULONG ShareAccess, 00131 IN ULONG CreateDisposition, 00132 IN ULONG CreateOptions, 00133 IN ULONG NamedPipeType, 00134 IN ULONG ReadMode, 00135 IN ULONG CompletionMode, 00136 IN ULONG MaximumInstances, 00137 IN ULONG InboundQuota, 00138 IN ULONG OutboundQuota, 00139 IN PLARGE_INTEGER DefaultTimeout OPTIONAL 00140 ) 00141 00142 /*++ 00143 00144 Routine Description: 00145 00146 Creates and opens the server end handle of the first instance of a 00147 specific named pipe or another instance of an existing named pipe. 00148 00149 Arguments: 00150 00151 FileHandle - Supplies a handle to the file on which the service is being 00152 performed. 00153 00154 DesiredAccess - Supplies the types of access that the caller would like to 00155 the file. 00156 00157 ObjectAttributes - Supplies the attributes to be used for file object 00158 (name, SECURITY_DESCRIPTOR, etc.) 00159 00160 IoStatusBlock - Address of the caller's I/O status block. 00161 00162 ShareAccess - Supplies the types of share access that the caller would 00163 like to the file. 00164 00165 CreateDisposition - Supplies the method for handling the create/open. 00166 00167 CreateOptions - Caller options for how to perform the create/open. 00168 00169 NamedPipeType - Type of named pipe to create (Bitstream or message). 00170 00171 ReadMode - Mode in which to read the pipe (Bitstream or message). 00172 00173 CompletionMode - Specifies how the operation is to be completed. 00174 00175 MaximumInstances - Maximum number of simultaneous instances of the named 00176 pipe. 00177 00178 InboundQuota - Specifies the pool quota that is reserved for writes to the 00179 inbound side of the named pipe. 00180 00181 OutboundQuota - Specifies the pool quota that is reserved for writes to 00182 the inbound side of the named pipe. 00183 00184 DefaultTimeout - Optional pointer to a timeout value that is used if a 00185 timeout value is not specified when waiting for an instance of a named 00186 pipe. 00187 00188 Return Value: 00189 00190 The function value is the final status of the create/open operation. 00191 00192 --*/ 00193 00194 { 00195 NAMED_PIPE_CREATE_PARAMETERS namedPipeCreateParameters; 00196 00197 PAGED_CODE(); 00198 00199 // 00200 // Check whether or not the DefaultTimeout parameter was specified. If 00201 // so, then capture it in the named pipe create parameter structure. 00202 // 00203 00204 if (ARGUMENT_PRESENT( DefaultTimeout )) { 00205 00206 // 00207 // Indicate that a default timeout period was specified. 00208 // 00209 00210 namedPipeCreateParameters.TimeoutSpecified = TRUE; 00211 00212 // 00213 // A default timeout parameter was specified. Check to see whether 00214 // the caller's mode is kernel and if not capture the parameter inside 00215 // of a try...except clause. 00216 // 00217 00218 if (KeGetPreviousMode() != KernelMode) { 00219 try { 00220 ProbeForRead( DefaultTimeout, 00221 sizeof( LARGE_INTEGER ), 00222 sizeof( ULONG ) ); 00223 namedPipeCreateParameters.DefaultTimeout = *DefaultTimeout; 00224 } except(EXCEPTION_EXECUTE_HANDLER) { 00225 00226 // 00227 // Something went awry attempting to access the parameter. 00228 // Get the reason for the error and return it as the status 00229 // value from this service. 00230 // 00231 00232 return GetExceptionCode(); 00233 } 00234 } else { 00235 00236 // 00237 // The caller's mode was kernel so simply store the parameter. 00238 // 00239 00240 namedPipeCreateParameters.DefaultTimeout = *DefaultTimeout; 00241 } 00242 } else { 00243 00244 // 00245 // Indicate that no default timeout period was specified. 00246 // 00247 00248 namedPipeCreateParameters.TimeoutSpecified = FALSE; 00249 } 00250 00251 // 00252 // Store the remainder of the named pipe-specific parameters in the 00253 // structure for use in the call to the common create file routine. 00254 // 00255 00256 namedPipeCreateParameters.NamedPipeType = NamedPipeType; 00257 namedPipeCreateParameters.ReadMode = ReadMode; 00258 namedPipeCreateParameters.CompletionMode = CompletionMode; 00259 namedPipeCreateParameters.MaximumInstances = MaximumInstances; 00260 namedPipeCreateParameters.InboundQuota = InboundQuota; 00261 namedPipeCreateParameters.OutboundQuota = OutboundQuota; 00262 00263 // 00264 // Simply perform the remainder of the service by allowing the common 00265 // file creation code to do the work. 00266 // 00267 00268 return IoCreateFile( FileHandle, 00269 DesiredAccess, 00270 ObjectAttributes, 00271 IoStatusBlock, 00272 (PLARGE_INTEGER) NULL, 00273 0L, 00274 ShareAccess, 00275 CreateDisposition, 00276 CreateOptions, 00277 (PVOID) NULL, 00278 0L, 00279 CreateFileTypeNamedPipe, 00280 &namedPipeCreateParameters, 00281 0 ); 00282 } 00283 00284 NTSTATUS 00285 NtCreateMailslotFile( 00286 OUT PHANDLE FileHandle, 00287 IN ULONG DesiredAccess, 00288 IN POBJECT_ATTRIBUTES ObjectAttributes, 00289 OUT PIO_STATUS_BLOCK IoStatusBlock, 00290 ULONG CreateOptions, 00291 IN ULONG MailslotQuota, 00292 IN ULONG MaximumMessageSize, 00293 IN PLARGE_INTEGER ReadTimeout 00294 ) 00295 00296 /*++ 00297 00298 Routine Description: 00299 00300 Creates and opens the server end handle of a mailslot file. 00301 00302 Arguments: 00303 00304 FileHandle - Supplies a handle to the file on which the service is being 00305 performed. 00306 00307 DesiredAccess - Supplies the types of access that the caller would like to 00308 the file. 00309 00310 ObjectAttributes - Supplies the attributes to be used for file object 00311 (name, SECURITY_DESCRIPTOR, etc.) 00312 00313 IoStatusBlock - Address of the caller's I/O status block. 00314 00315 CreateOptions - Caller options for how to perform the create/open. 00316 00317 MailslotQuota - Specifies the pool quota that is reserved for writes 00318 to this mailslot. 00319 00320 MaximumMessageSize - Specifies the size of the largest message that 00321 can be written to this mailslot. 00322 00323 ReadTimeout - The timeout period for a read operation. This must 00324 be specified as a relative time. 00325 00326 Return Value: 00327 00328 The function value is the final status of the create operation. 00329 00330 --*/ 00331 00332 { 00333 MAILSLOT_CREATE_PARAMETERS mailslotCreateParameters; 00334 00335 PAGED_CODE(); 00336 00337 // 00338 // Check whether or not the DefaultTimeout parameter was specified. If 00339 // so, then capture it in the mailslot create parameter structure. 00340 // 00341 00342 if (ARGUMENT_PRESENT( ReadTimeout )) { 00343 00344 // 00345 // Indicate that a read timeout period was specified. 00346 // 00347 00348 mailslotCreateParameters.TimeoutSpecified = TRUE; 00349 00350 // 00351 // A read timeout parameter was specified. Check to see whether 00352 // the caller's mode is kernel and if not capture the parameter inside 00353 // of a try...except clause. 00354 // 00355 00356 if (KeGetPreviousMode() != KernelMode) { 00357 try { 00358 ProbeForRead( ReadTimeout, 00359 sizeof( LARGE_INTEGER ), 00360 sizeof( ULONG ) ); 00361 mailslotCreateParameters.ReadTimeout = *ReadTimeout; 00362 } except(EXCEPTION_EXECUTE_HANDLER) { 00363 00364 // 00365 // Something went awry attempting to access the parameter. 00366 // Get the reason for the error and return it as the status 00367 // value from this service. 00368 // 00369 00370 return GetExceptionCode(); 00371 } 00372 } else { 00373 00374 // 00375 // The caller's mode was kernel so simply store the parameter. 00376 // 00377 00378 mailslotCreateParameters.ReadTimeout = *ReadTimeout; 00379 } 00380 } else { 00381 00382 // 00383 // Indicate that no default timeout period was specified. 00384 // 00385 00386 mailslotCreateParameters.TimeoutSpecified = FALSE; 00387 } 00388 00389 // 00390 // Store the mailslot-specific parameters in the structure for use 00391 // in the call to the common create file routine. 00392 // 00393 00394 mailslotCreateParameters.MailslotQuota = MailslotQuota; 00395 mailslotCreateParameters.MaximumMessageSize = MaximumMessageSize; 00396 00397 // 00398 // Simply perform the remainder of the service by allowing the common 00399 // file creation code to do the work. 00400 // 00401 00402 return IoCreateFile( FileHandle, 00403 DesiredAccess, 00404 ObjectAttributes, 00405 IoStatusBlock, 00406 (PLARGE_INTEGER) NULL, 00407 0L, 00408 FILE_SHARE_READ | FILE_SHARE_WRITE, 00409 FILE_CREATE, 00410 CreateOptions, 00411 (PVOID) NULL, 00412 0L, 00413 CreateFileTypeMailslot, 00414 &mailslotCreateParameters, 00415 0 ); 00416 }