Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/cron/dist Add -s (SINGLE_JOB) from OpenBSD.



details:   https://anonhg.NetBSD.org/src/rev/1b2ef5919edb
branches:  trunk
changeset: 971228:1b2ef5919edb
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Apr 18 19:32:19 2020 +0000

description:
Add -s (SINGLE_JOB) from OpenBSD.

diffstat:

 external/bsd/cron/dist/cron.c       |   5 +-
 external/bsd/cron/dist/crontab.5    |   9 +++-
 external/bsd/cron/dist/do_command.c |  11 +++-
 external/bsd/cron/dist/entry.c      |  12 ++++-
 external/bsd/cron/dist/externs.h    |   3 +-
 external/bsd/cron/dist/funcs.h      |   7 ++-
 external/bsd/cron/dist/job.c        |  84 +++++++++++++++++++++++++++---------
 external/bsd/cron/dist/structs.h    |   1 +
 external/bsd/cron/dist/user.c       |   5 +-
 9 files changed, 103 insertions(+), 34 deletions(-)

diffs (truncated from 354 to 300 lines):

diff -r cc6e6cf5f30f -r 1b2ef5919edb external/bsd/cron/dist/cron.c
--- a/external/bsd/cron/dist/cron.c     Sat Apr 18 19:27:48 2020 +0000
+++ b/external/bsd/cron/dist/cron.c     Sat Apr 18 19:32:19 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cron.c,v 1.10 2017/06/09 17:36:30 christos Exp $       */
+/*     $NetBSD: cron.c,v 1.11 2020/04/18 19:32:19 christos Exp $       */
 
 /* Copyright 1988,1990,1993,1994 by Paul Vixie
  * All rights reserved
@@ -25,7 +25,7 @@
 #if 0
 static char rcsid[] = "Id: cron.c,v 1.12 2004/01/23 18:56:42 vixie Exp";
 #else
-__RCSID("$NetBSD: cron.c,v 1.10 2017/06/09 17:36:30 christos Exp $");
+__RCSID("$NetBSD: cron.c,v 1.11 2020/04/18 19:32:19 christos Exp $");
 #endif
 #endif
 
@@ -532,6 +532,7 @@
                        Debug(DPROC,
                              ("[%ld] sigchld...pid #%ld died, stat=%d\n",
                               (long)getpid(), (long)pid, WEXITSTATUS(waiter)));
+                       job_exit(pid);
                        break;
                }
        }
diff -r cc6e6cf5f30f -r 1b2ef5919edb external/bsd/cron/dist/crontab.5
--- a/external/bsd/cron/dist/crontab.5  Sat Apr 18 19:27:48 2020 +0000
+++ b/external/bsd/cron/dist/crontab.5  Sat Apr 18 19:32:19 2020 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: crontab.5,v 1.9 2020/04/17 19:42:14 pgoyette Exp $
+.\" $NetBSD: crontab.5,v 1.10 2020/04/18 19:32:19 christos Exp $
 .\"
 .\"/* Copyright 1988,1990,1993,1994 by Paul Vixie
 .\" * All rights reserved
@@ -227,6 +227,13 @@
 .Xr cron 8 .
 .It Fl q Ar command
 Execution will not be logged.
+.It Fl s Ar command
+Only a single instance of
+.Ar command
+will be run concurrently.
+Additional instances of
+.Ar command
+will not be scheduled until the earlier one completes.
 .El
 .Pp
 Commands are executed by
diff -r cc6e6cf5f30f -r 1b2ef5919edb external/bsd/cron/dist/do_command.c
--- a/external/bsd/cron/dist/do_command.c       Sat Apr 18 19:27:48 2020 +0000
+++ b/external/bsd/cron/dist/do_command.c       Sat Apr 18 19:32:19 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: do_command.c,v 1.14 2019/08/03 07:06:47 christos Exp $ */
+/*     $NetBSD: do_command.c,v 1.15 2020/04/18 19:32:19 christos Exp $ */
 
 /* Copyright 1988,1990,1993,1994 by Paul Vixie
  * All rights reserved
@@ -25,7 +25,7 @@
 #if 0
 static char rcsid[] = "Id: do_command.c,v 1.9 2004/01/23 18:56:42 vixie Exp";
 #else
-__RCSID("$NetBSD: do_command.c,v 1.14 2019/08/03 07:06:47 christos Exp $");
+__RCSID("$NetBSD: do_command.c,v 1.15 2020/04/18 19:32:19 christos Exp $");
 #endif
 #endif
 
@@ -35,7 +35,7 @@
 static int             child_process(entry *);
 static int             safe_p(const char *, const char *);
 
-void
+pid_t
 do_command(entry *e, user *u) {
        int retval;
 
@@ -66,9 +66,14 @@
                break;
        default:
                /* parent process */
