summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ev.c49
-rw-r--r--ev.h17
2 files changed, 65 insertions, 1 deletions
diff --git a/ev.c b/ev.c
index ffd19c6..b2e48f8 100644
--- a/ev.c
+++ b/ev.c
@@ -38,6 +38,8 @@
#include <assert.h>
#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <sys/time.h>
#include <time.h>
@@ -61,6 +63,7 @@
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
#define MAX_BLOCKTIME 60.
+#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
#include "ev.h"
@@ -334,6 +337,30 @@ static int checkmax, checkcnt;
/*****************************************************************************/
+static struct ev_child *childs [PID_HASHSIZE];
+static struct ev_signal childev;
+
+#ifndef WCONTINUED
+# define WCONTINUED 0
+#endif
+
+static void
+childcb (struct ev_signal *sw, int revents)
+{
+ struct ev_child *w;
+ int pid, status;
+
+ while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
+ for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
+ if (w->pid == pid || w->pid == -1)
+ {
+ w->status = status;
+ event ((W)w, EV_CHILD);
+ }
+}
+
+/*****************************************************************************/
+
#if HAVE_EPOLL
# include "ev_epoll.c"
#endif
@@ -370,6 +397,9 @@ int ev_init (int flags)
{
evw_init (&sigev, sigcb);
siginit ();
+
+ evsignal_init (&childev, childcb, SIGCHLD);
+ evsignal_start (&childev);
}
return ev_method;
@@ -881,6 +911,25 @@ void evcheck_stop (struct ev_check *w)
ev_stop ((W)w);
}
+void evchild_start (struct ev_child *w)
+{
+ if (ev_is_active (w))
+ return;
+
+ ev_start ((W)w, 1);
+ wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
+}
+
+void evchild_stop (struct ev_child *w)
+{
+ ev_clear ((W)w);
+ if (ev_is_active (w))
+ return;
+
+ wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
+ ev_stop ((W)w);
+}
+
/*****************************************************************************/
struct ev_once
diff --git a/ev.h b/ev.h
index 3c550e2..ff7acaa 100644
--- a/ev.h
+++ b/ev.h
@@ -42,7 +42,8 @@ typedef double ev_tstamp;
#define EV_IDLE 0x10
#define EV_CHECK 0x20
#define EV_PREPARE 0x40
-#define EV_ERROR (0x7f|0x80)
+#define EV_CHILD 0x80
+#define EV_ERROR (0xff|0x8000)
/* can be used to add custom fields to all watchers */
#ifndef EV_COMMON
@@ -141,6 +142,15 @@ struct ev_check
EV_WATCHER (ev_check);
};
+/* invoked when sigchld is received and waitpid indicates the givne pid */
+struct ev_child
+{
+ EV_WATCHER_LIST (ev_child);
+
+ int pid; /* ro */
+ int status; /* holds the exit status, use the macros from sys/wait.h */
+};
+
#define EVMETHOD_NONE 0
#define EVMETHOD_SELECT 1
#define EVMETHOD_EPOLL 2
@@ -179,6 +189,7 @@ void ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, voi
#define evidle_set(ev) /* nop, yes, this is a serious in-joke */
#define evprepare_set(ev) /* nop, yes, this is a serious in-joke */
#define evcheck_set(ev) /* nop, yes, this is a serious in-joke */
+#define evchild_set(ev,pid_) do { (ev)->pid = (pid_); } while (0)
#define evio_init(ev,cb,fd,events) do { evw_init ((ev), (cb)); evio_set ((ev),(fd),(events)); } while (0)
#define evtimer_init(ev,cb,after,repeat) do { evw_init ((ev), (cb)); evtimer_set ((ev),(after),(repeat)); } while (0)
@@ -187,6 +198,7 @@ void ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, voi
#define evidle_init(ev,cb) do { evw_init ((ev), (cb)); evidle_set ((ev)); } while (0)
#define evprepare_init(ev,cb) do { evw_init ((ev), (cb)); evprepare_set ((ev)); } while (0)
#define evcheck_init(ev,cb) do { evw_init ((ev), (cb)); evcheck_set ((ev)); } while (0)
+#define evchild_init(ev,cb,pid) do { evw_init ((ev), (cb)); evchild_set ((ev),(pid)); } while (0)
#define ev_is_active(ev) (0 + (ev)->active) /* true when the watcher has been started */
@@ -214,6 +226,9 @@ void evprepare_stop (struct ev_prepare *w);
void evcheck_start (struct ev_check *w);
void evcheck_stop (struct ev_check *w);
+
+void evchild_start (struct ev_child *w);
+void evchild_stop (struct ev_child *w);
#endif
#endif