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