ReactOS  0.4.14-dev-606-g14ebc0b
deviceio.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: dll/win32/kernel32/client/file/deviceio.c
5  * PURPOSE: Device I/O Base Client Functionality
6  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include <k32.h>
12 
13 #include <ntddbeep.h>
14 
15 #define NDEBUG
16 #include <debug.h>
17 
18 /* FUNCTIONS ******************************************************************/
19 
20 VOID
21 WINAPI
23 {
24  BASE_API_MESSAGE ApiMessage;
25  PBASE_SOUND_SENTRY SoundSentryRequest = &ApiMessage.Data.SoundSentryRequest;
26 
27  /* Get the video mode */
28  if (!GetConsoleDisplayMode(&SoundSentryRequest->VideoMode))
29  {
30  SoundSentryRequest->VideoMode = 0;
31  }
32 
33  /* Make sure it's not fullscreen, and send the message if not */
34  if (SoundSentryRequest->VideoMode == 0)
35  {
37  NULL,
39  sizeof(*SoundSentryRequest));
40  }
41 }
42 
43 /*
44  * @implemented
45  */
46 BOOL
47 WINAPI
48 Beep(IN DWORD dwFreq,
49  IN DWORD dwDuration)
50 {
51  HANDLE hBeep;
52  UNICODE_STRING BeepDevice;
55  BEEP_SET_PARAMETERS BeepSetParameters;
57 
58  //
59  // On TS systems, we need to Load Winsta.dll and call WinstationBeepOpen
60  // after doing a GetProcAddress for it
61  //
62 
63  /* Open the device */
64  RtlInitUnicodeString(&BeepDevice, L"\\Device\\Beep");
70  NULL,
71  0,
74  0,
75  NULL,
76  0);
77  if (!NT_SUCCESS(Status))
78  {
80  return FALSE;
81  }
82 
83  /* check the parameters */
84  if ((dwFreq >= 0x25 && dwFreq <= 0x7FFF) ||
85  (dwFreq == 0x0 && dwDuration == 0x0))
86  {
87  /* Set beep data */
88  BeepSetParameters.Frequency = dwFreq;
89  BeepSetParameters.Duration = dwDuration;
90 
91  /* Send the beep */
93  NULL,
94  NULL,
95  NULL,
98  &BeepSetParameters,
99  sizeof(BeepSetParameters),
100  NULL,
101  0);
102  }
103  else
104  {
105  /* We'll fail the call, but still notify the sound sentry */
107  }
108 
109  /* Notify the sound sentry */
111 
112  /* Bail out if the hardware beep failed */
113  if (!NT_SUCCESS(Status))
114  {
115  NtClose(hBeep);
117  return FALSE;
118  }
119 
120  /* If an actual beep was emitted, wait for it */
121  if (((dwFreq != 0x0) || (dwDuration != 0x0)) && (dwDuration != MAXDWORD))
122  {
123  SleepEx(dwDuration, TRUE);
124  }
125 
126  /* Close the handle and return success */
127  NtClose(hBeep);
128  return TRUE;
129 }
130 
131 /*
132  * @implemented
133  */
134 BOOL
135 WINAPI
138  IN LPVOID lpInBuffer OPTIONAL,
139  IN DWORD nInBufferSize OPTIONAL,
140  OUT LPVOID lpOutBuffer OPTIONAL,
141  IN DWORD nOutBufferSize OPTIONAL,
144 {
145  BOOL FsIoCtl;
149 
150  //
151  // Note: on a TS Machine, we should call IsTSAppCompatEnabled and unless the
152  // IOCTLs are IOCTL_STORAGE_EJECT_MEDIA, IOCTL_DISK_EJECT_MEDIA, FSCTL_DISMOUNT_VOLUME
153  // we should call IsCallerAdminOrSystem and return STATUS_ACCESS_DENIED for
154  // any other IOCTLs.
155  //
156 
157  /* Check what kind of IOCTL to send */
158  FsIoCtl = ((dwIoControlCode >> 16) == FILE_DEVICE_FILE_SYSTEM);
159 
160  /* CHeck for async */
161  if (lpOverlapped != NULL)
162  {
163  /* Set pending status */
164  lpOverlapped->Internal = STATUS_PENDING;
165 
166  /* Check if there's an APC context */
167  ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
168 
169  /* Send file system control? */
170  if (FsIoCtl)
171  {
172  /* Send it */
173  Status = NtFsControlFile(hDevice,
174  lpOverlapped->hEvent,
175  NULL,
176  ApcContext,
179  lpInBuffer,
180  nInBufferSize,
181  lpOutBuffer,
182  nOutBufferSize);
183  }
184  else
185  {
186  /* Otherwise send a device control */
187  Status = NtDeviceIoControlFile(hDevice,
188  lpOverlapped->hEvent,
189  NULL,
190  ApcContext,
193  lpInBuffer,
194  nInBufferSize,
195  lpOutBuffer,
196  nOutBufferSize);
197  }
198 
199  /* Check for or information instead of failure */
200  if (!(NT_ERROR(Status)) && (lpBytesReturned))
201  {
202  /* Protect with SEH */
203  _SEH2_TRY
204  {
205  /* Return the bytes */
206  *lpBytesReturned = lpOverlapped->InternalHigh;
207  }
209  {
210  /* Return zero bytes */
211  *lpBytesReturned = 0;
212  }
213  _SEH2_END;
214  }
215 
216  /* Now check for any kind of failure except pending*/
217  if (!(NT_SUCCESS(Status)) || (Status == STATUS_PENDING))
218  {
219  /* Fail */
221  return FALSE;
222  }
223  }
224  else
225  {
226  /* Sync case -- send file system code? */
227  if (FsIoCtl)
228  {
229  /* Do it */
230  Status = NtFsControlFile(hDevice,
231  NULL,
232  NULL,
233  NULL,
234  &Iosb,
236  lpInBuffer,
237  nInBufferSize,
238  lpOutBuffer,
239  nOutBufferSize);
240  }
241  else
242  {
243  /* Send device code instead */
244  Status = NtDeviceIoControlFile(hDevice,
245  NULL,
246  NULL,
247  NULL,
248  &Iosb,
250  lpInBuffer,
251  nInBufferSize,
252  lpOutBuffer,
253  nOutBufferSize);
254  }
255 
256  /* Now check if the operation isn't done yet */
257  if (Status == STATUS_PENDING)
258  {
259  /* Wait for it and get the final status */
260  Status = NtWaitForSingleObject(hDevice, FALSE, NULL);
261  if (NT_SUCCESS(Status)) Status = Iosb.Status;
262  }
263 
264  /* Check for success */
265  if (NT_SUCCESS(Status))
266  {
267  /* Return the byte count */
268  *lpBytesReturned = Iosb.Information;
269  }
270  else
271  {
272  /* Check for informational or warning failure */
273  if (!NT_ERROR(Status)) *lpBytesReturned = Iosb.Information;
274 
275  /* Return a failure */
277  return FALSE;
278  }
279  }
280 
281  /* Return success */
282  return TRUE;
283 }
284 
285 /*
286  * @implemented
287  */
288 BOOL
289 WINAPI
291 {
294 
296  if (!NT_SUCCESS(Status))
297  {
299  return FALSE;
300  }
301 
302  return TRUE;
303 }
304 
305 /* EOF */
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define IN
Definition: typedefs.h:38
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE _In_opt_ PVOID ApcContext
Definition: iofuncs.h:719
#define TRUE
Definition: types.h:120
VOID WINAPI NotifySoundSentry(VOID)
Definition: deviceio.c:22
#define FILE_OPEN_IF
Definition: from_kernel.h:56
BOOL WINAPI GetConsoleDisplayMode(LPDWORD lpModeFlags)
Definition: console.c:526
GLint x0
Definition: linetemp.h:95
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
NTSTATUS NTAPI CsrClientCallServer(IN OUT PCSR_API_MESSAGE ApiMessage, IN OUT PCSR_CAPTURE_BUFFER CaptureBuffer OPTIONAL, IN CSR_API_NUMBER ApiNumber, IN ULONG DataLength)
Definition: connect.c:365
#define FILE_DEVICE_FILE_SYSTEM
Definition: winioctl.h:114
_In_ CLIPOBJ _In_ BRUSHOBJ _In_ LONG x1
Definition: winddi.h:3706
LONG NTSTATUS
Definition: precomp.h:26
NTSYSCALLAPI NTSTATUS NTAPI NtFsControlFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, ULONG FsControlCode, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength)
NTSYSAPI NTSTATUS NTAPI NtDeviceIoControlFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG DeviceIoControlCode, IN PVOID InBuffer OPTIONAL, IN ULONG InBufferLength, OUT PVOID OutBuffer OPTIONAL, IN ULONG OutBufferLength)
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_READ
Definition: compat.h:125
_SEH2_TRY
Definition: create.c:4250
unsigned int BOOL
Definition: ntddk_ex.h:94
#define FILE_READ_DATA
Definition: nt_native.h:628
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
BOOL WINAPI CancelIo(IN HANDLE hFile)
Definition: deviceio.c:290
smooth NULL
Definition: ftsmooth.c:416
#define MAXDWORD
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define FILE_WRITE_DATA
Definition: nt_native.h:631
#define CSR_CREATE_API_NUMBER(ServerId, ApiId)
Definition: csrmsg.h:37
DWORD WINAPI SleepEx(IN DWORD dwMilliseconds, IN BOOL bAlertable)
Definition: synch.c:802
union _BASE_API_MESSAGE::@3411 Data
NTSTATUS NTAPI NtCreateFile(OUT PHANDLE FileHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK IoStatusBlock, IN PLARGE_INTEGER AllocationSize OPTIONAL, IN ULONG FileAttributes, IN ULONG ShareAccess, IN ULONG CreateDisposition, IN ULONG CreateOptions, IN PVOID EaBuffer OPTIONAL, IN ULONG EaLength)
#define BASESRV_SERVERDLL_INDEX
Definition: basemsg.h:15
return Iosb
Definition: create.c:4426
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
#define WINAPI
Definition: msvc.h:6
unsigned long DWORD
Definition: ntddk_ex.h:95
#define NT_ERROR(Status)
Definition: umtypes.h:106
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
static const WCHAR L[]
Definition: oid.c:1250
BASE_SOUND_SENTRY SoundSentryRequest
Definition: basemsg.h:298
_In_ HANDLE hFile
Definition: mswsock.h:90
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
BOOL WINAPI DeviceIoControl(IN HANDLE hDevice, IN DWORD dwIoControlCode, IN LPVOID lpInBuffer OPTIONAL, IN DWORD nInBufferSize OPTIONAL, OUT LPVOID lpOutBuffer OPTIONAL, IN DWORD nOutBufferSize OPTIONAL, OUT LPDWORD lpBytesReturned OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: deviceio.c:136
_In_ HANDLE _In_ DWORD _In_ DWORD _Inout_opt_ LPOVERLAPPED lpOverlapped
Definition: mswsock.h:90
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
_In_ DWORD _In_ DWORD _In_ DWORD _Out_ LPDWORD lpBytesReturned
Definition: winddi.h:1704
static HANDLE hBeep
Definition: speaker.c:27
#define OUT
Definition: typedefs.h:39
uint32_t * LPDWORD
Definition: typedefs.h:57
NTSTATUS NTAPI NtCancelIoFile(IN HANDLE FileHandle, OUT PIO_STATUS_BLOCK IoStatusBlock)
Definition: file.c:4018
BOOL WINAPI Beep(IN DWORD dwFreq, IN DWORD dwDuration)
Definition: deviceio.c:48
#define IOCTL_BEEP_SET
Definition: ntddbeep.h:31
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define ULONG_PTR
Definition: config.h:101
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
_In_ DWORD dwIoControlCode
Definition: winddi.h:1700
NTSYSAPI NTSTATUS NTAPI NtWaitForSingleObject(IN HANDLE hObject, IN BOOLEAN bAlertable, IN PLARGE_INTEGER Timeout)