From 1c42ea16f8f04f01d757035d69494da12b19bb69 Mon Sep 17 00:00:00 2001 From: dholland Date: Sat, 14 Mar 2009 23:47:18 +0000 Subject: chdir to /var/games/sail while running, to simplify name handling. --- sail/main.c | 9 +++++++-- sail/pathnames.h | 9 ++++++--- sail/sync.c | 55 +++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 54 insertions(+), 19 deletions(-) (limited to 'sail') diff --git a/sail/main.c b/sail/main.c index 221a9def..f7911b64 100644 --- a/sail/main.c +++ b/sail/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.23 2009/03/02 06:54:01 dholland Exp $ */ +/* $NetBSD: main.c,v 1.24 2009/03/14 23:47:18 dholland Exp $ */ /* * Copyright (c) 1983, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ #if 0 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: main.c,v 1.23 2009/03/02 06:54:01 dholland Exp $"); +__RCSID("$NetBSD: main.c,v 1.24 2009/03/14 23:47:18 dholland Exp $"); #endif #endif /* not lint */ @@ -52,6 +52,7 @@ __RCSID("$NetBSD: main.c,v 1.23 2009/03/02 06:54:01 dholland Exp $"); #include #include #include "extern.h" +#include "pathnames.h" #include "restart.h" int @@ -70,6 +71,10 @@ main(int argc, char **argv) exit(1); close(fd); + if (chdir(_PATH_SAILDIR) < 0) { + err(1, "%s", _PATH_SAILDIR); + } + srandom((u_long)time(NULL)); if ((p = strrchr(*argv, '/')) != NULL) diff --git a/sail/pathnames.h b/sail/pathnames.h index c3ea1d03..65bc3641 100644 --- a/sail/pathnames.h +++ b/sail/pathnames.h @@ -1,4 +1,4 @@ -/* $NetBSD: pathnames.h,v 1.5 2003/08/07 09:37:43 agc Exp $ */ +/* $NetBSD: pathnames.h,v 1.6 2009/03/14 23:47:18 dholland Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -32,5 +32,8 @@ */ #define _PATH_LOGFILE "/var/games/saillog" -#define _PATH_SYNC "/var/games/sail/#sailsink.%d" -#define _PATH_LOCK "/var/games/sail/#saillock.%d" +#define _PATH_SAILDIR "/var/games/sail" + +/* in _PATH_SAILDIR */ +#define _FILE_SYNC "#sailsink.%d" +#define _FILE_LOCK "#saillock.%d" diff --git a/sail/sync.c b/sail/sync.c index 4b8a0d31..5362ab95 100644 --- a/sail/sync.c +++ b/sail/sync.c @@ -1,4 +1,4 @@ -/* $NetBSD: sync.c,v 1.32 2009/03/14 23:12:20 dholland Exp $ */ +/* $NetBSD: sync.c,v 1.33 2009/03/14 23:47:18 dholland Exp $ */ /* * Copyright (c) 1983, 1993 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: sync.c,v 1.32 2009/03/14 23:12:20 dholland Exp $"); +__RCSID("$NetBSD: sync.c,v 1.33 2009/03/14 23:47:18 dholland Exp $"); #endif #endif /* not lint */ @@ -42,6 +42,7 @@ __RCSID("$NetBSD: sync.c,v 1.32 2009/03/14 23:12:20 dholland Exp $"); #include #include +#include #include #include #include @@ -141,15 +142,29 @@ static void Writestr(int, struct ship *, const char *); static int sync_update(int, struct ship *, const char *, long, long, long, long); -static const char SF[] = _PATH_SYNC; -static const char LF[] = _PATH_LOCK; static char sync_buf[BUFSIZE]; static char *sync_bp = sync_buf; -static char sync_lock[sizeof LF+2]; -static char sync_file[sizeof SF+2]; static long sync_seek; static FILE *sync_fp; +static const char * +get_sync_file(int scenario_number) +{ + static char sync_file[NAME_MAX]; + + snprintf(sync_file, sizeof(sync_file), _FILE_SYNC, scenario_number); + return sync_file; +} + +static const char * +get_lock_file(int scenario_number) +{ + static char sync_lock[NAME_MAX]; + + snprintf(sync_lock, sizeof(sync_lock), _FILE_LOCK, scenario_number); + return sync_lock; +} + void fmtship(char *buf, size_t len, const char *fmt, struct ship *ship) { @@ -205,21 +220,21 @@ makemsg(struct ship *from, const char *fmt, ...) int sync_exists(int gamenum) { - char buf[sizeof sync_file]; + const char *path; struct stat s; time_t t; - snprintf(buf, sizeof(buf), SF, gamenum); + path = get_sync_file(gamenum); time(&t); setegid(egid); - if (stat(buf, &s) < 0) { + if (stat(path, &s) < 0) { setegid(gid); return 0; } if (s.st_mtime < t - 60*60*2) { /* 2 hours */ - unlink(buf); - snprintf(buf, sizeof(buf), LF, gamenum); - unlink(buf); + unlink(path); + path = get_lock_file(gamenum); + unlink(path); setegid(gid); return 0; } else { @@ -231,11 +246,14 @@ sync_exists(int gamenum) int sync_open(void) { + const char *sync_file; + const char *sync_lock; struct stat tmp; + if (sync_fp != NULL) fclose(sync_fp); - snprintf(sync_lock, sizeof(sync_lock), LF, game); - snprintf(sync_file, sizeof(sync_file), SF, game); + sync_file = get_sync_file(game); + sync_lock = get_lock_file(game); setegid(egid); if (stat(sync_file, &tmp) < 0) { mode_t omask = umask(002); @@ -253,9 +271,12 @@ sync_open(void) void sync_close(int doremove) { + const char *sync_file; + if (sync_fp != 0) fclose(sync_fp); if (doremove) { + sync_file = get_sync_file(game); setegid(egid); unlink(sync_file); setegid(gid); @@ -303,6 +324,10 @@ Sync(void) long a, b, c, d; char buf[80]; char erred = 0; +#ifndef LOCK_EX + const char *sync_file; + const char *sync_lock; +#endif sighup = signal(SIGHUP, SIG_IGN); sigint = signal(SIGINT, SIG_IGN); @@ -313,6 +338,8 @@ Sync(void) if (errno != EWOULDBLOCK) return -1; #else + sync_file = get_sync_file(game); + sync_lock = get_lock_file(game); setegid(egid); if (link(sync_file, sync_lock) >= 0) { setegid(gid); -- cgit v1.2.3-56-ge451