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