Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 21 Jul 2001 11:16:14 -0400
From:      Damien Tougas <damien@carroll.com>
To:        chat@freebsd.org
Subject:   C newbie needs help with kqueue
Message-ID:  <2042320000.995728574@sprig.tougas.net>

next in thread | raw e-mail | index | archive | help
Hello,

I am writing a simple program to try to learn a little bit about kqueue, 
but I seem to be having a problem with the file descriptor I am passing. It 
is probably a simple thing, but since I am quite green, the problem is not 
obvious to me. Perhaps someone could help me out.

When I run this program, I get:

Cannot create kevent: Bad file descriptor

Thanks for your help.

---
Damien Tougas
Systems Administrator
Carroll-Net, Inc.
http://www.carroll.com

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>

int main(int argc, char *argv[]) {

        FILE *fp;
        int i;

        int kq, nev;
        struct kevent kev, ev;
        struct timespec ts = { 0, 0 };

        if ((fp = fopen("/var/log/messages", "r")) == NULL) {
                perror("Cannot open file\n");
                return -1;
                }
        if (fseek(fp, 0L, SEEK_END) < 0) {
                perror("Cannot seek end of file");
                return -1;
                }

        if (kq = kqueue() < 0) {
                perror("Cannot create kqueue");
                }
        EV_SET(&kev, fileno(fp), EVFILT_READ, EV_ADD | EV_ENABLE,
                0 , 0, 0);
        if (kevent(kq, &kev, 1, NULL, 0, &ts) < 0) {
                perror("Cannot create kevent");
                return -1;
                }

        for (;;) {
                if (kevent(kq, NULL, 0, &ev, 1, NULL) > 0) {
                        printf("File Changed!!!\n");
                        }
                else {
                        usleep(10000);
                        }
                }

        }


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-chat" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?2042320000.995728574>