• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Proper usage of kqueue?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Proper usage of kqueue?


  • Subject: Proper usage of kqueue?
  • From: Will Johansson <email@hidden>
  • Date: Tue, 26 Jun 2007 11:28:09 -0500

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:
This email sent to email@hidden


  • Prev by Date: Re: Where can I find a comprehensive manual for the openfirmware of Mac?
  • Next by Date: Kernel Debug Kit for 10.4.10
  • Previous by thread: Re: A question of Rosetta.
  • Next by thread: Kernel Debug Kit for 10.4.10
  • Index(es):
    • Date
    • Thread