1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/**
* WinPR: Windows Portable Runtime
* Asynchronous I/O Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/io.h>
#ifndef _WIN32
#include "io.h"
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WINPR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/file.h>
/**
* I/O Manager Routines
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff551797/
*
* These routines are only accessible to kernel drivers, but we need
* similar functionality in WinPR in user space.
*
* This is a best effort non-conflicting port of this API meant for
* non-Windows, WinPR usage only.
*
* References:
*
* Device Objects and Device Stacks:
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff543153/
*
* Driver Development Part 1: Introduction to Drivers:
* http://www.codeproject.com/Articles/9504/Driver-Development-Part-1-Introduction-to-Drivers/
*/
#define DEVICE_FILE_PREFIX_PATH "\\Device\\"
static char* GetDeviceFileNameWithoutPrefixA(LPCSTR lpName)
{
char* lpFileName = NULL;
if (!lpName)
return NULL;
if (strncmp(lpName, DEVICE_FILE_PREFIX_PATH, sizeof(DEVICE_FILE_PREFIX_PATH) - 1) != 0)
return NULL;
lpFileName =
_strdup(&lpName[strnlen(DEVICE_FILE_PREFIX_PATH, sizeof(DEVICE_FILE_PREFIX_PATH))]);
return lpFileName;
}
static char* GetDeviceFileUnixDomainSocketBaseFilePathA(void)
{
char* lpTempPath = NULL;
char* lpPipePath = NULL;
lpTempPath = GetKnownPath(KNOWN_PATH_TEMP);
if (!lpTempPath)
return NULL;
lpPipePath = GetCombinedPath(lpTempPath, ".device");
free(lpTempPath);
return lpPipePath;
}
static char* GetDeviceFileUnixDomainSocketFilePathA(LPCSTR lpName)
{
char* lpPipePath = NULL;
char* lpFileName = NULL;
char* lpFilePath = NULL;
lpPipePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
if (!lpPipePath)
return NULL;
lpFileName = GetDeviceFileNameWithoutPrefixA(lpName);
if (!lpFileName)
{
free(lpPipePath);
return NULL;
}
lpFilePath = GetCombinedPath(lpPipePath, (char*)lpFileName);
free(lpPipePath);
free(lpFileName);
return lpFilePath;
}
/**
* IoCreateDevice:
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff548397/
*/
NTSTATUS _IoCreateDeviceEx(PDRIVER_OBJECT_EX DriverObject, ULONG DeviceExtensionSize,
PUNICODE_STRING DeviceName, DEVICE_TYPE DeviceType,
ULONG DeviceCharacteristics, BOOLEAN Exclusive,
PDEVICE_OBJECT_EX* DeviceObject)
{
int status = 0;
char* DeviceBasePath = NULL;
DEVICE_OBJECT_EX* pDeviceObjectEx = NULL;
DeviceBasePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
if (!DeviceBasePath)
return STATUS_NO_MEMORY;
if (!winpr_PathFileExists(DeviceBasePath))
{
if (mkdir(DeviceBasePath, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
{
free(DeviceBasePath);
return STATUS_ACCESS_DENIED;
}
}
free(DeviceBasePath);
pDeviceObjectEx = (DEVICE_OBJECT_EX*)calloc(1, sizeof(DEVICE_OBJECT_EX));
if (!pDeviceObjectEx)
return STATUS_NO_MEMORY;
pDeviceObjectEx->DeviceName =
ConvertWCharNToUtf8Alloc(DeviceName->Buffer, DeviceName->Length / sizeof(WCHAR), NULL);
if (!pDeviceObjectEx->DeviceName)
{
free(pDeviceObjectEx);
return STATUS_NO_MEMORY;
}
pDeviceObjectEx->DeviceFileName =
GetDeviceFileUnixDomainSocketFilePathA(pDeviceObjectEx->DeviceName);
if (!pDeviceObjectEx->DeviceFileName)
{
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx);
return STATUS_NO_MEMORY;
}
if (winpr_PathFileExists(pDeviceObjectEx->DeviceFileName))
{
if (unlink(pDeviceObjectEx->DeviceFileName) == -1)
{
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx);
return STATUS_ACCESS_DENIED;
}
}
status = mkfifo(pDeviceObjectEx->DeviceFileName, 0666);
if (status != 0)
{
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx);
switch (errno)
{
case EACCES:
return STATUS_ACCESS_DENIED;
case EEXIST:
return STATUS_OBJECT_NAME_EXISTS;
case ENAMETOOLONG:
return STATUS_NAME_TOO_LONG;
case ENOENT:
case ENOTDIR:
return STATUS_NOT_A_DIRECTORY;
case ENOSPC:
return STATUS_DISK_FULL;
default:
return STATUS_INTERNAL_ERROR;
}
}
*((ULONG_PTR*)(DeviceObject)) = (ULONG_PTR)pDeviceObjectEx;
return STATUS_SUCCESS;
}
/**
* IoDeleteDevice:
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff549083/
*/
VOID _IoDeleteDeviceEx(PDEVICE_OBJECT_EX DeviceObject)
{
DEVICE_OBJECT_EX* pDeviceObjectEx = NULL;
pDeviceObjectEx = (DEVICE_OBJECT_EX*)DeviceObject;
if (!pDeviceObjectEx)
return;
unlink(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx->DeviceName);
free(pDeviceObjectEx->DeviceFileName);
free(pDeviceObjectEx);
}
#endif
|