#include <assert.h>
#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <sys/time.h>
#include <time.h>
#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"
/*****************************************************************************/
+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
{
evw_init (&sigev, sigcb);
siginit ();
+
+ evsignal_init (&childev, childcb, SIGCHLD);
+ evsignal_start (&childev);
}
return ev_method;
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
#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
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
#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)
#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 */
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