2013-10-31 19:02:25 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2013-10-31 19:02:25 +04:00
|
|
|
|
|
|
|
#include <winpr/io.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
#include "io.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2015-06-17 23:08:02 +03:00
|
|
|
#include <errno.h>
|
2013-10-31 19:02:25 +04:00
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_UNISTD_H
|
2013-10-31 19:02:25 +04:00
|
|
|
#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/
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define DEVICE_FILE_PREFIX_PATH "\\Device\\"
|
2013-10-31 19:02:25 +04:00
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static char* GetDeviceFileNameWithoutPrefixA(LPCSTR lpName)
|
2013-10-31 19:02:25 +04:00
|
|
|
{
|
|
|
|
char* lpFileName;
|
|
|
|
|
|
|
|
if (!lpName)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (strncmp(lpName, DEVICE_FILE_PREFIX_PATH, sizeof(DEVICE_FILE_PREFIX_PATH) - 1) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
lpFileName =
|
|
|
|
_strdup(&lpName[strnlen(DEVICE_FILE_PREFIX_PATH, sizeof(DEVICE_FILE_PREFIX_PATH))]);
|
2013-10-31 19:02:25 +04:00
|
|
|
return lpFileName;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static char* GetDeviceFileUnixDomainSocketBaseFilePathA(void)
|
2013-10-31 19:02:25 +04:00
|
|
|
{
|
|
|
|
char* lpTempPath;
|
|
|
|
char* lpPipePath;
|
|
|
|
lpTempPath = GetKnownPath(KNOWN_PATH_TEMP);
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-03-25 19:37:46 +03:00
|
|
|
if (!lpTempPath)
|
|
|
|
return NULL;
|
2013-10-31 19:02:25 +04:00
|
|
|
|
2018-08-22 14:16:03 +03:00
|
|
|
lpPipePath = GetCombinedPath(lpTempPath, ".device");
|
2013-10-31 19:02:25 +04:00
|
|
|
free(lpTempPath);
|
|
|
|
return lpPipePath;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static char* GetDeviceFileUnixDomainSocketFilePathA(LPCSTR lpName)
|
2013-10-31 19:02:25 +04:00
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
char* lpPipePath = NULL;
|
|
|
|
char* lpFileName = NULL;
|
|
|
|
char* lpFilePath = NULL;
|
2013-10-31 19:02:25 +04:00
|
|
|
lpPipePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!lpPipePath)
|
|
|
|
return NULL;
|
2013-10-31 19:02:25 +04:00
|
|
|
|
|
|
|
lpFileName = GetDeviceFileNameWithoutPrefixA(lpName);
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!lpFileName)
|
|
|
|
{
|
2018-08-20 10:50:43 +03:00
|
|
|
free(lpPipePath);
|
2015-06-17 23:08:02 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
lpFilePath = GetCombinedPath(lpPipePath, (char*)lpFileName);
|
2013-10-31 19:02:25 +04:00
|
|
|
free(lpPipePath);
|
|
|
|
free(lpFileName);
|
|
|
|
return lpFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IoCreateDevice:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff548397/
|
|
|
|
*/
|
|
|
|
|
2018-08-22 14:16:03 +03:00
|
|
|
NTSTATUS _IoCreateDeviceEx(PDRIVER_OBJECT_EX DriverObject, ULONG DeviceExtensionSize,
|
2019-11-06 17:24:51 +03:00
|
|
|
PUNICODE_STRING DeviceName, DEVICE_TYPE DeviceType,
|
|
|
|
ULONG DeviceCharacteristics, BOOLEAN Exclusive,
|
2018-08-22 14:16:03 +03:00
|
|
|
PDEVICE_OBJECT_EX* DeviceObject)
|
2013-10-31 19:02:25 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
char* DeviceBasePath;
|
|
|
|
DEVICE_OBJECT_EX* pDeviceObjectEx;
|
|
|
|
DeviceBasePath = GetDeviceFileUnixDomainSocketBaseFilePathA();
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!DeviceBasePath)
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
2021-05-31 12:42:03 +03:00
|
|
|
if (!winpr_PathFileExists(DeviceBasePath))
|
2013-10-31 19:02:25 +04:00
|
|
|
{
|
2017-10-30 03:26:53 +03:00
|
|
|
if (mkdir(DeviceBasePath, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
|
2013-10-31 19:02:25 +04:00
|
|
|
{
|
|
|
|
free(DeviceBasePath);
|
|
|
|
return STATUS_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:16:03 +03:00
|
|
|
free(DeviceBasePath);
|
2019-11-06 17:24:51 +03:00
|
|
|
pDeviceObjectEx = (DEVICE_OBJECT_EX*)calloc(1, sizeof(DEVICE_OBJECT_EX));
|
2013-10-31 19:02:25 +04:00
|
|
|
|
|
|
|
if (!pDeviceObjectEx)
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
pDeviceObjectEx->DeviceName =
|
|
|
|
ConvertWCharNToUtf8Alloc(DeviceName->Buffer, DeviceName->Length / sizeof(WCHAR), NULL);
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!pDeviceObjectEx->DeviceName)
|
|
|
|
{
|
|
|
|
free(pDeviceObjectEx);
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
2013-10-31 19:02:25 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pDeviceObjectEx->DeviceFileName =
|
|
|
|
GetDeviceFileUnixDomainSocketFilePathA(pDeviceObjectEx->DeviceName);
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!pDeviceObjectEx->DeviceFileName)
|
|
|
|
{
|
|
|
|
free(pDeviceObjectEx->DeviceName);
|
|
|
|
free(pDeviceObjectEx);
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
2013-10-31 19:02:25 +04:00
|
|
|
|
2021-05-31 12:42:03 +03:00
|
|
|
if (winpr_PathFileExists(pDeviceObjectEx->DeviceFileName))
|
2013-10-31 19:02:25 +04:00
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
if (unlink(pDeviceObjectEx->DeviceFileName) == -1)
|
|
|
|
{
|
|
|
|
free(pDeviceObjectEx->DeviceName);
|
|
|
|
free(pDeviceObjectEx->DeviceFileName);
|
|
|
|
free(pDeviceObjectEx);
|
|
|
|
return STATUS_ACCESS_DENIED;
|
|
|
|
}
|
2013-10-31 19:02:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
status = mkfifo(pDeviceObjectEx->DeviceFileName, 0666);
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
free(pDeviceObjectEx->DeviceName);
|
|
|
|
free(pDeviceObjectEx->DeviceFileName);
|
|
|
|
free(pDeviceObjectEx);
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EACCES:
|
|
|
|
return STATUS_ACCESS_DENIED;
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
case EEXIST:
|
|
|
|
return STATUS_OBJECT_NAME_EXISTS;
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
case ENAMETOOLONG:
|
|
|
|
return STATUS_NAME_TOO_LONG;
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
case ENOENT:
|
|
|
|
case ENOTDIR:
|
|
|
|
return STATUS_NOT_A_DIRECTORY;
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
case ENOSPC:
|
|
|
|
return STATUS_DISK_FULL;
|
2018-08-22 14:16:03 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
default:
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2013-10-31 19:02:25 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*((ULONG_PTR*)(DeviceObject)) = (ULONG_PTR)pDeviceObjectEx;
|
2013-10-31 19:02:25 +04:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IoDeleteDevice:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff549083/
|
|
|
|
*/
|
|
|
|
|
|
|
|
VOID _IoDeleteDeviceEx(PDEVICE_OBJECT_EX DeviceObject)
|
|
|
|
{
|
|
|
|
DEVICE_OBJECT_EX* pDeviceObjectEx;
|
2019-11-06 17:24:51 +03:00
|
|
|
pDeviceObjectEx = (DEVICE_OBJECT_EX*)DeviceObject;
|
2013-10-31 19:02:25 +04:00
|
|
|
|
|
|
|
if (!pDeviceObjectEx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unlink(pDeviceObjectEx->DeviceFileName);
|
|
|
|
free(pDeviceObjectEx->DeviceName);
|
|
|
|
free(pDeviceObjectEx->DeviceFileName);
|
|
|
|
free(pDeviceObjectEx);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|