test-kqueue.c

ano, 09/07/2011 07:18 AM

Download (1.02 KB)

 
1

    
2
#include <sys/types.h>
3
#include <sys/event.h>
4
#include <sys/time.h>
5
#include <sys/socket.h>
6
#include <sysexits.h>
7

    
8
#include <err.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <unistd.h>
12

    
13
int
14
main()
15
{
16
    int sock[2];
17
    struct kevent kc;
18
    int kq;
19
    char foo[1024];
20

    
21
    if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sock) < 0)
22
        err(EX_TEMPFAIL, "socketpair");
23

    
24
    if (write(sock[0], foo, sizeof(foo)) < 0)
25
        err(EX_TEMPFAIL, "write");
26

    
27
    close(sock[0]);
28

    
29
    EV_SET(&kc, sock[1], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0);
30

    
31
    kq = kqueue();
32
    if (kevent(kq, &kc, 1, NULL, 0, NULL) < 0)
33
        err(EX_TEMPFAIL, "kqueue");
34

    
35
    if (kevent(kq, NULL, 0, &kc, 1, NULL) < 0)
36
        err(EX_TEMPFAIL, "kevent1");
37
    printf("kevent1 done, flags: %04x\n", kc.flags);
38

    
39
    if (kc.flags & EV_EOF) {
40
        printf("kevent1 reported EOF, exiting\n");
41
        exit(0);
42
    }
43

    
44
    if (kevent(kq, NULL, 0, &kc, 1, NULL) < 0)
45
        err(EX_TEMPFAIL, "kevent2");
46
    printf("kevent2 done, flags: %04x\n", kc.flags);
47

    
48
    exit(0);
49
}