return (0);
}
-void
-event_set(struct event *ev, evutil_socket_t fd, short events,
- void (*callback)(evutil_socket_t, short, void *), void *arg)
+int
+event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg)
{
/* Take the current base - caller needs to set the real base later */
ev->ev_base = current_base;
ev->ev_pncalls = NULL;
if (events & EV_SIGNAL) {
- if ((events & (EV_READ|EV_WRITE)) != 0)
- event_errx(1, "%s: EV_SIGNAL incompatible use",
- __func__);
+ if ((events & (EV_READ|EV_WRITE)) != 0) {
+ event_warnx("%s: EV_SIGNAL is not compatible with "
+ "EV_READ or EV_WRITE", __func__);
+ return -1;
+ }
ev->ev_closure = EV_CLOSURE_SIGNAL;
} else {
if (events & EV_PERSIST) {
min_heap_elem_init(ev);
- /* by default, we put new events into the middle priority */
- if (current_base)
- ev->ev_pri = current_base->nactivequeues/2;
+ if (base != NULL) {
+ if (event_base_set(base, ev) < 0) {
+ event_warnx("%s: event_base_set() failed", __func__);
+ return -1;
+ }
+ } else if (current_base) {
+ /* by default, we put new events into the middle priority */
+ ev->ev_pri = current_base->nactivequeues/2;
+ }
+ return 0;
}
int
}
void
-event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
+event_set(struct event *ev, evutil_socket_t fd, short events,
+ void (*callback)(evutil_socket_t, short, void *), void *arg)
{
- event_set(ev, fd, events, cb, arg);
- if (base != NULL)
- EVUTIL_ASSERT(event_base_set(base, ev) == 0);
+ int r;
+ r = event_assign(ev, NULL, fd, events, callback, arg);
+ EVUTIL_ASSERT(r == 0);
}
struct event *
ev = mm_malloc(sizeof(struct event));
if (ev == NULL)
return (NULL);
- event_assign(ev, base, fd, events, cb, arg);
+ if (event_assign(ev, base, fd, events, cb, arg) < 0) {
+ mm_free(ev);
+ return (NULL);
+ }
return (ev);
}
@param fn callback function to be invoked when the event occurs
@param arg an argument to be passed to the callback function
+ @return 0 if success, or -1 on invalid arguments.
+
@see event_add(), event_del(), event_once()
*/
-void event_assign(struct event *, struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *);
+int event_assign(struct event *, struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *);
/**
Create and allocate a new event structure, ready to be added.
event_del(&ev1);
}
+static void
+test_bad_assign(void *ptr)
+{
+ struct event ev;
+ int r;
+ /* READ|SIGNAL is not allowed */
+ r = event_assign(&ev, NULL, -1, EV_SIGNAL|EV_READ, dummy_read_cb, NULL);
+ tt_int_op(r,==,-1);
+
+end:
+ ;
+}
+
static void
test_event_base_new(void *ptr)
{
BASIC(manipulate_active_events, TT_FORK|TT_NEED_BASE),
+ BASIC(bad_assign, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
+
/* These are still using the old API */
LEGACY(persistent_timeout, TT_FORK|TT_NEED_BASE),
LEGACY(priorities, TT_FORK|TT_NEED_BASE),