+               if ((e->flags & SINGLE_JOB) == 0)
+                       jobpid = -1;
                break;
        }
        Debug(DPROC, ("[%ld] main process returning to work\n",(long)getpid()));
+
+       /* only return pid if a singleton */
+       return jobpid;
 }
 
 static void
diff -r cc6e6cf5f30f -r 1b2ef5919edb external/bsd/cron/dist/entry.c
--- a/external/bsd/cron/dist/entry.c    Sat Apr 18 19:27:48 2020 +0000
+++ b/external/bsd/cron/dist/entry.c    Sat Apr 18 19:32:19 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: entry.c,v 1.10 2018/07/28 13:55:08 kre Exp $   */
+/*     $OpenBSD: entry.c,v 1.51 2020/04/16 17:51:56 millert Exp $      */
 
 /*
  * Copyright 1988,1990,1993,1994 by Paul Vixie
@@ -26,7 +26,7 @@
 #if 0
 static char rcsid[] = "Id: entry.c,v 1.17 2004/01/23 18:56:42 vixie Exp";
 #else
-__RCSID("$NetBSD: entry.c,v 1.10 2018/07/28 13:55:08 kre Exp $");
+__RCSID("$NetBSD: entry.c,v 1.11 2020/04/18 19:32:19 christos Exp $");
 #endif
 #endif
 
@@ -361,6 +361,14 @@
                        }
                        e->flags |= DONT_LOG;
                        break;
+               case 's':
+                       /* only allow the user to set the option once */
+                       if ((e->flags & SINGLE_JOB) == SINGLE_JOB) {
+                               ecode = e_option;
+                               goto eof;
+                       }
+                       e->flags |= SINGLE_JOB;
+                       break;
                default:
                        ecode = e_option;
                        goto eof;
diff -r cc6e6cf5f30f -r 1b2ef5919edb external/bsd/cron/dist/externs.h
--- a/external/bsd/cron/dist/externs.h  Sat Apr 18 19:27:48 2020 +0000
+++ b/external/bsd/cron/dist/externs.h  Sat Apr 18 19:32:19 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: externs.h,v 1.2 2010/05/06 18:53:17 christos Exp $     */
+/*     $NetBSD: externs.h,v 1.3 2020/04/18 19:32:19 christos Exp $     */
 
 /* Copyright 1993,1994 by Paul Vixie
  * All rights reserved
@@ -30,6 +30,7 @@
 #include <sys/fcntl.h>
 #include <sys/file.h>
 #include <sys/stat.h>
+#include <sys/queue.h>
 
 #include <bitstring.h>
 #include <ctype.h>
diff -r cc6e6cf5f30f -r 1b2ef5919edb external/bsd/cron/dist/funcs.h
--- a/external/bsd/cron/dist/funcs.h    Sat Apr 18 19:27:48 2020 +0000
+++ b/external/bsd/cron/dist/funcs.h    Sat Apr 18 19:32:19 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: funcs.h,v 1.6 2018/06/14 22:04:28 christos Exp $       */
+/*     $NetBSD: funcs.h,v 1.7 2020/04/18 19:32:19 christos Exp $       */
 
 /*
  * Id: funcs.h,v 1.9 2004/01/23 18:56:42 vixie Exp
@@ -32,7 +32,8 @@
                open_logfile(void),
                sigpipe_func(void),
                job_add(entry *, user *, time_t),
-               do_command(entry *, user *),
+               job_remove(entry *, user *),
+               job_exit(pid_t),
                link_user(cron_db *, user *),
                unlink_user(cron_db *, user *),
                free_user(user *),
@@ -49,6 +50,8 @@
                log_itx(const char *, int, const char *, const char *, ...)
                    __printflike(4, 5);
 
+pid_t          do_command(entry *, user *);
+
 int            job_runqueue(void),
                set_debug_flags(const char *),
                get_char(FILE *),
diff -r cc6e6cf5f30f -r 1b2ef5919edb external/bsd/cron/dist/job.c
--- a/external/bsd/cron/dist/job.c      Sat Apr 18 19:27:48 2020 +0000
+++ b/external/bsd/cron/dist/job.c      Sat Apr 18 19:32:19 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: job.c,v 1.2 2010/05/06 18:53:17 christos Exp $ */
+/*     $NetBSD: job.c,v 1.3 2020/04/18 19:32:19 christos Exp $ */
 
 /* Copyright 1988,1990,1993,1994 by Paul Vixie
  * All rights reserved
@@ -26,20 +26,21 @@
 #if 0
 static char rcsid[] = "Id: job.c,v 1.6 2004/01/23 18:56:43 vixie Exp";
 #else
-__RCSID("$NetBSD: job.c,v 1.2 2010/05/06 18:53:17 christos Exp $");
+__RCSID("$NetBSD: job.c,v 1.3 2020/04/18 19:32:19 christos Exp $");
 #endif
 #endif
 
 #include "cron.h"
 
 typedef        struct _job {
-       struct _job     *next;
+       SIMPLEQ_ENTRY(_job) entries;
        entry           *e;
        user            *u;
        time_t          t;
+       pid_t           pid;
 } job;
 
-static job     *jhead = NULL, *jtail = NULL;
+static SIMPLEQ_HEAD(job_queue, _job) jobs = SIMPLEQ_HEAD_INITIALIZER(jobs);
 
 static int okay_to_go(job *);
 
@@ -48,48 +49,86 @@
        job *j;
 
        /* if already on queue, keep going */
