]> granicus.if.org Git - fcron/commitdiff
added option random
authorthib <thib>
Sun, 27 Jan 2002 16:32:53 +0000 (16:32 +0000)
committerthib <thib>
Sun, 27 Jan 2002 16:32:53 +0000 (16:32 +0000)
database.c
doc/en/fcrontab.5.sgml
doc/fcron-doc.mod.in
fcron.c
fileconf.c
option.h

index b0b6e7b3986d8c08771d0cf126490defce5a0fa7..22f3a0478cea09455012954e274142e5273c36e0 100644 (file)
@@ -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 {
index 11aa33c0bf98b95ae1ce0cf0ac35c1a081db4a9c..5a45eecf104eddf88b7f04cd05e70f1419a8ba65 100644 (file)
@@ -8,7 +8,7 @@ Foundation.
 A copy of the license is included in gfdl.sgml.
 -->
 
-<!-- $Id: fcrontab.5.sgml,v 1.2 2002-01-04 19:12:01 thib Exp $ -->
+<!-- $Id: fcrontab.5.sgml,v 1.3 2002-01-27 16:37:37 thib Exp $ -->
 
 <refentry id="fcrontab.5">
     <refmeta>
@@ -149,7 +149,7 @@ A copy of the license is included in gfdl.sgml.
            <para>&seealso; options &optdayor;, &optbootrun;, &optrunfreq;, &optmail;, &optnolog;, &optserial;, &optlavg;, &optnice;, &optrunas; (see below).</para>
        </refsect2>
 
-       <refsect2>
+       <refsect2 id="fcrontab.5.periodent">
            <title>Entries run periodically</title>
            <para>The third type of &fcrontabf;'s entries begin by a "%", followed by one of the keywords :</para>
            <para><simplelist type="inline">
@@ -214,6 +214,7 @@ A copy of the license is included in gfdl.sgml.
                <para>a single number in a field is considered as an interval : <programlisting>%mins 15 2-4 * * * echo</programlisting> will run at 2:15, 3:15 AND 4:15 every day.</para>
                <para>But every fields below the keywords are ignored in interval definition : <programlisting>%hours 15 2-4 * * * echo</programlisting> will run only ONCE either at 2:15, 3:15 OR 4:15.</para>
            </note>
+           <para>&seealso; option &optrandom; (see below).</para>
        </refsect2>
 
        <refsect2>
@@ -420,6 +421,13 @@ A copy of the license is included in gfdl.sgml.
                        <para>&seealso; options &optlavg;, &optstrict;.</para>
                    </listitem>
                </varlistentry>
+               <varlistentry id="fcrontab.5.random">
+                   <term>random</term>
+                   <listitem>
+                       <para><emphasis><type>boolean</type></emphasis>(<constant>0</constant>)</para>
+                       <para>In a <link linkend="fcrontab.5.periodent">line run periodically</link>, 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 <emphasis>all</emphasis>the execution interval. Besides, you must know that the random scheme may be quite easy to guess for skilled people.</para>
+                   </listitem>
+               </varlistentry>
            </variablelist>
            <para>A <type>boolean</type> argument can be inexistent, in which case brackets are not used and it means <constant>true</constant>; the string "true", "yes" or 1 to mean <constant>true</constant>; and the string "false", "no" or 0 to mean <constant>false</constant>. See above for explanations about time value (section <link linkend="uptent">"entries based on elapsed system up time"</link>).</para>
            <para>Note that <varname>dayand</varname> and <varname>dayor</varname> are in fact the same option : a false value to <varname>dayand</varname> is equivalent to a true to <varname>dayor</varname>, and reciprocally a false value to <varname>dayor</varname> is equivalent a true value to <varname>dayand</varname>. It is the same for <varname>lavgand</varname> and <varname>lavgor</varname>.</para>
index 8ba74ba1ded4cecd1b4dbc0e7b06b050d07083d5..cf70245b449b6d42d1832d29d422f4139b63c5e2 100644 (file)
@@ -70,6 +70,7 @@
 <!ENTITY optnice '<link linkend="fcrontab.5.nice"><varname>nice</varname></link>'>
 <!ENTITY optnolog '<link linkend="fcrontab.5.nolog"><varname>nolog</varname></link>'>
 <!ENTITY optnoticenotrun '<link linkend="fcrontab.5.noticenotrun"><varname>noticenotrun</varname></link>'>
+<!ENTITY optrandom '<link linkend="fcrontab.5.random"><varname>random</varname></link>'>
 <!ENTITY optreset '<link linkend="fcrontab.5.reset"><varname>reset</varname></link>'>
 <!ENTITY optrunas '<link linkend="fcrontab.5.runas"><varname>runas</varname></link>'>
 <!ENTITY optrunfreq '<link linkend="fcrontab.5.runfreq"><varname>runfreq</varname></link>'>
diff --git a/fcron.c b/fcron.c
index 4f243d2f64f9b49f897e30a2c10d3d650e629637..1faffba470a11ef2edf6213da01b8a8a123260fa 100644 (file)
--- 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();
 
index 3e949a48ddb536ac62098312a3af013d4ffafac3..966ec0a8afe5572f92b426484281367d7ac32e3a 100644 (file)
@@ -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);  
index 370363995b1dac749f386e0c9963657aed8fe640..e0a10b10efbe747d6bf3aee72b628757805f984f 100644 (file)
--- 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 ?
 
 */
 
        (_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__ */