/* daemon.c
 *
 * A daemon is a program that runs in the background and periodically
 * wakes up to do something useful.
 *
 * This is a simple outline for a generic daemon.  What is to be done
 * is the contents of the function do_whatever(), which currently
 * just returns 0.
 *
 * Default wakeup time is every five minutes, but setable by optional
 * argument in seconds on invocation.
 *
 *
 * Usage: daemon [-t][-v][-c seconds][-f logfile]
 *
 * September 1997 by William C. Hammel (bhammel@graham.main.nc.us).
 *
 */
#include <stdio.h>
#include <time.h>
#include  <stdfun.h> 
/*
*/
#include %lt;signal.h>

#define LOGFILE		"/var/log/daemon"
#define USAGESTRING	"[-t][-v][-c seconds][-f logfile]"

#define MAXLINE		(80 + 1)


#define TMPDIR		"/tmp"

/* Default cycle time value 5 minutes */
#define CYCLE		(5*60)

#define OPTSTRING	"tvc:f:"

static char _sccsid[] = { " daemon.c 1.0 9/6/97 " };

char progname[ MAXLINE ];
char logfile[ MAXLINE ];
char msgline[ MAXLINE + 1 ];
int test_mode;

int main( argc, argv )
int argc;
char *argv[];
{
	extern int getopt();
	extern int optind;
	extern char *optarg;	/* Captures arguments to options */

	int c_opt;

	int do_whatever();

	long elapsed;
	int cycle_time;

	/* End Declrations */

	/* Inits */
	strcpy( progname, argv[ 0 ] );
	strcpy( logfile, LOGFILE );
	test_mode = 0;
	cycle_time = CYCLE;

	while( ( c_opt = getopt( argc, argv, OPTSTRING ) ) != EOF )    {
		switch( c_opt )    {
			case 'v':
				/* -v (Just print version & exit) */
				printf( "%s\n", _sccsid );
				exit( 0 );
			case 't':
				/* -t (Set Test Mode) */
				test_mode = 1;
				break;
			case 'f':
				/* -f arg */
				if( sscanf( optarg, "%s", logfile ) != 1 )    {
					fprintf( stderr,
                               "%s: unreadable log file argument\n", progname );
					exit( 1 );
				}
				break;
			case 'c':
				/* -c arg */
				if( sscanf( optarg, "%d", &cycle_time ) != 1 ){
					fprintf( stderr,
                            "%s: unreadable cycle time argument\n", progname );
					exit( 1 );
				}
				break;
			default:
				fprintf( stderr,
                                "%s: Bad Option -%c\n", argv[ 0 ], c_opt );

				fprintf( stderr,
                                "Usage: %s %s\n", argv[ 0 ], USAGESTRING );
				exit( 1 );
		}
	}

	/* On exiting loop of option parsing optind indexes the next
	 * argv[] argument to the function.
	 */
	if( signal( SIGINT, SIG_IGN ) != SIG_IGN )
		signal( SIGINT, SIG_IGN );
	if( signal( SIGKILL, SIG_IGN ) != SIG_IGN )
		signal( SIGKILL, SIG_IGN );

	if( fork() == 0 )    {
		/* Fork a child process, sending the daemon into the
		 * background loop.
		 */

		fclose( stdin );
		fclose( stdout );

		/* Forever */
		while( 1 )    {

			do_whatever();
			 
			/* Wake up and look every cycle_time */
			sleep( cycle_time );
		}
	}
	else    {

		/* Exit parent process */
		if( signal( SIGINT, SIG_DFL ) != SIG_DFL )
			signal( SIGINT, SIG_DFL );
		if( signal( SIGKILL, SIG_DFL ) != SIG_DFL )
			signal( SIGKILL, SIG_DFL );
		exit( 0 );

	} /* End if( fork() ) */

	/* NOT REACHED */

	exit( 0 );

	return( 0 );

} /* End main() */


int do_whatever()
{
	return( 0 );
}



Return to Home Page
Return to Metayoga Page
Return to C Language Page


The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/CPROGS/daemon.html
Created: 1997
Last Updated: May 28, 2000 Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL