AboutSummaryRefsLogTreeCommitDiffStats
path: root/src/mcron/job-specifier.scm
diff options
context:
space:
mode:
authorMathieu Lirzin <mthl@gnu.org>2016-05-07 11:09:44 +0200
committerMathieu Lirzin <mthl@gnu.org>2016-05-07 23:38:53 +0200
commit995bc9ca6ebf6880d7e7e6f3d1baa2941758fc47 (patch)
tree1f12ba3e4218d91a8736df5a5fa6eb82ffe39181 /src/mcron/job-specifier.scm
parentbb8703b679509fe3b842f9f24f6871b3c2889916 (diff)
downloadmcron-995bc9ca6ebf6880d7e7e6f3d1baa2941758fc47.tar.gz
mcron-995bc9ca6ebf6880d7e7e6f3d1baa2941758fc47.tar.bz2
mcron-995bc9ca6ebf6880d7e7e6f3d1baa2941758fc47.zip
all: Rename 'scm' directory to 'src'.
* scm/mcron/config.scm.in: Rename to ... * src/mcron/config.scm.in: ... this. * scm/mcron/crontab.scm: Rename to ... * src/mcron/crontab.scm: ... this. * scm/mcron/environment.scm: Rename to ... * src/mcron/environment.scm: ... this. * scm/mcron/job-specifier.scm: Rename to ... * src/mcron/job-specifier.scm: ... this. * scm/mcron/main.scm: Rename to ... * src/mcron/main.scm: ... this. * scm/mcron/mcron-core.scm: Rename to ... * src/mcron/mcron-core.scm: ... this. * scm/mcron/redirect.scm: Rename to ... * src/mcron/redirect.scm: ... this. * scm/mcron/vixie-specification.scm: Rename to ... * src/mcron/vixie-specification.scm: ... this. * scm/mcron/vixie-time.scm: Rename to ... * src/mcron/vixie-time.scm: ... this. * mcron.c: Rename to ... * src/mcron.c: ... this. * Makefile.am: Adapt to them. * build-aux/pre-inst-env.in: Likewise. * configure.ac (AC_CONFIG_FILES): Likewise. (AC_CONFIG_HEADER): Set to 'src/config.h'. * .gitignore: Update.
Diffstat (limited to 'src/mcron/job-specifier.scm')
-rw-r--r--src/mcron/job-specifier.scm253
1 files changed, 253 insertions, 0 deletions
diff --git a/src/mcron/job-specifier.scm b/src/mcron/job-specifier.scm
new file mode 100644
index 0000000..1c2f9d9
--- /dev/null
+++ b/src/mcron/job-specifier.scm
@@ -0,0 +1,253 @@
+;; Copyright (C) 2003 Dale Mellor
+;; Copyright (C) 2016 Mathieu Lirzin
+;;
+;; This file is part of GNU mcron.
+;;
+;; GNU mcron is free software: you can redistribute it and/or modify it under
+;; the terms of the GNU General Public License as published by the Free
+;; Software Foundation, either version 3 of the License, or (at your option)
+;; any later version.
+;;
+;; GNU mcron is distributed in the hope that it will be useful, but WITHOUT
+;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+;; more details.
+;;
+;; You should have received a copy of the GNU General Public License along
+;; with GNU mcron. If not, see <http://www.gnu.org/licenses/>.
+
+
+
+;; This module defines all the functions that can be used by scheme mcron
+;; configuration files, namely the procedures for working out next times, the
+;; job procedure for registering new jobs (actually a wrapper around the core
+;; add-job function), and the procedure for declaring environment modifications.
+
+(define-module (mcron job-specifier)
+ #:use-module (ice-9 match)
+ #:use-module (mcron core)
+ #:use-module (mcron environment)
+ #:use-module (mcron vixie-time)
+ #:use-module (srfi srfi-1)
+ #:use-module (srfi srfi-26)
+ #:re-export (append-environment-mods)
+ #:export (range
+ next-year-from next-year
+ next-month-from next-month
+ next-day-from next-day
+ next-hour-from next-hour
+ next-minute-from next-minute
+ next-second-from next-second
+ set-configuration-user
+ set-configuration-time
+ job))
+
+(define* (range start end #:optional (step 1))
+ "Produces a list of values from START up to (but not including) END. An
+optional STEP may be supplied, and (if positive) only every step'th value will
+go into the list. For example, (range 1 6 2) returns '(1 3 5)."
+ (unfold (cut >= <> end) identity (cute + <> (max step 1)) start))
+
+(define (%find-best-next current next-list)
+ ;; Takes a value and a list of possible next values (all assumed less than
+ ;; 9999). It returns a pair consisting of the smallest element of the
+ ;; NEXT-LIST, and the smallest element larger than the CURRENT value. If an
+ ;; example of the latter cannot be found, 9999 will be returned.
+ (let loop ((smallest 9999) (closest+ 9999) (lst next-list))
+ (match lst
+ (() (cons smallest closest+))
+ ((time . rest)
+ (loop (min time smallest)
+ (if (> time current) (min time closest+) closest+)
+ rest)))))
+
+;; Internal function to return the time corresponding to some near future
+;; hour. If hour-list is not supplied, the time returned corresponds to the
+;; start of the next hour of the day.
+;;
+;; If the hour-list is supplied the time returned corresponds to the first hour
+;; of the day in the future which is contained in the list. If all the values in
+;; the list are less than the current hour, then the time returned will
+;; correspond to the first hour in the list *on the following day*.
+;;
+;; ... except that the function is actually generalized to deal with seconds,
+;; minutes, etc., in an obvious way :-)
+;;
+;; Note that value-list always comes from an optional argument to a procedure,
+;; so is wrapped up as the first element of a list (i.e. it is a list inside a
+;; list).
+
+(define (bump-time time value-list component higher-component
+ set-component! set-higher-component!)
+ (if (null? value-list)
+ (set-component! time (+ (component time) 1))
+ (let ((best-next (%find-best-next (component time) (car value-list))))
+ (if (eqv? 9999 (cdr best-next))
+ (begin
+ (set-higher-component! time (+ (higher-component time) 1))
+ (set-component! time (car best-next)))
+ (set-component! time (cdr best-next)))))
+ (car (mktime time)))
+
+
+
+
+;; Set of configuration methods which use the above general function to bump
+;; specific components of time to the next legitimate value. In each case, all
+;; the components smaller than that of interest are taken to zero, so that for
+;; example the time of the next year will be the time at which the next year
+;; actually starts.
+
+(define (next-year-from current-time . year-list)
+ (let ((time (localtime current-time)))
+ (set-tm:mon time 0)
+ (set-tm:mday time 1)
+ (set-tm:hour time 0)
+ (set-tm:min time 0)
+ (set-tm:sec time 0)
+ (bump-time time year-list tm:year tm:year set-tm:year set-tm:year)))
+
+(define (next-month-from current-time . month-list)
+ (let ((time (localtime current-time)))
+ (set-tm:mday time 1)
+ (set-tm:hour time 0)
+ (set-tm:min time 0)
+ (set-tm:sec time 0)
+ (bump-time time month-list tm:mon tm:year set-tm:mon set-tm:year)))
+
+(define (next-day-from current-time . day-list)
+ (let ((time (localtime current-time)))
+ (set-tm:hour time 0)
+ (set-tm:min time 0)
+ (set-tm:sec time 0)
+ (bump-time time day-list tm:mday tm:mon set-tm:mday set-tm:mon)))
+
+(define (next-hour-from current-time . hour-list)
+ (let ((time (localtime current-time)))
+ (set-tm:min time 0)
+ (set-tm:sec time 0)
+ (bump-time time hour-list tm:hour tm:mday set-tm:hour set-tm:mday)))
+
+(define (next-minute-from current-time . minute-list)
+ (let ((time (localtime current-time)))
+ (set-tm:sec time 0)
+ (bump-time time minute-list tm:min tm:hour set-tm:min set-tm:hour)))
+
+(define (next-second-from current-time . second-list)
+ (let ((time (localtime current-time)))
+ (bump-time time second-list tm:sec tm:min set-tm:sec set-tm:min)))
+
+
+
+;; The current-action-time is the time a job was last run, the time from which
+;; the next time to run a job must be computed. (When the program is first run,
+;; this time is set to the configuration time so that jobs run from that moment
+;; forwards.) Once we have this, we supply versions of the time computation
+;; commands above which implicitly assume this value.
+
+(define current-action-time 0)
+
+
+
+;; We want to provide functions which take a single optional argument (as well
+;; as implicitly the current action time), but unlike usual scheme behaviour if
+;; the argument is missing we want to act like it is really missing, and if it
+;; is there we want to act like it is a genuine argument, not a list of
+;; optionals.
+
+(define (maybe-args function args)
+ (if (null? args)
+ (function current-action-time)
+ (function current-action-time (car args))))
+
+
+
+;; These are the convenience functions we were striving to define for the
+;; configuration files. They are wrappers for the next-X-from functions above,
+;; but implicitly use the current-action-time for the time argument.
+
+(define (next-year . args) (maybe-args next-year-from args))
+(define (next-month . args) (maybe-args next-month-from args))
+(define (next-day . args) (maybe-args next-day-from args))
+(define (next-hour . args) (maybe-args next-hour-from args))
+(define (next-minute . args) (maybe-args next-minute-from args))
+(define (next-second . args) (maybe-args next-second-from args))
+
+
+
+;; The default user for running jobs is the current one (who invoked this
+;; program). There are exceptions: when cron parses /etc/crontab the user is
+;; specified on each individual line; when cron parses /var/cron/tabs/* the user
+;; is derived from the filename of the crontab. These cases are dealt with by
+;; mutating this variable. Note that the variable is only used at configuration
+;; time; a UID is stored with each job and it is that which takes effect when
+;; the job actually runs.
+
+(define configuration-user (getpw (getuid)))
+(define configuration-time (current-time))
+
+(define (set-configuration-user user)
+ (set! configuration-user (if (or (string? user)
+ (integer? user))
+ (getpw user)
+ user)))
+(define (set-configuration-time time) (set! configuration-time time))
+
+
+
+;; The job function, available to configuration files for adding a job rule to
+;; the system.
+;;
+;; Here we must 'normalize' the next-time-function so that it is always a lambda
+;; function which takes one argument (the last time the job ran) and returns a
+;; single value (the next time the job should run). If the input value is a
+;; string this is parsed as a Vixie-style time specification, and if it is a
+;; list then we arrange to eval it (but note that such lists are expected to
+;; ignore the function parameter - the last run time is always read from the
+;; current-action-time global variable). A similar normalization is applied to
+;; the action.
+;;
+;; Here we also compute the first time that the job is supposed to run, by
+;; finding the next legitimate time from the current configuration time (set
+;; right at the top of this program).
+
+(define (job time-proc action . displayable)
+ (let ((action (cond ((procedure? action) action)
+ ((list? action) (lambda () (primitive-eval action)))
+ ((string? action) (lambda () (system action)))
+ (else
+ (throw 'mcron-error 2
+ "job: invalid second argument (action; should be lambda "
+ "function, string or list)"))))
+
+ (time-proc
+ (cond ((procedure? time-proc) time-proc)
+ ((string? time-proc) (parse-vixie-time time-proc))
+ ((list? time-proc) (lambda (current-time)
+ (primitive-eval time-proc)))
+ (else
+ (throw 'mcron-error 3
+ "job: invalid first argument (next-time-function; "
+ "should be function, string or list)"))))
+ (displayable
+ (cond ((not (null? displayable)) (car displayable))
+ ((procedure? action) "Lambda function")
+ ((string? action) action)
+ ((list? action) (with-output-to-string
+ (lambda () (display action)))))))
+ (add-job (lambda (current-time)
+ (set! current-action-time current-time) ;; ?? !!!! Code
+
+ ;; Contributed by Sergey Poznyakoff to allow for daylight savings
+ ;; time changes.
+ (let* ((next (time-proc current-time))
+ (gmtoff (tm:gmtoff (localtime next)))
+ (d (+ next (- gmtoff
+ (tm:gmtoff (localtime current-time))))))
+ (if (eqv? (tm:gmtoff (localtime d)) gmtoff)
+ d
+ next)))
+ action
+ displayable
+ configuration-time
+ configuration-user)))