summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--configure.in9
-rw-r--r--include/Buffer.h2
-rw-r--r--include/Exceptions.h115
-rw-r--r--include/HttpServ.h2
-rw-r--r--include/TaskMan.h2
-rw-r--r--include/Variables.h2
-rw-r--r--lib/Buffer.cc2
-rw-r--r--lib/Exceptions.cc2
-rw-r--r--lib/HttpServ.cc6
-rw-r--r--lib/TaskMan.cc254
-rw-r--r--lib/Variables.cc2
11 files changed, 226 insertions, 172 deletions
diff --git a/configure.in b/configure.in
index ff5eaec..15d876b 100644
--- a/configure.in
+++ b/configure.in
@@ -80,12 +80,14 @@ AC_CHECK_FUNCS(getcwd getwd putenv strdup memmove memset select poll)
AC_CHECK_FUNCS(strerror strrchr strstr mempcpy nl_langinfo strcspn)
AC_SEARCH_LIBS(socket,socket)
AC_SEARCH_LIBS(gethostbyname,nsl)
-AC_SEARCH_LIBS(gzwrite,z)
+AC_SEARCH_LIBS(deflate,z)
if test x$ac_cv_search_deflate != xno ; then
+ OLDCPPFLAGS=$CPPFLAGS
+ CPPFLAGS="-Werror"
AC_CACHE_CHECK([for well defined gzwrite in zlib.h], ac_cv_wd_gzwrite,
AC_TRY_COMPILE(
[#include <zlib.h>],
- [const void * buf; gzwrite(NULL, buf, 0);],
+ [const char * buf = "Poide"; gzwrite(NULL, buf, 10);],
[AC_DEFINE(HAVE_WD_ZLIB)
ac_cv_wd_gzwrite="yes"],
[ac_cv_wd_gzwrite="no"]
@@ -93,8 +95,9 @@ if test x$ac_cv_search_deflate != xno ; then
[should patch this using the zconf.h-patch file.])
)
)
+ CPPFLAGS=$OLDCPPFLAGS
else
- AC_ERROR([This library needs the zlib library. You can found]
+ AC_ERROR([This library needs the zlib library. You can find]
[it at the url: http://www.zlib.org])
fi
diff --git a/include/Buffer.h b/include/Buffer.h
index 360abad..79d0e02 100644
--- a/include/Buffer.h
+++ b/include/Buffer.h
@@ -15,7 +15,7 @@ class Buffer : public Handle {
Buffer();
Buffer(const Buffer &);
virtual ~Buffer();
- virtual ssize_t write(const void *buf, size_t count);
+ virtual ssize_t write(const void *buf, size_t count) throw(GeneralException);
virtual ssize_t read(void *buf, size_t count) throw (GeneralException);
virtual bool CanRead();
virtual bool CanWrite();
diff --git a/include/Exceptions.h b/include/Exceptions.h
index 1b98598..85451f0 100644
--- a/include/Exceptions.h
+++ b/include/Exceptions.h
@@ -8,55 +8,25 @@
#include <string.h>
#include <stdlib.h>
-class GeneralException;
-
-char * xstrdup(const char *);
-void * xmalloc(size_t) throw (GeneralException);
-void xfree(void *&);
-void xfree(char *&);
-void * xrealloc(void *, size_t);
-int xpipe(int *, int = 0) throw (GeneralException);
-pid_t xfork() throw (GeneralException);
-
-class String;
+#define INLINE __inline__
class Base {
public:
- static char * strdup(const char * s) {
- return xstrdup(s);
- }
- static void * malloc(ssize_t s) {
- return xmalloc(s);
- }
- static void * realloc(void * p, size_t s) {
- return xrealloc(p, s);
- }
- static void * calloc(size_t n, size_t s) {
- return xmalloc(n * s);
- }
- void * operator new(size_t s) {
- return xmalloc(s);
- }
- void * operator new(size_t s, void * p) {
- return memset(p, 0, s);
- }
- void operator delete(void * p) {
- xfree(p);
- }
- static void free(void *& p) {
- xfree(p);
- }
- static void free(char *& p) {
- xfree(p);
- }
- static int pipe(int * p, int flag = 0) {
- return xpipe(p, flag);
- }
- static pid_t fork() {
- return xfork();
- }
+ static char * strdup(const char * s);
+ static void * malloc(ssize_t s);
+ static void * realloc(void * p, size_t s);
+ static void * calloc(size_t n, size_t s);
+ void * operator new(size_t s);
+ void * operator new(size_t s, void * p);
+ void operator delete(void * p);
+ static void free(void *& p);
+ static void free(char *& p);
+ static int pipe(int * p, int flag = 0);
+ static pid_t fork();
};
+class String;
+
class GeneralException : public Base {
public:
GeneralException(String);
@@ -70,11 +40,68 @@ class GeneralException : public Base {
static char t[BUFSIZ];
};
+char * xstrdup(const char *);
+void * xmalloc(size_t) throw (GeneralException);
+void xfree(void *&);
+void xfree(char *&);
+void * xrealloc(void *, size_t);
+int xpipe(int *, int = 0) throw (GeneralException);
+pid_t xfork() throw (GeneralException);
+
+INLINE char * Base::strdup(const char * s) {
+ return xstrdup(s);
+}
+
+INLINE void * Base::malloc(ssize_t s) {
+ return xmalloc(s);
+}
+
+INLINE void * Base::realloc(void * p, size_t s) {
+ return xrealloc(p, s);
+}
+
+INLINE void * Base::calloc(size_t n, size_t s) {
+ return xmalloc(n * s);
+}
+
+INLINE void * Base::operator new(size_t s) {
+ return xmalloc(s);
+}
+
+INLINE void * Base::operator new(size_t s, void * p) {
+ return memset(p, 0, s);
+}
+
+INLINE void Base::operator delete(void * p) {
+ xfree(p);
+}
+
+INLINE void Base::free(void *& p) {
+ xfree(p);
+}
+
+INLINE void Base::free(char *& p) {
+ xfree(p);
+}
+
+INLINE int Base::pipe(int * p, int flag = 0) {
+ return xpipe(p, flag);
+}
+
+INLINE pid_t Base::fork() {
+ return xfork();
+}
+
class MemoryException : public GeneralException {
public:
MemoryException(ssize_t);
};
+class TaskNotFound : public GeneralException {
+ public:
+ TaskNotFound();
+};
+
enum op_t {
IO_WRITE = 1,
IO_READ
diff --git a/include/HttpServ.h b/include/HttpServ.h
index a51a16e..7d63517 100644
--- a/include/HttpServ.h
+++ b/include/HttpServ.h
@@ -17,7 +17,7 @@ class HttpServ : public Task {
virtual String GetName();
protected:
- virtual int Do();
+ virtual int Do() throw (GeneralException);
private:
Socket Listener;
diff --git a/include/TaskMan.h b/include/TaskMan.h
index e61aa3f..2cab500 100644
--- a/include/TaskMan.h
+++ b/include/TaskMan.h
@@ -15,7 +15,7 @@
class TaskMan : public Base {
public:
static void AddTask(Task *);
- static vector<Task *>::iterator FindTask(Task *);
+ static vector<Task *>::iterator FindTask(Task *) throw (GeneralException);
static void RemoveFromWatches(Task *);
static void Init() throw (GeneralException);
static void MainLoop() throw (GeneralException);
diff --git a/include/Variables.h b/include/Variables.h
index f4eb6db..8d7189b 100644
--- a/include/Variables.h
+++ b/include/Variables.h
@@ -2,7 +2,7 @@
#define __VARIABLES_H__
#ifdef __cplusplus
-#include <vector>
+#include <vector.h>
#include <String.h>
#include <Handle.h>
#include <Exceptions.h>
diff --git a/lib/Buffer.cc b/lib/Buffer.cc
index 83369bd..905090e 100644
--- a/lib/Buffer.cc
+++ b/lib/Buffer.cc
@@ -14,7 +14,7 @@ Buffer::Buffer(const Buffer & b) : Handle(-1), buffer(0), zero(b.zero), realsiz(
memcpy(buffer, b.buffer, bufsiz);
}
-ssize_t Buffer::write(const void *buf, size_t count) {
+ssize_t Buffer::write(const void *buf, size_t count) throw (GeneralException) {
if (!count) {
return 0;
}
diff --git a/lib/Exceptions.cc b/lib/Exceptions.cc
index 7c9d318..fa9c6da 100644
--- a/lib/Exceptions.cc
+++ b/lib/Exceptions.cc
@@ -30,6 +30,8 @@ GeneralException::~GeneralException() {
free(msg);
}
+TaskNotFound::TaskNotFound() : GeneralException("Task not found") { }
+
char * GeneralException::GetMsg() {
return msg;
}
diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc
index e1ddd73..8824bcc 100644
--- a/lib/HttpServ.cc
+++ b/lib/HttpServ.cc
@@ -15,7 +15,7 @@ class ProcessRequest : public Task {
virtual ~ProcessRequest() {}
virtual String GetName();
protected:
- virtual int Do();
+ virtual int Do() throw (GeneralException);
private:
String GetMime(const String &);
bool ParseUri(String &, String &, String &, Handle *);
@@ -45,7 +45,7 @@ String ProcessRequest::GetName() {
return "Processing HTTP request";
}
-int ProcessRequest::Do() {
+int ProcessRequest::Do() throw(GeneralException) {
switch (current) {
case 0:
if (!s.IsConnected()) return TASK_DONE;
@@ -450,7 +450,7 @@ HttpServ::~HttpServ(void) {
Listener.close();
}
-int HttpServ::Do() {
+int HttpServ::Do() throw (GeneralException) {
try {
Socket s = Listener.Accept();
s.SetNonBlock();
diff --git a/lib/TaskMan.cc b/lib/TaskMan.cc
index a348ab0..e75863b 100644
--- a/lib/TaskMan.cc
+++ b/lib/TaskMan.cc
@@ -51,11 +51,12 @@ void taskman_sighole(int sig) {
int TaskMan::GotChild(pid_t pid, int status) {
int r = 0;
+ unsigned int i;
- for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) {
- if (p->pr == pid) {
- p->flag = true;
- p->status = status;
+ for (i = 0; i < w4pr.size(); i++) {
+ if (w4pr[i].pr == pid) {
+ w4pr[i].flag = true;
+ w4pr[i].status = status;
r = 1;
}
}
@@ -115,53 +116,64 @@ void TaskMan::AddTask(Task * t) {
}
}
-vector<Task *>::iterator TaskMan::FindTask(Task * t) {
+vector<Task *>::iterator TaskMan::FindTask(Task * t) throw (GeneralException) {
if (!inited) {
Init();
}
- for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) {
+ if (TaskList.empty())
+ throw TaskNotFound();
+
+ for (vector<Task *>::iterator p = TaskList.begin(); p != TaskList.end(); p++) {
if (*p == t) {
return p;
}
}
- return NULL;
+ throw TaskNotFound();
}
void TaskMan::RemoveFromWatches(Task * t) {
- for (vector<w4ha_t>::iterator p = w4ha.begin(); p && (p != w4ha.end()); p++) {
- if (p->T == t) {
- w4ha.erase(p);
- p--;
+ if (!w4ha.empty()) {
+ for (vector<w4ha_t>::iterator p = w4ha.begin(); p != w4ha.end(); p++) {
+ if (p->T == t) {
+ w4ha.erase(p);
+ p--;
+ }
}
}
- for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) {
- if (p->T == t) {
- w4pr.erase(p);
- p--;
+ if (!w4pr.empty()) {
+ for (vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) {
+ if (p->T == t) {
+ w4pr.erase(p);
+ p--;
+ }
}
}
- for (vector<w4to_t>::iterator p = w4to.begin(); p && (p != w4to.end()); p++) {
- if (p->T == t) {
- w4to.erase(p);
- p--;
+ if (!w4to.empty()) {
+ for (vector<w4to_t>::iterator p = w4to.begin(); p != w4to.end(); p++) {
+ if (p->T == t) {
+ w4to.erase(p);
+ p--;
+ }
}
}
- for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) {
- if ((*p)->WaitedBy() == t) {
- Zombies.push_back(*p);
- (*p)->RemoveFromWatches();
- TaskList.erase(p);
- number--;
- p--;
- } else if ((*p) == t) {
- TaskList.erase(p);
- number--;
- p--;
+ 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--;
+ } else if ((*p) == t) {
+ TaskList.erase(p);
+ number--;
+ p--;
+ }
}
}
}
@@ -173,12 +185,14 @@ void TaskMan::WaitFor(Handle * h, Task * t, int flags) {
void TaskMan::WaitFor(pid_t pid, Task * t, int status) {
if (status == -1) {
- for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) {
- if (p->pr == pid) {
- p->T = t;
- p->flag = true;
- got_sigchild++;
- return;
+ if (!w4pr.empty()) {
+ for (vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) {
+ if (p->pr == pid) {
+ p->T = t;
+ p->flag = true;
+ got_sigchild++;
+ return;
+ }
}
}
}
@@ -208,9 +222,11 @@ void TaskMan::MainLoop() throw (GeneralException) {
if (stopped) return;
// cerr << "-=- TaskMan: begin main loop with " << number << " task to manage.\n";
- for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) {
- Task * t = *p;
-// cerr << "-=- TaskMan: task " << t->GetName() << endl;
+ 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";
@@ -220,26 +236,28 @@ void TaskMan::MainLoop() throw (GeneralException) {
no_burst = 1;
/* First, we will check for any burning task and run 'em */
event = E_BURST;
- for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) {
- Task * t = *p;
+ if (!TaskList.empty()) {
+ for (TaskList_t::iterator p = TaskList.begin(); p != TaskList.end(); p++) {
+ Task * t = *p;
- if (t->IsStopped()) {
- continue;
- }
+ if (t->IsStopped()) {
+ continue;
+ }
- if (t->GetState() == TASK_BURST) {
-// cerr << "-=- TaskMan: running burning task " << t->GetName() << endl;
- t->Run();
- /* if the task added some new tasks, we have to rerun the loop */
- no_burst = 0;
- break;
- }
+ if (t->GetState() == TASK_BURST) {
+// cerr << "-=- TaskMan: running burning task " << t->GetName() << endl;
+ t->Run();
+ /* if the task added some new tasks, we have to rerun the loop */
+ no_burst = 0;
+ break;
+ }
- if (t->GetState() == TASK_DONE) {
- TaskList.erase(p);
- number--;
- p--;
- Zombies.push_back(t);
+ if (t->GetState() == TASK_DONE) {
+ TaskList.erase(p);
+ number--;
+ p--;
+ Zombies.push_back(t);
+ }
}
}
}
@@ -264,25 +282,27 @@ void TaskMan::MainLoop() throw (GeneralException) {
#ifdef USE_POLL
ufsd = (struct pollfd *) malloc(nfds * sizeof(struct pollfd));
- for (q = ufsd, p = w4ha.begin(); p && (p != w4ha.end()); p++, q++) {
- p->dirthy = false;
- if (p->T->IsStopped()) {
- q->fd = 0;
- q->events = 0;
- } else {
- if (p->ha->CanWatch()) {
- q->fd = p->ha->GetHandle();
- q->events = (p->flags & W4_READING ? POLLIN : 0) | (p->flags & W4_WRITING ? POLLOUT : 0);
- } else {
- p->T->SetBurst();
- no_burst = 0;
- p->dirthy = true;
- if (!(p->flags & W4_STICKY)) {
- w4ha.erase(p);
- p--;
- }
+ if (!w4ha.empty()) {
+ for (q = ufsd, p = w4ha.begin(); p != w4ha.end(); p++, q++) {
+ p->dirthy = false;
+ if (p->T->IsStopped()) {
q->fd = 0;
q->events = 0;
+ } else {
+ if (p->ha->CanWatch()) {
+ q->fd = p->ha->GetHandle();
+ q->events = (p->flags & W4_READING ? POLLIN : 0) | (p->flags & W4_WRITING ? POLLOUT : 0);
+ } else {
+ p->T->SetBurst();
+ no_burst = 0;
+ p->dirthy = true;
+ if (!(p->flags & W4_STICKY)) {
+ w4ha.erase(p);
+ p--;
+ }
+ q->fd = 0;
+ q->events = 0;
+ }
}
}
}
@@ -329,13 +349,13 @@ void TaskMan::MainLoop() throw (GeneralException) {
throw GeneralException(String(_("Error with poll, handle ")) + q->fd + _(" invalid."));
}
- if (q->revents & POLLERR) {
+// if (q->revents & POLLERR) {
// cerr << _("Error condition with poll, handle ") << q->fd << endl;
- }
+// }
- if (q->revents & POLLHUP) {
+// if (q->revents & POLLHUP) {
// cerr << _("Handle ") << q->fd << _(" hung up.\n");
- }
+// }
fd = q->fd;
if (q->revents & (POLLIN | POLLOUT | POLLERR | POLLHUP)) {
@@ -348,33 +368,36 @@ void TaskMan::MainLoop() throw (GeneralException) {
#endif
// We have to look into the handle structure now...
bool touched;
- for (vector<w4ha_t>::iterator p = w4ha.begin(); p && (p != w4ha.end()); p = touched ? w4ha.begin() : p + 1) {
- touched = false;
- if ((p->ha->GetHandle() == fd) && (!p->T->IsStopped()) && (p->T->GetState() != TASK_DONE) && (!p->dirthy)) {
- // We've got one, launch it.
-// cerr << "-=- TaskMan: launching task " << p->T->GetName() << " for handle " << p->ha->GetHandle() << endl;
- w4ha_t w4 = *p;
- p->dirthy = true;
-
- if (!(p->flags & W4_STICKY)) {
- w4ha.erase(p);
- }
+ if (!w4ha.empty()) {
+ for (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_DONE) && (!p->dirthy)) {
+ // We've got one, launch it.
+// cerr << "-=- TaskMan: launching task " << p->T->GetName() << " for handle " << p->ha->GetHandle() << endl;
+ w4ha_t w4 = *p;
+ p->dirthy = true;
+
+ if (!(p->flags & W4_STICKY)) {
+ w4ha.erase(p);
+ }
- touched = true;
+ touched = true;
- ehandle = p->ha;
- w4.T->Run();
+ ehandle = p->ha;
+ w4.T->Run();
- if (w4.T->GetState() == TASK_DONE) {
- // This task died, remove it.
- TaskList_t::iterator q = FindTask(w4.T);
- if (q) {
- TaskList.erase(q);
- number--;
- Zombies.push_back(w4.T);
+ if (w4.T->GetState() == TASK_DONE) {
+ // This task died, remove it.
+ try {
+ vector<Task *>::iterator q = FindTask(w4.T);
+ TaskList.erase(q);
+ number--;
+ Zombies.push_back(w4.T);
+ }
+ catch (TaskNotFound) {
+ }
}
}
-
}
}
}
@@ -399,7 +422,7 @@ void TaskMan::MainLoop() throw (GeneralException) {
Task * t = Zombies[0], * o;
if (!t) {
- cerr << "!?!?!? We have t = NULL ?!?!?! WTF\n";
+// cerr << "!?!?!? We have t = NULL ?!?!?! WTF\n";
break;
}
@@ -410,9 +433,6 @@ void TaskMan::MainLoop() throw (GeneralException) {
if (o->GetState() == TASK_DONE) {
TaskList_t::iterator f = FindTask(o);
- if (!f) {
- throw GeneralException(_("TaskMan: internal error (task not found) -- SHOULD NOT HAPPEN!!"));
- }
TaskList.erase(f);
number--;
Zombies.push_back(o);
@@ -431,20 +451,22 @@ void TaskMan::MainLoop() throw (GeneralException) {
// cerr << "-=- TaskMan: processing child-waiting tasks.\n";
if (got_sigchild) {
- for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) {
- if (p->flag) {
- Task * t;
- if (p->T->IsStopped()) {
- continue;
+ if (!w4pr.empty()) {
+ for (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;
}
- 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;
}
}
}
diff --git a/lib/Variables.cc b/lib/Variables.cc
index d96cad8..8ba5374 100644
--- a/lib/Variables.cc
+++ b/lib/Variables.cc
@@ -67,7 +67,7 @@ void Variables::Add(const String & s) {
void Variables::Del(int i) {
nbvars--;
- Vars.erase(&Vars[i], &Vars[i]);
+ Vars.erase(Vars.begin() + i, Vars.begin() + i);
}
void Variables::Del(const String & name) {