lock file program

Steve Barber steveb at shade.UUCP
Sun Dec 10 15:42:17 AEST 1989


Original-posting-by: steveb at shade.UUCP (Steve Barber)
Reposted-by: emv at math.lsa.umich.edu (Edward Vielmetti)
Posting-id: 891210.0443
Posting-number: Volume TEST, Number TEST
Archive-name: HDB-lock-KA9Q

[This is an experimental alt.sources re-posting from the
newsgroup(s) mi.misc.
No attempt has been made to edit, clean, modify, or otherwise
change the contents of the original posting, or to contact the
author.  Please consider cross-posting all sources postings to
alt.sources as a matter of course.]

[Comments on this service to emv at math.lsa.umich.edu (Edward Vielmetti)]


Enclosed in this SHAR file are a variety of things:

	lock.c		Source for the HDB-format lockfile utilities.
			Don't hold your breath, this program is just a
			quick hack that I just tacked some documentation
			onto.

	ka9q		A shell script showing how to use lock with KA9Q.

	startup.net	The startup.net file I use with KA9Q to connect
			to Merit with the SLFP protocol, since I've gotten
			a few questions about it.

Have fun with them!

Steve Barber
steveb at shade.Ann-Arbor.MI.US
------------------------------------------------------------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	README
#	lock.c
#	ka9q
#	startup.net
# This archive created: Sat Dec  9 17:24:40 1989
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'README'" '(373 characters)'
if test -f 'README'
then
	echo shar: "will not over-write existing file 'README'"
else
sed 's\/^XX\/\/' << \SHAR_EOF > 'README'
XXEnclosed in this SHAR file are a variety of things:
XX
XX	lock.c		Source for the HDB-format lockfile utilities
XX
XX	ka9q		A shell script showing how to use lock with KA9Q
XX
XX	startup.net	The startup.net file I use with KA9Q to connect
XX			to Merit with the SLFP protocol, since I've gotten
XX			a few questions about it.
XX
XXHave fun with them!
XX
XXSteve Barber
XXsteveb at shade.Ann-Arbor.MI.US
SHAR_EOF
if test 373 -ne "`wc -c < 'README'`"
then
	echo shar: "error transmitting 'README'" '(should have been 373 characters)'
fi
fi
echo shar: "extracting 'lock.c'" '(4221 characters)'
if test -f 'lock.c'
then
	echo shar: "will not over-write existing file 'lock.c'"
