home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume22
/
nfsxopen
/
part01
next >
Wrap
Text File
|
1991-08-23
|
6KB
|
152 lines
Newsgroups: comp.sources.misc
From: Stephen R. van den Berg <berg@messua.informatik.rwth-aachen.de>
Subject: v22i054: nfsxopen - NFS secure exclusive open lib v1.1, Part01/01
Message-ID: <1991Aug23.181109.6339@sparky.IMD.Sterling.COM>
X-Md4-Signature: dd020acb99be7d1db10faac0719b6e9d
Date: Fri, 23 Aug 1991 18:11:09 GMT
Approved: kent@sparky.imd.sterling.com
Submitted-by: Stephen R. van den Berg <berg@messua.informatik.rwth-aachen.de>
Posting-number: Volume 22, Issue 54
Archive-name: nfsxopen/part01
Environment: UNIX
This is a very small library that enables you create files exclusively.
This normally is accomplished by open(...,O_CREAT|O_EXCL,...); but this
does not work reliably across NFS mounted filesystems. That is where
this library comes in, it works across any UNIX-alike filesystem and
does not depend on lockd, lockf or flock.
Typical applications are the creation of lockfiles. This code is donated
to the public domain.
--
Sincerely, berg@messua.informatik.rwth-aachen.de
Stephen R. van den Berg (AKA BuGless). berg@physik.tu-muenchen.de
---- Cut Here and feed the following to sh ----
#!/bin/sh
# This is NFSxopen, a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 08/23/1991 13:28 UTC by berg@messua.informatik.rwth-aachen.de
# Source directory /tmp_mnt/tmp/.am/baghira/berg/tmp
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 3436 -rw-r--r-- NFSxopen.c
#
# ============= NFSxopen.c ==============
if test -f 'NFSxopen.c' -a X"$1" != X"-c"; then
echo 'x - skipping NFSxopen.c (File already exists)'
else
echo 'x - extracting NFSxopen.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'NFSxopen.c' &&
X/************************************************************************
X * NFS secure exclusive open/lock v1.1 *
X * *
X * Created 1990-1991, S.R.van den Berg, The Netherlands *
X * berg@messua.informatik.rwth-aachen.de *
X * berg@physik.tu-muenchen.de *
X * *
X * This file is donated to the public domain. *
X * *
X * Usage: NFSxopen(char*filename,mode_t mode) *
X * *
X * returns 0:success -1:failure *
X * *
X * sets errno on failure *
X * *
X * Sorry for the formatting style, feed it through your *
X * favourite C-beautifier if you can't resist :-) *
X * *
X ************************************************************************/
X#define HOSTNAMElen 8 /* significant characters for hostname */
X/*#define NOuname /* uncomment if uname is not available */
X#define const /* can be undefined for ANSI compilers */
Xstatic const char dirsep[]="/"; /* directory separators */
X
X#include <unistd.h> /* open() close() link() unlink() */
X#include <fcntl.h> /* O_WRONLY O_CREAT O_EXCL */
X#include <stdlib.h> /* malloc() free() */
X#include <string.h> /* strncpy() strcat() strpbrk() */
X#include <sys/stat.h> /* stat() struct stat */
X#ifndef NOuname
X#include <sys/utsname.h> /* uname() struct utsname */
X#endif
X#include <errno.h>
X
X/************************************************************************
X * Only edit below this line if you *think* you know what you are doing *
X ************************************************************************/
X
X#define log(string) /* should log string to stderr */
X#define SERIALchars 3
X#define UNIQnamelen (1+SERIALchars+HOSTNAMElen+1)
X#define SERIALmask ((1L<<6*SERIALchars)-1)
X
Xextern errno;
X
Xstatic const char*hostname(){static char name[HOSTNAMElen+1];
X#ifdef NOuname
X gethostname(name,HOSTNAMElen+1);
X#else
X struct utsname names;
X uname(&names);strncpy(name,names.nodename,HOSTNAMElen);
X#endif
X name[HOSTNAMElen]='\0';return name;}
X
Xstatic ultoan(val,dest)unsigned long val;char*dest;{register i;/* convert to */
X do{ /* a number within the set [0-9A-Za-z-_] */
X i=val&0x3f;
X *dest++=i+(i<10?'0':i<10+26?'A'-10:i<10+26+26?'a'-10-26:
X i==10+26+26?'-'-10-26-26:'_'-10-26-27);}
X while(val>>=6);
X *dest='\0';}
X
Xstatic unique(full,p,mode)const char*const full;char*const p;
X const mode_t mode;{
X unsigned long retry=3;int i; /* create unique file name */
X do{
X ultoan(SERIALmask&(retry<<16)+(unsigned long)getpid(),p+1);
X *p='_';strcat(p,hostname());}
X while(0>(i=open(full,O_WRONLY|O_CREAT|O_EXCL|O_SYNC,mode))&&errno==EEXIST
X &&retry--); /* casually check if it already exists (highly unlikely) */
X if(i<0){
X log("Error while writing to \"");log(full);log("\"\n");return 0;}
X close(i);return 1;}
X /* rename MUST fail if already existent */
Xstatic myrename(old,new)const char*const old,*const new;{int i,serrno;
X struct stat stbuf;
X link(old,new);serrno=errno;i=stat(old,&stbuf);unlink(old);errno=serrno;
X return stbuf.st_nlink==2?i:-1;}
X /* an NFS secure exclusive file open */
XNFSxopen(name,mode)char*name;const mode_t mode;{char*p,*q;int j= -1,i;
X for(q=name;p=strpbrk(q,dirsep);q=p+1); /* find last DIRSEP */
X if(!(p=malloc(i+UNIQnamelen))) /* out of memory */
X return j;
X i=q-name;strncpy(p,name,i);
X if(unique(p,p+i,mode))
X j=myrename(p,name); /* try and rename it, fails if nonexclusive */
X free(p);return j;}
SHAR_EOF
chmod 0644 NFSxopen.c ||
echo 'restore of NFSxopen.c failed'
Wc_c="`wc -c < 'NFSxopen.c'`"
test 3436 -eq "$Wc_c" ||
echo 'NFSxopen.c: original size 3436, current size' "$Wc_c"
fi
exit 0
--
Sincerely, berg@messua.informatik.rwth-aachen.de
Stephen R. van den Berg (AKA BuGless). berg@physik.tu-muenchen.de
"My name is Psmith, the P is not pronounced."
exit 0 # Just in case...