Home | History | Annotate | Line # | Download | only in sail
sync.c revision 1.6
      1 /*	$NetBSD: sync.c,v 1.6 1997/10/13 19:45:54 christos 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.6 1997/10/13 19:45:54 christos 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 int
    128 sync_exists(game)
    129 {
    130 	char buf[sizeof sync_file];
    131 	struct stat s;
    132 	time_t t;
    133 
    134 	(void) sprintf(buf, SF, game);
    135 	(void) time(&t);
    136 	if (stat(buf, &s) < 0)
    137 		return 0;
    138 	if (s.st_mtime < t - 60*60*2) {		/* 2 hours */
    139 		(void) unlink(buf);
    140 		(void) sprintf(buf, LF, game);
    141 		(void) unlink(buf);
    142 		return 0;
    143 	} else
    144 		return 1;
    145 }
    146 
    147 int
    148 sync_open()
    149 {
    150 	if (sync_fp != NULL)
    151 		(void) fclose(sync_fp);
    152 	(void) sprintf(sync_lock, LF, game);
    153 	(void) sprintf(sync_file, SF, game);
    154 	if (access(sync_file, 0) < 0) {
    155 		int omask = umask(issetuid ? 077 : 011);
    156 		sync_fp = fopen(sync_file, "w+");
    157 		(void) umask(omask);
    158 	} else
    159 		sync_fp = fopen(sync_file, "r+");
    160 	if (sync_fp == NULL)
    161 		return -1;
    162 	sync_seek = 0;
    163 	return 0;
    164 }
    165 
    166 void
    167 sync_close(remove)
    168 	char remove;
    169 {
    170 	if (sync_fp != 0)
    171 		(void) fclose(sync_fp);
    172 	if (remove)
    173 		(void) unlink(sync_file);
    174 }
    175 
    176 void
    177 Write(type, ship, isstr, a, b, c, d)
    178 	int type;
    179 	struct ship *ship;
    180 	int isstr;
    181 	long a, b, c, d;
    182 {
    183 
    184 	if (isstr)
    185 		(void) sprintf(sync_bp, "%d %d %d %s\n",
    186 			type, ship->file->index, isstr, (char *) a);
    187 	else
    188 		(void) sprintf(sync_bp, "%d %d %d %ld %ld %ld %ld\n",
    189 			type, ship->file->index, isstr, a, b, c, d);
    190 	while (*sync_bp++)
    191 		;
    192 	sync_bp--;
    193 	if (sync_bp >= &sync_buf[sizeof sync_buf])
    194 		abort();
    195 	(void) sync_update(type, ship, a, b, c, d);
    196 }
    197 
    198 int
    199 Sync()
    200 {
    201 	sig_t sighup, sigint;
    202 	int n;
    203 	int type, shipnum, isstr;
    204 	long a, b, c, d;
    205 	char buf[80];
    206 	char erred = 0;
    207 	extern errno;
    208 
    209 	sighup = signal(SIGHUP, SIG_IGN);
    210 	sigint = signal(SIGINT, SIG_IGN);
    211 	for (n = TIMEOUT; --n >= 0;) {
    212 #ifdef LOCK_EX
    213 		if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
    214 			break;
    215 		if (errno != EWOULDBLOCK)
    216 			return -1;
    217 #else
    218 		if (link(sync_file, sync_lock) >= 0)
    219 			break;
    220 		if (errno != EEXIST)
    221 			return -1;
    222 #endif
    223 		sleep(1);
    224 	}
    225 	if (n <= 0)
    226 		return -1;
    227 	(void) fseek(sync_fp, sync_seek, 0);
    228 	for (;;) {
    229 		switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
    230 		case 3:
    231 			break;
    232 		case EOF:
    233 			goto out;
    234 		default:
    235 			goto bad;
    236 		}
    237 		if (shipnum < 0 || shipnum >= cc->vessels)
    238 			goto bad;
    239 		if (isstr != 0 && isstr != 1)
    240 			goto bad;
    241 		if (isstr) {
    242 			char *p;
    243 			for (p = buf;;) {
    244 				switch (*p++ = getc(sync_fp)) {
    245 				case '\n':
    246 					p--;
    247 				case EOF:
    248 					break;
    249 				default:
    250 					if (p >= buf + sizeof buf)
    251 						p--;
    252 					continue;
    253 				}
    254 				break;
    255 			}
    256 			*p = 0;
    257 			for (p = buf; *p == ' '; p++)
    258 				;
    259 			a = (long)p;
    260 			b = c = d = 0;
    261 		} else
    262 			if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d) != 4)
    263 				goto bad;
    264 		if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0)
    265 			goto bad;
    266 	}
    267 bad:
    268 	erred++;
    269 out:
    270 	if (!erred && sync_bp != sync_buf) {
    271 		(void) fseek(sync_fp, 0L, 2);
    272 		(void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
    273 			sync_fp);
    274 		(void) fflush(sync_fp);
    275 		sync_bp = sync_buf;
    276 	}
    277 	sync_seek = ftell(sync_fp);
    278 #ifdef LOCK_EX
    279 	(void) flock(fileno(sync_fp), LOCK_UN);
    280 #else
    281 	(void) unlink(sync_lock);
    282 #endif
    283 	(void) signal(SIGHUP, sighup);
    284 	(void) signal(SIGINT, sigint);
    285 	return erred ? -1 : 0;
    286 }
    287 
    288 int
    289 sync_update(type, ship, a, b, c, d)
    290 	int type;
    291 	struct ship *ship;
    292 	long a, b, c, d;
    293 {
    294 	switch (type) {
    295 	case W_DBP: {
    296 		struct BP *p = &ship->file->DBP[a];
    297 		p->turnsent = b;
    298 		p->toship = SHIP(c);
    299 		p->mensent = d;
    300 		break;
    301 		}
    302 	case W_OBP: {
    303 		struct BP *p = &ship->file->OBP[a];
    304 		p->turnsent = b;
    305 		p->toship = SHIP(c);
    306 		p->mensent = d;
    307 		break;
    308 		}
    309 	case W_FOUL: {
    310 		struct snag *p = &ship->file->foul[a];
    311 		if (SHIP(a)->file->dir == 0)
    312 			break;
    313 		if (p->sn_count++ == 0)
    314 			p->sn_turn = turn;
    315 		ship->file->nfoul++;
    316 		break;
    317 		}
    318 	case W_GRAP: {
    319 		struct snag *p = &ship->file->grap[a];
    320 		if (SHIP(a)->file->dir == 0)
    321 			break;
    322 		if (p->sn_count++ == 0)
    323 			p->sn_turn = turn;
    324 		ship->file->ngrap++;
    325 		break;
    326 		}
    327 	case W_UNFOUL: {
    328 		struct snag *p = &ship->file->foul[a];
    329 		if (p->sn_count > 0)
    330 			if (b) {
    331 				ship->file->nfoul -= p->sn_count;
    332 				p->sn_count = 0;
    333 			} else {
    334 				ship->file->nfoul--;
    335 				p->sn_count--;
    336 			}
    337 		break;
    338 		}
    339 	case W_UNGRAP: {
    340 		struct snag *p = &ship->file->grap[a];
    341 		if (p->sn_count > 0)
    342 			if (b) {
    343 				ship->file->ngrap -= p->sn_count;
    344 				p->sn_count = 0;
    345 			} else {
    346 				ship->file->ngrap--;
    347 				p->sn_count--;
    348 			}
    349 		break;
    350 		}
    351 	case W_SIGNAL:
    352 		if (mode == MODE_PLAYER)
    353 			if (nobells)
    354 				Signal("%$: %s", ship, a);
    355 			else
    356 				Signal("\7%$: %s", ship, a);
    357 		break;
    358 	case W_CREW: {
    359 		struct shipspecs *s = ship->specs;
    360 		s->crew1 = a;
    361 		s->crew2 = b;
    362 		s->crew3 = c;
    363 		break;
    364 		}
    365 	case W_CAPTAIN:
    366 		(void) strncpy(ship->file->captain, (char *)a,
    367 			sizeof ship->file->captain - 1);
    368 		ship->file->captain[sizeof ship->file->captain - 1] = 0;
    369 		break;
    370 	case W_CAPTURED:
    371 		if (a < 0)
    372 			ship->file->captured = 0;
    373 		else
    374 			ship->file->captured = SHIP(a);
    375 		break;
    376 	case W_CLASS:
    377 		ship->specs->class = a;
    378 		break;
    379 	case W_DRIFT:
    380 		ship->file->drift = a;
    381 		break;
    382 	case W_EXPLODE:
    383 		if ((ship->file->explode = a) == 2)
    384 			ship->file->dir = 0;
    385 		break;
    386 	case W_FS:
    387 		ship->file->FS = a;
    388 		break;
    389 	case W_GUNL: {
    390 		struct shipspecs *s = ship->specs;
    391 		s->gunL = a;
    392 		s->carL = b;
    393 		break;
    394 		}
    395 	case W_GUNR: {
    396 		struct shipspecs *s = ship->specs;
    397 		s->gunR = a;
    398 		s->carR = b;
    399 		break;
    400 		}
    401 	case W_HULL:
    402 		ship->specs->hull = a;
    403 		break;
    404 	case W_MOVE:
    405 		(void) strncpy(ship->file->movebuf, (char *)a,
    406 			sizeof ship->file->movebuf - 1);
    407 		ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
    408 		break;
    409 	case W_PCREW:
    410 		ship->file->pcrew = a;
    411 		break;
    412 	case W_POINTS:
    413 		ship->file->points = a;
    414 		break;
    415 	case W_QUAL:
    416 		ship->specs->qual = a;
    417 		break;
    418 	case W_RIGG: {
    419 		struct shipspecs *s = ship->specs;
    420 		s->rig1 = a;
    421 		s->rig2 = b;
    422 		s->rig3 = c;
    423 		s->rig4 = d;
    424 		break;
    425 		}
    426 	case W_RIG1:
    427 		ship->specs->rig1 = a;
    428 		break;
    429 	case W_RIG2:
    430 		ship->specs->rig2 = a;
    431 		break;
    432 	case W_RIG3:
    433 		ship->specs->rig3 = a;
    434 		break;
    435 	case W_RIG4:
    436 		ship->specs->rig4 = a;
    437 		break;
    438 	case W_COL:
    439 		ship->file->col = a;
    440 		break;
    441 	case W_DIR:
    442 		ship->file->dir = a;
    443 		break;
    444 	case W_ROW:
    445 		ship->file->row = a;
    446 		break;
    447 	case W_SINK:
    448 		if ((ship->file->sink = a) == 2)
    449 			ship->file->dir = 0;
    450 		break;
    451 	case W_STRUCK:
    452 		ship->file->struck = a;
    453 		break;
    454 	case W_TA:
    455 		ship->specs->ta = a;
    456 		break;
    457 	case W_ALIVE:
    458 		alive = 1;
    459 		break;
    460 	case W_TURN:
    461 		turn = a;
    462 		break;
    463 	case W_WIND:
    464 		winddir = a;
    465 		windspeed = b;
    466 		break;
    467 	case W_BEGIN:
    468 		(void) strcpy(ship->file->captain, "begin");
    469 		people++;
    470 		break;
    471 	case W_END:
    472 		*ship->file->captain = 0;
    473 		ship->file->points = 0;
    474 		people--;
    475 		break;
    476 	case W_DDEAD:
    477 		hasdriver = 0;
    478 		break;
    479 	default:
    480 		fprintf(stderr, "sync_update: unknown type %d\r\n", type);
    481 		return -1;
    482 	}
    483 	return 0;
    484 }
    485