I ran into a problem this morning where the load average on one
of our systems was extremely high, and found that a major factor
was multiple daily runs of findutils rebuilding locate database.

This type of problem can occur when cron jobs start without
testing to see if the previous run is still active.

When I first tackled this issue many years ago, my solution was
to use the ``shlock'' program from the usenet news software, and
incorporate that in our maintenance scripts.

I would like to suggest implementing this in OpenPKG for the
quarterly, hourly, daily, weekly, and monthly cron jobs with lock
files like %{l_prefix}/var/package/quarterly.lock.  The code to
do this is simple (some might say Crude but Effective).

I've attached the source that we use for shlock, and I think it
would be easy to put this under %{l_prefix}/lib/openpkg or
%{l_prefix}/libexec/openpkg directories.  I've also included the
man page for this from ``inn''.

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

Bagdikian's Observation:
        Trying to be a first-rate reporter on the average American
        newspaper is like trying to play Bach's "St. Matthew Passion"
        on a ukelele.
/*  $Revision: 3.3 $
**
**  Produce reliable locks for shell scripts, by Peter Honeyman as told
**  to Rich $alz.
*/
#include <strings.h>
/* #include "configdata.h" */
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <getopt.h>

#define PID_T int
#define POINTER void*
#define SIZE_T size_t
#define NORETURN void

private bool    BinaryLock;
private char    CANTUNLINK[] = "Can't unlink \"%s\", %s\n";
private char    CANTOPEN[] = "Can't open \"%s\", %s\n";


/*
**  See if the process named in an existing lock still exists by
**  sending it a null signal.
*/
private bool
ValidLock(name, JustChecking)
    char                *name;
    bool                JustChecking;
{
    register int        fd;
    register int        i;
    PID_T               pid;
    char                buff[BUFSIZ];

    /* Open the file. */
    if ((fd = open(name, O_RDONLY)) < 0) {
        if (JustChecking)
            return FALSE;
        (void)fprintf(stderr, CANTOPEN, name, strerror(errno));
        return TRUE;
    }

    /* Read the PID that is written there. */
    if (BinaryLock) {
        if (read(fd, (char *)&pid, sizeof pid) != sizeof pid) {
            (void)close(fd);
            return FALSE;
        }
    }
    else {
        if ((i = read(fd, buff, sizeof buff - 1)) <= 0) {
            (void)close(fd);
            return FALSE;
        }
        buff[i] = '\0';
        pid = (PID_T) atol(buff);
    }
    (void)close(fd);
    if (pid <= 0)
        return FALSE;

    /* Send the signal. */
    if (kill(pid, 0) < 0 && errno == ESRCH)
        return FALSE;

    /* Either the kill worked, or we're optimistic about the error code. */
    return TRUE;
}


/*
**  Unlink a file, print a message on error, and exit.
*/
private NORETURN
UnlinkAndExit(name, x)
    char        *name;
    int         x;
{
    if (unlink(name) < 0)
        (void)fprintf(stderr, CANTUNLINK, name, strerror(errno));
    exit(x);
}


/*
**  Print a usage message and exit.
*/
private NORETURN
Usage()
{
    (void)fprintf(stderr, "Usage: shlock [-u|-b] -f file -p pid\n");
    exit(1);
    /* NOTREACHED */
}


