20 #include <winpr/config.h>
33 #ifdef WINPR_HAVE_UNISTD_H
37 #include <sys/types.h>
40 #include <winpr/crt.h>
41 #include <winpr/path.h>
42 #include <winpr/file.h>
63 #define DEVICE_FILE_PREFIX_PATH "\\Device\\"
65 static char* GetDeviceFileNameWithoutPrefixA(LPCSTR lpName)
67 char* lpFileName = NULL;
72 if (strncmp(lpName, DEVICE_FILE_PREFIX_PATH,
sizeof(DEVICE_FILE_PREFIX_PATH) - 1) != 0)
76 _strdup(&lpName[strnlen(DEVICE_FILE_PREFIX_PATH,
sizeof(DEVICE_FILE_PREFIX_PATH))]);
80 static char* GetDeviceFileUnixDomainSocketBaseFilePathA(
void)
82 char* lpTempPath = NULL;
83 char* lpPipePath = NULL;
84 lpTempPath = GetKnownPath(KNOWN_PATH_TEMP);
89 lpPipePath = GetCombinedPath(lpTempPath,
".device");
94 static char* GetDeviceFileUnixDomainSocketFilePathA(LPCSTR lpName)
96 char* lpPipePath = NULL;
97 char* lpFileName = NULL;
98 char* lpFilePath = NULL;
99 lpPipePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
104 lpFileName = GetDeviceFileNameWithoutPrefixA(lpName);
112 lpFilePath = GetCombinedPath(lpPipePath, lpFileName);
123 NTSTATUS _IoCreateDeviceEx(PDRIVER_OBJECT_EX DriverObject, ULONG DeviceExtensionSize,
125 ULONG DeviceCharacteristics, BOOLEAN Exclusive,
126 PDEVICE_OBJECT_EX* DeviceObject)
129 char* DeviceBasePath = NULL;
131 DeviceBasePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
134 return STATUS_NO_MEMORY;
136 if (!winpr_PathFileExists(DeviceBasePath))
138 if (mkdir(DeviceBasePath, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
140 free(DeviceBasePath);
141 return STATUS_ACCESS_DENIED;
145 free(DeviceBasePath);
148 if (!pDeviceObjectEx)
149 return STATUS_NO_MEMORY;
151 pDeviceObjectEx->DeviceName =
152 ConvertWCharNToUtf8Alloc(DeviceName->Buffer, DeviceName->Length /
sizeof(WCHAR), NULL);
153 if (!pDeviceObjectEx->DeviceName)
155 free(pDeviceObjectEx);
156 return STATUS_NO_MEMORY;
159 pDeviceObjectEx->DeviceFileName =
160 GetDeviceFileUnixDomainSocketFilePathA(pDeviceObjectEx->DeviceName);
162 if (!pDeviceObjectEx->DeviceFileName)
164 free(pDeviceObjectEx->DeviceName);
165 free(pDeviceObjectEx);
166 return STATUS_NO_MEMORY;
169 if (winpr_PathFileExists(pDeviceObjectEx->DeviceFileName))
171 if (unlink(pDeviceObjectEx->DeviceFileName) == -1)
173 free(pDeviceObjectEx->DeviceName);
174 free(pDeviceObjectEx->DeviceFileName);
175 free(pDeviceObjectEx);
176 return STATUS_ACCESS_DENIED;
180 status = mkfifo(pDeviceObjectEx->DeviceFileName, 0666);
184 free(pDeviceObjectEx->DeviceName);
185 free(pDeviceObjectEx->DeviceFileName);
186 free(pDeviceObjectEx);
191 return STATUS_ACCESS_DENIED;
194 return STATUS_OBJECT_NAME_EXISTS;
197 return STATUS_NAME_TOO_LONG;
201 return STATUS_NOT_A_DIRECTORY;
204 return STATUS_DISK_FULL;
207 return STATUS_INTERNAL_ERROR;
211 *((ULONG_PTR*)(DeviceObject)) = (ULONG_PTR)pDeviceObjectEx;
212 return STATUS_SUCCESS;
220 VOID _IoDeleteDeviceEx(PDEVICE_OBJECT_EX DeviceObject)
225 if (!pDeviceObjectEx)
228 unlink(pDeviceObjectEx->DeviceFileName);
229 free(pDeviceObjectEx->DeviceName);
230 free(pDeviceObjectEx->DeviceFileName);
231 free(pDeviceObjectEx);