From: skeezix Date: Thu, 17 Dec 2009 04:11:01 +0000 (-0500) Subject: UNTESTED on device! X-Git-Tag: Release-2010-05/1~143^2~4 X-Git-Url: https://git.openpandora.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a2e1c684f4754cce44d52f1572d8aaade9ff7434;p=pandora-libraries.git UNTESTED on device! Put in a workaround for the inotify being out in space issue, maybe. At least, pndnotifyd will try inotify and see if it notices anything; if so, continues, otherwise bitches. --- diff --git a/apps/pndnotifyd.c b/apps/pndnotifyd.c index b511930..3720131 100644 --- a/apps/pndnotifyd.c +++ b/apps/pndnotifyd.c @@ -118,6 +118,14 @@ int main ( int argc, char *argv[] ) { pnd_log ( pndn_rem, "Interval between checks is %u seconds\n", interval_secs ); + // check if inotify is awake yet; if not, try waiting for awhile to see if it does + if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) { + pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" ); + return ( -1 ); + } + + pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" ); + // basic daemon set up if ( g_daemon_mode ) { diff --git a/include/pnd_notify.h b/include/pnd_notify.h index a555bc9..f4bf594 100644 --- a/include/pnd_notify.h +++ b/include/pnd_notify.h @@ -36,6 +36,7 @@ unsigned char pnd_notify_rediscover_p ( pnd_notify_handle h ); * be responding. Returns 0 if inotify is never ready during the interval, otherwise * it suggests inotify is up and going. * secs_timeout may be 0 which means 'forever' + * NOTE: This wastes some time, always */ unsigned char pnd_notify_wait_until_ready ( unsigned int secs_timeout ); diff --git a/lib/pnd_notify.c b/lib/pnd_notify.c index 9be749b..7dd135b 100644 --- a/lib/pnd_notify.c +++ b/lib/pnd_notify.c @@ -3,6 +3,7 @@ #include // for stdio, NULL #include // for malloc, etc #include // for close +#include // for time() #define _XOPEN_SOURCE 500 #define __USE_XOPEN_EXTENDED @@ -17,7 +18,7 @@ typedef struct { static int notify_handle; -static void pnd_notify_hookup ( int fd ); +//static void pnd_notify_hookup ( int fd ); #define PND_INOTIFY_MASK IN_CREATE | IN_DELETE | IN_UNMOUNT \ | IN_DELETE_SELF | IN_MOVE_SELF \ @@ -43,7 +44,7 @@ pnd_notify_handle pnd_notify_init ( void ) { p -> fd = fd; // setup some default watches - pnd_notify_hookup ( fd ); + //pnd_notify_hookup ( fd ); return ( p ); } @@ -99,6 +100,7 @@ void pnd_notify_watch_path ( pnd_notify_handle h, char *fullpath, unsigned int f return; } +#if 0 static void pnd_notify_hookup ( int fd ) { inotify_add_watch ( fd, "./testdata", IN_CREATE | IN_DELETE | IN_UNMOUNT ); @@ -106,6 +108,7 @@ static void pnd_notify_hookup ( int fd ) { return; } +#endif unsigned char pnd_notify_rediscover_p ( pnd_notify_handle h ) { pnd_notify_t *p = (pnd_notify_t*) h; @@ -173,27 +176,73 @@ unsigned char pnd_notify_rediscover_p ( pnd_notify_handle h ) { return ( 1 ); } -/* we've run into the issue where inotify returns that it is set up, but in - * fact is not doing anything; restarting the process repairs it.. so here - * we devise a wank that continually tests inotify until it responds, then - * returns knowing we're good - */ -unsigned char pnd_notify_wait_until_ready ( unsigned int secs_timeout ) { - return ( 0 ); // fail -} - +#define _INOTIFY_TEST_PATH "/tmp/.notifytest" +#define _INOTIFY_TEST_FILE "/tmp/.notifytest/foopanda" static unsigned char _inotify_test_run ( void ) { -#if 0 + // set up inotify - int fd; + pnd_notify_t fdt; + int wd; // watch-descriptor - fd = inotify_init(); + // set up inotify + fdt.fd = inotify_init(); - if ( fd < 0 ) { + if ( fdt.fd < 0 ) { return ( 0 ); // failed to init at all } + // make a temp dir + mkdir ( _INOTIFY_TEST_PATH, 0777 ); // if it fails we assume it exists, which is fine + // watch the dir + if ( ( wd = inotify_add_watch ( fdt.fd, _INOTIFY_TEST_PATH, IN_DELETE ) ) < 0 ) { + return ( 0 ); // couldn't watch dir + } -#endif + // sleep a sec, just to be safe; seems to dislike being called immediately sometimes + usleep ( 1000000 / 2 ); + + // create a temp file + FILE *f; + if ( ! ( f = fopen ( _INOTIFY_TEST_FILE, "w" ) ) ) { + close ( fdt.fd ); + return ( 0 ); // couldn't create test file?! + } + + fclose ( f ); + + // delete the temp file; this should trigger an event + if ( unlink ( _INOTIFY_TEST_FILE ) < 0 ) { + return ( 0 ); // couldn't rm a file? life is harsh. + } + + // did we get anything? + unsigned char s; + s = pnd_notify_rediscover_p ( &fdt ); + + // ditch inotify + close ( fdt.fd ); + + // success + return ( s ); +} + +/* we've run into the issue where inotify returns that it is set up, but in + * fact is not doing anything; restarting the process repairs it.. so here + * we devise a wank that continually tests inotify until it responds, then + * returns knowing we're good + */ +unsigned char pnd_notify_wait_until_ready ( unsigned int secs_timeout ) { + time_t start = time ( NULL ); + + while ( ( secs_timeout == 0 ) || + ( time ( NULL ) - start < secs_timeout ) ) + { + if ( _inotify_test_run() ) { + return ( 1 ); + } + usleep ( 1000000 / 2 ); + } + + return ( 0 ); // fail }