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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include "c_defs.h"
/************************************************************************
*
* $Id: conqcm.c,v 1.15 2004/12/12 05:32:11 jon Exp $
*
* Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
***********************************************************************/
/* C O N Q C M */
/* Copyright (C)1983-1986 by Jef Poskanzer and Craig Leres */
/* Permission to use, copy, modify, and distribute this software and */
/* its documentation for any purpose and without fee is hereby granted, */
/* provided that this copyright notice appear in all copies and in all */
/* supporting documentation. Jef Poskanzer and Craig Leres make no */
/* representations about the suitability of this software for any */
/* purpose. It is provided "as is" without express or implied warranty. */
/**********************************************************************/
/* Unix/C specific porting and supporting code Copyright (C)1994-1996 */
/* by Jon Trulson <jon@radscan.com> under the same terms and */
/* conditions of the original copyright by Jef Poskanzer and Craig */
/* Leres. */
/* */
/**********************************************************************/
#include "global.h"
#include "conqdef.h"
#include "conqlb.h"
#define NOCOMEXTERN
#include "conqcom.h" /* common block vars defined here */
#include "context.h" /* some extra stuff */
#include "user.h"
#include "sem.h"
static char *cBasePtr = NULL; /* common block ptr */
static int coff = 0; /* offset into common block */
static int fakeCommon = FALSE; /* for the clients */
/* map a 1D variable into the common block */
#define map1d(thevarp, thetype, size) { \
thevarp = (thetype *) (cBasePtr + coff); \
coff += (sizeof(thetype) * (size)); \
}
static void map_vars(void);
/* we'll use a hack to translate the lock[mesg|word] pointers into
a semaphore selector */
void PVLOCK(int *lockptr)
{
int semnum;
if (lockptr == &ConqInfo->lockmesg)
semnum = LOCKMSG;
else
semnum = LOCKCMN;
Lock(semnum);
(*lockptr)++;
return;
}
void PVUNLOCK(int *lockptr)
{
int semnum;
if (lockptr == &ConqInfo->lockmesg)
semnum = LOCKMSG;
else
semnum = LOCKCMN;
Unlock(semnum);
return;
}
/* flush_common() - flush a common block */
void flush_common(void)
{
if (fakeCommon)
return;
/* fbsd doesn't like MS_SYNC */
/* which is prefered, but oh well */
#if defined(FREEBSD)
if (msync((caddr_t)cBasePtr, SIZEOF_COMMONBLOCK, 0) == -1)
#else
if (msync((caddr_t)cBasePtr, SIZEOF_COMMONBLOCK, MS_SYNC) == -1)
#endif
clog("flush_common(): msync(): %s", strerror(errno));
return;
}
/* check_cblock() - open/verify a common block - init if necc, return TRUE
if successful */
int check_cblock(char *fname, int fmode, int sizeofcb)
{
int ffd;
struct stat sbuf;
/* first stat the file, if it exists
then verify the size. unlink if size
mismatch */
if (stat(fname, &sbuf) != -1) /* ok if this fails */
{ /* file exists - verify size */
if (sbuf.st_size != sizeofcb)
{
printf("%s: File size mismatch (expected %d, was %d), removing.\n",
fname,
sizeofcb,
(unsigned int)sbuf.st_size);
if (unlink(fname) == -1)
{
printf("check_cblock(): unlink(%s) failed: %s\n",
fname,
strerror(errno));
return(FALSE);
}
}
}
/* ok, either the file exists with the right
size, or it doesn't exist at all -
now open (and create) if necc */
umask(0); /* clear umask, just in case... */
if ((ffd = open(fname, O_RDONLY)) == -1)
{ /* Error or not there... */
if (errno == ENOENT) /* Not There */
{ /* create it */
if ((ffd = creat(fname, fmode)) == -1)
{
printf("check_cblock(): creat(%s) failed: %s\n",
fname,
strerror(errno));
return(FALSE);
}
else
{ /* Create it */
printf("Initializing common block: %s\n", fname);
cBasePtr = (char *) mymalloc(sizeofcb); /* this exits if malloc fails */
memset(cBasePtr, 0, sizeofcb);
write(ffd, cBasePtr, sizeofcb);
close(ffd);
free(cBasePtr);
cBasePtr = NULL;
}
}
else
{ /* some other error */
printf("check_cblock(): open(%s, O_RDONLY) failed: %s\n",
fname,
strerror(errno));
return(FALSE);
}
}
close(ffd); /* everything ok.. */
/* set ownership */
chown(fname, 0, -1);
return(TRUE); /* everything there, and right size */
}
/* my malloc wrapper. used only when mapping
or initializing a commonblock */
char *mymalloc(int size)
{
char *ptr;
if ((ptr = malloc(size)) == NULL)
{
perror("mymalloc");
exit(1);
}
return(ptr);
}
#if defined(USE_COMMONMLOCK)
void lock_common(void)
{
/* Lock it in memory. this requires the
PLOCK privilege on Unixware. if we
fail, we'll complain to the logfile
and continue... */
if (fakeCommon)
return;
if (memcntl((caddr_t)cBasePtr, SIZEOF_COMMONBLOCK,
MC_LOCK, (caddr_t)0, 0, 0) == -1)
{
clog("map_common(): couldn't lock the common block: %s, continuing...",
strerror(errno));
}
}
#endif
void map_common(void)
{
int cmn_fd;
static char cmnfile[MID_BUFFER_SIZE];
if (fakeCommon)
return;
sprintf(cmnfile, "%s/%s", CONQSTATE, C_CONQ_COMMONBLK);
/* verify it's validity */
if (check_cblock(cmnfile, CMN_MODE, SIZEOF_COMMONBLOCK) == FALSE)
exit(1); /* an unrecoverable error */
/* reopen it... */
if ((cmn_fd = open(cmnfile, O_RDWR)) == -1)
{
perror("map_common:open(O_RDWR)");
exit(1);
}
/* Now lets map it */
#ifndef MAP_FILE
# define MAP_FILE 0 /* some arch's don't def this */
#endif
if ((cBasePtr = mmap((caddr_t) 0, (size_t) SIZEOF_COMMONBLOCK,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE,
cmn_fd, 0)) == (caddr_t) -1)
{
perror("map_common():mmap()");
exit(1);
}
/* now map the variables into the
common block */
map_vars();
/* now lets lock it */
#if defined(USE_COMMONMLOCK)
lock_common();
#endif
return;
}
void zero_common(void)
{ /* zero the common block, called from
init everything */
memset(cBasePtr, 0, SIZEOF_COMMONBLOCK);
upchuck(); /* flush the commonblock */
return;
}
/* maps the actual vars into the common block */
static void map_vars(void)
{
coff = 0;
map1d(CBlockRevision, int, 1); /* this *must* be the first var */
map1d(ConqInfo, ConqInfo_t, 1)
map1d(Users, User_t, MAXUSERS);
map1d(Robot, Robot_t, 1);
map1d(Planets, Planet_t, NUMPLANETS + 1);
map1d(Teams, Team_t, NUMALLTEAMS);
map1d(Doomsday, Doomsday_t, 1);
map1d(History, History_t, MAXHISTLOG);
map1d(Driver, Driver_t, 1);
map1d(Ships, Ship_t, MAXSHIPS + 1);
map1d(ShipTypes, ShipType_t, MAXNUMSHIPTYPES);
map1d(Msgs, Msg_t, MAXMESSAGES);
map1d(EndOfCBlock, int, 1);
return;
}
void fake_common(void)
{
fakeCommon = TRUE;
/* this will exit if it fails */
if (!cBasePtr)
cBasePtr = mymalloc(SIZEOF_COMMONBLOCK);
map_vars();
zero_common();
return;
}
/* short cut */
void map_lcommon(void)
{
/* a parallel universe, it is */
fake_common();
clbInitEverything();
clbInitMsgs();
*CBlockRevision = COMMONSTAMP;
ConqInfo->closed = FALSE;
Driver->drivstat = DRS_OFF;
Driver->drivpid = 0;
Driver->drivowner[0] = EOS;
return;
}
|