2013-10-10 01:47:09 -04:00
|
|
|
#include "estruct.h"
|
2013-05-20 01:16:08 -04:00
|
|
|
#include "lock.h"
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* LOCK.C
|
|
|
|
*
|
|
|
|
* File locking command routines
|
|
|
|
*
|
|
|
|
* written by Daniel Lawrence
|
|
|
|
*/
|
|
|
|
|
2013-10-10 01:47:09 -04:00
|
|
|
#if BSD | SVR4
|
2005-05-31 11:50:56 -04:00
|
|
|
#include <stdio.h>
|
2013-10-10 01:47:09 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-05-28 05:21:40 -04:00
|
|
|
|
2013-10-12 21:54:05 -04:00
|
|
|
#include "defines.h"
|
2013-05-28 05:21:40 -04:00
|
|
|
#include "display.h"
|
|
|
|
#include "input.h"
|
2013-10-12 21:54:05 -04:00
|
|
|
#include "retcode.h"
|
2013-05-28 05:21:40 -04:00
|
|
|
|
|
|
|
#if (FILOCK && BSD) || SVR4
|
|
|
|
#include "pklock.h"
|
|
|
|
#endif
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#include <sys/errno.h>
|
|
|
|
|
2013-10-10 23:20:06 -04:00
|
|
|
#define NLOCKS 100 /* max # of file locks active */
|
|
|
|
static char *lname[ NLOCKS] ; /* names of all locked files */
|
|
|
|
static int numlocks ; /* # of current locks active */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-01 01:42:09 -04:00
|
|
|
static void lckerror(char *errstr) ;
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* lockchk:
|
|
|
|
* check a file for locking and add it to the list
|
|
|
|
*
|
2013-09-20 22:24:45 -04:00
|
|
|
* const char *fname; file to check for a lock
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2013-09-20 22:24:45 -04:00
|
|
|
int lockchk( const char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int i; /* loop indexes */
|
|
|
|
int status; /* return status */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* check to see if that file is already locked here */
|
|
|
|
if (numlocks > 0)
|
2005-09-30 18:26:09 -04:00
|
|
|
for (i = 0; i < numlocks; ++i)
|
2005-05-31 11:50:56 -04:00
|
|
|
if (strcmp(fname, lname[i]) == 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if we have a full locking table, bitch and leave */
|
2013-10-10 23:20:06 -04:00
|
|
|
if( numlocks >= NLOCKS) {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("LOCK ERROR: Lock table full");
|
2010-08-29 06:03:55 -04:00
|
|
|
return ABORT;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* next, try to lock it */
|
|
|
|
status = lock(fname);
|
|
|
|
if (status == ABORT) /* file is locked, no override */
|
2010-08-29 06:03:55 -04:00
|
|
|
return ABORT;
|
2005-05-31 11:50:56 -04:00
|
|
|
if (status == FALSE) /* locked, overriden, dont add to table */
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* we have now locked it, add it to our table */
|
2005-09-30 18:26:09 -04:00
|
|
|
lname[++numlocks - 1] = (char *) malloc(strlen(fname) + 1);
|
2005-05-31 11:50:56 -04:00
|
|
|
if (lname[numlocks - 1] == NULL) { /* malloc failure */
|
2005-09-30 18:26:09 -04:00
|
|
|
undolock(fname); /* free the lock */
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("Cannot lock, out of memory");
|
|
|
|
--numlocks;
|
2010-08-29 06:03:55 -04:00
|
|
|
return ABORT;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* everthing is cool, add it to the table */
|
2005-09-30 18:26:09 -04:00
|
|
|
strcpy(lname[numlocks - 1], fname);
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* lockrel:
|
|
|
|
* release all the file locks so others may edit
|
|
|
|
*/
|
|
|
|
int lockrel(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int i; /* loop index */
|
|
|
|
int status; /* status of locks */
|
|
|
|
int s; /* status of one unlock */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
status = TRUE;
|
|
|
|
if (numlocks > 0)
|
2005-09-30 18:26:09 -04:00
|
|
|
for (i = 0; i < numlocks; ++i) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if ((s = unlock(lname[i])) != TRUE)
|
|
|
|
status = s;
|
|
|
|
free(lname[i]);
|
|
|
|
}
|
|
|
|
numlocks = 0;
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* lock:
|
|
|
|
* Check and lock a file from access by others
|
|
|
|
* returns TRUE = files was not locked and now is
|
|
|
|
* FALSE = file was locked and overridden
|
|
|
|
* ABORT = file was locked, abort command
|
|
|
|
*
|
2013-09-20 22:24:45 -04:00
|
|
|
* const char *fname; file name to lock
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2013-09-20 22:24:45 -04:00
|
|
|
int lock( const char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
char *locker; /* lock error message */
|
|
|
|
int status; /* return status */
|
2005-05-31 11:50:56 -04:00
|
|
|
char msg[NSTRING]; /* message string */
|
|
|
|
|
|
|
|
/* attempt to lock the file */
|
|
|
|
locker = dolock(fname);
|
|
|
|
if (locker == NULL) /* we win */
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* file failed...abort */
|
|
|
|
if (strncmp(locker, "LOCK", 4) == 0) {
|
|
|
|
lckerror(locker);
|
2010-08-29 06:03:55 -04:00
|
|
|
return ABORT;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* someone else has it....override? */
|
|
|
|
strcpy(msg, "File in use by ");
|
|
|
|
strcat(msg, locker);
|
|
|
|
strcat(msg, ", override?");
|
2005-09-30 18:26:09 -04:00
|
|
|
status = mlyesno(msg); /* ask them */
|
2005-05-31 11:50:56 -04:00
|
|
|
if (status == TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
else
|
2010-08-29 06:03:55 -04:00
|
|
|
return ABORT;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* unlock:
|
|
|
|
* Unlock a file
|
|
|
|
* this only warns the user if it fails
|
|
|
|
*
|
2013-09-20 22:24:45 -04:00
|
|
|
* const char *fname; file to unlock
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2013-09-20 22:24:45 -04:00
|
|
|
int unlock( const char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
char *locker; /* undolock return string */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* unclock and return */
|
|
|
|
locker = undolock(fname);
|
|
|
|
if (locker == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* report the error and come back */
|
|
|
|
lckerror(locker);
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* report a lock error
|
|
|
|
*
|
|
|
|
* char *errstr; lock error string to print out
|
|
|
|
*/
|
|
|
|
void lckerror(char *errstr)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
char obuf[NSTRING]; /* output buffer for error message */
|
|
|
|
|
|
|
|
strcpy(obuf, errstr);
|
|
|
|
strcat(obuf, " - ");
|
2005-10-01 02:52:55 -04:00
|
|
|
strcat(obuf, strerror(errno));
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite(obuf);
|
|
|
|
}
|
|
|
|
#endif
|