summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaleb Tennis <caleb@gentoo.org>2008-02-05 18:22:38 +0000
committerCaleb Tennis <caleb@gentoo.org>2008-02-05 18:22:38 +0000
commit5b6fa81ba23adc0c5616e872093f6961c9bb3bb8 (patch)
tree949c98b616722665a210de66307393dc2cbdfe3f /dev-util/lockrun
parentStable for HPPA (bug #208929). Fixed an unquoted variable. (diff)
downloadhistorical-5b6fa81ba23adc0c5616e872093f6961c9bb3bb8.tar.gz
historical-5b6fa81ba23adc0c5616e872093f6961c9bb3bb8.tar.bz2
historical-5b6fa81ba23adc0c5616e872093f6961c9bb3bb8.zip
Initial import
Package-Manager: portage-2.1.4.1
Diffstat (limited to 'dev-util/lockrun')
-rw-r--r--dev-util/lockrun/ChangeLog10
-rw-r--r--dev-util/lockrun/Manifest4
-rw-r--r--dev-util/lockrun/files/lockrun.c275
-rw-r--r--dev-util/lockrun/lockrun-2.ebuild30
-rw-r--r--dev-util/lockrun/metadata.xml9
5 files changed, 328 insertions, 0 deletions
diff --git a/dev-util/lockrun/ChangeLog b/dev-util/lockrun/ChangeLog
new file mode 100644
index 000000000000..78c05207893e
--- /dev/null
+++ b/dev-util/lockrun/ChangeLog
@@ -0,0 +1,10 @@
+# ChangeLog for dev-util/lockrun
+# Copyright 1999-2008 Gentoo Foundation; Distributed under the GPL v2
+# $Header: /var/cvsroot/gentoo-x86/dev-util/lockrun/ChangeLog,v 1.1 2008/02/05 18:22:37 caleb Exp $
+
+*lockrun-2 (05 Feb 2008)
+
+ 05 Feb 2008; Caleb Tennis <caleb@gentoo.org> +files/lockrun.c,
+ +metadata.xml, +lockrun-2.ebuild:
+ Initial import
+
diff --git a/dev-util/lockrun/Manifest b/dev-util/lockrun/Manifest
new file mode 100644
index 000000000000..e3bfc7476314
--- /dev/null
+++ b/dev-util/lockrun/Manifest
@@ -0,0 +1,4 @@
+AUX lockrun.c 6575 RMD160 91c6820a87669de5d240df0b58ebcb41bc5f9d54 SHA1 d50a9b5f3c22683c47bbe3173355cd6f0cbc2090 SHA256 a334776691c0e0c7ceea0ea78bac9124ab9df176797cd77cda95a1307065e715
+EBUILD lockrun-2.ebuild 661 RMD160 d21c311f7cd80659078031248ae6d90c0fc3ecd7 SHA1 8807d130c539d4788bce8ff5bae7a5c5bb4ad84d SHA256 5e4dbb9aa56b6430cdbfd969c4c28357f9c3682045eaed2c5d78fcc88ccb4aca
+MISC ChangeLog 349 RMD160 3f4f37e2dc227b5e0b78f247545b2fe018ad144c SHA1 0108ceee8548d939f8614ccd7837e60556bebbe9 SHA256 9ff68ea4c6ffd826ca9368c524ff532855854ab1191182368bd8a109767740e3
+MISC metadata.xml 247 RMD160 a5ff2ee9cafe947966a8dcf8d5cfeb342ec7bdae SHA1 69a85d48f08cc2be18ee30c1b8d4a7296cd4d61f SHA256 68128452ea375ff09cbe528db45b8aa0513183db50479de5e82aa1a0072aefb1
diff --git a/dev-util/lockrun/files/lockrun.c b/dev-util/lockrun/files/lockrun.c
new file mode 100644
index 000000000000..bccc0db7bcb7
--- /dev/null
+++ b/dev-util/lockrun/files/lockrun.c
@@ -0,0 +1,275 @@
+/*
+ * $Id: lockrun.c,v 1.1 2008/02/05 18:22:38 caleb Exp $
+ *
+ * written by : Stephen J. Friedl
+ * Software Consultant
+ * steve@unixwiz.net
+ * http://www.unixwiz.net/tools/
+ *
+ * ===================================================================
+ * ======== This software is in the public domain, and can be ========
+ * ======== used by anybody for any purpose ========
+ * ===================================================================
+ *
+ * Lockrun: This program is used to launch a program out with a lockout
+ * so that only one can run at a time. It's mainly intended for use out
+ * of cron so that our five-minute running jobs which run long don't get
+ * walked on. We find this a *lot* with Cacti jobs which just get hung
+ * up: it's better to miss a polling period than to stack them up and
+ * slow each other down.
+ *
+ * So we use a file which is used for locking: this program attempts to
+ * lock the file, and if the lock exists, we have to either exit with
+ * an error, or wait for it to release.
+ *
+ * lockrun --lockfile=FILE -- my command here
+ *
+ * COMMAND LINE
+ * ------------
+ *
+ * --lockfile=F
+ *
+ * Specify the name of a file which is used for locking. The file is
+ * created if necessary (with mode 0666), and no I/O of any kind is
+ * done. The file is never removed.
+ *
+ * --maxtime=N
+ *
+ * The script being controlled should run for no more than <N> seconds,
+ * and if it's beyond that time, we should report it to the standard
+ * error (which probably gets routed to the user via cron's email).
+ *
+ * --wait
+ *
+ * When a lock is hit, we normally exit with error, but --wait causes
+ * it to loop until the lock is released.
+ *
+ * --verbose
+ *
+ * Show a bit more runtime debugging.
+ *
+ * --
+ *
+ * Mark the end of the options: the command to run follows.
+ *
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/file.h>
+
+#ifndef __GNUC__
+# define __attribute__(x) /* nothing */
+#endif
+
+
+#define STRMATCH(a,b) (strcmp((a),(b)) == 0)
+
+#define UNUSED_PARAMETER(v) ((void)(v))
+
+#define TRUE 1
+#define FALSE 0
+
+static const char *lockfile = 0;
+static int wait_for_lock = FALSE;
+static mode_t openmode = 0666;
+static int sleeptime = 10; /* seconds */
+static int Verbose = FALSE;
+static int Maxtime = 0;
+
+static char *getarg(char *opt, char ***pargv);
+
+static void die(const char *format, ...)
+ __attribute__((noreturn))
+ __attribute__((format(printf, 1, 2)));
+
+int main(int argc, char **argv)
+{
+ char *Argv0 = *argv;
+ int rc;
+ int lfd;
+ pid_t childpid;
+ time_t starttime;
+
+ UNUSED_PARAMETER(argc);
+
+ time(&starttime);
+
+ for ( argv++ ; *argv ; argv++ )
+ {
+ char *arg = *argv;
+ char *opt = strchr(arg, '=');
+
+ /* the -- token marks the end of the list */
+
+ if ( strcmp(*argv, "--") == 0 )
+ {
+ argv++;
+ break;
+ }
+
+ if (opt) *opt++ = '\0'; /* pick off the =VALUE part */
+
+ if ( STRMATCH(arg, "-L") || STRMATCH(arg, "--lockfile"))
+ {
+ lockfile = getarg(opt, &argv);
+ }
+
+ else if ( STRMATCH(arg, "-W") || STRMATCH(arg, "--wait"))
+ {
+ wait_for_lock = TRUE;
+ }
+
+ else if ( STRMATCH(arg, "-S") || STRMATCH(arg, "--sleep"))
+ {
+ sleeptime = atoi(getarg(opt, &argv));
+ }
+
+ else if ( STRMATCH(arg, "-T") || STRMATCH(arg, "--maxtime"))
+ {
+ Maxtime = atoi(getarg(opt, &argv));
+ }
+
+ else if ( STRMATCH(arg, "-V") || STRMATCH(arg, "--verbose"))
+ {
+ Verbose++;
+ }
+
+ else
+ {
+ die("ERROR: \"%s\" is an invalid cmdline param", arg);
+ }
+ }
+
+ /*----------------------------------------------------------------
+ * SANITY CHECKING
+ *
+ * Make sure that we have all the parameters we require
+ */
+ if (*argv == 0)
+ die("ERROR: missing command to %s (must follow \"--\" marker) ", Argv0);
+
+ if (lockfile == 0)
+ die("ERROR: missing --lockfile=F parameter");
+
+ /*----------------------------------------------------------------
+ * Open or create the lockfile, then try to acquire the lock. If
+ * the lock is acquired immediately (==0), then we're done, but
+ * if the lock is not available, we have to wait for it.
+ *
+ * We can either loop trying for the lock (for --wait), or exit
+ * with error.
+ */
+
+ if ( (lfd = open(lockfile, O_RDWR|O_CREAT, openmode)) < 0)
+ die("ERROR: cannot open(%s) [err=%s]", lockfile, strerror(errno));
+
+ while ( flock(lfd, LOCK_EX | LOCK_NB) != 0 )
+ {
+ if ( ! wait_for_lock )
+ {
+ die("ERROR: cannot launch %s - run is locked", argv[0]);
+ }
+
+ /* waiting */
+ if ( Verbose ) printf("(locked: sleeping %d secs)\n", sleeptime);
+
+ sleep(sleeptime);
+ }
+
+ fflush(stdout);
+
+ /* run the child */
+
+
+ if ( (childpid = fork()) == 0 )
+ {
+ close(lfd); // don't need the lock file
+
+ execvp(argv[0], argv);
+ }
+ else if ( childpid > 0 )
+ {
+ time_t endtime;
+ pid_t pid;
+
+ if ( Verbose )
+ printf("Waiting for process %ld\n", (long) childpid);
+
+ pid = waitpid(childpid, &rc, 0);
+
+ time(&endtime);
+
+ endtime -= starttime;
+
+ if ( Verbose || (Maxtime > 0 && endtime > Maxtime) )
+ printf("pid %d exited with status %d (time=%ld sec)\n", pid, rc, endtime);
+ }
+ else
+ {
+ die("ERROR: cannot fork [%s]", strerror(errno));
+ }
+
+ exit(rc);
+}
+
+
+/*! \fn static char *getarg(char *opt, char ***pargv)
+ * \brief A function to parse calling parameters
+ *
+ * This is a helper for the main arg-processing loop: we work with
+ * options which are either of the form "-X=FOO" or "-X FOO"; we
+ * want an easy way to handle either one.
+ *
+ * The idea is that if the parameter has an = sign, we use the rest
+ * of that same argv[X] string, otherwise we have to get the *next*
+ * argv[X] string. But it's an error if an option-requiring param
+ * is at the end of the list with no argument to follow.
+ *
+ * The option name could be of the form "-C" or "--conf", but we
+ * grab it from the existing argv[] so we can report it well.
+ *
+ * \return character pointer to the argument
+ *
+ */
+static char *getarg(char *opt, char ***pargv)
+{
+ const char *const optname = **pargv;
+
+ /* option already set? */
+ if (opt) return opt;
+
+ /* advance to next argv[] and try that one */
+ if ((opt = *++(*pargv)) == 0)
+ die("ERROR: option %s requires a parameter", optname);
+
+ return opt;
+}
+
+/*
+ * die()
+ *
+ * Given a printf-style argument list, format it to the standard error,
+ * append a newline, then exit with error status.
+ */
+
+static void die(const char *format, ...)
+{
+va_list args;
+
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ putc('\n', stderr);
+ va_end(args);
+
+ exit(EXIT_FAILURE);
+}
+
diff --git a/dev-util/lockrun/lockrun-2.ebuild b/dev-util/lockrun/lockrun-2.ebuild
new file mode 100644
index 000000000000..433bdd218e85
--- /dev/null
+++ b/dev-util/lockrun/lockrun-2.ebuild
@@ -0,0 +1,30 @@
+# Copyright 1999-2008 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Header: /var/cvsroot/gentoo-x86/dev-util/lockrun/lockrun-2.ebuild,v 1.1 2008/02/05 18:22:37 caleb Exp $
+
+inherit toolchain-funcs
+
+DESCRIPTION="Lockrun - runs cronjobs with overrun protection"
+HOMEPAGE="http://www.unixwiz.net/tools/lockrun.html"
+SLOT="0"
+LICENSE="public-domain"
+KEYWORDS="~x86 ~amd64"
+IUSE=""
+RESTRICT="test"
+
+DEPEND="virtual/libc"
+
+S=${WORKDIR}
+
+src_unpack() {
+ cp "${FILESDIR}"/lockrun.c "${S}"/lockrun.c
+}
+
+src_compile() {
+ $(tc-getCC) ${CFLAGS} lockrun.c -o lockrun
+}
+
+src_install () {
+ dodir /usr/bin
+ cp lockrun "${D}"/usr/bin
+}
diff --git a/dev-util/lockrun/metadata.xml b/dev-util/lockrun/metadata.xml
new file mode 100644
index 000000000000..15a820c5f3a9
--- /dev/null
+++ b/dev-util/lockrun/metadata.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">
+<pkgmetadata>
+<herd>no-herd</herd>
+<maintainer>
+ <email>caleb@gentoo.org</email>
+ <name>Caleb Tennis</name>
+</maintainer>
+</pkgmetadata>