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