[comp.unix.xenix] a uucp unlock program

davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) (02/09/88)

Some systems to which I connect have a habit of doing a wierd disconnect
which leaves the lock (LCK..) files in /usr/spool/uucp, and prevents
using the lines, etc. This tiny program reads the lock files, gets the
processes id of the program asserting the lock, and if the process is
gone clears the lock.

Call via cron, "unlock /usr/spool/uucp/LCK*" as often as you feel is
needed. I used to do it every ten minutes, but now every hour, due to
better behavior on the part of calling systems.

		"DisClamer = TRUE;"

:
#!/bin/sh
# shar+ created from directory /usr2/davidsen/tmp
# 15:57 on Mon Feb 08, 1988 by davidsen
echo 'x - unlock.c (text)'
sed << 'E!O!F' 's/^X//' > unlock.c
X/*****************************************************************
X |  unlock.c - remove lock files no longer in use
X |----------------------------------------------------------------
X |  This routine is given a list of lock file (such as uucp and
X |  kermit use), and check each to be sure it is still in use.
X |  It reads the process id from each file, and if the process
X |  no longer exists it deletes the file.
X ****************************************************************/
X
X#include <stdio.h>
X#include <errno.h>
X
X#define DEBUG(f,v) if (debug) printf(f, v)
X
Xextern int  errno;
X
Xint     debug = 0;
X
Xmain (argc, argv)
Xint     argc;
Xchar   *argv[];
X{
X    FILE   *fp,
X           *fopen ();
X    short   pid,		/* process id */
X	    kstat,		/* kill status */
X            argnum;
X
X /* see if debug option */
X    if (strcmp (argv[1], "-d") == 0)
X    { /* set debug */
X	debug = 1;
X	argv++;
X	argc--;
X    }
X
X    for (argnum = 1; argnum < argc; argnum++)
X    { /* try to open the file */
X	DEBUG ("Opening file %s...", argv[argnum]);
X	fp = fopen (argv[argnum], "rb");
X    /* don't generate error messages, ignore all problems */
X	if (fp == NULL)
X	{ /* can't open, may be gone due to timeing 
X				*/
X	    DEBUG ("can't open\n", 0);
X	    continue;
X	}
X
X    /* read a value and always close the file */
X	fread (&pid, sizeof pid, 1, fp);
X	DEBUG("pid %d...", pid);
X	fclose (fp);
X
X    /* if the process exists, ignore this file */
X	if ((kstat = kill (pid, 0)) == 0 || errno != ESRCH)
X	{ /* still active */
X	    DEBUG ("active to process %d\n", pid);
X	    continue;
X	}
X	DEBUG("kstat %d ", kstat);
X	DEBUG("errno %d ", errno);
X	unlink (argv[argnum]);
X	DEBUG ("unlinked\n", 0);
X    }
X}
E!O!F
newsize=`wc -c < unlock.c`
if [ $newsize -ne 1670 ]
then echo "File unlock.c was $newsize bytes, 1670 expected"
fi
exit 0
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

chip@killer.UUCP (Chip Rosenthal) (02/12/88)

In article <9465@steinmetz.steinmetz.UUCP> davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) writes:
>
>Some systems to which I connect have a habit of doing a wierd disconnect
>which leaves the lock (LCK..) files in /usr/spool/uucp...
>
>X	if ((kstat = kill (pid, 0)) == 0 || errno != ESRCH)
>X	    continue;
>X	unlink (argv[argnum]);

As long as you are fixing this problem, there is another one you could
fix at the same time.  It LCK.XQT is old, uuxqt will assume it is stale
and remove it.  If you have a uuxqt which has been merrily unbatching
news for an hour or two, you will now have two uuxqt's running.  I
implemented a similar thing with a shell script (using od to read the
pid from the lockfile).  If the kill works, the lockfile is touched to
avoid that problem.  But, in any case, a lock cleaner upper has proved
invaluable.
-- 
Chip Rosenthal         chip@vector.UUCP		| But if you want to sing the
Dallas Semiconductor     (214) 450-0400		|  blues, then boy you better
{texsun,codas,ihnp4}!killer!vector!chip		|  learn how to lose.

chip@ateng.UUCP (Chip Salzenberg) (02/27/88)

In article <3289@killer.UUCP> chip@killer.UUCP (Chip Rosenthal) writes:
>
>As long as you are fixing this problem, there is another one you could
>fix at the same time.  It LCK.XQT is old, uuxqt will assume it is stale
>and remove it.

Well, you can define SPOOLNEWS (like I did) so that rnews doesn't run for
so long.

But failing that, just put this command in root's crontab:

    # Make sure that LCK.XQT is touched hourly.
    0  *   *   *   *   touch -c /usr/spool/uucp/LCK.XQT

Oh, and don't forget to make sure that a line like

	rm -f /usr/spool/uucp/LCK.*

is in your /etc/rc file, just in case of a system crash.

-- 
Chip Salzenberg                 UUCP: "{codas,uunet}!ateng!chip"
A T Engineering                 My employer's opinions are a trade secret.
       "Anything that works is better than anything that doesn't."