[26720] in Source-Commits

home help back first fref pref prev next nref lref last post

Re: /svn/athena r25696 - in trunk/athena/bin/desync: . debian

daemon@ATHENA.MIT.EDU (Jonathon Weiss)
Tue Aug 7 17:09:08 2012

Message-Id: <201208072109.q77L95sd025717@outgoing.mit.edu>
To: Jonathan D Reed <jdreed@MIT.EDU>
cc: source-commits@MIT.EDU
In-reply-to: <201208040332.q743W2VX006187@drugstore.mit.edu>
Date: Tue, 07 Aug 2012 17:09:04 -0400
From: Jonathon Weiss <jweiss@MIT.EDU>


You've changed the semantics for "range".  In crontab mode it's minutes,
but in the original mode it is seconds.  It probably doesn't make sense
to specify it in seconds for crontab mode, but that certainly needs to
be documented.  Hopefully it won't be too confusing for it to be
different in the two cases.

As for square bracketting, I think you want (in both the man page and
the code):

	desync -c hours [ range [ other arguments ] ]

since I believe the code requires the first non-option argument to be
the range, so if you are going to supply other arguments, you have to
supply the range.

Pulling these two together, there's the related problem that if you
don't specify the range or additional arguments, but you do specify
crontab mode, you get a default of 3600 *minutes*, which doesn't make
sense.

I'd also advocate for a check in crontab mode that interval is <= 1440
(one day), since the code outputs a cron entry that will be run daily.
(Being flexible enough to output something that isn't necessarily run
daily may not be of enough value to actually code, but I could see use
cases for it.)

	Jonathon




Jonathan D Reed <jdreed@MIT.EDU> wrote:

