Files
itgmania212121/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Linux.cpp
T

551 lines
16 KiB
C++
Raw Normal View History

#include "global.h"
#include "MemoryCardDriverThreaded_Linux.h"
#include "RageLog.h"
#include "RageUtil.h"
#include "RageFileManager.h"
2004-04-23 04:17:02 +00:00
#include "Profile.h"
#include "PrefsManager.h"
2004-08-17 05:50:51 +00:00
#include "Foreach.h"
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <fcntl.h>
#include <fstream>
2004-04-29 04:12:56 +00:00
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
2004-05-02 23:27:14 +00:00
#include <sys/poll.h>
2004-04-23 04:17:02 +00:00
const CString TEMP_MOUNT_POINT = "@mctemp/";
2004-08-17 05:50:51 +00:00
void GetNewStorageDevices( vector<UsbStorageDevice>& vDevicesOut );
template<class T>
bool VectorsAreEqual( const T &a, const T &b )
{
2004-06-06 18:37:57 +00:00
if( a.size() != b.size() )
return false;
for( unsigned i=0; i<a.size(); i++ )
{
2004-06-06 18:37:57 +00:00
if( a[i] != b[i] )
return false;
}
2004-06-06 18:37:57 +00:00
return true;
}
static bool TestWrite( CCStringRef sDir )
{
2004-06-06 18:37:57 +00:00
// Try to write a file.
// TODO: Can we use RageFile for this?
CString sFile = sDir + "/temp";
FILE* fp = fopen( sFile, "w" );
if( fp == NULL )
return false;
fclose( fp );
remove( sFile );
return true;
}
static bool ExecuteCommand( CCStringRef sCommand )
{
2004-06-06 18:37:57 +00:00
LOG->Trace( "executing '%s'", sCommand.c_str() );
int ret = system(sCommand);
LOG->Trace( "done executing '%s'", sCommand.c_str() );
if( ret != 0 )
LOG->Warn( "failed to execute '%s' with error %d.", sCommand.c_str(), ret );
return ret == 0;
}
MemoryCardDriverThreaded_Linux::MemoryCardDriverThreaded_Linux()
{
}
2004-07-17 20:38:59 +00:00
MemoryCardDriverThreaded_Linux::~MemoryCardDriverThreaded_Linux()
{
}
void MemoryCardDriverThreaded_Linux::Reset()
{
}
static bool ReadFile( const CString &sPath, CString &sBuf )
{
sBuf.clear();
int fd = open( sPath, O_RDONLY );
if( fd == -1 )
{
LOG->Warn( "Error opening \"%s\": %s", sPath.c_str(), strerror(errno) );
return false;
}
while(1)
{
char buf[1024];
int iGot = read( fd, buf, sizeof(buf) );
if( iGot == -1 )
{
close(fd);
LOG->Warn( "Error reading \"%s\": %s", sPath.c_str(), strerror(errno) );
return false;
}
sBuf.append( buf, iGot );
if( iGot < (int) sizeof(buf) )
break;
}
close(fd);
return true;
}
static void GetFileList( const CString &sPath, vector<CString> &out )
{
out.clear();
DIR *dp = opendir( sPath );
if( dp == NULL )
return; // false; // XXX warn
while( const struct dirent *ent = readdir(dp) )
out.push_back( ent->d_name );
closedir( dp );
}
static bool BlockDevicesChanged()
{
static CString sLastDevices = "";
CString sThisDevices;
/* If a device is removed and reinserted, the inode of the /sys/block entry
* will change. */
CString sDevicePath = "/sys/block/";
vector<CString> asDevices;
GetFileList( sDevicePath, asDevices );
for( unsigned i = 0; i < asDevices.size(); ++i )
{
struct stat buf;
if( stat( sDevicePath + asDevices[i], &buf ) == -1 )
continue; // XXX warn
sThisDevices += ssprintf( "%i,", (int) buf.st_ino );
}
bool bChanged = sThisDevices != sLastDevices;
sLastDevices = sThisDevices;
if( bChanged )
LOG->Trace( "Change in USB storage devices detected." );
return bChanged;
}
2005-02-06 19:49:34 +00:00
bool MemoryCardDriverThreaded_Linux::NeedUpdate( bool bMount ) const
{
2005-02-06 19:49:34 +00:00
if( bMount )
2004-08-17 06:16:49 +00:00
{
2005-02-06 19:49:34 +00:00
/* Check if any devices need a write test. */
for( unsigned i=0; i<m_vDevicesLastSeen.size(); i++ )
{
const UsbStorageDevice &d = m_vDevicesLastSeen[i];
if( d.m_State == UsbStorageDevice::STATE_CHECKING )
return true;
}
2004-08-17 06:16:49 +00:00
}
2005-02-06 19:49:34 +00:00
/* Nothing needs a write test (or we ca'nt do it right now). If no devices
* have changed, either, we have nothing to do. */
if( BlockDevicesChanged() )
return true;
2005-02-06 19:49:34 +00:00
/* Nothing to do. */
return false;
}
2005-02-06 19:49:34 +00:00
bool MemoryCardDriverThreaded_Linux::DoOneUpdate( bool bMount, vector<UsbStorageDevice>& vStorageDevicesOut )
{
2005-02-06 19:49:34 +00:00
if( !NeedUpdate(bMount) )
return false;
2004-08-17 05:50:51 +00:00
vector<UsbStorageDevice> vOld = m_vDevicesLastSeen; // copy
2005-02-06 19:49:34 +00:00
GetNewStorageDevices( vStorageDevicesOut );
vector<UsbStorageDevice> &vNew = vStorageDevicesOut;
2004-06-06 18:37:57 +00:00
// check for connects
2004-08-17 05:50:51 +00:00
vector<UsbStorageDevice> vConnects;
2004-08-17 06:16:49 +00:00
FOREACH( UsbStorageDevice, vNew, newd )
{
2004-08-17 06:35:42 +00:00
vector<UsbStorageDevice>::iterator iter = find( vOld.begin(), vOld.end(), *newd );
if( iter == vOld.end() ) // didn't find
2004-08-17 06:16:49 +00:00
{
LOG->Trace( "Connected bus %d port %d level %d path %s", newd->iBus, newd->iPort, newd->iLevel, newd->sOsMountDir.c_str() );
vConnects.push_back( *newd );
}
}
2005-02-06 19:49:34 +00:00
/* When we first see a device, regardless of bMount, just return it as CHECKING,
* so the main thread knows about the device. On the next call where bMount is
* true, check it. */
for( unsigned i=0; i<vStorageDevicesOut.size(); i++ )
2004-08-17 06:16:49 +00:00
{
2005-02-06 19:49:34 +00:00
UsbStorageDevice &d = vStorageDevicesOut[i];
/* If this device was just connected (it wasn't here last time), set it to
* CHECKING and return it, to let the main thread know about the device before
* we start checking. */
vector<UsbStorageDevice>::iterator iter = find( vOld.begin(), vOld.end(), d );
if( iter == vOld.end() ) // didn't find
2004-08-17 06:16:49 +00:00
{
2005-02-06 19:49:34 +00:00
d.m_State = UsbStorageDevice::STATE_CHECKING;
continue;
2004-08-17 06:16:49 +00:00
}
2005-02-06 19:49:34 +00:00
/* Preserve the state of the device. */
d.m_State = iter->m_State;
/* The device was here last time. If CHECKING, check the device now if
* we're allowed to. */
if( d.m_State == UsbStorageDevice::STATE_CHECKING )
2005-02-06 19:49:34 +00:00
{
if( !bMount )
{
/* We can't check it now. Keep the checking state and check it when
* we can. */
d.m_State = UsbStorageDevice::STATE_CHECKING;
continue;
}
CString sCommand = "mount " + d.sOsMountDir;
2004-08-17 06:16:49 +00:00
bool bMountedSuccessfully = ExecuteCommand( sCommand );
2005-02-06 05:46:07 +00:00
if( bMountedSuccessfully && TestWrite( d.sOsMountDir ) )
2005-02-06 19:49:34 +00:00
{
/* We've successfully mounted and tested the device. Read the
* profile name (by mounting a temporary, private mountpoint),
* and then unmount it until Mount() is called. */
2005-02-06 05:46:07 +00:00
d.m_State = UsbStorageDevice::STATE_READY;
2005-02-06 19:49:34 +00:00
FILEMAN->Mount( "dir", d.sOsMountDir, TEMP_MOUNT_POINT );
Profile profile;
CString sProfileDir = TEMP_MOUNT_POINT + PREFSMAN->m_sMemoryCardProfileSubdir + '/';
profile.LoadEditableDataFromDir( sProfileDir );
d.sName = profile.GetDisplayName();
FILEMAN->Unmount( "dir", d.sOsMountDir, TEMP_MOUNT_POINT );
CString sCommand = "umount " + d.sOsMountDir;
ExecuteCommand( sCommand );
}
2005-02-06 05:46:07 +00:00
else
d.m_State = UsbStorageDevice::STATE_WRITE_ERROR;
2005-01-27 04:36:11 +00:00
2005-02-06 05:46:07 +00:00
LOG->Trace( "WriteTest: %s, Name: %s", d.m_State == UsbStorageDevice::STATE_WRITE_ERROR? "failed":"succeeded", d.sName.c_str() );
2004-08-17 06:16:49 +00:00
}
}
2005-02-06 19:49:34 +00:00
m_vDevicesLastSeen = vNew;
2004-06-06 18:37:57 +00:00
CHECKPOINT;
2005-02-06 19:49:34 +00:00
return true;
}
struct WhiteListEntry
{
int idVendor; // -1 = "always match"
int idProduct; // -1 = "always match"
const char *szVendorRegex; // empty string matches all
const char *szProductRegex; // empty string matches all
};
static const WhiteListEntry g_AllowedEntries[] =
2004-10-03 20:58:12 +00:00
{
#if 0
2004-10-07 23:17:16 +00:00
{ 0x0781, -1, "", "Cruzer" }, // SanDisk Cruzer* drives
2004-10-03 21:10:18 +00:00
// Disallow the Rio Carbon. After several mounts, usb-storage gets into a state where mounting will fail.
// { 0x045a, -1, "", "" }, // Diamond Multimedia Systems (Rio)
{ 0x04e8, -1, "Kingston|KINGSTON", "" }, // Kingston pen drives manufactured by Samsung
{ 0x041e, -1, "", "^NOMAD MuVo$|^NOMAD MuVo .X" }, // Creative Labs Nomad MuVo flash drives (hard drive players excluded)
2004-10-12 01:36:37 +00:00
// Some Iomega Micro Mini drives cause mount to hang
// { 0x4146, -1, "", "Flash" }, // Iomega Micro Mini drive
2004-10-07 23:17:16 +00:00
{ 0x05dc, -1, "", "JUMP|Jump" }, // All Lexar Jump* drives
{ 0x1915, 0x2220, "", "" }, // Linksys USB 2.0 Disk 128MB
{ 0x0d7d, -1, "", "USB DISK" }, // PNY Attache pen drives
2004-10-08 07:29:51 +00:00
{ 0x0ea0, -1, "", "Flash Disk" }, // other PNY Attache pen drives
2004-10-07 23:44:12 +00:00
{ 0x0ef5, -1, "", "Intelligent|Traveling" }, // PQI Intelligent Stick and Traveling Disk
2004-10-08 07:29:51 +00:00
{ 0x08ec, -1, "", "M-Disk" }, // M-Systems flash drive
#endif
{ -1, -1, "", "" }, // allow anything
2004-10-03 20:58:12 +00:00
};
bool IsDeviceAllowed( int idVendor, int idProduct, CString sVendor, CString sProduct )
2004-10-03 20:58:12 +00:00
{
bool bAllowed = false;
for( unsigned i=0; i<ARRAYSIZE(g_AllowedEntries); i++ )
2004-10-03 20:58:12 +00:00
{
const WhiteListEntry &entry = g_AllowedEntries[i];
if( entry.idVendor != -1 && entry.idVendor != idVendor )
continue;
if( entry.idProduct != -1 && entry.idProduct != idProduct )
continue;
Regex regexVendor( entry.szVendorRegex );
if( !regexVendor.Compare(sVendor) )
continue;
Regex regexProduct( entry.szProductRegex );
if( !regexProduct.Compare(sProduct) )
continue;
bAllowed = true;
break;
}
2004-10-03 20:58:12 +00:00
LOG->Trace( "idVendor 0x%04X, idDevice 0x%04X, Vendor '%s', Product '%s' is %sallowed.", idVendor, idProduct, sVendor.c_str(), sProduct.c_str(), bAllowed?"":"not " );
2004-10-03 20:58:12 +00:00
return bAllowed;
}
2004-08-17 05:50:51 +00:00
void GetNewStorageDevices( vector<UsbStorageDevice>& vDevicesOut )
{
2004-06-06 18:37:57 +00:00
LOG->Trace( "GetNewStorageDevices" );
vDevicesOut.clear();
{
vector<CString> asDevices;
CString sBlockDevicePath = "/sys/block/";
GetFileList( sBlockDevicePath, asDevices );
for( unsigned i = 0; i < asDevices.size(); ++i )
2004-04-29 06:00:20 +00:00
{
2005-04-22 03:04:58 +00:00
const CString &sDevice = asDevices[i];
if( sDevice == "." || sDevice == ".." )
continue;
2004-10-03 20:58:12 +00:00
UsbStorageDevice usbd;
2005-04-22 03:04:58 +00:00
CString sPath = sBlockDevicePath + sDevice + "/";
/* Ignore non-removable devices. */
CString sBuf;
if( !ReadFile( sPath + "removable", sBuf ) )
continue; // already warned
if( atoi(sBuf) != 1 )
continue;
2005-04-22 03:16:14 +00:00
usbd.sDevice = "/dev/" + sDevice;
/*
* sPath/device should be a symlink to the actual device. For USB
* devices, it looks like this:
*
* device -> ../../devices/pci0000:00/0000:00:02.1/usb2/2-1/2-1:1.0
*
* "2-1" is "bus-port".
*/
char szLink[256];
2005-01-27 04:36:11 +00:00
int iRet = readlink( sPath + "device", szLink, sizeof(szLink) );
if( iRet == -1 )
2004-06-06 18:37:57 +00:00
{
2005-01-27 04:36:11 +00:00
LOG->Warn( "readlink(\"%s\"): %s", (sPath + "device").c_str(), strerror(errno) );
2004-06-06 18:37:57 +00:00
}
else
2004-06-06 18:37:57 +00:00
{
2005-01-27 04:36:11 +00:00
/*
* The full path looks like
*
* ../../devices/pci0000:00/0000:00:02.1/usb2/2-2/2-2.1/2-2.1:1.0
*
* Each path element refers to a new hop in the chain.
* "usb2" = second USB host
* 2- second USB host,
* -2 port 1 on the host,
* .1 port 1 on an attached hub
* .2 ... port 2 on the next hub ...
*
* We want the bus number and the port of the last hop. The level is
* the number of hops.
*/
szLink[iRet] = 0;
vector<CString> asBits;
split( szLink, "/", asBits );
2005-01-27 04:36:11 +00:00
if( strstr( szLink, "usb" ) != NULL )
{
CString sHostPort = asBits[asBits.size()-2];
2005-01-27 04:36:11 +00:00
sHostPort.Replace( "-", "." );
asBits.clear();
2005-01-27 04:36:11 +00:00
split( sHostPort, ".", asBits );
if( asBits.size() > 1 )
{
2005-01-27 04:36:11 +00:00
usbd.iBus = atoi( asBits[0] );
usbd.iPort = atoi( asBits[asBits.size()-1] );
usbd.iLevel = asBits.size() - 1;
}
2004-06-06 18:37:57 +00:00
}
}
if( ReadFile( sPath + "device/../idVendor", sBuf ) )
sscanf( sBuf, "%x", &usbd.idVendor );
if( ReadFile( sPath + "device/../idProduct", sBuf ) )
sscanf( sBuf, "%x", &usbd.idProduct );
if( ReadFile( sPath + "device/../serial", sBuf ) )
2004-06-06 18:37:57 +00:00
{
usbd.sSerial = sBuf;
TrimRight( usbd.sSerial );
}
if( ReadFile( sPath + "device/../product", sBuf ) )
{
usbd.sProduct = sBuf;
TrimRight( usbd.sProduct );
}
if( ReadFile( sPath + "device/../manufacturer", sBuf ) )
{
usbd.sVendor = sBuf;
TrimRight( usbd.sVendor );
}
bool bAllowed = IsDeviceAllowed( usbd.idVendor, usbd.idProduct, usbd.sVendor, usbd.sProduct );
LOG->Trace( "iBus: %d, iLevel: %d, iPort: %d, sSerial = %s (%s)",
usbd.iBus, usbd.iLevel, usbd.iPort, usbd.sSerial.c_str(), bAllowed? "allowed":"disallowed" );
if( bAllowed )
vDevicesOut.push_back( usbd );
2004-06-06 18:37:57 +00:00
}
}
{
// Find where each device is mounted. Output looks like:
2004-06-06 18:37:57 +00:00
// /dev/sda1 /mnt/flash1 auto noauto,owner 0 0
// /dev/sdb1 /mnt/flash2 auto noauto,owner 0 0
// /dev/sdc1 /mnt/flash3 auto noauto,owner 0 0
2004-06-06 18:37:57 +00:00
CString fn = "/etc/fstab";
2004-06-16 04:07:19 +00:00
LOG->Trace( "Reading %s", fn.c_str() );
RageFile f;
if( !f.Open(fn) )
{
LOG->Warn( "can't open '%s': %s", fn.c_str(), f.GetError().c_str() );
return;
}
2004-06-06 18:37:57 +00:00
CString sLine;
2004-04-19 03:35:46 +00:00
while( !f.AtEOF() )
{
2004-04-19 03:35:46 +00:00
switch( f.GetLine(sLine) )
{
case 0: continue; /* eof */
case -1:
LOG->Warn( "error reading '%s': %s", fn.c_str(), f.GetError().c_str() );
return;
}
2004-06-06 18:37:57 +00:00
2004-04-29 04:12:56 +00:00
char szScsiDevice[1024];
char szMountPoint[1024];
2004-04-29 04:12:56 +00:00
int iRet = sscanf( sLine, "%s %s", szScsiDevice, szMountPoint );
if( iRet != 2 )
continue; // don't process this line
2004-06-06 18:37:57 +00:00
CString sMountPoint = szMountPoint;
TrimLeft( sMountPoint );
TrimRight( sMountPoint );
2004-06-06 18:37:57 +00:00
2005-04-22 03:16:14 +00:00
// search for the mountpoint corresponding to the device
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
2005-04-22 03:16:14 +00:00
if( usbd.sDevice+"1" == szScsiDevice ) // found our match
{
usbd.sOsMountDir = sMountPoint;
2004-06-06 18:37:57 +00:00
2005-04-22 03:16:14 +00:00
LOG->Trace( "iScsiIndex: %d, sDevice: %s, iBus: %d, iLevel: %d, iPort: %d, sOsMountDir: %s",
usbd.iScsiIndex, usbd.sDevice.c_str(), usbd.iBus, usbd.iLevel, usbd.iPort, usbd.sOsMountDir.c_str() );
2004-06-06 18:37:57 +00:00
break; // stop looking for a match
}
}
}
}
2004-06-06 18:37:57 +00:00
2004-04-17 01:02:54 +00:00
/* Remove any devices that we couldn't find a mountpoint for. */
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
if( usbd.sOsMountDir.empty() )
{
2004-06-16 04:07:19 +00:00
LOG->Trace( "Ignoring %s (couldn't find in /etc/fstab)", usbd.sSerial.c_str() );
2004-08-17 06:16:49 +00:00
2004-04-17 01:02:54 +00:00
vDevicesOut.erase( vDevicesOut.begin()+i );
--i;
}
}
2004-06-06 18:37:57 +00:00
2004-04-29 06:00:20 +00:00
LOG->Trace( "Done with GetNewStorageDevices" );
}
2004-04-29 06:00:20 +00:00
bool MemoryCardDriverThreaded_Linux::Mount( UsbStorageDevice* pDevice )
{
2004-06-06 18:37:57 +00:00
ASSERT( !pDevice->sOsMountDir.empty() );
CString sCommand = "mount " + pDevice->sOsMountDir;
LOG->Trace( "hack mount (%s)", sCommand.c_str() );
bool bMountedSuccessfully = ExecuteCommand( sCommand );
return bMountedSuccessfully;
}
void MemoryCardDriverThreaded_Linux::Unmount( UsbStorageDevice* pDevice )
{
if( pDevice->sOsMountDir.empty() )
return;
2004-06-06 18:37:57 +00:00
CString sCommand = "umount " + pDevice->sOsMountDir;
LOG->Trace( "hack unmount (%s)", sCommand.c_str() );
ExecuteCommand( sCommand );
}
void MemoryCardDriverThreaded_Linux::Flush( UsbStorageDevice* pDevice )
{
if( pDevice->sOsMountDir.empty() )
return;
2004-06-06 18:37:57 +00:00
// "sync" will only flush all file systems at the same time. -Chris
// I don't think so. Also, sync() merely queues a flush; it doesn't guarantee
// that the flush is completed on return. However, we can mount the filesystem
// with the flag "-o sync", which forces synchronous access (but that's probably
// very slow.) -glenn
ExecuteCommand( "mount -o remount " + pDevice->sOsMountDir );
}
/*
* (c) 2003-2005 Chris Danford, Glenn Maynard
2004-05-15 22:30:30 +00:00
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/