Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Proper usage of kqueue?



Hi guys,

I'm trying to play around with kqueue and learning it. Right now I'm trying to see how I can register both VNODE and PROC for use with one single kqueue. However, I've had two kernel panics this morning. First kernel panics I've had on my MacBook since I bought it. I'm suspecting something's up possibly with my code. If anyone would look at my code below and comment about it, that'd be appreciated!


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

#define NUMBER_OF_CHANGES 2

int main( int argc, char *argv[] ) {
    int fd, queue_id, ev;
    struct kevent changelist[ NUMBER_OF_CHANGES ];
    struct kevent eventlist[ NUMBER_OF_CHANGES ];
    struct timespec * timeout = NULL;

    if( argc != 2 ) {
        fprintf( stderr, "usage: %s [directory]\n", argv[ 0 ] );
        return EXIT_FAILURE;
    }

    printf( "Observing %s\n", argv[ 1 ] );

    queue_id = kqueue();

    if( queue_id < 0 ) {
        perror( "kqueue" );
        return EXIT_FAILURE;
    }

    fd = open( argv[ 1 ], O_RDONLY );

    if( fd < 0 ) {
        perror( "open" );
        return EXIT_FAILURE;
    }

EV_SET( &changelist[ 0 ], fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0 );
EV_SET( &changelist[ 1 ], 1, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, 0 );


    for( ;; ) {
        ev = kevent( queue_id, changelist, 2, eventlist, 2, timeout );

        printf( "We have %d events registered.\n", ev );

        if( ev < 0 ) {
            perror( "kevent" );
            continue;
        }

        if( eventlist[ 0 ].fflags & NOTE_DELETE ) {
            printf( "File deleted.\n" );
            break;
        }

if( eventlist[ 0 ].fflags & NOTE_EXTEND || eventlist [ 0 ].fflags & NOTE_WRITE ) {
printf( "File modified.\n" );
}


        if( eventlist[ 0 ].fflags & NOTE_ATTRIB ) {
            printf( "File attributes modified.\n" );
        }

        if( eventlist[ 1 ].fflags & NOTE_FORK ) {
            printf( "launchd forked.\n" );
        }

        if( eventlist[ 1 ].fflags & NOTE_EXIT ) {
            printf( "forked process exited.\n" );
        }

        if( eventlist[ 1 ].fflags & NOTE_EXEC ) {
            printf( "launchd exec'ed.\n" );
        }

        if( eventlist[ 1 ].fflags & NOTE_TRACKERR ) {
            printf( "eeps, not enough resources.\n" );
        }

        if( eventlist[ 1 ].fflags & NOTE_CHILD ) {
            printf( "\tParentPID: %d\n", eventlist[ 1 ].data );
        }

        if( eventlist[ 1 ].fflags & NOTE_TRACK ) {
            printf( "NOTE_TRACK returned from parent.\n" );
        }
    }

    close( queue_id );
    close( fd );

    return EXIT_SUCCESS;
}


Thanks,

Will Johansson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.