/******************************************************************************* * * Copyright (c) 2000, Tortuga Technologies Pty Ltd. All rights reserved. * * This is unpublished proprietary source code of Tortuga Technologies. * The copyright notice above does not evidence any actual or intended * publication of such source code. * ******************************************************************************* * * Filename: $Id: tim.c,v 1.1 2002/04/22 10:15:36 jill Exp $ * * Description: Example timeout tester * * History * ------- * $Log: tim.c,v $ * Revision 1.1 2002/04/22 10:15:36 jill * Initial import * * Revision 1.1.1.1 2000/02/18 06:41:30 graham * initial import * ******************************************************************************/ /* standard includes */ #include "machdeps.h" #include #include #include #include #include #include /* local includes */ #include "log.h" #include "injun.h" /* definitions */ /* statics */ static int lid = 0; static char *timeout_id = "timeout"; static boolean_t debug = B_FALSE; static int startup_time = 5000; /* prototypes */ static void timeout( void *args ); static time_t time_until_midnight( void ); static void parse_args( int argc, char *argv[] ); /* exported globals */ int tim_init( int argc, char *argv[] ); int tim_dele( int argc, char *argv[] ); char tim_version[SMLBUF] = "@(#) 1.0.0"; /*ARGSUSED*/ /******************************************************************************* * * Unit: example_init() * * Description: initialises the example application in the injun environment * * Notes: called when this application is loaded into injun * * Parameters: standard argc/argv format * * Globals: none * * Returns: OK - always * ******************************************************************************/ int tim_init( int argc, char *argv[] ) { int status; /* initialise the logging for this code */ log_next_id( "tim", &lid ); /* process any options */ parse_args( argc, argv ); /* setup a timeout to occur n seconds from now */ status = injun_add_timeout( startup_time, (void *)timeout, (void *)timeout_id ); if ( status != OK ) log( lid, LOG, "_init: failed to add timeout" ); /* well, we're all set so register with the injun administration gui */ status = injun_register_application( "Tim", NULL, "Injun timeout test application" ); /* this is not fatal, just inconvenient */ if ( status != OK ) log( lid, LOG, "_init: failed to register with injun administration gui" ); /* and return */ return( OK ); } /*ARGSUSED*/ /******************************************************************************* * * Unit: tim_dele() * * Description: cleanup our application and deregister us from injun * * Notes: * * Parameters: standard argc/argv format * * Globals: none * * Returns: OK - always * ******************************************************************************/ int tim_dele( int argc, char *argv[] ) { int status; /* remove our midnightly timeout */ status = injun_remove_timeout( (void *)timeout, (void *)timeout_id, NULL ); if ( status != OK ) log( lid, LOG, "_dele: failed to remove timeout" ); /* deregister from the injun administration gui */ status = injun_deregister_application( "Tim" ); if ( status != OK ) log( lid, LOG, "_dele: failed to deregister" ); /* remove the logging for this module */ log_remove( lid ); /* and we're outta here */ return( OK ); } /******************************************************************************* * * Unit: timeout() * * Description: this timeout is executed startup_time seconds after the * module is loaded and then reschedules itself every midnight. * * Notes: we use poll() for a timeout, this doesn't suspend the whole * process whereas sleep() will stop injun as well ! * * Parameters: args - timeout identification * * Globals: none * * Returns: none * ******************************************************************************/ static void timeout( void *args ) { int status; struct pollfd pfd[1]; /* we should be at 1 second to midnight here */ /* so we wait for the big kahuna to drop */ (void) poll( pfd, 0, 1000 ); /* print a useful message */ log( lid, LOG, "Ding Dong !" ); /* wait until 1 second after midnight */ (void) poll( pfd, 0, 1000 ); /* reschedule us for the next midnight (tonight) */ status = injun_add_timeout( time_until_midnight(), (void *)timeout, args ); if ( status != OK ) log( lid, LOG, "failed to add midnight timeout" ); /* and return */ return; } /******************************************************************************* * * Unit: time_until_midnight * * Description: returns the number of milliseconds until midnight * (well one second before, actually) * * Parameters: none. * * Globals: none * * Returns: number of milliseconds before midnight * ******************************************************************************/ static time_t time_until_midnight( void ) { int hours, mins, secs; time_t time_now; time_t time_at_midnight; time_t time_togo; struct tm tm_time; /* clear down the time structure */ (void) memset( &tm_time, 0, sizeof(tm_time) ); /* get the time now into a tm structure */ time_now = time( NULL ); (void) localtime_r( &time_now, &tm_time ); /* boost the tm structure values to near midnight */ tm_time.tm_sec = 59; tm_time.tm_min = 59; tm_time.tm_hour = 23; time_at_midnight = mktime( &tm_time ); /* calculate the time to go until midnight in mSecs */ time_togo = ( time_at_midnight - time_now ) * 1000; if ( time_togo < 0 ) /* sanity/safety test */ time_togo += ( 24 * 60 * 60 * 1000 ); /* printout some math/time verification */ if ( debug == B_TRUE ) { hours = time_togo/3600000; mins = (((time_togo/3600) - (hours*1000)) * 60) / 1000; secs = (time_togo/1000) % 60; log( lid, LOG, "midnight at %02d:%02d:%02d", hours, mins, secs ); } /* and return it */ return( time_togo ); } /******************************************************************************* * * Unit: parse_args * * Description: process the arguments * * Notes: * * Parameters: argc -- number of parameters * argv -- parameter list in standard format * * Globals: optind * * Returns: nada * ******************************************************************************/ static void parse_args( int argc, char *argv[] ) { int c; extern char *optarg; extern int optind, opterr; optind = 1; /* enable this getopt command */ opterr = 0; /* disable stderr output */ while( (c = getopt(argc,argv,":ds:")) != -1 ) { switch( c ) { /* web page configuration */ case 'd': debug = B_TRUE; log( lid, LOG, "setting debug on" ); break; case 's': startup_time = atoi(optarg) * 1000; log( lid, LOG, "setting startup time to %d", startup_time ); break; case ':': /* missing option to argument letter */ log( lid, LOG, "missing option to argument !" ); break; default: /* error */ log( lid, LOG, "unknown option !" ); break; } } return; }