2009-12-22 14:19:39 -07:00
|
|
|
/*
|
|
|
|
www.sourceforge.net/projects/dfhack
|
|
|
|
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2011-02-23 02:02:14 -07:00
|
|
|
#include "SHMProcess.h"
|
2010-08-20 06:10:05 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFError.h"
|
2010-04-04 16:48:19 -06:00
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <time.h>
|
2010-04-02 09:27:35 -06:00
|
|
|
#include <shms.h>
|
|
|
|
#include <mod-core.h>
|
2009-12-26 20:51:54 -07:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
2010-02-28 10:08:44 -07:00
|
|
|
#include <sched.h>
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-02-23 02:02:14 -07:00
|
|
|
SHMProcess::Private::Private(SHMProcess * self_)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2011-02-23 02:02:14 -07:00
|
|
|
memdescriptor = NULL;
|
|
|
|
process_ID = 0;
|
|
|
|
shm_ID = -1;
|
|
|
|
attached = false;
|
|
|
|
identified = false;
|
|
|
|
useYield = false;
|
|
|
|
server_lock = -1;
|
|
|
|
client_lock = -1;
|
|
|
|
suspend_lock = -1;
|
|
|
|
locked = false;
|
|
|
|
self = self_;
|
|
|
|
}
|
2010-03-07 21:15:11 -07:00
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
bool SHMProcess::Private::SetAndWait (uint32_t state)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
uint32_t cnt = 0;
|
2010-03-12 10:29:11 -07:00
|
|
|
if(!attached) return false;
|
2010-03-11 16:13:50 -07:00
|
|
|
SHMCMD = state;
|
2011-02-23 00:26:55 -07:00
|
|
|
|
2010-03-02 21:43:38 -07:00
|
|
|
while (SHMCMD == state)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2011-02-23 00:26:55 -07:00
|
|
|
// yield the CPU, only on single-core CPUs
|
|
|
|
if(useYield)
|
|
|
|
{
|
|
|
|
SCHED_YIELD
|
|
|
|
}
|
2010-03-20 10:30:13 -06:00
|
|
|
if(cnt == 10000)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2011-02-23 00:26:55 -07:00
|
|
|
if(!AreLocksOk())// DF not there anymore?
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
|
|
|
//detach the shared memory
|
2010-03-11 16:13:50 -07:00
|
|
|
shmdt(shm_addr);
|
2010-03-13 09:44:36 -07:00
|
|
|
FreeLocks();
|
|
|
|
attached = locked = identified = false;
|
2010-03-09 13:25:17 -07:00
|
|
|
// we aren't the current process anymore
|
|
|
|
throw Error::SHMServerDisappeared();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cnt = 0;
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
}
|
2010-03-11 16:13:50 -07:00
|
|
|
// server returned a generic error
|
2010-03-07 21:15:11 -07:00
|
|
|
if(SHMCMD == CORE_ERROR)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-04 16:05:01 -07:00
|
|
|
/*
|
2010-03-20 10:30:13 -06:00
|
|
|
Yeah. with no way to synchronize things (locks are slow, the OS doesn't give us
|
|
|
|
enough control over scheduling)
|
2010-03-04 16:05:01 -07:00
|
|
|
we end up with this silly thing
|
|
|
|
*/
|
2010-03-11 16:13:50 -07:00
|
|
|
bool SHMProcess::SetAndWait (uint32_t state)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
return d->SetAndWait(state);
|
2010-03-04 16:05:01 -07:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:40:06 -07:00
|
|
|
uint32_t OS_getAffinity()
|
|
|
|
{
|
|
|
|
cpu_set_t mask;
|
|
|
|
sched_getaffinity(0,sizeof(cpu_set_t),&mask);
|
|
|
|
// FIXME: truncation
|
2010-03-07 21:15:11 -07:00
|
|
|
uint32_t affinity = *(uint32_t *) &mask;
|
|
|
|
return affinity;
|
2010-03-03 20:40:06 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 13:25:17 -07:00
|
|
|
void SHMProcess::Private::FreeLocks()
|
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
attachmentIdx = -1;
|
|
|
|
if(client_lock != -1)
|
|
|
|
{
|
|
|
|
lockf(client_lock,F_ULOCK,0);
|
|
|
|
close(client_lock);
|
|
|
|
client_lock = -1;
|
|
|
|
}
|
|
|
|
if(server_lock != -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
close(server_lock);
|
|
|
|
server_lock = -1;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
2010-03-11 16:13:50 -07:00
|
|
|
if(suspend_lock != -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
close(suspend_lock);
|
|
|
|
locked = false;
|
|
|
|
suspend_lock = -1;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SHMProcess::Private::GetLocks()
|
|
|
|
{
|
|
|
|
char name[256];
|
|
|
|
// try to acquire locks
|
|
|
|
// look at the server lock, if it's locked, the server is present
|
2010-03-11 16:13:50 -07:00
|
|
|
sprintf(name, "/tmp/DFHack/%d/SVlock",process_ID);
|
|
|
|
server_lock = open(name,O_WRONLY);
|
|
|
|
if(server_lock == -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
// cerr << "can't open sv lock" << endl;
|
2010-03-09 13:25:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
if(lockf( server_lock, F_TEST, 0 ) != -1)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
cerr << "sv lock not locked" << endl;
|
|
|
|
close(server_lock);
|
|
|
|
server_lock = -1;
|
2010-03-09 13:25:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-03-11 16:13:50 -07:00
|
|
|
for(int i = 0; i < SHM_MAX_CLIENTS; i++)
|
2010-03-09 13:25:17 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
// open the client suspend locked
|
|
|
|
sprintf(name, "/tmp/DFHack/%d/CLSlock%d",process_ID,i);
|
|
|
|
suspend_lock = open(name,O_WRONLY);
|
|
|
|
if(suspend_lock == -1)
|
|
|
|
{
|
|
|
|
cerr << "can't open cl S-lock " << i << endl;
|
|
|
|
// couldn't open lock
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the client lock, try to lock it
|
|
|
|
sprintf(name, "/tmp/DFHack/%d/CLlock%d",process_ID,i);
|
|
|
|
client_lock = open(name,O_WRONLY);
|
|
|
|
if(client_lock == -1)
|
|
|
|
{
|
|
|
|
cerr << "can't open cl lock " << i << endl;
|
|
|
|
close(suspend_lock);
|
|
|
|
locked = false;
|
|
|
|
suspend_lock = -1;
|
|
|
|
// couldn't open lock
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(lockf(client_lock,F_TLOCK, 0) == -1)
|
|
|
|
{
|
|
|
|
// couldn't acquire lock
|
|
|
|
cerr << "can't acquire cl lock " << i << endl;
|
|
|
|
close(suspend_lock);
|
|
|
|
locked = false;
|
|
|
|
suspend_lock = -1;
|
|
|
|
close(client_lock);
|
|
|
|
client_lock = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// ok, we have all the locks we need!
|
|
|
|
attachmentIdx = i;
|
|
|
|
return true;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
2010-03-11 16:13:50 -07:00
|
|
|
close(server_lock);
|
|
|
|
server_lock = -1;
|
|
|
|
cerr << "can't get any client locks" << endl;
|
|
|
|
return false;
|
2010-03-09 13:25:17 -07:00
|
|
|
}
|
|
|
|
|
2011-02-23 00:26:55 -07:00
|
|
|
// test if we have client and server locks and the server is present
|
|
|
|
bool SHMProcess::Private::AreLocksOk()
|
|
|
|
{
|
|
|
|
// both locks are inited (we hold our lock)
|
|
|
|
if(client_lock != -1 && server_lock != -1)
|
|
|
|
{
|
|
|
|
if(lockf(server_lock,F_TEST,0) == -1) // and server holds its lock
|
|
|
|
{
|
|
|
|
return true; // OK, locks are good
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// locks are bad
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
|
2011-02-24 03:43:33 -07:00
|
|
|
bool SHMProcess::Private::validate(VersionInfoFactory * factory)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
char exe_link_name [256];
|
|
|
|
char target_name[1024];
|
|
|
|
int target_result;
|
|
|
|
// find the binary
|
|
|
|
sprintf(exe_link_name,"/proc/%d/exe", process_ID);
|
|
|
|
target_result = readlink(exe_link_name, target_name, sizeof(target_name)-1);
|
|
|
|
if (target_result == -1)
|
|
|
|
{
|
|
|
|
perror("readlink");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// make sure we have a null terminated string...
|
|
|
|
// see http://www.opengroup.org/onlinepubs/000095399/functions/readlink.html
|
|
|
|
target_name[target_result] = 0;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
// get hash of the running DF process
|
2011-02-24 03:43:33 -07:00
|
|
|
md5wrapper md5;
|
2010-03-13 09:44:36 -07:00
|
|
|
string hash = md5.getHashFromFile(target_name);
|
2011-02-24 03:43:33 -07:00
|
|
|
|
|
|
|
// create linux process, add it to the vector
|
|
|
|
VersionInfo * vinfo = factory->getVersionInfoByMD5(hash);
|
|
|
|
if(vinfo)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2011-02-24 03:43:33 -07:00
|
|
|
memdescriptor = vinfo;
|
|
|
|
// FIXME: BIG BAD BUG RIGHT HERE!!!!
|
|
|
|
memdescriptor->setParentProcess(self);
|
|
|
|
identified = true;
|
2011-03-24 23:40:33 -06:00
|
|
|
vector_start = memdescriptor->getGroup("vector")->getOffset("start");
|
2011-02-24 03:43:33 -07:00
|
|
|
return true;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
// there is only one we care about.
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::getThreadIDs(vector<uint32_t> & threads )
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->attached)
|
|
|
|
{
|
|
|
|
threads.clear();
|
|
|
|
threads.push_back(d->process_ID);
|
|
|
|
return true;
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: cross-reference with ELF segment entries?
|
2010-03-09 07:15:15 -07:00
|
|
|
void SHMProcess::getMemRanges( vector<t_memrange> & ranges )
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
char permissions[5]; // r/-, w/-, x/-, p/s, 0
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2011-03-01 15:36:26 -07:00
|
|
|
sprintf(buffer, "/proc/%lu/maps", (long unsigned)d->process_ID);
|
2009-12-22 14:19:39 -07:00
|
|
|
FILE *mapFile = ::fopen(buffer, "r");
|
|
|
|
uint64_t offset, device1, device2, node;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
while (fgets(buffer, 1024, mapFile))
|
|
|
|
{
|
|
|
|
t_memrange temp;
|
|
|
|
temp.name[0] = 0;
|
2011-03-01 15:36:26 -07:00
|
|
|
sscanf(buffer, "%zx-%zx %s %zx %2zu:%2zu %zu %s",
|
2009-12-22 14:19:39 -07:00
|
|
|
&temp.start,
|
|
|
|
&temp.end,
|
|
|
|
(char*)&permissions,
|
|
|
|
&offset, &device1, &device2, &node,
|
|
|
|
(char*)&temp.name);
|
|
|
|
temp.read = permissions[0] == 'r';
|
|
|
|
temp.write = permissions[1] == 'w';
|
|
|
|
temp.execute = permissions[2] == 'x';
|
2011-02-13 19:58:32 -07:00
|
|
|
temp.valid = true;
|
2009-12-22 14:19:39 -07:00
|
|
|
ranges.push_back(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-23 02:02:14 -07:00
|
|
|
bool SHMProcess::acquireSuspendLock()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2011-02-23 02:02:14 -07:00
|
|
|
return (lockf(d->suspend_lock,F_LOCK,0) == 0);
|
2010-01-01 19:25:21 -07:00
|
|
|
}
|
|
|
|
|
2011-02-23 02:02:14 -07:00
|
|
|
bool SHMProcess::releaseSuspendLock()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2011-02-23 02:02:14 -07:00
|
|
|
return (lockf(d->suspend_lock,F_ULOCK,0) == 0);
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::attach()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-04-18 13:30:02 -06:00
|
|
|
if(d->attached)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-04-18 13:30:02 -06:00
|
|
|
if(!d->locked)
|
|
|
|
return suspend();
|
|
|
|
return true;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2011-02-23 02:02:14 -07:00
|
|
|
//cerr << "attach" << endl;// FIXME: throw
|
2010-03-09 13:25:17 -07:00
|
|
|
if(!d->GetLocks())
|
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
//cerr << "server is full or not really there!" << endl;
|
2010-03-09 13:25:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-13 09:44:36 -07:00
|
|
|
/*
|
|
|
|
* Locate the segment.
|
|
|
|
*/
|
|
|
|
if ((d->shm_ID = shmget(SHM_KEY + d->process_ID, SHM_SIZE, 0666)) < 0)
|
|
|
|
{
|
|
|
|
d->FreeLocks();
|
|
|
|
cerr << "can't find segment" << endl; // FIXME: throw
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-12-26 20:51:54 -07:00
|
|
|
/*
|
|
|
|
* Attach the segment
|
|
|
|
*/
|
2010-03-12 04:14:20 -07:00
|
|
|
if ((d->shm_addr = (char *) shmat(d->shm_ID, NULL, 0)) == (char *) -1)
|
|
|
|
{
|
|
|
|
d->FreeLocks();
|
2010-03-13 09:44:36 -07:00
|
|
|
cerr << "can't attach segment" << endl; // FIXME: throw
|
2010-03-12 04:14:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
d->attached = true;
|
|
|
|
if(!suspend())
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
shmdt(d->shm_addr);
|
2010-03-09 13:25:17 -07:00
|
|
|
d->FreeLocks();
|
2010-03-12 04:14:20 -07:00
|
|
|
cerr << "unable to suspend" << endl;
|
2009-12-26 20:51:54 -07:00
|
|
|
return false;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
2010-03-12 04:14:20 -07:00
|
|
|
return true;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
bool SHMProcess::detach()
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-08-13 17:25:18 -06:00
|
|
|
if(!d->attached) return true;
|
2010-03-13 09:44:36 -07:00
|
|
|
if(d->locked)
|
2009-12-25 09:55:10 -07:00
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
2010-01-01 15:19:09 -07:00
|
|
|
// detach segment
|
2010-03-11 16:13:50 -07:00
|
|
|
if(shmdt(d->shm_addr) != -1)
|
2010-01-01 15:19:09 -07:00
|
|
|
{
|
2010-03-13 09:44:36 -07:00
|
|
|
d->FreeLocks();
|
|
|
|
d->locked = false;
|
2010-01-01 15:19:09 -07:00
|
|
|
d->attached = false;
|
2010-03-11 16:13:50 -07:00
|
|
|
d->shm_addr = 0;
|
2010-01-01 15:19:09 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fail if we can't detach
|
2010-03-09 13:25:17 -07:00
|
|
|
// FIXME: throw exception here??
|
2010-01-01 15:19:09 -07:00
|
|
|
perror("failed to detach shared segment");
|
|
|
|
return false;
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2011-03-24 23:40:33 -06:00
|
|
|
void SHMProcess::readSTLVector(const uint32_t address, t_vecTriplet & triplet)
|
|
|
|
{
|
|
|
|
read(address + d->vector_start, sizeof(triplet), (uint8_t *) &triplet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
string SHMProcess::readClassName (uint32_t vptr)
|
2010-02-18 18:48:03 -07:00
|
|
|
{
|
2011-03-27 18:09:13 -06:00
|
|
|
if(!d->locked) throw Error::MemoryAccessDenied(vptr);
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2011-02-22 23:50:21 -07:00
|
|
|
int typeinfo = Process::readDWord(vptr - 0x4);
|
|
|
|
int typestring = Process::readDWord(typeinfo + 0x4);
|
2010-02-18 18:48:03 -07:00
|
|
|
string raw = readCString(typestring);
|
|
|
|
size_t start = raw.find_first_of("abcdefghijklmnopqrstuvwxyz");// trim numbers
|
|
|
|
size_t end = raw.length();
|
2010-04-09 06:44:00 -06:00
|
|
|
return raw.substr(start,end-start);
|
2010-03-04 16:05:01 -07:00
|
|
|
}
|
|
|
|
|
2011-02-23 00:26:55 -07:00
|
|
|
string SHMProcess::getPath()
|
|
|
|
{
|
|
|
|
char cwd_name[256];
|
|
|
|
char target_name[1024];
|
|
|
|
int target_result;
|
|
|
|
|
|
|
|
sprintf(cwd_name,"/proc/%d/cwd", getPID());
|
|
|
|
// resolve /proc/PID/exe link
|
|
|
|
target_result = readlink(cwd_name, target_name, sizeof(target_name));
|
|
|
|
target_name[target_result] = '\0';
|
|
|
|
return(string(target_name));
|
|
|
|
}
|
|
|
|
|
2010-03-09 07:15:15 -07:00
|
|
|
char * SHMProcess::getSHMStart (void)
|
2010-03-04 16:05:01 -07:00
|
|
|
{
|
2010-03-11 16:13:50 -07:00
|
|
|
if(!d->locked) return 0; //THROW HERE!
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2011-02-23 02:02:14 -07:00
|
|
|
return d->shm_addr;
|
2010-04-20 10:13:00 -06:00
|
|
|
}
|