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