home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume18
/
mush6.4
/
part02
/
lock.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-03-12
|
4KB
|
171 lines
/*
* lock.c -- deal with file locking on various architectures and UNIXs.
* dot_lock() creates a file with the same name as the parameter passed
* with the appendage ".lock" -- this is to be compatible with certain
* systems that don't use flock or lockf or whatever they have available
* that they don't use.
*/
#ifdef USG
#include <unistd.h>
#endif /* USG */
#include "mush.h"
#if defined(SYSV) && !defined(USG)
#include <sys/locking.h>
#endif /* SYSV && !USG */
extern int sgid;
#ifdef BSD
extern int rgid;
#endif /* BSD */
#ifdef DOT_LOCK
dot_lock(filename)
char *filename;
{
char buf[MAXPATHLEN];
int lockfd, cnt = 0;
SIGRET (*oldint)(), (*oldquit)();
#ifdef BSD
setregid(rgid, sgid);
#else
setgid(sgid);
#endif /* BSD */
#ifdef M_XENIX
if (strcmp(spoolfile, mailfile) == 0)
(void) sprintf(buf, "/tmp/%.10s.mlk", login);
else
#endif /* M_XENIX */
(void) sprintf(buf, "%s.lock", filename);
on_intr();
while ((lockfd = open(buf, O_CREAT|O_WRONLY|O_EXCL, 0600)) == -1) {
if (errno != EEXIST) {
error("unable to lock %s", filename);
break;
}
if (cnt++ == 0)
print("%s already locked, waiting", filename);
else
print_more(".");
sleep(1);
if (ison(glob_flags, WAS_INTR)) {
print_more("\nAborted.\n");
break;
}
}
off_intr();
if (lockfd != -1) {
if (cnt)
print("done.\n");
(void) close(lockfd);
}
#ifdef BSD
setregid(sgid, rgid);
#else
setgid(getgid());
#endif /* BSD */
return lockfd == -1? -1 : 0;
}
#endif /* DOT_LOCK */
lock_file(filename, fp)
char *filename;
FILE *fp;
{
#ifdef MMDF
if (Access(filename, W_OK) || lk_lock(filename, NULL, NULL, 0))
return -1;
#else /* MMDF */
int fd = fileno(fp);
if (debug && do_set(set_options, "deadlock")) {
un_set(&set_options, "deadlock");
return -1;
}
#ifdef SYSV
#ifndef USG
(void) locking(fd, LK_LOCK, 0); /* old xenix (sys III) */
#else
/* if unable to lock, tell them */
if (Access(filename, W_OK) || lockf(fd, F_LOCK, 0L)) /* system-v */
return -1;
#endif /* USG */
#else
#ifdef BSD
{
int cnt = 0;
SIGRET (*oldint)(), (*oldquit)();
on_intr();
while (isoff(glob_flags, WAS_INTR) && flock(fd, LOCK_EX | LOCK_NB)) {
if (errno == EWOULDBLOCK)
if (!cnt)
print("waiting to lock \"%s\"", filename);
else
cnt++, print(".");
else {
error("Unable to lock %s", filename);
off_intr();
return -1;
}
sleep(1);
}
if (cnt)
print("\n");
off_intr();
if (ison(glob_flags, WAS_INTR))
return -1;
}
#else /* BSD */
if (ison(glob_flags, WARNING))
print("There is no supported file locking function!\n");
#endif /* BSD */
#endif /* SYSV */
#endif /* MMDF */
return 0;
}
void
close_lock(filename, fp)
char *filename;
FILE *fp;
#ifdef MMDF
{
(void) lk_unlock(filename, NULL, NULL);
fclose(fp);
}
#endif /* MMDF */
{
#ifdef DOT_LOCK
char buf[MAXPATHLEN];
#ifdef BSD
setregid(rgid, sgid);
#else
setgid(sgid);
#endif /* BSD */
#ifdef M_XENIX
if (strcmp(spoolfile, mailfile) == 0)
(void) unlink(sprintf(buf, "/tmp/%.10s.mlk", login));
else
#endif /* M_XENIX */
(void) unlink(sprintf(buf, "%s.lock", filename));
#ifdef BSD
setregid(sgid, rgid);
#else
setgid(getgid());
#endif /* BSD */
#endif /* DOT_LOCK */
#ifdef BSD
flock(fileno(fp), LOCK_UN);
#endif /* BSD */
#ifdef SYSV
#ifndef USG
locking(fileno(fp), LK_UNLCK, 0);
#else
lockf(fileno(fp), F_ULOCK, 0L);
#endif /* USG */
#endif /* SYSV */
fclose(fp);
}