> Author: jdreed
> Date: 2012-08-03 23:32:02 -0400 (Fri, 03 Aug 2012)
> New Revision: 25696
> 
> Modified:
>    trunk/athena/bin/desync/debian/changelog
>    trunk/athena/bin/desync/desync.1
>    trunk/athena/bin/desync/desync.c
> Log:
> In desync:
>   * Add -c option to generate crontab output (Trac: #1173)
> 
> 
> Modified: trunk/athena/bin/desync/debian/changelog
> ===================================================================
> --- trunk/athena/bin/desync/debian/changelog	2012-08-03 23:54:13 UTC (rev 25695)
> +++ trunk/athena/bin/desync/debian/changelog	2012-08-04 03:32:02 UTC (rev 25696)
> @@ -1,8 +1,9 @@
> -debathena-desync (10.0.0-0debathena3) UNRELEASED; urgency=low
> +debathena-desync (10.1-0debathena1) unstable; urgency=low
>  
>    * Bump debian/compat to 6
> +  * Add -c option to generate crontab output (Trac: #1173)
>  
> - -- Jonathan Reed <jdreed@mit.edu>  Sun, 01 Jul 2012 12:21:32 -0400
> + -- Jonathan Reed <jdreed@mit.edu>  Fri, 03 Aug 2012 23:26:43 -0400
>  
>  debathena-desync (10.0.0-0debathena2) unstable; urgency=low
>  
> 
> Modified: trunk/athena/bin/desync/desync.1
> ===================================================================
> --- trunk/athena/bin/desync/desync.1	2012-08-03 23:54:13 UTC (rev 25695)
> +++ trunk/athena/bin/desync/desync.1	2012-08-04 03:32:02 UTC (rev 25696)
> @@ -14,7 +14,7 @@
>  .\" this software for any purpose.  It is provided "as is"
>  .\" without express or implied warranty.
>  .\"
> -.TH DESYNC 1 "5 March 1997"
> +.TH DESYNC 1 "3 August 2012"
>  .SH NAME
>  desync \- desynchronize timed jobs on networks
>  .SH SYNOPSIS
> @@ -22,6 +22,11 @@
>  [
>  .B \-t
>  timefile ] [ range ]
> +
> +.B desync 
> +[ 
> +.B \-c 
> +hours ] [ range ] [ other arguments ]
>  .SH DESCRIPTION
>  .I desync
>  is a tool which sleeps a random (hostname seeded) period of time (up
> @@ -83,7 +88,7 @@
>  .I timefile
>  exists and the current time is equal to or greater than the time value
>  listed in
> -.IR timefile ,
> +q.IR timefile ,
>  then
>  .I desync
>  unlinks
> @@ -99,6 +104,28 @@
>  	fi
>  .fi
>  
> +.TP 8
> +.B \-c hours
> +This option changes the behavior of
> +.I desync
> +and causes it to generate output suitable for use in a crontab file.
> +This is useful on modern Linux distributions, where sleeping for
> +extended periods inside a cron job can confuse power management software
> +or packages such as ConsoleKit, and running a cron job every 5 minutes
> +to see if desync thinks it is "time to run" is undesirable.  Since
> +desync will generate the same value each time on the same machine, it is
> +fine to generate crontab files in a package's post-install script, for
> +example.  In this mode, desync will output the crontab fields and then
> +any additional arguments you supply.  So to generate a crontab with a
> +randomized job, one might do something like this:
> +
> +.nf
> +	for i in 2 4 8 14 20; do
> +	    desync -c $i 120 root /etc/athena/update_ws >> /etc/cron.d/update
> +	done
> +.fi
> +
> +
>  .SH SEE ALSO
>  cron(8)
>  .SH AUTHOR
> @@ -106,4 +133,6 @@
>  .br
>  Greg Hudson, MIT Information Systems
>  .br
> -Copyright (c) 1995, 1996, 1997, Massachusetts Institute of Technology
> +Debathena Project
> +.br
> +Copyright (c) 1995, 1996, 1997, 2012 Massachusetts Institute of Technology
> 
> Modified: trunk/athena/bin/desync/desync.c
> ===================================================================
> --- trunk/athena/bin/desync/desync.c	2012-08-03 23:54:13 UTC (rev 25695)
> +++ trunk/athena/bin/desync/desync.c	2012-08-04 03:32:02 UTC (rev 25696)
> @@ -34,6 +34,7 @@
>  #include <errno.h>
>  #include <unistd.h>
>  #include <time.h>
> +#include <limits.h>
>  
>  extern int optind;
>  extern char *optarg;
> @@ -46,7 +47,7 @@
>  
>  int main(int argc, char **argv)
>  {
> -  const char *timefile = NULL, *hostname = NULL;
> +  const char *timefile = NULL, *hostname = NULL, *crontabhour = NULL;
>    char buf[128];
>    int range, interval, c, noop = 0;
>    unsigned long tval;
> @@ -57,7 +58,7 @@
>    progname = argv[0];
>  
>    /* Parse command-line flags. */
> -  while ((c = getopt(argc, argv, "h:nt:")) != -1)
> +  while ((c = getopt(argc, argv, "h:nc:t:")) != -1)
>      {
>        switch (c) {
>        case 'h':
> @@ -66,6 +67,9 @@
>        case 'n':
>  	noop = 1;
>  	break;
> +      case 'c':
> +	crontabhour = optarg;
> +	break;
>        case 't':
>  	timefile = optarg;
>  	break;
> @@ -75,10 +79,24 @@
>        }
>      }
>  
> +  if (crontabhour && ((timefile != NULL) || (noop == 1))) {
> +    usage();
> +    return 2;
> +  }
>    /* Get the time interval from the remaining argument, if there is one. */
>    argc -= optind;
>    argv += optind;
> -  range = (argc == 1) ? atoi(argv[0]) : 3600;
> +  if ((argc > 1) && ! crontabhour) {
> +    usage();
> +    return 2;
> +  }
> +  if (argc >= 1) {
> +    range = atoi(argv[0]);
> +    argc -= 1;
> +    argv += 1;
> +  } else {
> +    range = 3600;
> +  }
>    if (range == 0)
>      {
>        fprintf(stderr, "%s: Invalid range value\n", progname);
> @@ -155,6 +173,35 @@
>  	  return 1;
>  	}
>      }
> +  else if (crontabhour)
> +    {
> +      char *endptr;
> +      int mins, hours = 0;
> +      int j;
> +      errno = 0;
> +      hours = strtol(crontabhour, &endptr, 10);
> +      if ((errno == ERANGE && (hours == LONG_MAX || hours == LONG_MIN))
> +	  || (errno != 0 && hours == 0)
> +	  || (endptr == crontabhour))
> +	{
> +	  fprintf(stderr, "%s: Could not convert %s to integer\n", progname,
> +		  crontabhour);
> +	return 1;
> +      }
> +      if ((hours > 23) || (hours < 0 ))
> +	{
> +	  fprintf(stderr, 
> +		  "%s: in crontab mode, hours must be between 0 and 23\n",
> +		  progname);
> +	return 1;
> +      }
> +      mins = interval % 60;
> +      hours = (hours + (interval / 60)) % 24;
> +      printf("%d %d * * *", mins, hours);
> +      for (j = 0; j < argc; j++)
> +	printf(" %s", argv[j]);
> +      printf("\n");
> +    }
>    else if (noop)
>      printf("%lu\n", (unsigned long) interval);
>    else
> @@ -187,6 +234,6 @@
>  static void usage()
>  {
>    fprintf(stderr,
> -	  "Usage: %s [-h name] [-n] [-t timefile] [range]\n",
> -	  progname);
> +	  "Usage: %s [-h name] [-n] [-t timefile] [range]\n       %s [-c hour] [range] [crontab arguments]\n",
> +	  progname, progname);
>  }
> 

home help back first fref pref prev next nref lref last post