2003-07-24 08:19:20 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "ArchHooks_Unix.h"
|
2005-12-10 18:58:43 +00:00
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageUtil.h"
|
|
|
|
|
#include "RageThreads.h"
|
2005-12-30 05:48:45 +00:00
|
|
|
#include "LocalizedString.h"
|
2003-07-24 08:19:20 +00:00
|
|
|
#include "archutils/Unix/SignalHandler.h"
|
2003-11-28 21:34:05 +00:00
|
|
|
#include "archutils/Unix/GetSysInfo.h"
|
2004-03-28 02:04:23 +00:00
|
|
|
#include "archutils/Unix/LinuxThreadHelpers.h"
|
2004-06-14 17:04:57 +00:00
|
|
|
#include "archutils/Unix/EmergencyShutdown.h"
|
2004-12-01 02:45:14 +00:00
|
|
|
#include "archutils/Unix/AssertionHandler.h"
|
2004-04-13 08:05:50 +00:00
|
|
|
#include <unistd.h>
|
2004-06-11 00:33:02 +00:00
|
|
|
#include <sys/time.h>
|
2003-07-24 08:19:20 +00:00
|
|
|
|
2003-07-29 21:00:35 +00:00
|
|
|
#if defined(CRASH_HANDLER)
|
2003-07-24 08:19:20 +00:00
|
|
|
#include "archutils/Unix/CrashHandler.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-03-12 01:09:38 +00:00
|
|
|
static bool IsFatalSignal( int signal )
|
|
|
|
|
{
|
|
|
|
|
switch( signal )
|
|
|
|
|
{
|
|
|
|
|
case SIGINT:
|
|
|
|
|
case SIGTERM:
|
|
|
|
|
case SIGHUP:
|
|
|
|
|
return false;
|
2004-03-12 02:58:54 +00:00
|
|
|
default:
|
|
|
|
|
return true;
|
2004-03-12 01:09:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-19 05:01:53 +00:00
|
|
|
static void DoCleanShutdown( int signal, siginfo_t *si, const ucontext_t *uc )
|
2004-03-12 01:09:38 +00:00
|
|
|
{
|
|
|
|
|
if( IsFatalSignal(signal) )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* ^C. */
|
2005-12-17 12:44:46 +00:00
|
|
|
ArchHooks::SetUserQuit();
|
2004-03-12 01:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
2004-05-06 08:41:34 +00:00
|
|
|
#if defined(CRASH_HANDLER)
|
2004-03-19 05:01:53 +00:00
|
|
|
static void DoCrashSignalHandler( int signal, siginfo_t *si, const ucontext_t *uc )
|
2004-03-12 01:09:38 +00:00
|
|
|
{
|
|
|
|
|
/* Don't dump a debug file if the user just hit ^C. */
|
|
|
|
|
if( !IsFatalSignal(signal) )
|
|
|
|
|
return;
|
|
|
|
|
|
2005-12-31 04:00:41 +00:00
|
|
|
CrashHandler::CrashSignalHandler( signal, si, uc );
|
2004-03-12 01:09:38 +00:00
|
|
|
}
|
2004-05-06 08:41:34 +00:00
|
|
|
#endif
|
2004-03-12 01:09:38 +00:00
|
|
|
|
2004-03-19 05:01:53 +00:00
|
|
|
static void EmergencyShutdown( int signal, siginfo_t *si, const ucontext_t *uc )
|
2003-07-29 21:00:35 +00:00
|
|
|
{
|
2004-03-12 01:09:38 +00:00
|
|
|
if( !IsFatalSignal(signal) )
|
|
|
|
|
return;
|
|
|
|
|
|
2004-06-14 17:04:57 +00:00
|
|
|
DoEmergencyShutdown();
|
2004-03-12 01:09:38 +00:00
|
|
|
|
2004-05-06 08:41:34 +00:00
|
|
|
#if defined(CRASH_HANDLER)
|
|
|
|
|
/* If we ran the crash handler, then die. */
|
2004-04-09 20:57:58 +00:00
|
|
|
kill( getpid(), SIGKILL );
|
2004-05-06 08:41:34 +00:00
|
|
|
#else
|
|
|
|
|
/* We didn't run the crash handler. Run the default handler, so we can dump core. */
|
|
|
|
|
SignalHandler::ResetSignalHandlers();
|
|
|
|
|
raise( signal );
|
|
|
|
|
#endif
|
2004-03-12 01:09:38 +00:00
|
|
|
}
|
2003-07-29 21:00:35 +00:00
|
|
|
|
2004-09-09 03:22:24 +00:00
|
|
|
#if defined(HAVE_TLS)
|
|
|
|
|
static thread_local int g_iTestTLS = 0;
|
|
|
|
|
|
|
|
|
|
static int TestTLSThread( void *p )
|
|
|
|
|
{
|
|
|
|
|
g_iTestTLS = 2;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void TestTLS()
|
|
|
|
|
{
|
2004-09-20 06:13:39 +00:00
|
|
|
/* TLS won't work on older threads libraries, and may crash. */
|
|
|
|
|
if( !UsingNPTL() )
|
|
|
|
|
return;
|
|
|
|
|
|
2004-09-09 03:22:24 +00:00
|
|
|
/* TLS won't work on older Linux kernels. Do a simple check. */
|
|
|
|
|
g_iTestTLS = 1;
|
|
|
|
|
|
|
|
|
|
RageThread TestThread;
|
|
|
|
|
TestThread.SetName( "TestTLS" );
|
|
|
|
|
TestThread.Create( TestTLSThread, NULL );
|
|
|
|
|
TestThread.Wait();
|
|
|
|
|
|
|
|
|
|
if( g_iTestTLS == 1 )
|
|
|
|
|
RageThread::SetSupportsTLS( true );
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-12-08 22:10:51 +00:00
|
|
|
#if 1
|
|
|
|
|
/* If librt is available, use CLOCK_MONOTONIC to implement GetMicrosecondsSinceStart,
|
|
|
|
|
* if supported, so changes to the system clock don't cause problems. */
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
clockid_t g_Clock = CLOCK_REALTIME;
|
|
|
|
|
|
|
|
|
|
void OpenGetTime()
|
|
|
|
|
{
|
|
|
|
|
static bool bInitialized = false;
|
|
|
|
|
if( bInitialized )
|
|
|
|
|
return;
|
|
|
|
|
bInitialized = true;
|
|
|
|
|
|
|
|
|
|
/* Check whether the clock is actually supported. */
|
|
|
|
|
timespec ts;
|
|
|
|
|
if( clock_getres(CLOCK_MONOTONIC, &ts) == -1 )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* If the resolution is worse than a millisecond, fall back on CLOCK_REALTIME. */
|
|
|
|
|
if( ts.tv_sec > 0 || ts.tv_nsec > 1000000 )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_Clock = CLOCK_MONOTONIC;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2006-01-04 18:03:42 +00:00
|
|
|
RString ArchHooks::GetPreferredLanguage()
|
|
|
|
|
{
|
2006-01-07 05:43:38 +00:00
|
|
|
return "EN";
|
2006-01-04 18:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
2005-12-08 22:10:51 +00:00
|
|
|
int64_t ArchHooks::GetMicrosecondsSinceStart( bool bAccurate )
|
|
|
|
|
{
|
|
|
|
|
OpenGetTime();
|
|
|
|
|
|
|
|
|
|
timespec ts;
|
|
|
|
|
clock_gettime( g_Clock, &ts );
|
|
|
|
|
|
|
|
|
|
int64_t iRet = int64_t(ts.tv_sec) * 1000000 + int64_t(ts.tv_nsec)/1000;
|
|
|
|
|
if( g_Clock != CLOCK_MONOTONIC )
|
|
|
|
|
iRet = ArchHooks::FixupTimeIfBackwards( iRet );
|
|
|
|
|
return iRet;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
int64_t ArchHooks::GetMicrosecondsSinceStart( bool bAccurate )
|
2005-03-24 19:46:50 +00:00
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
|
|
2005-12-08 22:10:51 +00:00
|
|
|
int64_t iRet = int64_t(tv.tv_sec) * 1000000 + int64_t(tv.tv_usec);
|
|
|
|
|
ret = FixupTimeIfBackwards( ret );
|
|
|
|
|
return iRet;
|
2005-03-24 19:46:50 +00:00
|
|
|
}
|
2005-12-08 22:10:51 +00:00
|
|
|
#endif
|
2005-03-24 19:46:50 +00:00
|
|
|
|
2003-07-24 08:19:20 +00:00
|
|
|
ArchHooks_Unix::ArchHooks_Unix()
|
|
|
|
|
{
|
2004-03-12 01:09:38 +00:00
|
|
|
/* First, handle non-fatal termination signals. */
|
|
|
|
|
SignalHandler::OnClose( DoCleanShutdown );
|
|
|
|
|
|
2003-07-29 21:00:35 +00:00
|
|
|
#if defined(CRASH_HANDLER)
|
2005-12-31 04:00:41 +00:00
|
|
|
CrashHandler::CrashHandlerHandleArgs( g_argc, g_argv );
|
|
|
|
|
CrashHandler::InitializeCrashHandler();
|
2004-03-12 02:58:54 +00:00
|
|
|
SignalHandler::OnClose( DoCrashSignalHandler );
|
2003-07-24 08:19:20 +00:00
|
|
|
#endif
|
2003-07-29 21:00:35 +00:00
|
|
|
|
|
|
|
|
/* Set up EmergencyShutdown, to try to shut down the window if we crash.
|
|
|
|
|
* This might blow up, so be sure to do it after the crash handler. */
|
|
|
|
|
SignalHandler::OnClose( EmergencyShutdown );
|
2004-09-09 03:22:24 +00:00
|
|
|
|
2004-12-01 02:45:14 +00:00
|
|
|
InstallExceptionHandler();
|
|
|
|
|
|
2004-09-09 03:22:24 +00:00
|
|
|
#if defined(HAVE_TLS)
|
|
|
|
|
TestTLS();
|
|
|
|
|
#endif
|
2003-07-24 08:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-28 02:04:23 +00:00
|
|
|
#ifndef _CS_GNU_LIBC_VERSION
|
|
|
|
|
#define _CS_GNU_LIBC_VERSION 2
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
static RString LibcVersion()
|
2004-03-28 02:04:23 +00:00
|
|
|
{
|
2004-03-29 20:47:29 +00:00
|
|
|
char buf[1024] = "(error)";
|
2004-03-28 02:04:23 +00:00
|
|
|
int ret = confstr( _CS_GNU_LIBC_VERSION, buf, sizeof(buf) );
|
|
|
|
|
if( ret == -1 )
|
|
|
|
|
return "(unknown)";
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-16 03:20:18 +00:00
|
|
|
void ArchHooks_Unix::DumpDebugInfo()
|
|
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sys;
|
2003-11-28 21:34:05 +00:00
|
|
|
int vers;
|
|
|
|
|
GetKernel( sys, vers );
|
2004-01-02 07:01:27 +00:00
|
|
|
LOG->Info( "OS: %s ver %06i", sys.c_str(), vers );
|
2003-11-28 21:34:05 +00:00
|
|
|
|
2003-09-16 03:20:18 +00:00
|
|
|
#if defined(CRASH_HANDLER)
|
|
|
|
|
LOG->Info( "Crash backtrace component: %s", BACKTRACE_METHOD_TEXT );
|
|
|
|
|
LOG->Info( "Crash lookup component: %s", BACKTRACE_LOOKUP_METHOD_TEXT );
|
2003-12-22 02:16:33 +00:00
|
|
|
#if defined(BACKTRACE_DEMANGLE_METHOD_TEXT)
|
2003-12-22 02:16:04 +00:00
|
|
|
LOG->Info( "Crash demangle component: %s", BACKTRACE_DEMANGLE_METHOD_TEXT );
|
2003-09-16 03:20:18 +00:00
|
|
|
#endif
|
2003-12-22 02:16:33 +00:00
|
|
|
#endif
|
2004-03-28 02:04:23 +00:00
|
|
|
|
|
|
|
|
LOG->Info( "Runtime library: %s", LibcVersion().c_str() );
|
|
|
|
|
LOG->Info( "Threads library: %s", ThreadsVersion().c_str() );
|
2003-09-16 03:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
2004-04-18 23:13:22 +00:00
|
|
|
void ArchHooks_Unix::SetTime( tm newtime )
|
|
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sCommand = ssprintf( "date %02d%02d%02d%02d%04d.%02d",
|
2004-04-18 23:13:22 +00:00
|
|
|
newtime.tm_mon+1,
|
|
|
|
|
newtime.tm_mday,
|
|
|
|
|
newtime.tm_hour,
|
|
|
|
|
newtime.tm_min,
|
|
|
|
|
newtime.tm_year+1900,
|
|
|
|
|
newtime.tm_sec );
|
|
|
|
|
|
|
|
|
|
LOG->Trace( "executing '%s'", sCommand.c_str() );
|
|
|
|
|
system( sCommand );
|
2004-05-27 02:41:48 +00:00
|
|
|
|
|
|
|
|
system( "hwclock --systohc" );
|
2004-04-18 23:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
2005-12-10 18:04:43 +00:00
|
|
|
#include "RageFileManager.h"
|
|
|
|
|
#include <sys/stat.h>
|
2005-12-08 21:36:03 +00:00
|
|
|
|
2005-12-20 08:35:47 +00:00
|
|
|
static LocalizedString COULDNT_FIND_SONGS( "ArchHooks_Unix", "Couldn't find 'Songs'" );
|
2006-01-22 01:00:06 +00:00
|
|
|
void ArchHooks::MountInitialFilesystems( const RString &sDirOfExecutable )
|
2005-12-08 21:36:03 +00:00
|
|
|
{
|
|
|
|
|
#if defined(LINUX)
|
|
|
|
|
/* Mount the root filesystem, so we can read files in /proc, /etc, and so on.
|
|
|
|
|
* This is /rootfs, not /root, to avoid confusion with root's home directory. */
|
2005-12-10 18:04:43 +00:00
|
|
|
FILEMAN->Mount( "dir", "/", "/rootfs" );
|
2005-12-08 21:36:03 +00:00
|
|
|
|
|
|
|
|
/* Mount /proc, so Alsa9Buf::GetSoundCardDebugInfo() and others can access it.
|
|
|
|
|
* (Deprecated; use rootfs.) */
|
2005-12-10 18:04:43 +00:00
|
|
|
FILEMAN->Mount( "dir", "/proc", "/proc" );
|
2005-12-08 21:36:03 +00:00
|
|
|
|
|
|
|
|
/* We can almost do this, to have machine profiles be system-global to eg. share
|
|
|
|
|
* scores. It would need to handle permissions properly. */
|
|
|
|
|
/* RageFileManager::Mount( "dir", "/var/lib/games/stepmania", "/Save/Profiles" ); */
|
|
|
|
|
|
2006-02-26 00:31:31 +00:00
|
|
|
// RString Home = getenv( "HOME" ) + "/" + PRODUCT_ID;
|
2005-12-08 21:36:03 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Next: path to write general mutable user data. If the above path fails (eg.
|
|
|
|
|
* wrong permissions, doesn't exist), machine memcard data will also go in here.
|
|
|
|
|
* XXX: It seems silly to have two ~ directories. If we're going to create a
|
|
|
|
|
* directory on our own, it seems like it should be a dot directory, but it
|
|
|
|
|
* seems wrong to put lots of data (eg. music) in one. Hmm.
|
|
|
|
|
*/
|
|
|
|
|
/* XXX: create */
|
2006-02-26 00:31:31 +00:00
|
|
|
/* RageFileManager::Mount( "dir", Home + "." PRODUCT_ID, "/Data" ); */
|
2005-12-08 21:36:03 +00:00
|
|
|
|
|
|
|
|
/* Next, search ~/StepMania. This is where users can put music, themes, etc. */
|
2006-02-26 00:31:31 +00:00
|
|
|
/* RageFileManager::Mount( "dir", Home + PRODUCT_ID, "/" ); */
|
2005-12-08 21:36:03 +00:00
|
|
|
|
|
|
|
|
/* Search for a directory with "Songs" in it. Be careful: the CWD is likely to
|
|
|
|
|
* be ~, and it's possible that some users will have a ~/Songs/ directory that
|
|
|
|
|
* has nothing to do with us, so check the initial directory last. */
|
2006-01-22 01:00:06 +00:00
|
|
|
RString Root = "";
|
2005-12-08 21:36:03 +00:00
|
|
|
struct stat st;
|
|
|
|
|
if( Root == "" && !stat( sDirOfExecutable + "/Songs", &st ) && st.st_mode&S_IFDIR )
|
|
|
|
|
Root = sDirOfExecutable;
|
2005-12-10 18:04:43 +00:00
|
|
|
if( Root == "" && !stat( RageFileManagerUtil::sInitialWorkingDirectory + "/Songs", &st ) && st.st_mode&S_IFDIR )
|
|
|
|
|
Root = RageFileManagerUtil::sInitialWorkingDirectory;
|
2005-12-08 21:36:03 +00:00
|
|
|
if( Root == "" )
|
2005-12-30 05:48:45 +00:00
|
|
|
RageException::Throw( COULDNT_FIND_SONGS.GetValue() );
|
2005-12-08 21:36:03 +00:00
|
|
|
|
2005-12-10 18:04:43 +00:00
|
|
|
FILEMAN->Mount( "dir", Root, "/" );
|
2005-12-08 21:36:03 +00:00
|
|
|
#else
|
|
|
|
|
/* Paths relative to the CWD: */
|
2005-12-10 18:04:43 +00:00
|
|
|
FILEMAN->Mount( "dir", ".", "/" );
|
2005-12-08 21:36:03 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-24 08:19:20 +00:00
|
|
|
/*
|
2004-05-15 21:53:06 +00:00
|
|
|
* (c) 2003-2004 Glenn Maynard
|
|
|
|
|
* 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.
|
2003-07-24 08:19:20 +00:00
|
|
|
*/
|