summaryrefslogtreecommitdiff
path: root/lib/TaskMan.cc
blob: 4f18a85f183327c57d626eb1061473cbbd68555e (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
 *  Baltisot
 *  Copyright (C) 1999-2007 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* $Id: TaskMan.cc,v 1.46 2007-05-30 11:57:10 pixel Exp $ */

#ifndef _WIN32
#include <signal.h>
#include <sys/wait.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <unistd.h>
#endif
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <vector>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "TaskMan.h"
#include "gettext.h"

TaskMan::TaskList_t TaskMan::TaskList;
TaskMan::TaskList_t TaskMan::Zombies;
std::vector<TaskMan::w4ha_t> TaskMan::w4ha;
std::vector<TaskMan::w4pr_t> TaskMan::w4pr;
std::vector<TaskMan::w4to_t> TaskMan::w4to;
bool TaskMan::stopped = false;

int TaskMan::number = 0;
int TaskMan::got_sigchild = 0;
bool TaskMan::inited = false;

int TaskMan::event, TaskMan::eprocess, TaskMan::estatus;
Task * TaskMan::etask;
Handle * TaskMan::ehandle;

sigset_t TaskMan::sigchildset;

#ifndef _WIN32
void taskman_sigchild(int sig) {
    TaskMan::SigChild();
    signal(SIGCHLD, taskman_sigchild);
}

void taskman_sigpipe(int sig) {
    signal(sig, taskman_sigpipe);
}
void taskman_sighup(int sig) {
    signal(sig, taskman_sighup);
}
#endif

void TaskMan::SigChild() {
    int status;
    pid_t pid;
    
    pid = wait(&status);
    if (GotChild(pid, status)) {
	got_sigchild++;
    } else {
	WaitFor(pid, 0, status);
    }
    
//    cerr << "Got SIGCHILD, pid = " << pid << " and status = " << status << endl;
}

// Windows implementations of poll and gettimeofday.
#ifdef _WIN32
#define POLLIN  1       /* Set if data to read. */
#define POLLPRI 2       /* Set if urgent data to read. */
#define POLLOUT 4       /* Set if writing data wouldn't block. */
#define POLLERR   8     /* An error occured. */
#define POLLHUP  16     /* Shutdown or close happened. */
#define POLLNVAL 32     /* Invalid file descriptor. */

#define NPOLLFILE 64    /* Number of canonical fd's in one call to poll(). */

/* The following values are defined by XPG4. */
#define POLLRDNORM POLLIN
#define POLLRDBAND POLLPRI
#define POLLWRNORM POLLOUT
#define POLLWRBAND POLLOUT

struct pollfd {
  int fd;
  short events;
  short revents;
};

static int poll (struct pollfd *fds, unsigned int nfds, int timeout) {
    fd_set read_fds, write_fds, except_fds;
    struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
    int max_fd = 0, retval, changedfds;
    unsigned int i;
    int n_non_socket = 0;

    FD_ZERO(&read_fds);
    FD_ZERO(&write_fds);
    FD_ZERO(&except_fds);

    for (int i = 0; i < nfds; i++) {
        if (!fds[i].fd && !fds[i].events)
            continue;

        BOOL dummy;
        int dummy_s = sizeof(dummy);

        if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ACCEPTCONN, (char *) &dummy, &dummy_s) == SOCKET_ERROR) {
            if (WSAGetLastError() == WSAENOTSOCK) {
                if (fds[i].events & POLLIN) {
                    n_non_socket++;
                    fds[i].revents |= POLLIN;
                }
                continue;
            }
        }

        if (fds[i].fd > max_fd)
            max_fd = fds[i].fd;

        if (fds[i].events & POLLIN)
            FD_SET(fds[i].fd, &read_fds);

        if (fds[i].events & POLLOUT)
            FD_SET(fds[i].fd, &write_fds);

        FD_SET(fds[i].fd, &except_fds);

        fds[i].revents = 0;
    }

    if (n_non_socket) {
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        timeout = 0;
    }

    changedfds = retval = select(max_fd + 1, &read_fds, &write_fds, &except_fds, timeout < 0 ? NULL : &tv);

    if (retval <= 0) {
        if (retval < 0) {
            int err = WSAGetLastError();
            // got timeout or error
            switch (err) {
                case WSANOTINITIALISED:
                    Base::printm(M_INFO, "WSANOTINITIALISED\n");
                    break;
                case WSAEFAULT:
                    Base::printm(M_INFO, "WSAEFAULT\n");
                    break;
                case WSAENETDOWN:
                    Base::printm(M_INFO, "WSAENETDOWN\n");
                    break;
                case WSAEINVAL:
                    Base::printm(M_INFO, "WSAEINVAL\n");
                    break;
                case WSAEINTR:
                    Base::printm(M_INFO, "WSAEINTR\n");
                    break;
                case WSAEINPROGRESS:
                    Base::printm(M_INFO, "WSAEINPROGRESS\n");
                    break;
                case WSAENOTSOCK:
                    Base::printm(M_INFO, "WSAENOTSOCK\n");
                    break;
                default:
                    Base::printm(M_INFO, "Unknown error\n");
            }
            return retval;
        } else {
            return n_non_socket;
        }
    }

    for (i = 0; i < nfds; i++) {
        if (FD_ISSET(fds[i].fd, &read_fds)) {
            fds[i].revents |= POLLIN;
            changedfds--;
        }
        
        if (FD_ISSET(fds[i].fd, &write_fds)) {
            fds[i].revents |= POLLOUT;
            changedfds--;
        }

        if (FD_ISSET(fds[i].fd, &except_fds)) {
            fds[i].revents |= POLLERR;
            changedfds--;
        }

        if (changedfds <= 0)
            break;
    }

    return retval + n_non_socket;
}

