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