int
main(ac, av)
    int                 ac;
    char                *av[];
{
    register int        i;
    register char       *p;
    register int        fd;
    char                tmp[BUFSIZ];
    char                buff[BUFSIZ];
    char                *name;
    PID_T               pid;
    bool                ok;
    bool                JustChecking;

    /* Set defaults. */
    pid = 0;
    name = NULL;
    JustChecking = FALSE;
    /* (void)umask(NEWSUMASK); */

    /* Parse JCL. */
    while ((i = getopt(ac, av, "bcup:f:")) != EOF)
        switch (i) {
        default:
            Usage();
            /* NOTREACHED */
        case 'b':
        case 'u':
            BinaryLock = TRUE;
            break;
        case 'c':
            JustChecking = TRUE;
            break;
        case 'p':
            pid = (PID_T) atol(optarg);
            break;
        case 'f':
            name = optarg;
            break;
        }
    ac -= optind;
    av += optind;
    if (ac || pid == 0 || name == NULL)
        Usage();

    /* Create the temp file in the same directory as the destination. */
    if ((p = strrchr(name, '/')) != NULL) {
        *p = '\0';
        (void)sprintf(tmp, "%s/shlock%ld", name, (long)getpid());
        *p = '/';
    }
    else
        (void)sprintf(tmp, "shlock%ld", (long)getpid());

    /* Loop until we can open the file. */
    while ((fd = open(tmp, O_RDWR | O_CREAT | O_EXCL, 0644)) < 0)
        switch (errno) {
        default:
            /* Unknown error -- give up. */
            (void)fprintf(stderr, CANTOPEN, tmp, strerror(errno));
            exit(1);
        case EEXIST:
            /* If we can remove the old temporary, retry the open. */
            if (unlink(tmp) < 0) {
                (void)fprintf(stderr, CANTUNLINK, tmp, strerror(errno));
                exit(1);
            }
            break;
        }

    /* Write the process ID. */
    if (BinaryLock)
        ok = write(fd, (POINTER)&pid, (SIZE_T)sizeof pid) == sizeof pid;
    else {
        (void)sprintf(buff, "%ld\n", (long) pid);
        i = strlen(buff);
        ok = write(fd, (POINTER)buff, (SIZE_T)i) == i;
    }
    if (!ok) {
        (void)fprintf(stderr, "Can't write PID to \"%s\", %s\n",
            tmp, strerror(errno));
        (void)close(fd);
        UnlinkAndExit(tmp, 1);
    }

    (void)close(fd);

    /* Handle the "-c" flag. */
    if (JustChecking) {
        if (ValidLock(name, TRUE))
            UnlinkAndExit(tmp, 1);
        UnlinkAndExit(tmp, 0);
    }

    /* Try to link the temporary to the lockfile. */
    while (link(tmp, name) < 0)
        switch (errno) {
        default:
            /* Unknown error -- give up. */
            (void)fprintf(stderr, "Can't link \"%s\" to \"%s\", %s\n",
                    tmp, name, strerror(errno));
            UnlinkAndExit(tmp, 1);
            /* NOTREACHED */
        case EEXIST:
            /* File exists; if lock is valid, give up. */
            if (ValidLock(name, FALSE))
                UnlinkAndExit(tmp, 1);
            if (unlink(name) < 0) {
                (void)fprintf(stderr, CANTUNLINK, name, strerror(errno));
                UnlinkAndExit(tmp, 1);
            }
        }

    UnlinkAndExit(tmp, 0);
    /* NOTREACHED */
}
.\" $Revision: 1.7 $
.TH SHLOCK 1
.SH NAME
shlock \- create lock files for use in shell scripts
.SH SYNOPSIS
.B shlock
.BI \-p " pid"
.BI \-f " name"
[
.B \-b
]
[
.B \-u
]
[
.B \-c
]
.SH DESCRIPTION
.I Shlock
tries to create a lock file named
.I name
and write the process ID
.I pid
into it.
If the file already exists,
.I shlock
will read the process ID from the file and test to see if the process
is currently running.
If the process exists, then the file will not be created.
.PP
.I Shlock
exits with a zero status if it was able to create the lock file, or
non-zero if the file refers to currently-active process.
.SH OPTIONS
.TP
.B \-b
Process IDs are normally read and written in ASCII.
If the ``\-b'' flag is used, then they will be written as a binary
.IR int .
For compatibility with other systems, the ``\-u'' flag is accepted as
a synonym for ``\-b'' since binary locks are used by many UUCP packages.
.TP
.B \-c
If the ``\-c'' flag is used, then
.I shlock
will not create a lock file, but will instead use the file to see if
the lock is held by another program.
If the lock is valid, the program will exit with a non-zero status; if
the lock is not valid (i.e., invoking
.I shlock
without the flag would have succeeded), then the program will exit
with a zero status.
.SH EXAMPLES
The following example shows how
.I shlock
would be used within a shell script:
.RS
.nf
LOCK=<pathrun in inn.conf>/LOCK.send
trap 'rm -f ${LOCK} ; exit 1' 1 2 3 15
if shlock -p $$ -f ${LOCK} ; then
    # Do appropriate work
else
    echo Locked by `cat ${LOCK}`
f\&i
.fi
.RE
.SH BUGS
.I shlock
assumes that it will not be used in an environment with multiple
locks/unlocks in a short time (due to a race condition).  That is,
.I shlock
is intended for daily or hourly jobs.
.SH HISTORY
Written by Rich $alz <[EMAIL PROTECTED]> after a description of HDB UUCP
locking given by Peter Honeyman.
.de R$
This is revision \\$3, dated \\$4.
..
.R$ $Id: shlock.1,v 1.7 2002/10/01 23:31:53 vinocur Exp $
.SH "SEE ALSO"
inn.conf(5)

Reply via email to