sync.c revision 1.3 1 /* $NetBSD: sync.c,v 1.3 1995/04/22 10:37:26 cgd 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)sync.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: sync.c,v 1.3 1995/04/22 10:37:26 cgd Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include "externs.h"
45 #include <sys/file.h>
46 #include <sys/errno.h>
47
48 #define BUFSIZE 4096
49
50 static char sync_buf[BUFSIZE];
51 static char *sync_bp = sync_buf;
52 static char sync_lock[25];
53 static char sync_file[25];
54 static long sync_seek;
55 static FILE *sync_fp;
56 #define SF "/tmp/#sailsink.%d"
57 #define LF "/tmp/#saillock.%d"
58
59 /*VARARGS3*/
60 makesignal(from, fmt, ship, a, b, c)
61 struct ship *from;
62 char *fmt;
63 register struct ship *ship;
64 {
65 char message[80];
66
67 if (ship == 0)
68 (void) sprintf(message, fmt, a, b, c);
69 else
70 (void) sprintf(message, fmt,
71 ship->shipname, colours(ship),
72 sterncolour(ship), a, b, c);
73 Write(W_SIGNAL, from, 1, (int)message, 0, 0, 0);
74 }
75
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 sync_exists(game)
79 {
80 char buf[sizeof sync_file];
81 struct stat s;
82 time_t t;
83
84 (void) sprintf(buf, SF, game);
85 (void) time(&t);
86 if (stat(buf, &s) < 0)
87 return 0;
88 if (s.st_mtime < t - 60*60*2) { /* 2 hours */
89 (void) unlink(buf);
90 (void) sprintf(buf, LF, game);
91 (void) unlink(buf);
92 return 0;
93 } else
94 return 1;
95 }
96
97 sync_open()
98 {
99 if (sync_fp != NULL)
100 (void) fclose(sync_fp);
101 (void) sprintf(sync_lock, LF, game);
102 (void) sprintf(sync_file, SF, game);
103 if (access(sync_file, 0) < 0) {
104 int omask = umask(issetuid ? 077 : 011);
105 sync_fp = fopen(sync_file, "w+");
106 (void) umask(omask);
107 } else
108 sync_fp = fopen(sync_file, "r+");
109 if (sync_fp == NULL)
110 return -1;
111 sync_seek = 0;
112 return 0;
113 }
114
115 sync_close(remove)
116 char remove;
117 {
118 if (sync_fp != 0)
119 (void) fclose(sync_fp);
120 if (remove)
121 (void) unlink(sync_file);
122 }
123
124 Write(type, ship, isstr, a, b, c, d)
125 int type;
126 struct ship *ship;
127 char isstr;
128 int a, b, c, d;
129 {
130 if (isstr)
131 (void) sprintf(sync_bp, "%d %d %d %s\n",
132 type, ship->file->index, isstr, a);
133 else
134 (void) sprintf(sync_bp, "%d %d %d %d %d %d %d\n",
135 type, ship->file->index, isstr, a, b, c, d);
136 while (*sync_bp++)
137 ;
138 sync_bp--;
139 if (sync_bp >= &sync_buf[sizeof sync_buf])
140 abort();
141 (void) sync_update(type, ship, a, b, c, d);
142 }
143
144 Sync()
145 {
146 sig_t sighup, sigint;
147 register n;
148 int type, shipnum, isstr, a, b, c, d;
149 char buf[80];
150 char erred = 0;
151 extern errno;
152
153 sighup = signal(SIGHUP, SIG_IGN);
154 sigint = signal(SIGINT, SIG_IGN);
155 for (n = TIMEOUT; --n >= 0;) {
156 #ifdef LOCK_EX
157 if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
158 break;
159 if (errno != EWOULDBLOCK)
160 return -1;
161 #else
162 if (link(sync_file, sync_lock) >= 0)
163 break;
164 if (errno != EEXIST)
165 return -1;
166 #endif
167 sleep(1);
168 }
169 if (n <= 0)
170 return -1;
171 (void) fseek(sync_fp, sync_seek, 0);
172 for (;;) {
173 switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
174 case 3:
175 break;
176 case EOF:
177 goto out;
178 default:
179 goto bad;
180 }
181 if (shipnum < 0 || shipnum >= cc->vessels)
182 goto bad;
183 if (isstr != 0 && isstr != 1)
184 goto bad;
185 if (isstr) {
186 register char *p;
187 for (p = buf;;) {
188 switch (*p++ = getc(sync_fp)) {
189 case '\n':
190 p--;
191 case EOF:
192 break;
193 default:
194 if (p >= buf + sizeof buf)
195 p--;
196 continue;
197 }
198 break;
199 }
200 *p = 0;
201 for (p = buf; *p == ' '; p++)
202 ;
203 a = (int)p;
204 b = c = d = 0;
205 } else
206 if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
207 goto bad;
208 if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0)
209 goto bad;
210 }
211 bad:
212 erred++;
213 out:
214 if (!erred && sync_bp != sync_buf) {
215 (void) fseek(sync_fp, 0L, 2);
216 (void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
217 sync_fp);
218 (void) fflush(sync_fp);
219 sync_bp = sync_buf;
220 }
221 sync_seek = ftell(sync_fp);
222 #ifdef LOCK_EX
223 (void) flock(fileno(sync_fp), LOCK_UN);
224 #else
225 (void) unlink(sync_lock);
226 #endif
227 (void) signal(SIGHUP, sighup);
228 (void) signal(SIGINT, sigint);
229 return erred ? -1 : 0;
230 }
231
232 sync_update(type, ship, a, b, c, d)
233 int type;
234 register struct ship *ship;
235 int a, b, c, d;
236 {
237 switch (type) {
238 case W_DBP: {
239 register struct BP *p = &ship->file->DBP[a];
240 p->turnsent = b;
241 p->toship = SHIP(c);
242 p->mensent = d;
243 break;
244 }
245 case W_OBP: {
246 register struct BP *p = &ship->file->OBP[a];
247 p->turnsent = b;
248 p->toship = SHIP(c);
249 p->mensent = d;
250 break;
251 }
252 case W_FOUL: {
253 register struct snag *p = &ship->file->foul[a];
254 if (SHIP(a)->file->dir == 0)
255 break;
256 if (p->sn_count++ == 0)
257 p->sn_turn = turn;
258 ship->file->nfoul++;
259 break;
260 }
261 case W_GRAP: {
262 register struct snag *p = &ship->file->grap[a];
263 if (SHIP(a)->file->dir == 0)
264 break;
265 if (p->sn_count++ == 0)
266 p->sn_turn = turn;
267 ship->file->ngrap++;
268 break;
269 }
270 case W_UNFOUL: {
271 register struct snag *p = &ship->file->foul[a];
272 if (p->sn_count > 0)
273 if (b) {
274 ship->file->nfoul -= p->sn_count;
275 p->sn_count = 0;
276 } else {
277 ship->file->nfoul--;
278 p->sn_count--;
279 }
280 break;
281 }
282 case W_UNGRAP: {
283 register struct snag *p = &ship->file->grap[a];
284 if (p->sn_count > 0)
285 if (b) {
286 ship->file->ngrap -= p->sn_count;
287 p->sn_count = 0;
288 } else {
289 ship->file->ngrap--;
290 p->sn_count--;
291 }
292 break;
293 }
294 case W_SIGNAL:
295 if (mode == MODE_PLAYER)
296 if (nobells)
297 Signal("%s (%c%c): %s", ship, a);
298 else
299 Signal("\7%s (%c%c): %s", ship, a);
300 break;
301 case W_CREW: {
302 register struct shipspecs *s = ship->specs;
303 s->crew1 = a;
304 s->crew2 = b;
305 s->crew3 = c;
306 break;
307 }
308 case W_CAPTAIN:
309 (void) strncpy(ship->file->captain, (char *)a,
310 sizeof ship->file->captain - 1);
311 ship->file->captain[sizeof ship->file->captain - 1] = 0;
312 break;
313 case W_CAPTURED:
314 if (a < 0)
315 ship->file->captured = 0;
316 else
317 ship->file->captured = SHIP(a);
318 break;
319 case W_CLASS:
320 ship->specs->class = a;
321 break;
322 case W_DRIFT:
323 ship->file->drift = a;
324 break;
325 case W_EXPLODE:
326 if ((ship->file->explode = a) == 2)
327 ship->file->dir = 0;
328 break;
329 case W_FS:
330 ship->file->FS = a;
331 break;
332 case W_GUNL: {
333 register struct shipspecs *s = ship->specs;
334 s->gunL = a;
335 s->carL = b;
336 break;
337 }
338 case W_GUNR: {
339 register struct shipspecs *s = ship->specs;
340 s->gunR = a;
341 s->carR = b;
342 break;
343 }
344 case W_HULL:
345 ship->specs->hull = a;
346 break;
347 case W_MOVE:
348 (void) strncpy(ship->file->movebuf, (char *)a,
349 sizeof ship->file->movebuf - 1);
350 ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
351 break;
352 case W_PCREW:
353 ship->file->pcrew = a;
354 break;
355 case W_POINTS:
356 ship->file->points = a;
357 break;
358 case W_QUAL:
359 ship->specs->qual = a;
360 break;
361 case W_RIGG: {
362 register struct shipspecs *s = ship->specs;
363 s->rig1 = a;
364 s->rig2 = b;
365 s->rig3 = c;
366 s->rig4 = d;
367 break;
368 }
369 case W_RIG1:
370 ship->specs->rig1 = a;
371 break;
372 case W_RIG2:
373 ship->specs->rig2 = a;
374 break;
375 case W_RIG3:
376 ship->specs->rig3 = a;
377 break;
378 case W_RIG4:
379 ship->specs->rig4 = a;
380 break;
381 case W_COL:
382 ship->file->col = a;
383 break;
384 case W_DIR:
385 ship->file->dir = a;
386 break;
387 case W_ROW:
388 ship->file->row = a;
389 break;
390 case W_SINK:
391 if ((ship->file->sink = a) == 2)
392 ship->file->dir = 0;
393 break;
394 case W_STRUCK:
395 ship->file->struck = a;
396 break;
397 case W_TA:
398 ship->specs->ta = a;
399 break;
400 case W_ALIVE:
401 alive = 1;
402 break;
403 case W_TURN:
404 turn = a;
405 break;
406 case W_WIND:
407 winddir = a;
408 windspeed = b;
409 break;
410 case W_BEGIN:
411 (void) strcpy(ship->file->captain, "begin");
412 people++;
413 break;
414 case W_END:
415 *ship->file->captain = 0;
416 ship->file->points = 0;
417 people--;
418 break;
419 case W_DDEAD:
420 hasdriver = 0;
421 break;
422 default:
423 fprintf(stderr, "sync_update: unknown type %d\r\n", type);
424 return -1;
425 }
426 return 0;
427 }
428