#define EPOCHFILETIME (116444736000000000i64)

// special version without timezone support...
static int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    FILETIME        ft;
    LARGE_INTEGER   li;
    __int64         t;

    if (tv)
    {
        GetSystemTimeAsFileTime(&ft);
        li.LowPart  = ft.dwLowDateTime;
        li.HighPart = ft.dwHighDateTime;
        t  = li.QuadPart;       /* In 100-nanosecond intervals */
        t -= EPOCHFILETIME;     /* Offset to the Epoch time */
        t /= 10;                /* In microseconds */
        tv->tv_sec  = (long)(t / 1000000);
        tv->tv_usec = (long)(t % 1000000);
    }

    return 0;
}
#endif

// Static class to handle operations on the timeval structure.
class TimeOps : public Base {
  public:
    static timeval Add(const timeval & t1, const timeval & t2) {
        struct timeval r;

        r.tv_usec = t1.tv_usec + t2.tv_usec;
        r.tv_sec = t1.tv_sec + t2.tv_sec;

        if (r.tv_usec >= 1000000) {
            r.tv_usec %= 1000000;
            r.tv_sec++;
        }

        return r;
    }
    static long Diff(const timeval & t1, const timeval & t2) {
        return (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000;
    }
    static bool IsLE(const timeval & t1, const timeval & t2) {
        return (t1.tv_sec <= t2.tv_sec) || ((t1.tv_sec == t2.tv_sec) && (t1.tv_usec <= t2.tv_usec));
    }
    static long ToMS(const timeval & t) {
        return t.tv_usec / 1000 + t.tv_sec * 1000;
    }
    static void Sleep(const timeval & t) {
        Sleep(ToMS(t));
    }
    static void Sleep(Uint32 t) {
#ifdef _WIN32
        ::Sleep(t);
#else
        usleep(t * 1000);
#endif
    }
};

int TaskMan::GotChild(pid_t pid, int status) {
    int r = 0;
    unsigned int i;

    for (i = 0; i < w4pr.size(); i++) {
	if (w4pr[i].pr == pid) {
	    w4pr[i].flag = true;
	    w4pr[i].status = status;
	    r = 1;
	}
    }
    
    return r;
}

void TaskMan::Init() throw (GeneralException) {
    if (inited) {
	throw GeneralException(_("Task Manager already initialised."));
    }
    
#ifndef _WIN32
    signal(SIGCHLD, taskman_sigchild);
    signal(SIGPIPE, taskman_sigpipe);
    signal(SIGHUP, taskman_sighup);
    
    sigemptyset(&sigchildset);
    sigaddset(&sigchildset, SIGCHLD);
    sigprocmask(SIG_BLOCK, &sigchildset, 0);
#endif    

    inited = true;
    number = 0;
}

void TaskMan::Stop() {
    stopped = true;
}

// Accessor of the current processed event.
int TaskMan::Event() {
    return event;
}

// Accessor of the current task triggering the EVT_TASK event.
Task * TaskMan::Etask() {
    return etask;
}

// Accessor of the current handle triggering the EVT_HANDLE event.
Handle * TaskMan::Ehandle() {
    return ehandle;
}

// accessor of the current pid_t triggering the EVT_PROCESS event.
int TaskMan::Eprocess() {
    return eprocess;
}

// Accessor of the status of the processus triggering the EVT_PROCESS event.
int TaskMan::Estatus() {
    return estatus;
}

// Should be only called by the constructor of a Task()
void TaskMan::AddTask(Task * t) {
    if (!inited) {
	Init();
    }
    
    if (t) {
	TaskList.push_back(t);
	number++;
    }
}

std::vector<Task *>::iterator TaskMan::FindTask(Task * t) throw (GeneralException) {
    if (!inited) {
	Init();
    }
    
    if (TaskList.empty())
	throw TaskNotFound();
    
    for (std::vector<Task *>::iterator p = TaskList.begin(); p != TaskList.end(); p++) {
	if (*p == t) {
	    return p;
	}
    }
    
    throw TaskNotFound();
}

// Should only be called by the destructor of a task.
void TaskMan::RemoveFromWatches(Task * t) {
    if (!w4ha.empty()) {
	for (std::vector<w4ha_t>::iterator p = w4ha.begin(); p != w4ha.end(); p++) {
	    if (p->T == t) {
		w4ha.erase(p);
		p = w4ha.begin();
                if (w4ha.empty())
                    break;
	    }
	}
    }

    if (!w4pr.empty()) {
	for (std::vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) {
	    if (p->T == t) {
		w4pr.erase(p);
		p = w4pr.begin();
                if (w4pr.empty())
                    break;
	    }
	}
    }

    if (!w4to.empty()) {
	for (std::vector<w4to_t>::iterator p = w4to.begin(); p != w4to.end(); p++) {
	    if (p->T == t) {
		w4to.erase(p);
		p = w4to.begin();
                if (w4to.empty())
                    break;
	    }
	}
    }
    
    if (!TaskList.empty()) {
	for (TaskList_t::iterator p = TaskList.begin(); p != TaskList.end(); p++) {
	    if ((*p)->WaitedBy() == t) {
		Zombies.push_back(*p);
	        (*p)->RemoveFromWatches();
		TaskList.erase(p);
		number--;
	        p = TaskList.begin();
                if (TaskList.empty())
                    break;
	    } else if ((*p) == t) {
		TaskList.erase(p);
	        number--;
		p = TaskList.begin();
                if (TaskList.empty())
                    break;
	    }
	}
    }
}

// Possibility to cancel a timeout.
void TaskMan::RemoveTimeout(Task * t) {
    if (!w4to.empty()) {
	for (std::vector<w4to_t>::iterator p = w4to.begin(); p != w4to.end(); p++) {
	    if (p->T == t) {
		w4to.erase(p);
		p = w4to.begin();
                if (w4to.empty())
                    break;
	    }
	}
    }
}

void TaskMan::WaitFor(Handle * h, Task * t, int flags) {
    h->SetNonBlock();
    w4ha.push_back(w4ha_t(h, flags, t));
}

void TaskMan::WaitFor(pid_t pid, Task * t, int status) {
    if (status == -1) {
	if (!w4pr.empty()) {
	    for (std::vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) {
		if (p->pr == pid) {
		    p->T = t;
		    p->flag = true;
		    got_sigchild++;
		    return;
		}
	    }
	}
    }
    w4pr.push_back(w4pr_t(pid, t));
    w4pr[w4pr.size() - 1].status = status;
}

void TaskMan::WaitFor(const timeval & t, Task * T, int flags) {
    struct timeval tod;
    gettimeofday(&tod, 0);
    w4to.push_back(w4to_t(TimeOps::Add(tod, t), flags, T));
}

// The big ugly mainloop.
void TaskMan::MainLoop() throw (GeneralException) {
    struct pollfd * ufsd;
    unsigned int nfds;
    
    int no_burst;
    
    if (!inited) {
	Init();
    }

    while (1) {
	if (number == 0) {
	    throw GeneralException(_("TaskMan: No more task to manage."));
	}
	
	if (stopped) return;

        // We should have a small fuse here, and check if there is no 'dead' task,
        // that is, a task which doesn't have anything to wait for, but which is
        // not a zombie.
	
//	cerr << "-=- TaskMan: begin main loop with " << number << " task to manage.\n";
	if (!TaskList.empty()) {
	    for (TaskList_t::iterator p = TaskList.begin(); p != TaskList.end(); p++) {
		Task * t = *p;
//	 	   cerr << "-=- TaskMan: task " << t->GetName() << endl;
	    }
	}
	
//	cerr << "-=- TaskMan: processing burning tasks.\n";
	
	no_burst = 0;
	while (!no_burst) {
	    no_burst = 1;
            /* First, we will check for any burning task and run 'em */
            event = Task::EVT_BURST;
	    if (!TaskList.empty()) {
		for (TaskList_t::iterator p = TaskList.begin(); p != TaskList.end(); p++) {
		    Task * t = *p;
		
		    if (t->IsStopped()) {
			continue;
		    }
		
                    if (t->GetState() == Task::TASK_BURST) {
//		    	cerr << "-=- TaskMan: running burning task " << t->GetName() << endl;
                        if (t->BurstHandle) {
                            // This task got the burst status from
                            // a non-watchable handle. Let's fake the handle event.
                            event = Task::EVT_HANDLE;
                            ehandle = t->BurstHandle;
                        }
			t->Run();
                        if (t->BurstHandle) {
                            event = Task::EVT_BURST;
                            t->BurstHandle = 0;
                        }
		        /* if the task added some new tasks, we have to rerun the loop */
			no_burst = 0;
		        break;
		    }
		
                    // Additionnally, if the task died, let's put it in the zombies list.
                    // This check is done on the whole TaskList at each loop.
                    if (CheckDead(t))
                        p = TaskList.begin();
		}
	    }
	}

	/* Let's compute the nearest timeout, and run a round trip for expired timeouts. */
	int timeout = -1;
	event = Task::EVT_TIMEOUT;
        bool got_timeout = true, ran_on_timeout = false;

	while (!w4to.empty() && got_timeout) {
            got_timeout = false;
	    struct timeval current;
            gettimeofday(&current, 0);
            timeout = -1;
	    for (std::vector<w4to_t>::iterator p = w4to.begin(); p != w4to.end(); p++) {
                if (TimeOps::IsLE(p->to, current)) {
                    // This timeout expired; run the task.
                    w4to_t w4 = *p;
		    w4to.erase(p);
                    w4.T->Run();
                    ran_on_timeout = true;
                    got_timeout = true;
                    CheckDead(w4.T);
                    break;
                } else {
                    // Otherwise, let's keep track of the smallest timeout value.
                    long diff = TimeOps::Diff(current, p->to);
                    if ((timeout == -1) || (timeout > diff)) {
                        timeout = diff;
                    }
                }
	    }
	}

//	cerr << "-=- TaskMan: processing handle-waiting tasks.\n";

        if ((w4ha.size() == 0) && !ran_on_timeout && (timeout != -1) && (Zombies.size() == 0)) {
            // In order to avoid the engine to run full speed without anything real to do,
            // let's eat the timeout here.
            TimeOps::Sleep(timeout);
        }
	
	/* Now is time to check all the handle and enter into a wait state. */

	event = Task::EVT_HANDLE;
	nfds = w4ha.size();
	no_burst = 1;

        if (nfds != 0) {
	    int r;
	    std::vector<w4ha_t>::iterator p;
	    struct pollfd * q;

	    ufsd = (struct pollfd *) malloc(nfds * sizeof(struct pollfd));
            // Let's build the pollfd structure.
	    for (q = ufsd, p = w4ha.begin(); p != w4ha.end(); p++, q++) {
	        p->dirty = false;
                // A stopped task doesn't get cookies.
	        if (p->T->IsStopped()) {
		    q->fd = 0;
		    q->events = 0;
	        } else {
		    if (p->ha->CanWatch()) {
                        // If that handle can be watched, then let's fill in the pollfd structure.
	    	        q->fd = p->ha->GetHandle();
		        q->events = (p->flags & Task::W4_READING ? POLLIN : 0) | (p->flags & Task::W4_WRITING ? POLLOUT : 0);
		    } else {
                        // Otherwise, let's put the task in burst mode, and set its status to dirty,
                        // considering it already processed for this handle loop.
                        // It'll be run immediately next cycle.
		        p->T->SetBurst();
		        no_burst = 0;
		        p->dirty = true;
		        q->fd = 0;
		        q->events = 0;
		    }
	        }
	    }
            // Now the list is done, let's clean up the w4ha list from the non watchable handles.
	    for (p = w4ha.begin(); p != w4ha.end(); p++) {
                if (!p->T->IsStopped() && !p->ha->CanWatch() && !(p->flags & Task::W4_STICKY)) {
		    w4ha.erase(p);
		    p = w4ha.begin();
                }
            }

#ifndef _WIN32
	    sigprocmask(SIG_UNBLOCK, &sigchildset, 0);
#endif
	    r = poll(ufsd, nfds, (no_burst) && !(Zombies.size()) && !(got_sigchild) ? timeout: 0);
#ifndef _WIN32
            sigprocmask(SIG_BLOCK, &sigchildset, 0);
#endif
	    if (r < 0) {
		if (errno != EINTR) {
		    throw GeneralException(String(_("Error during poll: ")) + strerror(errno));
		}
	    } else if (r == 0) {
		// timeout.
		// We shouldn't do anything here, since the next loop should catch
                // the expired timeouts.
	    } else {
		int fd;
		struct pollfd * q;
		unsigned int i;
		for (q = ufsd, i = 0; i < nfds; i++, q++) {
		    if (q->revents & POLLNVAL) {
			throw GeneralException(String(_("Error with poll, handle ")) + q->fd + _(" invalid."));
		    }
		    
//		    if (q->revents & POLLERR) {
//			cerr << _("Error condition with poll, handle ") << q->fd << endl;
//		    }
		    
//		    if (q->revents & POLLHUP) {
//			cerr << _("Handle ") << q->fd << _(" hung up.\n");
//		    }
		    
		    fd = q->fd;
		    if (q->revents & (POLLIN | POLLOUT | POLLERR | POLLHUP)) {
			// We have to look into the handle structure now...
			bool touched;
			if (!w4ha.empty()) {
			    for (std::vector<w4ha_t>::iterator p = w4ha.begin(); p != w4ha.end(); p = touched ? w4ha.begin() : p + 1) {
				touched = false;
			        if ((p->ha->GetHandle() == fd) && (!p->T->IsStopped()) && (p->T->GetState() != Task::TASK_DONE) && (!p->dirty)) {
			    	    // We've got one, launch it.
//				    cerr << "-=- TaskMan: launching task " << p->T->GetName() << " for handle " << p->ha->GetHandle() << endl;
				    w4ha_t w4 = *p;
				    p->dirty = true;
				    ehandle = p->ha;

				    if (!(p->flags & Task::W4_STICKY)) {
					w4ha.erase(p);
				    }

				    touched = true;

				    w4.T->Run();
                                    CheckDead(w4.T);
				}
			    }
			}
		    }
		}
	    }
	    free(ufsd);
	}
	
	/* And finally, let's clean-up all the zombies around here. */
	
	int no_zombies;
	no_zombies = 0;
	
	event = Task::EVT_TASK;
//	cerr << "-=- TaskMan: processing zombies loop.\n";
	
	while (!no_zombies) {
	    no_zombies = 1;
	    while (Zombies.size()) {
		Task * t = Zombies[0], * o;
		
		if (!t) {
//		    cerr << "!?!?!? We have t = NULL ?!?!?! WTF\n";
		    break;
		}
		
		if ((o = t->WaitedBy())) {
//		    cerr << "-=- TaskMan: running task " << o->GetName() << " for task " << t->GetName() << endl;
		    etask = t;
		    o->Run();
                    if (CheckDead(o))
                        no_zombies = 0;
		} else {
		    delete t;
		}
		Zombies.erase(Zombies.begin());
	    }
	}
	
	/* To end up the loop, let's recall tasks waiting for processes */
	
	event = Task::EVT_PROCESS;
//	cerr << "-=- TaskMan: processing child-waiting tasks.\n";
	
	if (got_sigchild) {
	    if (!w4pr.empty()) {
		for (std::vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) {
		    if (p->flag) {
			Task * t;
		        if (p->T->IsStopped()) {
		    	    continue;
			}
			eprocess = p->pr;
			estatus = p->status;
//		        cerr << "-=- TaskMan: running task " << p->T->GetName() << " for process " << p->pr << " (" << p->status << ")\n";
			t = p->T;
			w4pr.erase(p);
			got_sigchild--;
			t->Run();
		        break;
		    }
		}
	    }
	}
    }
}

bool TaskMan::CheckDead(Task * t) {
    if (t->GetState() == Task::TASK_DONE) {
	TaskList_t::iterator f = FindTask(t);
	TaskList.erase(f);
	number--;
	Zombies.push_back(t);
        return true;
    }
    return false;
}