[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Equivalent of event_self_cbarg() in libevent-2.0?




On Sep 18, 2012, at 2:28 PM, Chirag Kantharia wrote:

Hi,

I had a signal handler which used libevent-1.4.x, and which looked
like this:

int main (void) {
 ...
 struct event sigev;
 memset(&sigev, 0, sizeof(sigev));
 event_set(&sigev, SIGINT, EV_SIGNAL|EV_PERSIST, sig_handler, &sigev);
 ...
 event_dispatch();
 return 0;
}

The sig_handler looked like:

static void sig_handler (int fd, short event, void *arg)
{
 struct event *signal = arg;
 event_del(signal);
 exit(1);
}

After porting it to libevent-2.0, the code in main() now looks like:

struct event *sigev;
sigev = evsignal_new(base, SIGINT, sig_handler, NULL);

I want to send the sigev as argument to the sig_handler. I read in
Nick's libevent book[*]  that this can be accomplished using
event_self_cbarg(), in 2.1.1-alpha+. Do we have something in
libevent-2.0.x for the same?

This should work:

main()
{
	struct event *sigev = NULL;
	sigev = event_new(base, -1, SIGINT, sig_handler, &sigev);
}

static void sig_handler(int fd, short event, void *arg)
{
	struct event *event = *(struct event **)arg;
}

But in this case you don't need to pass the event to the signal handler. You can have only one signal handler per application, so the cleanest way would be to make the signal event static in the same module where you handle signals:

static struct event *sig_event;

main()
{
	sig_event = event_new(base, -1, SIGINT, sig_handler, NULL);
}

static void sig_handler(int fd, short event, void *arg)
{
	// use sig_event...
}

***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.