else
sed 's\/^XX\/\/' << \SHAR_EOF > 'lock.c'
XX/*
XX * lock.c - establish an exclusive lock file in a directory storing a
XX * binary id number in the lockfile for authentication purposes.  (The
XX * format of the contents of the lockfile just happens to correspond to
XX * the format used by the implementation of HDB UUCP on the AT&T 3B1.)
XX *
XX * by Stephen W. Barber  (steveb at shade.Ann-Arbor.MI.US)
XX *
XX * This utility is hereby placed in the public domain for all to use
XX * and modify as they see fit.  There are no warranties, and I can not
XX * be held liable for any damages caused by this program.  If you do
XX * have a problem though feel free to write me and I'll help you if I can.
XX *
XX * I prefer that if you have to make changes, that you document them in
XX * this header comment, and that you implement them using an appropriate
XX * #ifdef statement so that the base version can still be compiled from
XX * your source.  You might also send me any patches you make, if you feel
XX * they are important.
XX *
XX * SIMPLIFIED PSEUDOCODE FOR HOW IT WORKS:
XX * =======================================
XX * - set done flag to false
XX * - set count
XX * - loop while not done and count > 0
XX *    - open(".../LOCK", O_CREAT, 0644)
XX *    - if it fails,
XX *       + sleep for a while
XX *	 + count--
XX *    - else
XX *	 + close the file
XX *	 + done=TRUE
XX *    - endif
XX * - endloop
XX *
XX * INSTALLATION:
XX * =============
XX * Compile lock.c.  It requires no special options, so
XX *	cc -O lock.c -o lock
XX * should work just fine.  Next, install the lock executable in an
XX * appropriate directory (mine's in /usr/local):
XX *	cp lock /usr/local
XX * Finally, make a link to lock named "unlock":
XX *	cd /usr/local
XX *	ln lock unlock
XX *
XX * USAGE:
XX * ======
XX * To establish a lock, issue the command:
XX *	lock directory lockname id retries wait
XX * where directory is the name of the directory to create the lockfile in,
XX * lockname is the name of the lockfile to create, id is a numerical id
XX * (intended to be a process id, but could be anything), retries is the
XX * number of times to try again if a lock already exists, and wait is the
XX * number of seconds to wait between attempts.  The result code ($? in
XX * sh and ksh, $status in csh) will be 0 if a lock is create successfully,
XX * or any of several various non-zero numbers depending on the nature of
XX * the failure.
XX *
XX * To release the lock:
XX *	unlock directory lockname id
XX * The lock will not be removed if the id does not match the one used when
XX * the lockfile was created, and an error code will be returned.
XX *
XX */
XX
XX#include <stdio.h>
XX#include <fcntl.h>
XX#include <errno.h>
XX
XXvoid my_lock(), my_unlock();
XX
XXmain(argc, argv)
XXint argc;
XXchar *argv[];
XX{
XX	if (!strcmp("lock", argv[0]))
XX	{
XX		if (argc != 6)
XX		{
XX			fprintf(stderr, "lock: invalid arguments\n");
XX			fprintf(stderr,
XX				"Usage: lock dir name id retries wait\n");
XX			exit(2);
XX		}
XX		my_lock(argv[1], argv[2], argv[3], argv[4], argv[5]);
XX		exit(0);
XX	}
XX	else if (!strcmp("unlock", argv[0]))
XX	{
XX		if (argc != 4)
XX		{
XX			fprintf(stderr, "unlock: invalid arguments\n");
XX			fprintf(stderr,
XX				"Usage: unlock dir name id\n");
XX			exit(2);
XX		}
XX		my_unlock(argv[1], argv[2], argv[3]);
XX		exit(0);
XX	}
XX	else
XX	{
XX		fprintf(stderr,
XX			"Eh? this program should be named lock or unlock!\n");
XX		exit(2);
XX	}
XX}
XX
XX
XXvoid my_lock(dir, name, id, ret, wait)
XXchar *dir, *name, *id, *ret, *wait;
XX{
XX	short retries, idle;
XX	int fd, pid;
XX
XX	retries = (short) atoi(ret);
XX	idle    = (short) atoi(wait);
XX	pid	= atoi(id);
XX	if (retries < 1 || retries > 50)
XX		retries = 5;	/* 5 retries by default */
XX	if (idle < 1 || idle > 900)
XX		idle = 60;	/* 5 minutes by default */
XX	if (chdir(dir) == -1)
XX	{
XX		fprintf(stderr, "Bad directory %s\n", dir);
XX		exit(2);
XX	}
XX	while (retries > 0)
XX	{
XX		if ((fd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0644)) == -1)
XX		{
XX			if (errno != EEXIST)
XX			{
XX				fprintf(stderr, "Bad file %s\n", name);
XX				exit(2);
XX			}
XX			sleep(idle);
XX			retries--;
XX		}
XX		else
XX		{
XX			write(fd, &pid, sizeof(pid));
XX			close(fd);
XX			exit(0);
XX		}
XX	}
XX	exit(1);
XX}
XX
XXvoid my_unlock(dir, name, id)
XXchar *dir, *name, *id;
XX{
XX	int fd, pid;
XX
XX	if (chdir(dir) == -1)
XX		exit(2);
XX	if ((fd = open(name, O_RDONLY)) == -1)
XX		exit(3);
XX	read(fd, &pid, sizeof(pid));
XX	close(fd);
XX	if (pid != atoi(id))
XX		exit(1);
XX	if (unlink(name) == -1)
XX		exit(4);
XX	exit(0);
XX}
SHAR_EOF
if test 4221 -ne "`wc -c < 'lock.c'`"
then
	echo shar: "error transmitting 'lock.c'" '(should have been 4221 characters)'
