Home | History | Annotate | Line # | Download | only in sail
sync.c revision 1.17
      1 /*	$NetBSD: sync.c,v 1.17 2001/01/04 03:21:17 jwise Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)sync.c	8.2 (Berkeley) 4/28/95";
     40 #else
     41 __RCSID("$NetBSD: sync.c,v 1.17 2001/01/04 03:21:17 jwise Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <fcntl.h>
     46 #include <errno.h>
     47 #include <stdarg.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <time.h>
     51 #include <unistd.h>
     52 #include "extern.h"
     53 #include "pathnames.h"
     54 
     55 #define BUFSIZE 4096
     56 
     57 void	fmtship(char *, size_t, const char *, struct ship *);
     58 void	makesignal(struct ship *, const char *, struct ship *, ...);
     59 void	makemsg(struct ship *, const char *, ...);
     60 int	sync_exists(int);
     61 int	sync_open(void);
     62 void	sync_close(int);
     63 void	Write(int, struct ship *, long, long, long, long);
     64 void	Writestr(int, struct ship *, const char *);
     65 int	Sync(void);
     66 static int	sync_update(int, struct ship *, const char *, long, long, long, long);
     67 
     68 static const char SF[] = _PATH_SYNC;
     69 static const char LF[] = _PATH_LOCK;
     70 static char sync_buf[BUFSIZE];
     71 static char *sync_bp = sync_buf;
     72 static char sync_lock[sizeof SF];
     73 static char sync_file[sizeof LF];
     74 static long sync_seek;
     75 static FILE *sync_fp;
     76 
     77 void
     78 fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
     79 {
     80 	while (*fmt) {
     81 		if (len-- == 0) {
     82 			*buf = '\0';
     83 			return;
     84 		}
     85 		if (*fmt == '$' && fmt[1] == '$') {
     86 			size_t l = snprintf(buf, len, "%s (%c%c)",
     87 			    ship->shipname, colours(ship), sterncolour(ship));
     88 			buf += l;
     89 			len -= l - 1;
     90 			fmt += 2;
     91 		}
     92 		else
     93 			*buf++ = *fmt++;
     94 	}
     95 
     96 	if (len > 0)
     97 		*buf = '\0';
     98 }
     99 
    100 
    101 /*VARARGS3*/
    102 void
    103 makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
    104 {
    105 	char message[BUFSIZ];
    106 	char format[BUFSIZ];
    107 	va_list ap;
    108 
    109 	va_start(ap, ship);
    110 	fmtship(format, sizeof(format), fmt, ship);
    111 	vsprintf(message, format, ap);
    112 	va_end(ap);
    113 	Writestr(W_SIGNAL, from, message);
    114 }
    115 
    116 /*VARARGS2*/
    117 void
    118 makemsg(struct ship *from, const char *fmt, ...)
    119 {
    120 	char message[BUFSIZ];
    121 	va_list ap;
    122 
    123 	va_start(ap, fmt);
    124 	vsprintf(message, fmt, ap);
    125 	va_end(ap);
    126 	Writestr(W_SIGNAL, from, message);
    127 }
    128 
    129 int
    130 sync_exists(int game)
    131 {
    132 	char buf[sizeof sync_file];
    133 	struct stat s;
    134 	time_t t;
    135 
    136 	sprintf(buf, SF, game);
    137 	time(&t);
    138 	setegid(egid);
    139 	if (stat(buf, &s) < 0) {
    140 		setegid(gid);
    141 		return 0;
    142 	}
    143 	if (s.st_mtime < t - 60*60*2) {		/* 2 hours */
    144 		unlink(buf);
    145 		sprintf(buf, LF, game);
    146 		unlink(buf);
    147 		setegid(gid);
    148 		return 0;
    149 	} else {
    150 		setegid(gid);
    151 		return 1;
    152 	}
    153 }
    154 
    155 int
    156 sync_open(void)
    157 {
    158 	struct stat tmp;
    159 	if (sync_fp != NULL)
    160 		fclose(sync_fp);
    161 	sprintf(sync_lock, LF, game);
    162 	sprintf(sync_file, SF, game);
    163 	setegid(egid);
    164 	if (stat(sync_file, &tmp) < 0) {
    165 		mode_t omask = umask(002);
    166 		sync_fp = fopen(sync_file, "w+");
    167 		umask(omask);
    168 	} else
    169 		sync_fp = fopen(sync_file, "r+");
    170 	setegid(gid);
    171 	if (sync_fp == NULL)
    172 		return -1;
    173 	sync_seek = 0;
    174 	return 0;
    175 }
    176 
    177 void
    178 sync_close(int remove)
    179 {
    180 	if (sync_fp != 0)
    181 		fclose(sync_fp);
    182 	if (remove) {
    183 		setegid(egid);
    184 		unlink(sync_file);
    185 		setegid(gid);
    186 	}
    187 }
    188 
    189 void
    190 Write(int type, struct ship *ship, long a, long b, long c, long d)
    191 {
    192 
    193 	sprintf(sync_bp, "%d %d 0 %ld %ld %ld %ld\n",
    194 		       type, ship->file->index, a, b, c, d);
    195 	while (*sync_bp++)
    196 		;
    197 	sync_bp--;
    198 	if (sync_bp >= &sync_buf[sizeof sync_buf])
    199 		abort();
    200 	sync_update(type, ship, NULL, a, b, c, d);
    201 }
    202 
    203 void
    204 Writestr(int type, struct ship *ship, const char *a)
    205 {
    206 	sprintf(sync_bp, "%d %d 1 %s\n", type, ship->file->index, a);
    207 	while (*sync_bp++)
    208 		;
    209 	sync_bp--;
    210 	if (sync_bp >= &sync_buf[sizeof sync_buf])
    211 		abort();
    212 	sync_update(type, ship, a, 0, 0, 0, 0);
    213 }
    214 
    215 int
    216 Sync(void)
    217 {
    218 	sig_t sighup, sigint;
    219 	int n;
    220 	int type, shipnum, isstr;
    221 	char *astr;
    222 	long a, b, c, d;
    223 	char buf[80];
    224 	char erred = 0;
    225 
    226 	sighup = signal(SIGHUP, SIG_IGN);
    227 	sigint = signal(SIGINT, SIG_IGN);
    228 	for (n = TIMEOUT; --n >= 0;) {
    229 #ifdef LOCK_EX
    230 		if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
    231 			break;
    232 		if (errno != EWOULDBLOCK)
    233 			return -1;
    234 #else
    235 		setegid(egid);
    236 		if (link(sync_file, sync_lock) >= 0) {
    237 			setegid(gid);
    238 			break;
    239 		}
    240 		setegid(gid);
    241 		if (errno != EEXIST)
    242 			return -1;
    243 #endif
    244 		sleep(1);
    245 	}
    246 	if (n <= 0)
    247 		return -1;
    248 	fseek(sync_fp, sync_seek, SEEK_SET);
    249 	for (;;) {
    250 		switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
    251 		case 3:
    252 			break;
    253 		case EOF:
    254 			goto out;
    255 		default:
    256 			goto bad;
    257 		}
    258 		if (shipnum < 0 || shipnum >= cc->vessels)
    259 			goto bad;
    260 		if (isstr != 0 && isstr != 1)
    261 			goto bad;
    262 		if (isstr) {
    263 			char *p;
    264 			for (p = buf;;) {
    265 				switch (*p++ = getc(sync_fp)) {
    266 				case '\n':
    267 					p--;
    268 				case EOF:
    269 					break;
    270 				default:
    271 					if (p >= buf + sizeof buf)
    272 						p--;
    273 					continue;
    274 				}
    275 				break;
    276 			}
    277 			*p = 0;
    278 			for (p = buf; *p == ' '; p++)
    279 				;
    280 			astr = p;
    281 			a = b = c = d = 0;
    282 		} else {
    283 			if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d) != 4)
    284 				goto bad;
    285 			astr = NULL;
    286 		}
    287 		if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
    288 			goto bad;
    289 	}
    290 bad:
    291 	erred++;
    292 out:
    293 	if (!erred && sync_bp != sync_buf) {
    294 		fseek(sync_fp, 0L, SEEK_END);
    295 		fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
    296 			sync_fp);
    297 		fflush(sync_fp);
    298 		sync_bp = sync_buf;
    299 	}
    300 	sync_seek = ftell(sync_fp);
    301 #ifdef LOCK_EX
    302 	flock(fileno(sync_fp), LOCK_UN);
    303 #else
    304 	setegid(egid);
    305 	unlink(sync_lock);
    306 	setegid(gid);
    307 #endif
    308 	signal(SIGHUP, sighup);
    309 	signal(SIGINT, sigint);
    310 	return erred ? -1 : 0;
    311 }
    312 
    313 static int
    314 sync_update(int type, struct ship *ship, const char *astr, long a, long b, long c, long d)
    315 {
    316 	switch (type) {
    317 	case W_DBP: {
    318 		struct BP *p = &ship->file->DBP[a];
    319 		p->turnsent = b;
    320 		p->toship = SHIP(c);
    321 		p->mensent = d;
    322 		break;
    323 		}
    324 	case W_OBP: {
    325 		struct BP *p = &ship->file->OBP[a];
    326 		p->turnsent = b;
    327 		p->toship = SHIP(c);
    328 		p->mensent = d;
    329 		break;
    330 		}
    331 	case W_FOUL: {
    332 		struct snag *p = &ship->file->foul[a];
    333 		if (SHIP(a)->file->dir == 0)
    334 			break;
    335 		if (p->sn_count++ == 0)
    336 			p->sn_turn = turn;
    337 		ship->file->nfoul++;
    338 		break;
    339 		}
    340 	case W_GRAP: {
    341 		struct snag *p = &ship->file->grap[a];
    342 		if (SHIP(a)->file->dir == 0)
    343 			break;
    344 		if (p->sn_count++ == 0)
    345 			p->sn_turn = turn;
    346 		ship->file->ngrap++;
    347 		break;
    348 		}
    349 	case W_UNFOUL: {
    350 		struct snag *p = &ship->file->foul[a];
    351 		if (p->sn_count > 0) {
    352 			if (b) {
    353 				ship->file->nfoul -= p->sn_count;
    354 				p->sn_count = 0;
    355 			} else {
    356 				ship->file->nfoul--;
    357 				p->sn_count--;
    358 			}
    359 		}
    360 		break;
    361 		}
    362 	case W_UNGRAP: {
    363 		struct snag *p = &ship->file->grap[a];
    364 		if (p->sn_count > 0) {
    365 			if (b) {
    366 				ship->file->ngrap -= p->sn_count;
    367 				p->sn_count = 0;
    368 			} else {
    369 				ship->file->ngrap--;
    370 				p->sn_count--;
    371 			}
    372 		}
    373 		break;
    374 		}
    375 	case W_SIGNAL:
    376 		if (mode == MODE_PLAYER) {
    377 			if (nobells)
    378 				Signal("$$: %s", ship, astr);
    379 			else
    380 				Signal("\7$$: %s", ship, astr);
    381 		}
    382 		break;
    383 	case W_CREW: {
    384 		struct shipspecs *s = ship->specs;
    385 		s->crew1 = a;
    386 		s->crew2 = b;
    387 		s->crew3 = c;
    388 		break;
    389 		}
    390 	case W_CAPTAIN:
    391 		strncpy(ship->file->captain, astr,
    392 			sizeof ship->file->captain - 1);
    393 		ship->file->captain[sizeof ship->file->captain - 1] = 0;
    394 		break;
    395 	case W_CAPTURED:
    396 		if (a < 0)
    397 			ship->file->captured = 0;
    398 		else
    399 			ship->file->captured = SHIP(a);
    400 		break;
    401 	case W_CLASS:
    402 		ship->specs->class = a;
    403 		break;
    404 	case W_DRIFT:
    405 		ship->file->drift = a;
    406 		break;
    407 	case W_EXPLODE:
    408 		if ((ship->file->explode = a) == 2)
    409 			ship->file->dir = 0;
    410 		break;
    411 	case W_FS:
    412 		ship->file->FS = a;
    413 		break;
    414 	case W_GUNL: {
    415 		struct shipspecs *s = ship->specs;
    416 		s->gunL = a;
    417 		s->carL = b;
    418 		break;
    419 		}
    420 	case W_GUNR: {
    421 		struct shipspecs *s = ship->specs;
    422 		s->gunR = a;
    423 		s->carR = b;
    424 		break;
    425 		}
    426 	case W_HULL:
    427 		ship->specs->hull = a;
    428 		break;
    429 	case W_MOVE:
    430 		strncpy(ship->file->movebuf, astr,
    431 			sizeof ship->file->movebuf - 1);
    432 		ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
    433 		break;
    434 	case W_PCREW:
    435 		ship->file->pcrew = a;
    436 		break;
    437 	case W_POINTS:
    438 		ship->file->points = a;
    439 		break;
    440 	case W_QUAL:
    441 		ship->specs->qual = a;
    442 		break;
    443 	case W_RIGG: {
    444 		struct shipspecs *s = ship->specs;
    445 		s->rig1 = a;
    446 		s->rig2 = b;
    447 		s->rig3 = c;
    448 		s->rig4 = d;
    449 		break;
    450 		}
    451 	case W_RIG1:
    452 		ship->specs->rig1 = a;
    453 		break;
    454 	case W_RIG2:
    455 		ship->specs->rig2 = a;
    456 		break;
    457 	case W_RIG3:
    458 		ship->specs->rig3 = a;
    459 		break;
    460 	case W_RIG4:
    461 		ship->specs->rig4 = a;
    462 		break;
    463 	case W_COL:
    464 		ship->file->col = a;
    465 		break;
    466 	case W_DIR:
    467 		ship->file->dir = a;
    468 		break;
    469 	case W_ROW:
    470 		ship->file->row = a;
    471 		break;
    472 	case W_SINK:
    473 		if ((ship->file->sink = a) == 2)
    474 			ship->file->dir = 0;
    475 		break;
    476 	case W_STRUCK:
    477 		ship->file->struck = a;
    478 		break;
    479 	case W_TA:
    480 		ship->specs->ta = a;
    481 		break;
    482 	case W_ALIVE:
    483 		alive = 1;
    484 		break;
    485 	case W_TURN:
    486 		turn = a;
    487 		break;
    488 	case W_WIND:
    489 		winddir = a;
    490 		windspeed = b;
    491 		break;
    492 	case W_BEGIN:
    493 		strcpy(ship->file->captain, "begin");
    494 		people++;
    495 		break;
    496 	case W_END:
    497 		*ship->file->captain = 0;
    498 		ship->file->points = 0;
    499 		people--;
    500 		break;
    501 	case W_DDEAD:
    502 		hasdriver = 0;
    503 		break;
    504 	default:
    505 		fprintf(stderr, "sync_update: unknown type %d\r\n", type);
    506 		return -1;
    507 	}
    508 	return 0;
    509 }
    510