sync.c revision 1.16 1 1.16 jwise /* $NetBSD: sync.c,v 1.16 2001/01/04 02:43:33 jwise Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1983, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.6 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.3 cgd #if 0
39 1.5 tls static char sccsid[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95";
40 1.3 cgd #else
41 1.16 jwise __RCSID("$NetBSD: sync.c,v 1.16 2001/01/04 02:43:33 jwise Exp $");
42 1.3 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.6 christos #include <fcntl.h>
46 1.5 tls #include <errno.h>
47 1.6 christos #ifdef __STDC__
48 1.6 christos #include <stdarg.h>
49 1.6 christos #else
50 1.6 christos #include <varargs.h>
51 1.6 christos #endif
52 1.6 christos #include <stdlib.h>
53 1.6 christos #include <unistd.h>
54 1.6 christos #include <sys/types.h>
55 1.6 christos #include <sys/stat.h>
56 1.13 jsm #include <time.h>
57 1.5 tls #include "extern.h"
58 1.14 jsm #include "pathnames.h"
59 1.1 cgd
60 1.1 cgd #define BUFSIZE 4096
61 1.1 cgd
62 1.16 jwise void fmtship(char *, size_t, const char *, struct ship *);
63 1.16 jwise void makesignal(struct ship *, const char *, struct ship *, ...);
64 1.16 jwise void makemsg(struct ship *, const char *, ...);
65 1.16 jwise int sync_exists(int);
66 1.16 jwise int sync_open(void);
67 1.16 jwise void sync_close(int);
68 1.16 jwise void Write(int, struct ship *, long, long, long, long);
69 1.16 jwise void Writestr(int, struct ship *, const char *);
70 1.16 jwise int Sync(void);
71 1.16 jwise static int sync_update(int, struct ship *, const char *, long, long, long, long);
72 1.16 jwise
73 1.14 jsm static const char SF[] = _PATH_SYNC;
74 1.14 jsm static const char LF[] = _PATH_LOCK;
75 1.1 cgd static char sync_buf[BUFSIZE];
76 1.1 cgd static char *sync_bp = sync_buf;
77 1.14 jsm static char sync_lock[sizeof SF];
78 1.14 jsm static char sync_file[sizeof LF];
79 1.1 cgd static long sync_seek;
80 1.1 cgd static FILE *sync_fp;
81 1.1 cgd
82 1.6 christos void
83 1.15 jwise fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
84 1.6 christos {
85 1.6 christos while (*fmt) {
86 1.6 christos if (len-- == 0) {
87 1.6 christos *buf = '\0';
88 1.6 christos return;
89 1.6 christos }
90 1.7 christos if (*fmt == '$' && fmt[1] == '$') {
91 1.6 christos size_t l = snprintf(buf, len, "%s (%c%c)",
92 1.6 christos ship->shipname, colours(ship), sterncolour(ship));
93 1.6 christos buf += l;
94 1.6 christos len -= l - 1;
95 1.6 christos fmt += 2;
96 1.6 christos }
97 1.6 christos else
98 1.6 christos *buf++ = *fmt++;
99 1.6 christos }
100 1.6 christos
101 1.6 christos if (len > 0)
102 1.6 christos *buf = '\0';
103 1.6 christos }
104 1.6 christos
105 1.6 christos
106 1.1 cgd /*VARARGS3*/
107 1.6 christos void
108 1.6 christos makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
109 1.6 christos {
110 1.6 christos char message[BUFSIZ];
111 1.6 christos char format[BUFSIZ];
112 1.6 christos va_list ap;
113 1.15 jwise
114 1.6 christos va_start(ap, ship);
115 1.6 christos fmtship(format, sizeof(format), fmt, ship);
116 1.15 jwise vsprintf(message, format, ap);
117 1.6 christos va_end(ap);
118 1.11 hubertf Writestr(W_SIGNAL, from, message);
119 1.1 cgd }
120 1.1 cgd
121 1.15 jwise /*VARARGS2*/
122 1.7 christos void
123 1.7 christos makemsg(struct ship *from, const char *fmt, ...)
124 1.7 christos {
125 1.7 christos char message[BUFSIZ];
126 1.7 christos va_list ap;
127 1.15 jwise
128 1.7 christos va_start(ap, fmt);
129 1.15 jwise vsprintf(message, fmt, ap);
130 1.7 christos va_end(ap);
131 1.11 hubertf Writestr(W_SIGNAL, from, message);
132 1.7 christos }
133 1.11 hubertf
134 1.6 christos int
135 1.15 jwise sync_exists(int game)
136 1.1 cgd {
137 1.1 cgd char buf[sizeof sync_file];
138 1.1 cgd struct stat s;
139 1.1 cgd time_t t;
140 1.1 cgd
141 1.15 jwise sprintf(buf, SF, game);
142 1.15 jwise time(&t);
143 1.14 jsm setegid(egid);
144 1.14 jsm if (stat(buf, &s) < 0) {
145 1.14 jsm setegid(gid);
146 1.1 cgd return 0;
147 1.14 jsm }
148 1.1 cgd if (s.st_mtime < t - 60*60*2) { /* 2 hours */
149 1.15 jwise unlink(buf);
150 1.15 jwise sprintf(buf, LF, game);
151 1.15 jwise unlink(buf);
152 1.14 jsm setegid(gid);
153 1.1 cgd return 0;
154 1.14 jsm } else {
155 1.14 jsm setegid(gid);
156 1.1 cgd return 1;
157 1.14 jsm }
158 1.1 cgd }
159 1.1 cgd
160 1.6 christos int
161 1.15 jwise sync_open(void)
162 1.1 cgd {
163 1.14 jsm struct stat tmp;
164 1.1 cgd if (sync_fp != NULL)
165 1.15 jwise fclose(sync_fp);
166 1.15 jwise sprintf(sync_lock, LF, game);
167 1.15 jwise sprintf(sync_file, SF, game);
168 1.14 jsm setegid(egid);
169 1.14 jsm if (stat(sync_file, &tmp) < 0) {
170 1.14 jsm mode_t omask = umask(002);
171 1.1 cgd sync_fp = fopen(sync_file, "w+");
172 1.15 jwise umask(omask);
173 1.1 cgd } else
174 1.1 cgd sync_fp = fopen(sync_file, "r+");
175 1.14 jsm setegid(gid);
176 1.1 cgd if (sync_fp == NULL)
177 1.1 cgd return -1;
178 1.1 cgd sync_seek = 0;
179 1.1 cgd return 0;
180 1.1 cgd }
181 1.1 cgd
182 1.6 christos void
183 1.15 jwise sync_close(int remove)
184 1.1 cgd {
185 1.1 cgd if (sync_fp != 0)
186 1.15 jwise fclose(sync_fp);
187 1.14 jsm if (remove) {
188 1.14 jsm setegid(egid);
189 1.15 jwise unlink(sync_file);
190 1.14 jsm setegid(gid);
191 1.14 jsm }
192 1.1 cgd }
193 1.1 cgd
194 1.6 christos void
195 1.15 jwise Write(int type, struct ship *ship, long a, long b, long c, long d)
196 1.1 cgd {
197 1.6 christos
198 1.15 jwise sprintf(sync_bp, "%d %d 0 %ld %ld %ld %ld\n",
199 1.11 hubertf type, ship->file->index, a, b, c, d);
200 1.11 hubertf while (*sync_bp++)
201 1.11 hubertf ;
202 1.11 hubertf sync_bp--;
203 1.11 hubertf if (sync_bp >= &sync_buf[sizeof sync_buf])
204 1.11 hubertf abort();
205 1.15 jwise sync_update(type, ship, NULL, a, b, c, d);
206 1.11 hubertf }
207 1.11 hubertf
208 1.11 hubertf void
209 1.15 jwise Writestr(int type, struct ship *ship, const char *a)
210 1.11 hubertf {
211 1.15 jwise sprintf(sync_bp, "%d %d 1 %s\n", type, ship->file->index, a);
212 1.1 cgd while (*sync_bp++)
213 1.1 cgd ;
214 1.1 cgd sync_bp--;
215 1.1 cgd if (sync_bp >= &sync_buf[sizeof sync_buf])
216 1.1 cgd abort();
217 1.15 jwise sync_update(type, ship, a, 0, 0, 0, 0);
218 1.1 cgd }
219 1.1 cgd
220 1.6 christos int
221 1.15 jwise Sync(void)
222 1.1 cgd {
223 1.1 cgd sig_t sighup, sigint;
224 1.6 christos int n;
225 1.4 cgd int type, shipnum, isstr;
226 1.11 hubertf char *astr;
227 1.4 cgd long a, b, c, d;
228 1.1 cgd char buf[80];
229 1.1 cgd char erred = 0;
230 1.1 cgd
231 1.1 cgd sighup = signal(SIGHUP, SIG_IGN);
232 1.1 cgd sigint = signal(SIGINT, SIG_IGN);
233 1.1 cgd for (n = TIMEOUT; --n >= 0;) {
234 1.1 cgd #ifdef LOCK_EX
235 1.1 cgd if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
236 1.1 cgd break;
237 1.1 cgd if (errno != EWOULDBLOCK)
238 1.1 cgd return -1;
239 1.1 cgd #else
240 1.14 jsm setegid(egid);
241 1.14 jsm if (link(sync_file, sync_lock) >= 0) {
242 1.14 jsm setegid(gid);
243 1.1 cgd break;
244 1.14 jsm }
245 1.14 jsm setegid(gid);
246 1.1 cgd if (errno != EEXIST)
247 1.1 cgd return -1;
248 1.1 cgd #endif
249 1.1 cgd sleep(1);
250 1.1 cgd }
251 1.1 cgd if (n <= 0)
252 1.1 cgd return -1;
253 1.15 jwise fseek(sync_fp, sync_seek, SEEK_SET);
254 1.1 cgd for (;;) {
255 1.1 cgd switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
256 1.1 cgd case 3:
257 1.1 cgd break;
258 1.1 cgd case EOF:
259 1.1 cgd goto out;
260 1.1 cgd default:
261 1.1 cgd goto bad;
262 1.1 cgd }
263 1.1 cgd if (shipnum < 0 || shipnum >= cc->vessels)
264 1.1 cgd goto bad;
265 1.1 cgd if (isstr != 0 && isstr != 1)
266 1.1 cgd goto bad;
267 1.1 cgd if (isstr) {
268 1.6 christos char *p;
269 1.1 cgd for (p = buf;;) {
270 1.1 cgd switch (*p++ = getc(sync_fp)) {
271 1.1 cgd case '\n':
272 1.1 cgd p--;
273 1.1 cgd case EOF:
274 1.1 cgd break;
275 1.1 cgd default:
276 1.1 cgd if (p >= buf + sizeof buf)
277 1.1 cgd p--;
278 1.1 cgd continue;
279 1.1 cgd }
280 1.1 cgd break;
281 1.1 cgd }
282 1.1 cgd *p = 0;
283 1.1 cgd for (p = buf; *p == ' '; p++)
284 1.1 cgd ;
285 1.11 hubertf astr = p;
286 1.11 hubertf a = b = c = d = 0;
287 1.11 hubertf } else {
288 1.6 christos if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d) != 4)
289 1.1 cgd goto bad;
290 1.11 hubertf astr = NULL;
291 1.11 hubertf }
292 1.11 hubertf if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
293 1.1 cgd goto bad;
294 1.1 cgd }
295 1.1 cgd bad:
296 1.1 cgd erred++;
297 1.1 cgd out:
298 1.1 cgd if (!erred && sync_bp != sync_buf) {
299 1.15 jwise fseek(sync_fp, 0L, SEEK_END);
300 1.15 jwise fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
301 1.1 cgd sync_fp);
302 1.15 jwise fflush(sync_fp);
303 1.1 cgd sync_bp = sync_buf;
304 1.1 cgd }
305 1.1 cgd sync_seek = ftell(sync_fp);
306 1.1 cgd #ifdef LOCK_EX
307 1.15 jwise flock(fileno(sync_fp), LOCK_UN);
308 1.1 cgd #else
309 1.14 jsm setegid(egid);
310 1.15 jwise unlink(sync_lock);
311 1.14 jsm setegid(gid);
312 1.1 cgd #endif
313 1.15 jwise signal(SIGHUP, sighup);
314 1.15 jwise signal(SIGINT, sigint);
315 1.1 cgd return erred ? -1 : 0;
316 1.1 cgd }
317 1.1 cgd
318 1.16 jwise static int
319 1.15 jwise sync_update(int type, struct ship *ship, const char *astr, long a, long b, long c, long d)
320 1.1 cgd {
321 1.1 cgd switch (type) {
322 1.1 cgd case W_DBP: {
323 1.6 christos struct BP *p = &ship->file->DBP[a];
324 1.1 cgd p->turnsent = b;
325 1.1 cgd p->toship = SHIP(c);
326 1.1 cgd p->mensent = d;
327 1.1 cgd break;
328 1.1 cgd }
329 1.1 cgd case W_OBP: {
330 1.6 christos struct BP *p = &ship->file->OBP[a];
331 1.1 cgd p->turnsent = b;
332 1.1 cgd p->toship = SHIP(c);
333 1.1 cgd p->mensent = d;
334 1.1 cgd break;
335 1.1 cgd }
336 1.1 cgd case W_FOUL: {
337 1.6 christos struct snag *p = &ship->file->foul[a];
338 1.1 cgd if (SHIP(a)->file->dir == 0)
339 1.1 cgd break;
340 1.1 cgd if (p->sn_count++ == 0)
341 1.1 cgd p->sn_turn = turn;
342 1.1 cgd ship->file->nfoul++;
343 1.1 cgd break;
344 1.1 cgd }
345 1.1 cgd case W_GRAP: {
346 1.6 christos struct snag *p = &ship->file->grap[a];
347 1.1 cgd if (SHIP(a)->file->dir == 0)
348 1.1 cgd break;
349 1.1 cgd if (p->sn_count++ == 0)
350 1.1 cgd p->sn_turn = turn;
351 1.1 cgd ship->file->ngrap++;
352 1.1 cgd break;
353 1.1 cgd }
354 1.1 cgd case W_UNFOUL: {
355 1.6 christos struct snag *p = &ship->file->foul[a];
356 1.9 veego if (p->sn_count > 0) {
357 1.1 cgd if (b) {
358 1.1 cgd ship->file->nfoul -= p->sn_count;
359 1.1 cgd p->sn_count = 0;
360 1.1 cgd } else {
361 1.1 cgd ship->file->nfoul--;
362 1.1 cgd p->sn_count--;
363 1.1 cgd }
364 1.9 veego }
365 1.1 cgd break;
366 1.1 cgd }
367 1.1 cgd case W_UNGRAP: {
368 1.6 christos struct snag *p = &ship->file->grap[a];
369 1.9 veego if (p->sn_count > 0) {
370 1.1 cgd if (b) {
371 1.1 cgd ship->file->ngrap -= p->sn_count;
372 1.1 cgd p->sn_count = 0;
373 1.1 cgd } else {
374 1.1 cgd ship->file->ngrap--;
375 1.1 cgd p->sn_count--;
376 1.1 cgd }
377 1.9 veego }
378 1.1 cgd break;
379 1.1 cgd }
380 1.1 cgd case W_SIGNAL:
381 1.9 veego if (mode == MODE_PLAYER) {
382 1.1 cgd if (nobells)
383 1.11 hubertf Signal("$$: %s", ship, astr);
384 1.1 cgd else
385 1.11 hubertf Signal("\7$$: %s", ship, astr);
386 1.9 veego }
387 1.1 cgd break;
388 1.1 cgd case W_CREW: {
389 1.6 christos struct shipspecs *s = ship->specs;
390 1.1 cgd s->crew1 = a;
391 1.1 cgd s->crew2 = b;
392 1.1 cgd s->crew3 = c;
393 1.1 cgd break;
394 1.1 cgd }
395 1.1 cgd case W_CAPTAIN:
396 1.15 jwise strncpy(ship->file->captain, astr,
397 1.1 cgd sizeof ship->file->captain - 1);
398 1.1 cgd ship->file->captain[sizeof ship->file->captain - 1] = 0;
399 1.1 cgd break;
400 1.1 cgd case W_CAPTURED:
401 1.1 cgd if (a < 0)
402 1.1 cgd ship->file->captured = 0;
403 1.1 cgd else
404 1.1 cgd ship->file->captured = SHIP(a);
405 1.1 cgd break;
406 1.1 cgd case W_CLASS:
407 1.1 cgd ship->specs->class = a;
408 1.1 cgd break;
409 1.1 cgd case W_DRIFT:
410 1.1 cgd ship->file->drift = a;
411 1.1 cgd break;
412 1.1 cgd case W_EXPLODE:
413 1.1 cgd if ((ship->file->explode = a) == 2)
414 1.1 cgd ship->file->dir = 0;
415 1.1 cgd break;
416 1.1 cgd case W_FS:
417 1.1 cgd ship->file->FS = a;
418 1.1 cgd break;
419 1.1 cgd case W_GUNL: {
420 1.6 christos struct shipspecs *s = ship->specs;
421 1.1 cgd s->gunL = a;
422 1.1 cgd s->carL = b;
423 1.1 cgd break;
424 1.1 cgd }
425 1.1 cgd case W_GUNR: {
426 1.6 christos struct shipspecs *s = ship->specs;
427 1.1 cgd s->gunR = a;
428 1.1 cgd s->carR = b;
429 1.1 cgd break;
430 1.1 cgd }
431 1.1 cgd case W_HULL:
432 1.1 cgd ship->specs->hull = a;
433 1.1 cgd break;
434 1.1 cgd case W_MOVE:
435 1.15 jwise strncpy(ship->file->movebuf, astr,
436 1.1 cgd sizeof ship->file->movebuf - 1);
437 1.1 cgd ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
438 1.1 cgd break;
439 1.1 cgd case W_PCREW:
440 1.1 cgd ship->file->pcrew = a;
441 1.1 cgd break;
442 1.1 cgd case W_POINTS:
443 1.1 cgd ship->file->points = a;
444 1.1 cgd break;
445 1.1 cgd case W_QUAL:
446 1.1 cgd ship->specs->qual = a;
447 1.1 cgd break;
448 1.1 cgd case W_RIGG: {
449 1.6 christos struct shipspecs *s = ship->specs;
450 1.1 cgd s->rig1 = a;
451 1.1 cgd s->rig2 = b;
452 1.1 cgd s->rig3 = c;
453 1.1 cgd s->rig4 = d;
454 1.1 cgd break;
455 1.1 cgd }
456 1.1 cgd case W_RIG1:
457 1.1 cgd ship->specs->rig1 = a;
458 1.1 cgd break;
459 1.1 cgd case W_RIG2:
460 1.1 cgd ship->specs->rig2 = a;
461 1.1 cgd break;
462 1.1 cgd case W_RIG3:
463 1.1 cgd ship->specs->rig3 = a;
464 1.1 cgd break;
465 1.1 cgd case W_RIG4:
466 1.1 cgd ship->specs->rig4 = a;
467 1.1 cgd break;
468 1.1 cgd case W_COL:
469 1.1 cgd ship->file->col = a;
470 1.1 cgd break;
471 1.1 cgd case W_DIR:
472 1.1 cgd ship->file->dir = a;
473 1.1 cgd break;
474 1.1 cgd case W_ROW:
475 1.1 cgd ship->file->row = a;
476 1.1 cgd break;
477 1.1 cgd case W_SINK:
478 1.1 cgd if ((ship->file->sink = a) == 2)
479 1.1 cgd ship->file->dir = 0;
480 1.1 cgd break;
481 1.1 cgd case W_STRUCK:
482 1.1 cgd ship->file->struck = a;
483 1.1 cgd break;
484 1.1 cgd case W_TA:
485 1.1 cgd ship->specs->ta = a;
486 1.1 cgd break;
487 1.1 cgd case W_ALIVE:
488 1.1 cgd alive = 1;
489 1.1 cgd break;
490 1.1 cgd case W_TURN:
491 1.1 cgd turn = a;
492 1.1 cgd break;
493 1.1 cgd case W_WIND:
494 1.1 cgd winddir = a;
495 1.1 cgd windspeed = b;
496 1.1 cgd break;
497 1.1 cgd case W_BEGIN:
498 1.15 jwise strcpy(ship->file->captain, "begin");
499 1.1 cgd people++;
500 1.1 cgd break;
501 1.1 cgd case W_END:
502 1.1 cgd *ship->file->captain = 0;
503 1.1 cgd ship->file->points = 0;
504 1.1 cgd people--;
505 1.1 cgd break;
506 1.1 cgd case W_DDEAD:
507 1.1 cgd hasdriver = 0;
508 1.1 cgd break;
509 1.1 cgd default:
510 1.1 cgd fprintf(stderr, "sync_update: unknown type %d\r\n", type);
511 1.1 cgd return -1;
512 1.1 cgd }
513 1.1 cgd return 0;
514 1.1 cgd }
515