fi
fi
echo shar: "extracting 'ka9q'" '(1076 characters)'
if test -f 'ka9q'
then
	echo shar: "will not over-write existing file 'ka9q'"
else
sed 's\/^XX\/\/' << \SHAR_EOF > 'ka9q'
XX#!/bin/sh
XX#
XX# Example application script for the lock/unlock utilities using
XX# the KA9Q (net) package, which should create a lock file for the
XX# device but doesn't.
XX
XX# All KA9Q-related files (including the executable) are kept in $NETHOME
XXNETHOME=/usr/local/lib/KA9Q ; export NETHOME
XXNETSPOOL=/usr/local/lib/KA9Q ; export NETSPOOL
XXPATH=/usr/local/lib/KA9Q:/usr/local:/bin:/usr/bin ; export PATH
XX
XX# Attempt to create a lockfile for tty002 (my modem) using this shell script's
XX# PID, with 2 attempts and a 5 second pause between attempts.
XXlock /usr/spool/uucp LCK..tty002 $$ 2 5
XX
XX# If the result code $? does not equal 0, the lock file could not be created.
XXif [ $? -ne 0 ]
XXthen
XX	echo "Device tty002 is busy now; please try again later."
XX	exit 1
XXfi
XX
XX# Start up KA9Q and wait around for it to exit.
XXnet
XX
XX# Now remove the lockfile.
XXunlock /usr/spool/uucp LCK..tty002 $$
XX
XX# if we got an error removing the lockfile then something nuked our lockfile
XX# (and possibly installed its own...)
XXif [ $? -ne 0 ]
XXthen
XX	echo "Uh oh!!  Can't remove my lockfile!  I'm sick!!"
XX	exit 2
XXfi
XXexit 0
SHAR_EOF
if test 1076 -ne "`wc -c < 'ka9q'`"
then
	echo shar: "error transmitting 'ka9q'" '(should have been 1076 characters)'
fi
chmod +x 'ka9q'
fi
echo shar: "extracting 'startup.net'" '(4716 characters)'
if test -f 'startup.net'
then
	echo shar: "will not over-write existing file 'startup.net'"
