making stuff a bit more readable

develop
Petr Mrázek 2009-12-25 16:55:10 +00:00
parent de7c2185eb
commit b0d65de269
1 changed files with 76 additions and 60 deletions

@ -125,45 +125,49 @@ SHMProcess::SHMProcess(vector <memory_info> & known_versions)
*/ */
if ((d->my_shmid = shmget(SHM_KEY, SHM_SIZE, 0666)) < 0) if ((d->my_shmid = shmget(SHM_KEY, SHM_SIZE, 0666)) < 0)
{ {
perror("shmget"); return;
} }
else
{ /*
* Attach the segment
*/
if ((d->my_shm = (char *) shmat(d->my_shmid, NULL, 0)) == (char *) -1) if ((d->my_shm = (char *) shmat(d->my_shmid, NULL, 0)) == (char *) -1)
{ {
perror("shmat"); return;
} }
else
{
struct shmid_ds descriptor;
shmctl(d->my_shmid, IPC_STAT, &descriptor);
/* /*
* Now we attach the segment to our data space. * Check if there are two processes connected to the segment
*/ */
struct shmid_ds descriptor;
shmctl(d->my_shmid, IPC_STAT, &descriptor);
if(descriptor.shm_nattch != 2)// badness if(descriptor.shm_nattch != 2)// badness
{ {
fprintf(stderr,"dfhack: no DF or different client already connected\n"); fprintf(stderr,"dfhack: no DF or different client already connected\n");
return;
} }
else
{ /*
* Test bridge version, will also detect when we connect to something that doesn't respond
*/
bool bridgeOK; bool bridgeOK;
if(!d->DF_TestBridgeVersion(bridgeOK)) if(!d->DF_TestBridgeVersion(bridgeOK))
{ {
fprintf(stderr,"DF terminated during reading\n"); fprintf(stderr,"DF terminated during reading\n");
return;
} }
else
{
if(!bridgeOK) if(!bridgeOK)
{ {
fprintf(stderr,"SHM bridge version mismatch\n"); fprintf(stderr,"SHM bridge version mismatch\n");
return;
} }
else /*
{ * get the PID from DF
*/
if(d->DF_GetPID(d->my_pid)) if(d->DF_GetPID(d->my_pid))
{ {
// find its binary
sprintf(exe_link_name,"/proc/%d/exe", d->my_pid); sprintf(exe_link_name,"/proc/%d/exe", d->my_pid);
// resolve /proc/PID/exe link
target_result = readlink(exe_link_name, target_name, sizeof(target_name)-1); target_result = readlink(exe_link_name, target_name, sizeof(target_name)-1);
if (target_result == -1) if (target_result == -1)
{ {
@ -171,21 +175,16 @@ SHMProcess::SHMProcess(vector <memory_info> & known_versions)
return; return;
} }
// make sure we have a null terminated string... // make sure we have a null terminated string...
// see http://www.opengroup.org/onlinepubs/000095399/functions/readlink.html
target_name[target_result] = 0; target_name[target_result] = 0;
// is this the regular linux DF? // try to identify the DF version
// create linux process, add it to the vector
d->validate(target_name, d->my_pid, known_versions); d->validate(target_name, d->my_pid, known_versions);
d->my_window = new DFWindow(this); d->my_window = new DFWindow(this);
} }
} // at this point, DF is stopped and waiting for commands. make it run again
}
// make sure we restart the process
((shm_cmd *)d->my_shm)->pingpong = DFPP_RUNNING; ((shm_cmd *)d->my_shm)->pingpong = DFPP_RUNNING;
} }
}
}
}
bool SHMProcess::isSuspended() bool SHMProcess::isSuspended()
{ {
@ -233,9 +232,13 @@ SHMProcess::~SHMProcess()
} }
// destroy data model. this is assigned by processmanager // destroy data model. this is assigned by processmanager
if(d->my_datamodel) if(d->my_datamodel)
{
delete d->my_datamodel; delete d->my_datamodel;
}
if(d->my_window) if(d->my_window)
{
delete d->my_window; delete d->my_window;
}
delete d; delete d;
} }
@ -297,12 +300,18 @@ bool SHMProcess::suspend()
{ {
int status; int status;
if(!d->attached) if(!d->attached)
{
return false; return false;
}
if(d->suspended) if(d->suspended)
{
return true; return true;
}
((shm_cmd *)d->my_shm)->pingpong = DFPP_SUSPEND; ((shm_cmd *)d->my_shm)->pingpong = DFPP_SUSPEND;
if(!d->waitWhile(DFPP_SUSPEND)) if(!d->waitWhile(DFPP_SUSPEND))
{
return false; return false;
}
d->suspended = true; d->suspended = true;
return true; return true;
} }
@ -346,10 +355,17 @@ bool SHMProcess::attach()
bool SHMProcess::detach() bool SHMProcess::detach()
{ {
if(!d->attached) return false; if(!d->attached)
if(d->suspended) resume(); {
return false;
}
if(d->suspended)
{
resume();
}
d->attached = false; d->attached = false;
d->suspended = false; d->suspended = false;
return true;
} }