From: Thibault Godouet Date: Sun, 27 Jan 2002 16:32:53 +0000 (+0000) Subject: added option random X-Git-Tag: ver2_9_4~211 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c2d609b07eaa581c1dae7318968118fef5875b1;p=thirdparty%2Ffcron.git added option random --- diff --git a/database.c b/database.c index b0b6e7b..22f3a04 100644 --- a/database.c +++ b/database.c @@ -22,7 +22,7 @@ * `LICENSE' that comes with the fcron source distribution. */ - /* $Id: database.c,v 1.59 2001-12-23 22:04:55 thib Exp $ */ + /* $Id: database.c,v 1.60 2002-01-27 16:33:20 thib Exp $ */ #include "fcron.h" @@ -793,6 +793,7 @@ set_next_exe(CL *line, char option) struct tm *ft; struct tm ftime; + time_t nextexe = 0; register int i; int max; register char has_changed = 0; @@ -946,13 +947,39 @@ set_next_exe(CL *line, char option) set_cl_nextexe: /* set cl_nextexe (handle the timezone differences) */ - line->cl_nextexe = mktime(&ftime) + (line->cl_file->cf_tzdiff * 3600); + nextexe = mktime(&ftime); - if ( option != NO_GOTO ) + if ( is_random(line->cl_option) ) { + /* run the job at a random time during its interval of execution */ + struct tm intend; + time_t intend_int; + + debug(" cmd: %s begin int exec %d/%d/%d wday:%d %02d:%02d " + "(tzdiff=%d)", line->cl_shell, (ftime.tm_mon + 1), + ftime.tm_mday, (ftime.tm_year + 1900), ftime.tm_wday, + ftime.tm_hour, ftime.tm_min, line->cl_file->cf_tzdiff); + + memcpy(&intend, &ftime, sizeof(intend)); + goto_non_matching(line, &intend, END_OF_INTERVAL); + intend_int = mktime(&intend); + + /* set a random time to add to the first allowed time of execution */ + nextexe += ( (i= intend_int - nextexe) > 0) ? (time_t)( rand() % i) : 0 ; + + } + + line->cl_nextexe = nextexe + (line->cl_file->cf_tzdiff * 3600); + + if ( option != NO_GOTO ) { + if ( is_random(line->cl_option) ) { + ft = localtime(&nextexe); + memcpy(&ftime, ft, sizeof(ftime)); + } debug(" cmd: %s next exec %d/%d/%d wday:%d %02d:%02d " "(tzdiff=%d)", line->cl_shell, (ftime.tm_mon + 1), ftime.tm_mday, (ftime.tm_year + 1900), ftime.tm_wday, ftime.tm_hour, ftime.tm_min, line->cl_file->cf_tzdiff); + } } else { diff --git a/doc/en/fcrontab.5.sgml b/doc/en/fcrontab.5.sgml index 11aa33c..5a45eec 100644 --- a/doc/en/fcrontab.5.sgml +++ b/doc/en/fcrontab.5.sgml @@ -8,7 +8,7 @@ Foundation. A copy of the license is included in gfdl.sgml. --> - + @@ -149,7 +149,7 @@ A copy of the license is included in gfdl.sgml. &seealso; options &optdayor;, &optbootrun;, &optrunfreq;, &optmail;, &optnolog;, &optserial;, &optlavg;, &optnice;, &optrunas; (see below). - + Entries run periodically The third type of &fcrontabf;'s entries begin by a "%", followed by one of the keywords : @@ -214,6 +214,7 @@ A copy of the license is included in gfdl.sgml. a single number in a field is considered as an interval : %mins 15 2-4 * * * echo will run at 2:15, 3:15 AND 4:15 every day. But every fields below the keywords are ignored in interval definition : %hours 15 2-4 * * * echo will run only ONCE either at 2:15, 3:15 OR 4:15. + &seealso; option &optrandom; (see below). @@ -420,6 +421,13 @@ A copy of the license is included in gfdl.sgml. &seealso; options &optlavg;, &optstrict;. + + random + + boolean(0) + In a line run periodically, this option answer the question : should this job be run as soon as possible in its interval of execution (safer), or should fcron set a random time of execution in that interval ? Note that if this option is set, the job may not run if fcron is not running during allthe execution interval. Besides, you must know that the random scheme may be quite easy to guess for skilled people. + + A boolean argument can be inexistent, in which case brackets are not used and it means true; the string "true", "yes" or 1 to mean true; and the string "false", "no" or 0 to mean false. See above for explanations about time value (section "entries based on elapsed system up time"). Note that dayand and dayor are in fact the same option : a false value to dayand is equivalent to a true to dayor, and reciprocally a false value to dayor is equivalent a true value to dayand. It is the same for lavgand and lavgor. diff --git a/doc/fcron-doc.mod.in b/doc/fcron-doc.mod.in index 8ba74ba..cf70245 100644 --- a/doc/fcron-doc.mod.in +++ b/doc/fcron-doc.mod.in @@ -70,6 +70,7 @@ nice'> nolog'> noticenotrun'> +random'> reset'> runas'> runfreq'> diff --git a/fcron.c b/fcron.c index 4f243d2..1faffba 100644 --- a/fcron.c +++ b/fcron.c @@ -21,7 +21,7 @@ * `LICENSE' that comes with the fcron source distribution. */ - /* $Id: fcron.c,v 1.57 2001-12-23 22:04:37 thib Exp $ */ + /* $Id: fcron.c,v 1.58 2002-01-27 16:32:53 thib Exp $ */ #include "fcron.h" @@ -29,7 +29,7 @@ #include "conf.h" #include "job.h" -char rcs_info[] = "$Id: fcron.c,v 1.57 2001-12-23 22:04:37 thib Exp $"; +char rcs_info[] = "$Id: fcron.c,v 1.58 2002-01-27 16:32:53 thib Exp $"; void main_loop(void); void check_signal(void); @@ -566,7 +566,9 @@ main(int argc, char **argv) if ( (lavg_array = calloc(lavg_array_size, sizeof(lavg))) == NULL ) die_e("could not calloc lavg_array"); - + /* initialize random number generator : + * WARNING : easy to guess !!! */ + srand(time(NULL)); main_loop(); diff --git a/fileconf.c b/fileconf.c index 3e949a4..966ec0a 100644 --- a/fileconf.c +++ b/fileconf.c @@ -22,7 +22,7 @@ * `LICENSE' that comes with the fcron source distribution. */ - /* $Id: fileconf.c,v 1.55 2001-12-23 22:04:44 thib Exp $ */ + /* $Id: fileconf.c,v 1.56 2002-01-27 16:33:10 thib Exp $ */ #include "fcrontab.h" @@ -930,6 +930,17 @@ read_opt(char *ptr, CL *cl) } } + else if( strcmp(opt_name, "random") == 0 ) { + if ( in_brackets && (ptr = get_bool(ptr, &i)) == NULL ) + Handle_err; + if ( i == 0 ) + clear_random(cl->cl_option); + else + set_random(cl->cl_option); + if (debug_opt) + fprintf(stderr, " Opt : \"%s\" %d\n", opt_name, i); + } + else { fprintf(stderr, "%s:%d: Option \"%s\" unknown: " "skipping option.\n", file_name, line, opt_name); diff --git a/option.h b/option.h index 3703639..e0a10b1 100644 --- a/option.h +++ b/option.h @@ -21,7 +21,7 @@ * `LICENSE' that comes with the fcron source distribution. */ - /* $Id: option.h,v 1.19 2001-12-23 22:06:22 thib Exp $ */ + /* $Id: option.h,v 1.20 2002-01-27 16:33:31 thib Exp $ */ /* This has been inspired from bitstring(3) : here is the original copyright : */ @@ -84,6 +84,7 @@ 20 Should we remove a %-job from lavg queue if the interval is exceeded ? 21 Should user be mailed if a %-job has not run during a period ? 22 Should fcron log everything about this job or just errors ? + 23 Should this job be run asap, or randomly in its allowed interval of execution ? */ @@ -402,5 +403,17 @@ (_bit_clear(opt, 22)) +/* + bit 23 : set to 1 : run this job at a random time in its allowed interval of execution. + set to 0 : run this job asap (safer) +*/ +#define is_random(opt) \ + (_bit_test(opt, 23)) +#define set_random(opt) \ + (_bit_set(opt, 23)) +#define clear_random(opt) \ + (_bit_clear(opt, 23)) + + #endif /* __OPTIONH__ */