Files
aaruremote/linux/device.c

322 lines
9.0 KiB
C
Raw Normal View History

2019-10-13 22:57:06 +01:00
/*
2020-03-01 05:44:49 +00:00
* This file is part of the Aaru Remote Server.
2020-01-03 17:42:13 +00:00
* Copyright (c) 2019-2020 Natalia Portillo.
2019-10-13 22:57:06 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "linux.h"
#include <errno.h>
#include <fcntl.h>
2019-10-16 20:17:33 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2019-10-13 22:57:06 +01:00
#ifdef HAS_UDEV
#include <libudev.h>
#endif
2020-03-01 19:47:15 +00:00
void* DeviceOpen(const char* device_path)
2019-10-13 22:57:06 +01:00
{
2020-03-01 19:47:15 +00:00
DeviceContext* ctx;
2019-10-13 22:57:06 +01:00
2020-03-01 19:47:15 +00:00
ctx = malloc(sizeof(DeviceContext));
2019-10-13 22:57:06 +01:00
2019-10-26 16:11:19 +01:00
if(!ctx) return NULL;
2019-10-13 22:57:06 +01:00
2020-03-01 19:47:15 +00:00
memset(ctx, 0, sizeof(DeviceContext));
2019-10-26 16:11:19 +01:00
ctx->fd = open(device_path, O_RDWR | O_NONBLOCK | O_CREAT);
if((ctx->fd < 0) && (errno == EACCES || errno == EROFS)) { ctx->fd = open(device_path, O_RDONLY | O_NONBLOCK); }
if(ctx->fd <= 0)
{
free(ctx);
return NULL;
}
strncpy(ctx->device_path, device_path, 4096);
return ctx;
2019-10-16 20:17:33 +01:00
}
2020-03-01 19:47:15 +00:00
void DeviceClose(void* device_ctx)
2019-10-26 16:11:19 +01:00
{
2020-03-01 19:47:15 +00:00
DeviceContext* ctx = device_ctx;
2019-10-26 15:47:49 +01:00
2019-10-26 16:11:19 +01:00
if(!ctx) return;
close(ctx->fd);
free(ctx);
}
2020-03-01 19:47:15 +00:00
int32_t GetDeviceType(void* device_ctx)
2019-10-16 20:17:33 +01:00
{
2020-03-01 19:47:15 +00:00
DeviceContext* ctx = device_ctx;
2019-10-26 16:11:19 +01:00
if(!ctx) return -1;
#ifdef HAS_UDEV
2019-10-16 20:17:33 +01:00
struct udev* udev;
struct udev_device* udev_device;
2019-10-19 18:30:45 +01:00
const char* tmp_string;
2019-10-16 20:17:33 +01:00
char* chrptr;
2020-03-01 05:50:46 +00:00
int32_t device_type = AARUREMOTE_DEVICE_TYPE_UNKNOWN;
2019-10-16 20:17:33 +01:00
udev = udev_new();
2020-03-01 05:50:46 +00:00
if(!udev) return AARUREMOTE_DEVICE_TYPE_UNKNOWN;
2019-10-16 20:17:33 +01:00
2019-10-26 16:11:19 +01:00
chrptr = strrchr(ctx->device_path, '/');
2020-03-01 05:50:46 +00:00
if(chrptr == 0) return AARUREMOTE_DEVICE_TYPE_UNKNOWN;
2019-10-16 20:17:33 +01:00
chrptr++;
2020-03-01 05:50:46 +00:00
if(chrptr == 0) return AARUREMOTE_DEVICE_TYPE_UNKNOWN;
2019-10-16 20:17:33 +01:00
udev_device = udev_device_new_from_subsystem_sysname(udev, "block", chrptr);
if(udev_device)
{
2019-10-19 18:30:45 +01:00
tmp_string = udev_device_get_property_value(udev_device, "ID_BUS");
if(tmp_string)
2019-10-16 20:17:33 +01:00
{
2019-10-19 18:30:45 +01:00
if(strncmp(tmp_string, "ata", 3) == 0)
2019-10-16 20:17:33 +01:00
{
2020-03-01 05:50:46 +00:00
device_type = AARUREMOTE_DEVICE_TYPE_ATA;
2019-10-16 20:17:33 +01:00
2019-10-19 18:30:45 +01:00
free((void*)tmp_string);
tmp_string = udev_device_get_property_value(udev_device, "ID_TYPE");
2019-10-16 20:17:33 +01:00
2019-10-19 18:30:45 +01:00
if(tmp_string)
2019-10-16 20:17:33 +01:00
{
// TODO: ATAPI removable non optical disks
2020-03-01 05:50:46 +00:00
if(strncmp(tmp_string, "cd", 2) == 0) { device_type = AARUREMOTE_DEVICE_TYPE_ATAPI; }
2019-10-16 20:17:33 +01:00
2019-10-19 18:30:45 +01:00
free((void*)tmp_string);
2019-10-16 20:17:33 +01:00
}
}
2019-10-19 18:30:45 +01:00
else if(strncmp(tmp_string, "mmc", 3) == 0)
2019-10-16 20:17:33 +01:00
{
2019-10-19 18:30:45 +01:00
free((void*)tmp_string);
tmp_string = malloc(1024);
2019-10-16 20:17:33 +01:00
2020-03-01 05:50:46 +00:00
device_type = AARUREMOTE_DEVICE_TYPE_MMC;
2019-10-16 20:17:33 +01:00
2019-10-19 18:30:45 +01:00
if(tmp_string)
2019-10-16 20:17:33 +01:00
{
2019-10-19 18:30:45 +01:00
memset((void*)tmp_string, 0, 1024);
snprintf((char*)tmp_string, 1024, "/sys/block/%s/device/scr", chrptr);
2019-10-16 20:17:33 +01:00
2020-03-01 05:50:46 +00:00
if(access(tmp_string, R_OK) == 0) device_type = AARUREMOTE_DEVICE_TYPE_SECURE_DIGITAL;
2019-10-16 20:17:33 +01:00
2019-10-19 18:30:45 +01:00
free((void*)tmp_string);
2019-10-16 20:17:33 +01:00
}
}
2019-10-19 18:30:45 +01:00
else if(strncmp(tmp_string, "scsi", 4) == 0 || strncmp(tmp_string, "ieee1394", 8) == 0 ||
strncmp(tmp_string, "usb", 3) == 0)
2019-10-16 20:17:33 +01:00
{
2019-10-19 18:30:45 +01:00
free((void*)tmp_string);
tmp_string = udev_device_get_property_value(udev_device, "ID_TYPE");
2019-10-16 20:17:33 +01:00
2019-10-19 18:30:45 +01:00
if(tmp_string)
2019-10-16 20:17:33 +01:00
{
2019-10-19 18:30:45 +01:00
if(strncmp(tmp_string, "cd", 2) == 0 || strncmp(tmp_string, "disk", 4) == 0)
2020-03-01 05:50:46 +00:00
{ device_type = AARUREMOTE_DEVICE_TYPE_SCSI; }
2019-10-16 20:17:33 +01:00
2019-10-19 18:30:45 +01:00
free((void*)tmp_string);
2019-10-16 20:17:33 +01:00
}
}
2019-10-19 18:30:45 +01:00
else if(strncmp(tmp_string, "nvme", 4) == 0)
2019-10-16 20:17:33 +01:00
{
2019-10-19 18:30:45 +01:00
free((void*)tmp_string);
2020-03-01 05:50:46 +00:00
device_type = AARUREMOTE_DEVICE_TYPE_NVME;
2019-10-16 20:17:33 +01:00
}
}
}
udev_unref(udev);
2019-10-19 18:30:45 +01:00
return device_type;
#else
2020-03-01 05:50:46 +00:00
int32_t dev_type = AARUREMOTE_DEVICE_TYPE_UNKNOWN;
const char* dev_name;
const char* sysfs_path;
char* dev_path;
char* host_no;
char* scsi_path;
char* iscsi_path;
char* spi_path;
char* fc_path;
char* sas_path;
int ret;
char* chrptr;
char* sysfs_path_scr;
FILE* file;
size_t len = 4096;
2019-10-26 16:11:19 +01:00
if(strlen(ctx->device_path) <= 5) return dev_type;
2020-03-01 05:50:46 +00:00
if(strstr(ctx->device_path, "nvme")) return AARUREMOTE_DEVICE_TYPE_NVME;
2019-10-26 16:11:19 +01:00
dev_name = ctx->device_path + 5;
2019-10-26 16:11:19 +01:00
if(strstr(ctx->device_path, "mmcblk"))
{
2020-03-01 05:50:46 +00:00
dev_type = AARUREMOTE_DEVICE_TYPE_MMC;
sysfs_path_scr = malloc(len);
if(sysfs_path_scr)
{
snprintf(sysfs_path_scr, len, "/sys/block/%s/device/scr", dev_name);
2020-03-01 05:50:46 +00:00
if(access(sysfs_path_scr, F_OK) == 0) dev_type = AARUREMOTE_DEVICE_TYPE_SECURE_DIGITAL;
free(sysfs_path_scr);
}
return dev_type;
}
sysfs_path = malloc(len);
dev_path = malloc(len);
host_no = malloc(len);
scsi_path = malloc(len);
iscsi_path = malloc(len);
spi_path = malloc(len);
fc_path = malloc(len);
sas_path = malloc(len);
if(!sysfs_path || !dev_path || !host_no || !scsi_path || !iscsi_path || !spi_path || !fc_path || !sas_path)
{
free((void*)sysfs_path);
free((void*)dev_path);
free((void*)host_no);
free((void*)iscsi_path);
free((void*)scsi_path);
free((void*)spi_path);
free((void*)fc_path);
free((void*)sas_path);
return dev_type;
}
memset((void*)sysfs_path, 0, len);
memset((void*)dev_path, 0, len);
memset((void*)host_no, 0, len);
memset((void*)iscsi_path, 0, len);
memset((void*)scsi_path, 0, len);
memset((void*)spi_path, 0, len);
memset((void*)fc_path, 0, len);
memset((void*)sas_path, 0, len);
snprintf((char*)sysfs_path, len, "%s/%s/device", PATH_SYS_DEVBLOCK, dev_name);
ret = readlink(sysfs_path, dev_path, len);
if(ret <= 0)
{
free((void*)sysfs_path);
free((void*)dev_path);
free((void*)host_no);
free((void*)iscsi_path);
free((void*)scsi_path);
free((void*)spi_path);
free((void*)fc_path);
free((void*)sas_path);
return dev_type;
}
ret = 0;
chrptr = strchr(dev_path, ':') - 1;
while(chrptr != dev_path)
{
if(chrptr[0] == '/')
{
chrptr++;
break;
}
ret++;
chrptr--;
}
memcpy((void*)host_no, chrptr, ret);
snprintf(spi_path, len, "/sys/class/spi_host/host%s", host_no);
snprintf(fc_path, len, "/sys/class/fc_host/host%s", host_no);
snprintf(sas_path, len, "/sys/class/sas_host/host%s", host_no);
snprintf(iscsi_path, len, "/sys/class/iscsi_host/host%s", host_no);
snprintf(scsi_path, len, "/sys/class/scsi_host/host%s", host_no);
if(access(spi_path, F_OK) == 0 || access(fc_path, F_OK) == 0 || access(sas_path, F_OK) == 0 ||
access(iscsi_path, F_OK) == 0)
2020-03-01 05:50:46 +00:00
dev_type = AARUREMOTE_DEVICE_TYPE_SCSI;
else if(access(scsi_path, F_OK) == 0)
{
2020-03-01 05:50:46 +00:00
dev_type = AARUREMOTE_DEVICE_TYPE_SCSI;
memset(scsi_path, 0, len);
snprintf(scsi_path, len, "/sys/class/scsi_host/host%s/proc_name", host_no);
if(access(scsi_path, F_OK) == 0)
{
file = fopen(scsi_path, "r");
if(file)
{
memset(scsi_path, 0, len);
ret = getline(&scsi_path, &len, file);
len = 4096;
fclose(file);
if(ret > 0)
{
if(strncmp(scsi_path, "ata", 3) == 0 || strncmp(scsi_path, "sata", 4) == 0 ||
strncmp(scsi_path, "ahci", 4) == 0)
{
2020-03-01 05:50:46 +00:00
dev_type = AARUREMOTE_DEVICE_TYPE_ATA;
memset(scsi_path, 0, len);
snprintf(scsi_path, len, "%s/%s/removable", PATH_SYS_DEVBLOCK, dev_name);
file = fopen(scsi_path, "r");
if(file)
{
ret = (size_t)fread(scsi_path, 1, 1, file);
if(ret == 1)
{
2020-03-01 05:50:46 +00:00
if(scsi_path[0] == '1') dev_type = AARUREMOTE_DEVICE_TYPE_ATAPI;
}
fclose(file);
}
}
}
}
}
}
free((void*)sysfs_path);
free((void*)dev_path);
free((void*)host_no);
free((void*)iscsi_path);
free((void*)scsi_path);
free((void*)spi_path);
free((void*)fc_path);
free((void*)sas_path);
return dev_type;
2019-10-21 08:11:37 +01:00
#endif
}