[26706] in Source-Commits
Re: /svn/athena r25696 - in trunk/athena/bin/desync: . debian
daemon@ATHENA.MIT.EDU (Benjamin Kaduk)
Sun Aug 5 14:14:40 2012
Date: Sun, 5 Aug 2012 14:14:36 -0400 (EDT)
From: Benjamin Kaduk <kaduk@MIT.EDU>
To: Jonathan D Reed <jdreed@MIT.EDU>
cc: source-commits@MIT.EDU
In-Reply-To: <201208040332.q743W2VX006187@drugstore.mit.edu>
Message-ID: <alpine.GSO.1.10.1208042130320.22690@multics.mit.edu>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII
On Fri, 3 Aug 2012, Jonathan D Reed 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/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 ]
I think the -c should not be in square brackets, as it is necessary in
order to get this form of the command.
> .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 ,
Typo?
> 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))) {
Style would indicate that both checks for non-NULL-ness were written the
same way...
> + 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]);
The new code prefers strtol() to atoi(); I guess this keeps bug-for-bug
compatibility with the old version...
> + 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",
The square bracketing here is also probably not quite right.
-Ben
> + progname, progname);
> }
>
>