1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
#include <stdio.h>
#include <vdk/forms.h>
#include "vdkchildtask.h"
#include <ctime>
#define VERBOSE 1
VDKPid::VDKPid (pid_t pid, VDKChildTask *task):
pid (pid), task (task)
{
}
/*
*/
DEFINE_SIGNAL_LIST (VDKChildTask, VDKObject);
PIDLIST VDKChildTask::PidList = PIDLIST();
/*
*/
VDKChildTask::VDKChildTask (VDKForm* owner):
VDKObject (owner)
{
/*
even if isn't really a widget we add it to gc and signals
handling
*/
owner->Objects().add(this);
Parent(owner);
/*
allocates empty data_buffer
*/
data_buffer = NULL;
/*
*/
struct sigaction sac;
sigemptyset(&(sac.sa_mask));
sac.sa_flags=0;
sac.sa_handler = VDKChildTask::reaper;
sigaction(SIGCHLD, &sac, NULL);
for(int i = 0; i < NSIG; i++)
{
if(i != SIGQUIT && i != SIGKILL && i != SIGTERM && i != SIGCHLD &&
i != SIGSTOP && i != SIGCONT && i != SIGTSTP)
{
sigemptyset(&(sac.sa_mask));
sac.sa_flags=0;
sac.sa_handler = VDKChildTask::plumber;
sigaction(i, &sac, NULL);
}
}
Pid (-1);
Xmit (-1);
}
/*
*/
VDKChildTask::~VDKChildTask ()
{
if (pid > 0)
{
kill (pid, SIGTERM);
VDKPid* vdkpid = NULL;
VDKPid _pid(pid);
if( (vdkpid = VDKChildTask::PidList.find (_pid)) != NULL)
{
int ndx = VDKChildTask::PidList.at (*vdkpid);
if (ndx >= 0)
{
#if VERBOSE
printf ("\nchild dies - pid:%d - removed at:%d",
vdkpid->pid, ndx);
fflush (stdout);
#endif
vdkpid->task->SignalEmit (CHILD_TASK_DIED_SIGNAL);
vdkpid->task->SignalEmit ("child_task_died");
vdkpid->task->Pid (-1);
vdkpid->task->Xmit (-1);
VDKChildTask::PidList.unlink (ndx);
}
}
}
if (data_buffer)
delete[] data_buffer;
}
/*
*/
void VDKChildTask::plumber(int sig)
{
#if VERBOSE
printf ("\nOtherwise unhandled Unix Signal:%d ",sig);
fflush (stdout);
#endif
}
void VDKChildTask::reaper(int)
{
int sts;
int local_pid;
VDKPid* vdkpid = NULL;
local_pid = waitpid(-1, &sts, WNOHANG);
#if VERBOSE
printf ("\nreaper () - pid:%d", local_pid);
fflush (stdout);
#endif
VDKPid _pid(local_pid);
if( (vdkpid = VDKChildTask::PidList.find (_pid)) != NULL)
{
int ndx = VDKChildTask::PidList.at (_pid);
if (ndx >= 0)
{
#if VERBOSE
printf ("\nchild dies - pid:%d - status:%d - removed at:%d",
vdkpid->pid, sts, ndx);
fflush (stdout);
#endif
vdkpid->task->SignalEmit (CHILD_TASK_DIED_SIGNAL);
vdkpid->task->SignalEmit ("child_task_died");
vdkpid->task->Pid (-1);
vdkpid->task->Xmit (-1);
VDKChildTask::PidList.unlink (ndx);
}
}
}
pid_t VDKChildTask::StartChild(char* args[], bool duplex)
{
int inpipe[2], outpipe[2];
if (Pid () >= 0)
{
#if VERBOSE
printf ("\nStartChild () aborted child running");
fflush (stdout);
#endif
return -1;
}
else
{
#if VERBOSE
printf ("\nStartChild ()");
fflush (stdout);
#endif
}
if(access (args[0], X_OK) == 0)
{
sigset_t mask,omask;
// We don't want to get any signals while setting up the task lists
sigemptyset(&mask);
sigaddset(&mask,SIGCHLD);
sigprocmask(SIG_BLOCK,&mask,&omask);
if (duplex)
pipe(inpipe);
pipe(outpipe);
pid = fork();
switch(pid)
{
// Child
case 0:
if (duplex)
dup2(inpipe[0], 0);
else
close (0);
dup2(outpipe[1], 1);
dup2(outpipe[1], 2);
execvp(args[0], args);
break;
case -1:
break;
default:
// Parent
close(outpipe[1]); // Important! Close my end here
if (duplex)
{
close(inpipe[0]); // Important! Close my end here
xmit = inpipe[1];
}
// Set pipes none blocking, so we can read big buffers
// in the callback without having to use FIONREAD
// to make sure the callback doesn't block.
/*
int md;
if((md = fcntl(outpipe[0], F_GETFL)) != -1)
fcntl(outpipe[0], F_SETFL, O_NONBLOCK | md );
*/
VDKInputChannel *inpch = new VDKInputChannel(Owner (),outpipe[0]);
inpch->Parent (this);
SignalConnect (inpch, "input_signal", &VDKChildTask::DoIO, false);
#if VERBOSE
printf ("\npid =%d", pid);
fflush (stdout);
#endif
VDKPid vdkpid (pid, this);
if (!VDKChildTask::PidList.find (vdkpid))
{
VDKChildTask::PidList.add (vdkpid);
#if VERBOSE
printf ("\nadded pid =%d", pid);
fflush (stdout);
#endif
}
break;
}
sigprocmask(SIG_UNBLOCK ,&mask ,NULL);
}
else
{
;
#if VERBOSE
char x[256];
sprintf(x, "Can't execute \"%s\"",
(args[0]) ? args[0] : "????");
puts (x);
fflush (stdout);
return -1;
#endif
}
#if VERBOSE
printf("\nReturning pid %x\n", Pid());
fflush (stdout);
#endif
return Pid ();
}
/*
*/
bool VDKChildTask::DoIO (VDKObject *obj)
{
const int DATA_BUFFER_SIZE = 1024;
VDKInputChannel *ip = dynamic_cast<VDKInputChannel*>(obj);
if (!ip)
return true;
int res;
// char * buf = GetData ();
if (data_buffer)
delete[] data_buffer;
data_buffer = new char[DATA_BUFFER_SIZE];
res = read (ip->getfd(), data_buffer, DATA_BUFFER_SIZE);
if(res > 0)
{
data_buffer[res] = '\0';
SignalEmit (CHILD_TASK_DATA_SIGNAL);
SignalEmit ("child_task_data");
}
else if(res == 0)
{
close(ip->getfd());
ip->Destroy();
}
return true;
}
/*
*/
bool
VDKChildTask::SendData(char* data)
{
char *txt;
char *t;
bool result = false;
if(xmit < 0)
return result;
else
{
txt = new char[strlen (data+1)];
strcpy(txt,data);
t = txt+strlen(txt);
write(xmit, txt, (t - txt));
result = true;
}
delete[] txt;
return result;
}
|