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