else
sed 's\/^XX\/\/' << \SHAR_EOF > 'startup.net'
XX#
XX#  The configuration file for the NET.EXE program...
XX#
XX#  Note: NET.EXE ignores all lines beginning with a pound sign (#).
XX#
XX#  **********************************************************
XX#  **** Read USEGUIDE.DOC to help understand this file! *****
XX#  **********************************************************
XX#
XX#    There are many commands which must be provided to NET.EXE 
XX#    each time it is started to configure the program.  To keep
XX#    from having to type them by hand each time, we put them in
XX#    this file, which is read each time NET starts up.  Commands
XX#    in this configuration file are entered exactly as they would
XX#    be typed at the keyboard in the program.
XX#
XX#
XX#-----------------------------------------------
XX# 
XX# This entry tells NET.EXE the name of your machine.
XX#
XXhostname shade.ann-arbor.mi.us
XX#
XX#-----------------------------------------------
XX#
XX# The attach command tells NET.EXE about the interfaces in your computer
XX# that you will be using for TCP/IP.  These can include normal IBM-type
XX# serial "comm ports", Ethernet controllers, or dedicated packet cards.
XX# 
XX# The syntax is:
XX# attach <hw type> <I/O address> <vector> <mode> <label> <bufsize> 
XX#        <mtu> [<speed>]
XX#
XX# NOTE: /dev/tty002nb is a nonblocking device otherwise equivalent to 
XX#	/dev/tty002.  On a 3B1, this can be created by using
XX#	"ls -li /dev/tty002" and noting the major and minor device numbers,
XX#	then using the mknod command to create /dev/tty002nb with the same
XX#	major device number, and a minor device number of 128+the old minor
XX#	device number.  This is needed since my modem drops CD when no
XX#	carrier is present.
XX#
XX# NOTE: (906) 487-1517 is the number for Merit in Houghton.  SLFP has been
XX#	proven to work fine with this number.  300, 1200, and 2400 baud
XX#	connections are supported here, although I've only used SLFP at
XX#	2400 baud.  If you have problems with a Merit number in a low-
XX#	(computer-)population area, you might try it once with this number
XX#	or the Ann Arbor number, (313) 764-4800
XX#
XXattach asy 0 /dev/tty002nb slfp sf0 4096 400 2400 ATDT4871517
XX#
XX# See USEGUIDE.DOC for explanation of the attach command and its options.
XX#
XX# Merit Users Only:
XX#    If you think you have everything right, you can try calling the folks
XX#    at Merit.  Their number is: (313) 764-9430.  Be patient.  They receive
XX#    a large number of calls.  Note that they have little knowledge about
XX#    KA9Q itself.  The type of problem they're best equipped to handle is
XX#    determining whether your hard-wired Merit line is functioning properly.
XX#    If you don't have a hard-wired Merit line, Merit is just not going to
XX#    be able to help you.
XX#
XX#-------------------------------------------------
XX#
XX# Print out the IP address that we were assigned by the SLFP host:
XX#
XX! echo "Assigned IP address is \\c"
XXip addr
XX#
XX#-------------------------------------------------
XX#
XX# The following entry tells the program to route all packets out 
XX# the interface labeled sf0 in the attach command.
XX#
XXroute add default sf0
XX#
XX#-------------------------------------------------
XX#
XX# Time To Live is the maximum number of hops a packet can take 
XX# before it is thrown away. This command prevents an inadvertent 
XX# infinite loop from occuring with packets in the network.
XX#
XXip ttl 127
XX#
XX#-------------------------------------------------
XX#
XX# The Maximum Segment Size is the largest single transmission that 
XX# you will send. An mss of 216 corresponds to the mtu of 256 set
XX# up in the attach command above, overhead bytes considered.
XX#
XX# tcp mss 192
XXtcp mss 360 
XX#
XX#-------------------------------------------------
XX#
XX# The Window parameter establishes the maximum number of bytes 
XX# that may be outstanding before your system expects an ack.
XX# If window is twice as big as mss, for example, there will be two
XX# active packets on the channel at any given time... large values of
XX# window provide improved throughput on full-duplex links, but are a
XX# problem on the air.  Keep  mss <= window <= 2*mss if you're on the air.
XX# 
XX#
XX#tcp window 768
XXtcp window 2160 
XX#
XX#------------------------------------------------- 
XX#
XX# This entry will open the specified log file and will 
XX# record the server activity of your system.  If you don't want a log,
XX# comment out this line.
XX#
XXlog /tmp/net.log
XX#
XX#-------------------------------------------------
XX#
XX# Each of the servers (services you will provide) must be turned 
XX# on before they will be active. The following entries turn all 
XX# of them on. To turn any function off use the command 'stop' after
XX# NET gets fired up, or just comment out the line here.
XX#
XXstart smtp
XXstart ftp
XXstart echo
XXstart discard
XXstart telnet
XX#
XX#---------------------------------------------------
XX# THE END
SHAR_EOF
if test 4716 -ne "`wc -c < 'startup.net'`"
then
	echo shar: "error transmitting 'startup.net'" '(should have been 4716 characters)'
fi
fi
exit 0
#	End of shell archive
-- 
--**-Steve Barber----steveb at shade.Ann-Arbor.MI.US----(cmode)-------------------



More information about the Alt.sources mailing list