Home | History | Annotate | Line # | Download | only in sail
sync.c revision 1.9
      1 /*	$NetBSD: sync.c,v 1.9 1998/08/30 09:19:40 veego 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.9 1998/08/30 09:19:40 veego Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <fcntl.h>
     46 #include <errno.h>
     47 #ifdef __STDC__
     48 #include <stdarg.h>
     49 #else
     50 #include <varargs.h>
     51 #endif
     52 #include <stdlib.h>
     53 #include <unistd.h>
     54 #include <sys/types.h>
     55 #include <sys/stat.h>
     56 #include "extern.h"
     57 
     58 #define BUFSIZE 4096
     59 
     60 static char sync_buf[BUFSIZE];
     61 static char *sync_bp = sync_buf;
     62 static char sync_lock[25];
     63 static char sync_file[25];
     64 static long sync_seek;
     65 static FILE *sync_fp;
     66 #define SF "/tmp/#sailsink.%d"
     67 #define LF "/tmp/#saillock.%d"
     68 
     69 void
     70 fmtship(buf, len, fmt, ship)
     71 	char *buf;
     72 	size_t len;
     73 	const char *fmt;
     74 	struct ship *ship;
     75 {
     76 	while (*fmt) {
     77 		if (len-- == 0) {
     78 			*buf = '\0';
     79 			return;
     80 		}
     81 		if (*fmt == '$' && fmt[1] == '$') {
     82 			size_t l = snprintf(buf, len, "%s (%c%c)",
     83 			    ship->shipname, colours(ship), sterncolour(ship));
     84 			buf += l;
     85 			len -= l - 1;
     86 			fmt += 2;
     87 		}
     88 		else
     89 			*buf++ = *fmt++;
     90 	}
     91 
     92 	if (len > 0)
     93 		*buf = '\0';
     94 }
     95 
     96 
     97 /*VARARGS3*/
     98 void
     99 #ifdef __STDC__
    100 makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
    101 #else
    102 makesignal(va_alias)
    103 	va_dcl
    104 #endif
    105 {
    106 	char message[BUFSIZ];
    107 	char format[BUFSIZ];
    108 	va_list ap;
    109 #ifndef __STDC__
    110 	struct ship *from;
    111 	const char *fmt;
    112 	struct ship *ship;
    113 
    114 	va_start(ap);
    115 	from = va_arg(ap, struct ship *);
    116 	fmt = va_arg(ap, const char *);
    117 	ship = va_arg(ap, struct ship *);
    118 #else
    119 	va_start(ap, ship);
    120 #endif
    121 	fmtship(format, sizeof(format), fmt, ship);
    122 	(void) vsprintf(message, format, ap);
    123 	va_end(ap);
    124 	Write(W_SIGNAL, from, 1, (long)message, 0, 0, 0);
    125 }
    126 
    127 void
    128 #ifdef __STDC__
    129 makemsg(struct ship *from, const char *fmt, ...)
    130 #else
    131 makemsg(va_alias)
    132 	va_dcl
    133 #endif
    134 {
    135 	char message[BUFSIZ];
    136 	va_list ap;
    137 #ifndef __STDC__
    138 	struct ship *from;
    139 	const char *fmt;
    140 
    141 	va_start(ap);
    142 	from = va_arg(ap, struct ship *);
    143 	fmt = va_arg(ap, const char *);
    144 #else
    145 	va_start(ap, fmt);
    146 #endif
    147 	(void) vsprintf(message, fmt, ap);
    148 	va_end(ap);
    149 	Write(W_SIGNAL, from, 1, (long)message, 0, 0, 0);
    150 }
    151 int
    152 sync_exists(game)
    153 {
    154 	char buf[sizeof sync_file];
    155 	struct stat s;
    156 	time_t t;
    157 
    158 	(void) sprintf(buf, SF, game);
    159 	(void) time(&t);
    160 	if (stat(buf, &s) < 0)
    161 		return 0;
    162 	if (s.st_mtime < t - 60*60*2) {		/* 2 hours */
    163 		(void) unlink(buf);
    164 		(void) sprintf(buf, LF, game);
    165 		(void) unlink(buf);
    166 		return 0;
    167 	} else
    168 		return 1;
    169 }
    170 
    171 int
    172 sync_open()
    173 {
    174 	if (sync_fp != NULL)
    175 		(void) fclose(sync_fp);
    176 	(void) sprintf(sync_lock, LF, game);
    177 	(void) sprintf(sync_file, SF, game);
    178 	if (access(sync_file, 0) < 0) {
    179 		int omask = umask(issetuid ? 077 : 011);
    180 		sync_fp = fopen(sync_file, "w+");
    181 		(void) umask(omask);
    182 	} else
    183 		sync_fp = fopen(sync_file, "r+");
    184 	if (sync_fp == NULL)
    185 		return -1;
    186 	sync_seek = 0;
    187 	return 0;
    188 }
    189 
    190 void
    191 sync_close(remove)
    192 	char remove;
    193 {
    194 	if (sync_fp != 0)
    195 		(void) fclose(sync_fp);
    196 	if (remove)
    197 		(void) unlink(sync_file);
    198 }
    199 
    200 void
    201 Write(type, ship, isstr, a, b, c, d)
    202 	int type;
    203 	struct ship *ship;
    204 	int isstr;
    205 	long a, b, c, d;
    206 {
    207 
    208 	if (isstr)
    209 		(void) sprintf(sync_bp, "%d %d %d %s\n",
    210 			type, ship->file->index, isstr, (char *) a);
    211 	else
    212 		(void) sprintf(sync_bp, "%d %d %d %ld %ld %ld %ld\n",
    213 			type, ship->file->index, isstr, a, b, c, d);
    214 	while (*sync_bp++)
    215 		;
    216 	sync_bp--;
    217 	if (sync_bp >= &sync_buf[sizeof sync_buf])
    218 		abort();
    219 	(void) sync_update(type, ship, a, b, c, d);
    220 }
    221 
    222 int
    223 Sync()
    224 {
    225 	sig_t sighup, sigint;
    226 	int n;
    227 	int type, shipnum, isstr;
    228 	long a, b, c, d;
    229 	char buf[80];
    230 	char erred = 0;
    231 
    232 	sighup = signal(SIGHUP, SIG_IGN);
    233 	sigint = signal(SIGINT, SIG_IGN);
    234 	for (n = TIMEOUT; --n >= 0;) {
    235 #ifdef LOCK_EX
    236 		if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
    237 			break;
    238 		if (errno != EWOULDBLOCK)
    239 			return -1;
    240 #else
    241 		if (link(sync_file, sync_lock) >= 0)
    242 			break;
    243 		if (errno != EEXIST)
    244 			return -1;
    245 #endif
    246 		sleep(1);
    247 	}
    248 	if (n <= 0)
    249 		return -1;
    250 	(void) fseek(sync_fp, sync_seek, 0);
    251 	for (;;) {
    252 		switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
    253 		case 3:
    254 			break;
    255 		case EOF:
    256 			goto out;
    257 		default:
    258 			goto bad;
    259 		}
    260 		if (shipnum < 0 || shipnum >= cc->vessels)
    261 			goto bad;
    262 		if (isstr != 0 && isstr != 1)
    263 			goto bad;
    264 		if (isstr) {
    265 			char *p;
    266 			for (p = buf;;) {
    267 				switch (*p++ = getc(sync_fp)) {
    268 				case '\n':
    269 					p--;
    270 				case EOF:
    271 					break;
    272 				default:
    273 					if (p >= buf + sizeof buf)
    274 						p--;
    275 					continue;
    276 				}
    277 				break;
    278 			}
    279 			*p = 0;
    280 			for (p = buf; *p == ' '; p++)
    281 				;
    282 			a = (long)p;
    283 			b = c = d = 0;
    284 		} else
    285 			if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d) != 4)
    286 				goto bad;
    287 		if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0)
    288 			goto bad;
    289 	}
    290 bad:
    291 	erred++;
    292 out:
    293 	if (!erred && sync_bp != sync_buf) {
    294 		(void) fseek(sync_fp, 0L, 2);
    295 		(void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
    296 			sync_fp);
    297 		(void) fflush(sync_fp);
    298 		sync_bp = sync_buf;
    299 	}
    300 	sync_seek = ftell(sync_fp);
    301 #ifdef LOCK_EX
    302 	(void) flock(fileno(sync_fp), LOCK_UN);
    303 #else
    304 	(void) unlink(sync_lock);
    305 #endif
    306 	(void) signal(SIGHUP, sighup);
    307 	(void) signal(SIGINT, sigint);
    308 	return erred ? -1 : 0;
    309 }
    310 
    311 int
    312 sync_update(type, ship, a, b, c, d)
    313 	int type;
    314 	struct ship *ship;
    315 	long a, b, c, d;
    316 {
    317 	switch (type) {
    318 	case W_DBP: {
    319 		struct BP *p = &ship->file->DBP[a];
    320 		p->turnsent = b;
    321 		p->toship = SHIP(c);
    322 		p->mensent = d;
    323 		break;
    324 		}
    325 	case W_OBP: {
    326 		struct BP *p = &ship->file->OBP[a];
    327 		p->turnsent = b;
    328 		p->toship = SHIP(c);
    329 		p->mensent = d;
    330 		break;
    331 		}
    332 	case W_FOUL: {
    333 		struct snag *p = &ship->file->foul[a];
    334 		if (SHIP(a)->file->dir == 0)
    335 			break;
    336 		if (p->sn_count++ == 0)
    337 			p->sn_turn = turn;
    338 		ship->file->nfoul++;
    339 		break;
    340 		}
    341 	case W_GRAP: {
    342 		struct snag *p = &ship->file->grap[a];
    343 		if (SHIP(a)->file->dir == 0)
    344 			break;
    345 		if (p->sn_count++ == 0)
    346 			p->sn_turn = turn;
    347 		ship->file->ngrap++;
    348 		break;
    349 		}
    350 	case W_UNFOUL: {
    351 		struct snag *p = &ship->file->foul[a];
    352 		if (p->sn_count > 0) {
    353 			if (b) {
    354 				ship->file->nfoul -= p->sn_count;
    355 				p->sn_count = 0;
    356 			} else {
    357 				ship->file->nfoul--;
    358 				p->sn_count--;
    359 			}
    360 		}
    361 		break;
    362 		}
    363 	case W_UNGRAP: {
    364 		struct snag *p = &ship->file->grap[a];
    365 		if (p->sn_count > 0) {
    366 			if (b) {
    367 				ship->file->ngrap -= p->sn_count;
    368 				p->sn_count = 0;
    369 			} else {
    370 				ship->file->ngrap--;
    371 				p->sn_count--;
    372 			}
    373 		}
    374 		break;
    375 		}
    376 	case W_SIGNAL:
    377 		if (mode == MODE_PLAYER) {
    378 			if (nobells)
    379 				Signal("$$: %s", ship, (char *) a);
    380 			else
    381 				Signal("\7$$: %s", ship, (char *) a);
    382 		}
    383 		break;
    384 	case W_CREW: {
    385 		struct shipspecs *s = ship->specs;
    386 		s->crew1 = a;
    387 		s->crew2 = b;
    388 		s->crew3 = c;
    389 		break;
    390 		}
    391 	case W_CAPTAIN:
    392 		(void) strncpy(ship->file->captain, (char *)a,
    393 			sizeof ship->file->captain - 1);
    394 		ship->file->captain[sizeof ship->file->captain - 1] = 0;
    395 		break;
    396 	case W_CAPTURED:
    397 		if (a < 0)
    398 			ship->file->captured = 0;
    399 		else
    400 			ship->file->captured = SHIP(a);
    401 		break;
    402 	case W_CLASS:
    403 		ship->specs->class = a;
    404 		break;
    405 	case W_DRIFT:
    406 		ship->file->drift = a;
    407 		break;
    408 	case W_EXPLODE:
    409 		if ((ship->file->explode = a) == 2)
    410 			ship->file->dir = 0;
    411 		break;
    412 	case W_FS:
    413 		ship->file->FS = a;
    414 		break;
    415 	case W_GUNL: {
    416 		struct shipspecs *s = ship->specs;
    417 		s->gunL = a;
    418 		s->carL = b;
    419 		break;
    420 		}
    421 	case W_GUNR: {
    422 		struct shipspecs *s = ship->specs;
    423 		s->gunR = a;
    424 		s->carR = b;
    425 		break;
    426 		}
    427 	case W_HULL:
    428 		ship->specs->hull = a;
    429 		break;
    430 	case W_MOVE:
    431 		(void) strncpy(ship->file->movebuf, (char *)a,
    432 			sizeof ship->file->movebuf - 1);
    433 		ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
    434 		break;
    435 	case W_PCREW:
    436 		ship->file->pcrew = a;
    437 		break;
    438 	case W_POINTS:
    439 		ship->file->points = a;
    440 		break;
    441 	case W_QUAL:
    442 		ship->specs->qual = a;
    443 		break;
    444 	case W_RIGG: {
    445 		struct shipspecs *s = ship->specs;
    446 		s->rig1 = a;
    447 		s->rig2 = b;
    448 		s->rig3 = c;
    449 		s->rig4 = d;
    450 		break;
    451 		}
    452 	case W_RIG1:
    453 		ship->specs->rig1 = a;
    454 		break;
    455 	case W_RIG2:
    456 		ship->specs->rig2 = a;
    457 		break;
    458 	case W_RIG3:
    459 		ship->specs->rig3 = a;
    460 		break;
    461 	case W_RIG4:
    462 		ship->specs->rig4 = a;
    463 		break;
    464 	case W_COL:
    465 		ship->file->col = a;
    466 		break;
    467 	case W_DIR:
    468 		ship->file->dir = a;
    469 		break;
    470 	case W_ROW:
    471 		ship->file->row = a;
    472 		break;
    473 	case W_SINK:
    474 		if ((ship->file->sink = a) == 2)
    475 			ship->file->dir = 0;
    476 		break;
    477 	case W_STRUCK:
    478 		ship->file->struck = a;
    479 		break;
    480 	case W_TA:
    481 		ship->specs->ta = a;
    482 		break;
    483 	case W_ALIVE:
    484 		alive = 1;
    485 		break;
    486 	case W_TURN:
    487 		turn = a;
    488 		break;
    489 	case W_WIND:
    490 		winddir = a;
    491 		windspeed = b;
    492 		break;
    493 	case W_BEGIN:
    494 		(void) strcpy(ship->file->captain, "begin");
    495 		people++;
    496 		break;
    497 	case W_END:
    498 		*ship->file->captain = 0;
    499 		ship->file->points = 0;
    500 		people--;
    501 		break;
    502 	case W_DDEAD:
    503 		hasdriver = 0;
    504 		break;
    505 	default:
    506 		fprintf(stderr, "sync_update: unknown type %d\r\n", type);
    507 		return -1;
    508 	}
    509 	return 0;
    510 }
    511