1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/***************************************************************************
* *
* Squish Developers Kit Source, Version 2.00 *
* Copyright 1989-1994 by SCI Communications. All rights reserved. *
* *
* USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE *
* SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN. IF YOU DO NOT *
* FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU *
* DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT *
* ONE OF THE ADDRESSES LISTED BELOW. IN NO EVENT SHOULD YOU PROCEED TO *
* USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH *
* DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
* ABLE TO REACH WITH THE AUTHOR. *
* *
* You can contact the author at one of the address listed below: *
* *
* Scott Dudley FidoNet 1:249/106 *
* 777 Downing St. Internet sjd@f106.n249.z1.fidonet.org *
* Kingston, Ont. CompuServe >INTERNET:sjd@f106.n249.z1.fidonet.org *
* Canada K7M 5N3 BBS 1-613-634-3058, V.32bis *
* *
***************************************************************************/
/*
#pragma off(unreferenced)
static char rcs_id[]="$Id: sq_lock.c,v 1.23 2003/01/15 05:40:38 stas_degteff Exp $";
#pragma on(unreferenced)
*/
#define MSGAPI_HANDLERS
#define MSGAPI_NO_OLD_TYPES
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "compiler.h"
#ifdef HAS_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAS_IO_H
# include <io.h>
#endif
#ifdef HAS_SHARE_H
#include <share.h>
#endif
#ifdef HAS_MALLOC_H
#include <malloc.h>
#endif
#include "prog.h"
#include "old_msg.h"
#include "msgapi.h"
#include "api_sq.h"
#include "api_sqp.h"
#include "apidebug.h"
#include "unused.h"
/* Base is locked for other processes */
#ifdef ALTLOCKING
int _alt_lock(HAREA ha)
{
if (ha->lck_handle > 0) return 0;
ha->lck_handle = open(ha->lck_path, O_RDWR|O_CREAT|O_EXCL, S_IREAD|S_IWRITE);
if (ha->lck_handle > 0)
return 0;
return -1;
}
int _squnlock(HAREA ha)
{
if (ha->lck_handle > 0) close(ha->lck_handle);
ha->lck_handle = 0;
remove(ha->lck_path);
return 1;
}
int _sqlock(HAREA ha, int t)
{
int forever = 0;
int rc;
if (t == -1)
return _alt_lock(ha) == 0;
if (t == 0)
forever = 1;
t *= 10;
while( (rc=_alt_lock(ha)) && (t>0 || forever))
{
tdelay(100);
t--;
}
return rc == 0;
}
#else
int _sqlock(HAREA ha, int t)
{
if (t==-1)
/* lock return 0 on success */
return lock(Sqd->sfd, 0, 1) == 0;
return waitlock2(Sqd->sfd, 0, 1, t) == 0;
}
int _squnlock(HAREA ha)
{
/* unlock returns 0 on success */
return unlock(Sqd->sfd, 0, 1) == 0;
}
#endif
/* Lock the first byte of the Squish file header. Do this up to *
* SQUISH_LOCK_RETRY number of times, in case someone else is using *
* the message base. */
static unsigned near _SquishLockBase(HAREA ha)
{
unsigned rc;
/* Only need to lock the area the first time */
if (Sqd->fLocked++ != 0)
return TRUE;
/* The first step is to obtain a lock on the Squish file header. Another *
* process may be attempting to do the same thing, so we retry a couple *
* of times just in case. */
rc = _sqlock(ha, SQUISH_LOCK_RETRY);
if (!rc)
{
msgapierr = MERR_SHARE;
Sqd->fLocked--;
}
return rc;
}
/* Unlock the first byte of the Squish file */
static unsigned near _SquishUnlockBase(HAREA ha)
{
/* If we have it locked more than once, only unlock on the last call */
if (--Sqd->fLocked)
return TRUE;
/* Unlock the first byte of the file */
if (mi.haveshare)
(void)_squnlock(ha);
return TRUE;
}
/* Obtain exclusive access to this message area. We need to do this to *
* synchronize access to critical fields in the Squish file header. */
unsigned _SquishExclusiveBegin(HAREA ha)
{
SQBASE sqb;
/* We can't open the header for exclusive access more than once */
if (Sqd->fHaveExclusive)
{
msgapierr=MERR_SHARE;
return FALSE;
}
/* Lock the header */
if (!_SquishLockBase(ha))
return FALSE;
/* Obtain an up-to-date copy of the file header */
if (!_SquishReadBaseHeader(ha, &sqb) ||
!_SquishCopyBaseToData(ha, &sqb))
{
(void)_SquishUnlockBase(ha);
return FALSE;
}
Sqd->fHaveExclusive=TRUE;
return TRUE;
}
/* Finish exclusive access to the area header. Sync the base header *
* with what we have in memory, then unlock the file. */
unsigned _SquishExclusiveEnd(HAREA ha)
{
SQBASE sqb;
unsigned rc;
if (!Sqd->fHaveExclusive)
{
msgapierr=MERR_NOLOCK;
return FALSE;
}
/* Copy the in-memory struct to sqb, then write to disk */
rc=_SquishCopyDataToBase(ha, &sqb);
rc = rc && _SquishWriteBaseHeader(ha, &sqb);
/* Relinquish access to the base */
if (!_SquishUnlockBase(ha))
rc=FALSE;
Sqd->fHaveExclusive=FALSE;
return rc;
}
/* Lock this message area for exclusive access */
sword _XPENTRY apiSquishLock(HAREA ha)
{
/* Only need to lock once */
if (Sqd->fLockFunc++ != 0)
{
return 0;
}
/* Lock the header */
if (!_SquishLockBase(ha))
{
return -1;
}
/* Read the index into memory */
if (!_SquishBeginBuffer(Sqd->hix))
{
(void)_SquishUnlockBase(ha);
return -1;
}
return 0;
}
/* Unlock an area that was opened for exclusive access */
sword _XPENTRY apiSquishUnlock(HAREA ha)
{
if (Sqd->fLockFunc==0)
{
msgapierr=MERR_NOLOCK;
return -1;
}
if (--Sqd->fLockFunc != 0)
{
return 0;
}
(void)_SquishEndBuffer(Sqd->hix);
(void)_SquishUnlockBase(ha);
return 0;
}
|