/* ******************************************************************************* * Copyright (c) 1997 Martin Poole ******************************************************************************* ** ** WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! ** ** Any changes to be made to this file should first be checked with ** mplib1 source control for library integrity. ** ** mplib1 source control can be reached at mplib1@quatermass.co.uk ** * * $Source: /home/cvs/cvsroot/onelan/onelan/src/mplib1/util/watch_clean.c,v $ * $Author: mpoole $ * $Date: 2002/10/07 09:37:30 $ * $Revision: 1.2 $ * ******************************************************************************* * * Change History * * $Log: watch_clean.c,v $ * Revision 1.2 2002/10/07 09:37:30 mpoole * Initial checkin of mplib1-3.1.0 * * Revision 1.1 2002/10/07 09:37:09 mpoole * Initial checkin of mplib1-3.1.0 * * ******************************************************************************* */ #ident "$Header: /home/cvs/cvsroot/onelan/onelan/src/mplib1/util/watch_clean.c,v 1.2 2002/10/07 09:37:30 mpoole Exp $" /* ------------------------------------------------------------------ Include files ------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "include/bpo_q_internal.h" #include "include/bpo_proc_internal.h" #include "watchdog.h" /* ------------------------------------------------------------------ structures / defines ------------------------------------------------------------------ */ static int need_cleanup=0; static int clean_debug=0; /* ------------------------------------------------------------------ Code start here ------------------------------------------------------------------ */ static void pipeclean( void ) { const char *pipe_dir; pipe_dir= eval_config_default( "PIPE_DIR", "$HOME/pipe" ); scan_dead_pipes( pipe_dir ); return; } static int clean_this_proc( struct shm_process_private *sppp ) { return(SHM_DELETE_PROC); } static void clean_procs( const void *hint ) { Validate_Processes( hint, SHM_CHECK_DEAD_PROCESSES, (Validate_Processes_t)clean_this_proc, NULL ); return; } static int clean_this_q( struct shm_process_private *sppp ) { return(Q_DELETE_QUEUE); } static void clean_queues( const void *hint ) { Validate_Queues( hint, Q_CHECK_DEAD_QUEUES, (Validate_Queues_t)clean_this_q, NULL ); return; } static void cleanup_now( const void *hint ) { if (clean_debug) fprintfile( stderr, "cleanup: About to pipeclean\n" ); pipeclean(); if (hint) { if (clean_debug) fprintfile( stderr, "cleanup: About to clean processes\n" ); clean_procs( hint ); if (clean_debug) fprintfile( stderr, "cleanup: About to clean queues\n" ); clean_queues( hint ); } if (clean_debug) fprintfile( stderr, "cleanup: complete\n" ); return; } void set_need_cleanup( void ) { need_cleanup=1; return; } void do_cleanup( const void *hint, int force ) { static pid_t running=(pid_t)0; static time_t until=(time_t)0; static time_t when_next=(time_t)0; time_t now; int interval,duration; char run_str[PID_STR_LEN]; clean_debug = get_config_flag("WATCH_CLEAN_DEBUG"); interval = get_config_int("CLEANUP_INTERVAL"); if (interval<1) interval=5; duration = get_config_int("CLEANUP_DURATION"); if (duration<1 || duration has finished\n", run_str ); running=(pid_t)0; }else { /* has it run too long */ if( now > until ) { fprintfile( stderr, "Cleanup child <%s> has been running too long\n", run_str ); kill_pid( running, (hint!=NULL), stderr ); until = now + duration; } } } if ( running==(pid_t)0 && ( (now > when_next) || force || need_cleanup ) ) { when_next = now + interval; until = now + duration; need_cleanup=0; running=fork(); gen_pid_str( run_str, running ); if (running==(pid_t)0) { /* I am the child */ cleanup_now( hint ); exit(0); }else if (running==(pid_t)-1) { fprintfile( stderr, "Unable to spawn cleanup child errno=%d\n", errno ); }else if (clean_debug) fprintfile( stderr, "Cleanup child <%s> spawned\n", run_str ); } return; } /* -- End of File -- */