-       for (j = jhead; j != NULL; j = j->next)
+       SIMPLEQ_FOREACH(j, &jobs, entries) {
                if (j->e == e && j->u == u) {
                        j->t = target_time;
                        return;
                }
+       }
 
        /* build a job queue element */
-       if ((j = malloc(sizeof(*j))) == NULL)
+       if ((j = calloc(1, sizeof(*j))) == NULL)
                return;
-       j->next = NULL;
        j->e = e;
        j->u = u;
        j->t = target_time;
+       j->pid = -1;
 
        /* add it to the tail */
-       if (jhead == NULL)
-               jhead = j;
-       else
-               jtail->next = j;
-       jtail = j;
+       SIMPLEQ_INSERT_TAIL(&jobs, j, entries);
+}
+
+void
+job_remove(entry *e, user *u)
+{
+       job *j, *prev = NULL;
+
+       SIMPLEQ_FOREACH(j, &jobs, entries) {
+               if (j->e == e && j->u == u) {
+                       if (prev == NULL)
+                               SIMPLEQ_REMOVE_HEAD(&jobs, entries);
+                       else
+                               SIMPLEQ_REMOVE_AFTER(&jobs, prev, entries);
+                       free(j);
+                       break;
+               }
+               prev = j;
+       }
+}
+
+void
+job_exit(pid_t jobpid)
+{
+       job *j, *prev = NULL;
+
+       /* If a singleton exited, remove and free it. */
+       SIMPLEQ_FOREACH(j, &jobs, entries) {
+               if (jobpid == j->pid) {
+                       if (prev == NULL)
+                               SIMPLEQ_REMOVE_HEAD(&jobs, entries);
+                       else
+                               SIMPLEQ_REMOVE_AFTER(&jobs, prev, entries);
+                       free(j);
+                       break;
+               }
+               prev = j;
+       }
 }
 
 int
 job_runqueue(void) {
-       job *j, *jn;
+       struct job_queue singletons = SIMPLEQ_HEAD_INITIALIZER(singletons);
+       job *j;
        int run = 0;
 
-       for (j = jhead; j; j = jn) {
-               if (okay_to_go(j))
-                       do_command(j->e, j->u);
-               else {
+       while ((j = SIMPLEQ_FIRST(&jobs))) {
+               SIMPLEQ_REMOVE_HEAD(&jobs, entries);
+               if (okay_to_go(j)) {
+                       j->pid = do_command(j->e, j->u);
+                       run++;
+               } else {
                        char *x = mkprints(j->e->cmd, strlen(j->e->cmd));
                        char *usernm = env_get("LOGNAME", j->e->envp);
 
                        log_it(usernm, getpid(), "CMD (skipped)", x);
                        free(x);
                }
-               jn = j->next;
-               free(j);
-               run++;
+               if (j->pid != -1)
+                       SIMPLEQ_INSERT_TAIL(&singletons, j, entries);
+               else



Home | Main Index | Thread Index | Old Index