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