device.c 5.9 KB

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