I'm having trouble using kevent(2) to catch signals. Below is a sample
program. It should catch SIGHUP and SIGUSR1 and just print it out.
Instead when i send the process sighup with `kill -SIGHUP $PID` it
exits and the word Hangup is writtin to the terminal. If I use
`kill -SIGUSR1 $PID` the process exits and the words User defined signal 1
are written to the terminal. What am I doing wrong?

Thanks!

Edgar
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>

#include <err.h>
#include <signal.h>
#include <stdio.h>

int
main(int argc, char** argv)
{
        int kq;
        struct kevent ev[2];

        if ((kq = kqueue()) == -1)
                err(1, "kqueue()");

        EV_SET(&ev[0], SIGHUP, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
        EV_SET(&ev[1], SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);

        if ((kevent(kq, ev, 2, NULL, 0, NULL)) == -1)
                err(1, "kevent()");

        for (;;) {
                struct kevent kev[2];
                int ret;
                int i;

                ret = kevent(kq, NULL, 0, kev, 2, NULL);
                if (ret == -1)
                        err(1, "kevent()");
                else if (ret > 0) {
                        for (i = 0; i < ret; i++) {
                                struct kevent* k = &kev[i];
                                switch (k->filter) {
                                case EVFILT_SIGNAL:
                                        if (k->ident == SIGHUP)
                                                printf("received sighup\n");
                                        else if (k->ident == SIGUSR1)
                                                printf("received sigusr1\n");
                                        break;
                                default:
                                        printf("received unknown event\n");
                                        break;
                                }
                        }
                }
        }

        return 0;
}

Reply via email to