Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!samsung!zaphod.mps.ohio-state.edu!math.lsa.umich.edu!emv From: steveb@shade.UUCP (Steve Barber) Newsgroups: alt.sources Subject: Re: lock file program Message-ID: <10300@stag.math.lsa.umich.edu> Date: 10 Dec 89 04:42:17 GMT Sender: news@math.lsa.umich.edu Reply-To: steveb@shade.Ann-Arbor.MI.US (Steve Barber) Followup-To: mi.misc Lines: 441 Original-posting-by: steveb@shade.UUCP (Steve Barber) Reposted-by: emv@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@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@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@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@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 XX#include XX#include 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