]> git.pond.sub.org Git - empserver/commitdiff
Finally merge the journal patch:
authorMarkus Armbruster <armbru@pond.sub.org>
Thu, 8 Jun 2006 20:11:26 +0000 (20:11 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Thu, 8 Jun 2006 20:11:26 +0000 (20:11 +0000)
(keep_journal): New econfig key.
(player_main): Log player login and logout.
(recvclient): Log player input.
(ef_open_srv, ef_close_srv): Log startup and shutdown.
(update_main): Log update.

Support the common SIGHUP log rotation idiom:
(empth_wait_for_shutdown, empth_wait_for_signal): Rename.
[EMPTH_LWP, EMPTH_POSIX] (empth_init, empth_wait_for_signal): Wait for
SIGHUP as well.
(main) [SIGHUP]: Reopen journal when empth_wait_for_signal() returns
SIGHUP.

13 files changed:
include/econfig-spec.h
include/empthread.h
include/journal.h [new file with mode: 0644]
src/lib/common/journal.c [new file with mode: 0644]
src/lib/empthread/lwp.c
src/lib/empthread/ntthread.c
src/lib/empthread/pthread.c
src/lib/global/constants.c
src/lib/player/player.c
src/lib/player/recvclient.c
src/lib/subs/fileinit.c
src/lib/update/main.c
src/server/main.c

index c19e5c268dc113c8d27862f4ded97c34a2dcfd3e..3983ad31df0d5d5b0d90e50a5492abbe75a248f9 100644 (file)
@@ -88,6 +88,8 @@ EMPCFBOTH("listen_addr", listen_addr, char *, NSC_STRING, KM_INTERNAL,
 EMPCF_COMMENT("# \"\" listens on all, localhost just on the loopback interface")
 EMPCFBOTH("port", loginport, char *, NSC_STRING, KM_INTERNAL,
     "TCP port the server will bind")
+EMPCFBOTH("keep_journal", keep_journal, int, NSC_INT, KM_INTERNAL,
+    "Enable journal log file")
 EMPCFBOTH("privname", privname, char *, NSC_STRING, 0,
     "Name of the deity")
 EMPCFBOTH("privlog", privlog, char *, NSC_STRING, 0,
index 172e89a0d9917405b547620fd7ae693eee27b6d3..00d6a05425b2dd2a14b72e0eb048568d3b79afe6 100644 (file)
@@ -188,10 +188,9 @@ void empth_wakeup(empth_t *thread);
 void empth_sleep(time_t until);
 
 /*
- * Wait for some implementation-defined external shutdown signal.
- * Return a non-negative number identifying the signal.
+ * Wait for signal, return the signal number
  */
-int empth_wait_for_shutdown(void);
+int empth_wait_for_signal(void);
 
 /*
  * Create a semaphore.
diff --git a/include/journal.h b/include/journal.h
new file mode 100644 (file)
index 0000000..61d4170
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  Empire - A multi-player, client/server Internet based war game.
+ *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                           Ken Stevens, Steve McClure
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  ---
+ *
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
+ *
+ *  ---
+ *
+ *  journal.h: Log a journal of events to a file
+ * 
+ *  Known contributors to this file:
+ *     Markus Armbruster, 2004-2006
+ */
+
+#ifndef JOURNAL_H
+#define JOURNAL_H
+
+int journal_open(void);
+int journal_close(void);
+void journal_entry(char *fmt, ...);
+
+int journal_startup(void);
+void journal_shutdown(void);
+void journal_login(void);
+void journal_logout(void);
+void journal_input(char *input);
+void journal_update(int);
+
+#endif
diff --git a/src/lib/common/journal.c b/src/lib/common/journal.c
new file mode 100644 (file)
index 0000000..31124de
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ *  Empire - A multi-player, client/server Internet based war game.
+ *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                           Ken Stevens, Steve McClure
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  ---
+ *
+ *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
+ *  related information and legal notices. It is expected that any future
+ *  projects/authors will amend these files as needed.
+ *
+ *  ---
+ *
+ *  journal.c: Log a journal of events to a file
+ * 
+ *  Known contributors to this file:
+ *     Markus Armbruster, 2004-2006
+ */
+
+/*
+ * Journal file format: each line logs an event, and looks like this:
+ *
+ *     TIME THREAD EVENT DATA
+ *
+ * Events and their data are:
+ *
+ *     startup
+ *     shutdown
+ *     login CNUM HOSTADDR USER
+ *     logout CNUM
+ *     input INPUT
+ *     update ETU
+ */
+
+#include <config.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include "misc.h"
+#include "empthread.h"
+#include "journal.h"
+#include "optlist.h"
+#include "player.h"
+#include "prototypes.h"
+
+static char journal_fname[] = "journal.log";
+static FILE *journal;
+
+int
+journal_open(void)
+{
+    journal = fopen(journal_fname, "a+");
+    return journal ? 0 : -1;
+}
+
+int
+journal_close(void)
+{
+    FILE *j = journal;
+    journal = NULL;
+    return j ? fclose(j) : 0;
+}
+
+void
+journal_entry(char *fmt, ...)
+{
+    static char buf[1024];
+    va_list ap;
+    time_t now;
+    int n, i, olderr;
+
+    if (journal) {
+       time(&now);
+       n = sprintf(buf, "%.24s %p ", ctime(&now), empth_self());
+       va_start(ap, fmt);
+       vsnprintf(buf + n, sizeof(buf) - n - 1, fmt, ap);
+       va_end(ap);
+
+       for (i = n; buf[i]; ++i) {
+           if (!isprint(buf[i]))
+               buf[i] = '?';   /* FIXME replace by escape */
+       }
+       buf[i++] = '\n';
+       buf[i] = 0;
+       if (fputs(buf, journal) == EOF)
+           logerror("Error writing journal (%s)", strerror(errno));
+    }
+}
+
+int
+journal_startup(void)
+{
+    if (!keep_journal)
+       return 0;
+    if (journal_open() < 0) {
+       logerror("Can't open %s (%s)", journal_fname, strerror(errno));
+       return -1;
+    }
+    journal_entry("startup");
+    return 0;
+}
+
+void
+journal_shutdown(void)
+{
+    journal_entry("shutdown");
+    journal_close();
+}
+
+void
+journal_login(void)
+{
+    journal_entry("login %d %s %s",
+                 player->cnum, player->hostaddr, player->userid);
+}
+
+void
+journal_logout(void)
+{
+    journal_entry("logout %d", player->cnum);
+}
+
+void
+journal_input(char *input)
+{
+    journal_entry("input %s", input);
+}
+
+void
+journal_update(int etu)
+{
+    journal_entry("update %d", etu);
+}
index 4643583f201069f318e1bd01d0dfe15b304b95e1..2d8736493c39eb5b20d922c2e2a4d111e474ed5f 100644 (file)
@@ -49,6 +49,7 @@ empth_init(void **ctx, int flags)
     empth_flags = flags;
     empth_init_signals();
     sigemptyset(&set);
+    sigaddset(&set, SIGHUP);
     sigaddset(&set, SIGINT);
     sigaddset(&set, SIGTERM);
     lwpInitSystem(PP_MAIN, ctx, flags, &set);
@@ -108,13 +109,14 @@ empth_sleep(time_t until)
 }
 
 int
-empth_wait_for_shutdown(void)
+empth_wait_for_signal(void)
 {
     sigset_t set;
     int sig, err;
     time_t now;
 
     sigemptyset(&set);
+    sigaddset(&set, SIGHUP);
     sigaddset(&set, SIGINT);
     sigaddset(&set, SIGTERM);
     for (;;) {
index 3942cbe6d0b8debc5a193b6ef2e8eec111d8a79d..b8228f06a346ea010d00d27f08503a2da57af1d4 100644 (file)
@@ -577,7 +577,7 @@ empth_sleep(time_t until)
 /************************
  * empth_request_shutdown
  *
- * This wakes up empth_wait_for_shutdown() so shutdown can proceed.
+ * This wakes up empth_wait_for_signal() so shutdown can proceed.
  * This is done by signalling hShutdownEvent.
  */
 void
@@ -587,7 +587,7 @@ empth_request_shutdown(void)
 }
 
 int
-empth_wait_for_shutdown(void)
+empth_wait_for_signal(void)
 {
     /* Get the MUTEX semaphore, wait the number of MS */
     WaitForSingleObject(hShutdownEvent, INFINITE);
index 6cf2ffa3ed579830a4e93fb9b54cbbe5c97fa982..4ada7f015bb56b97ec5665cd0d31ac0466851421 100644 (file)
@@ -152,6 +152,7 @@ empth_init(void **ctx_ptr, int flags)
 
     empth_init_signals();
     sigemptyset(&set);
+    sigaddset(&set, SIGHUP);
     sigaddset(&set, SIGINT);
     sigaddset(&set, SIGTERM);
     pthread_sigmask(SIG_BLOCK, &set, NULL);
@@ -379,12 +380,13 @@ empth_sleep(time_t until)
 }
 
 int
-empth_wait_for_shutdown(void)
+empth_wait_for_signal(void)
 {
     sigset_t set;
     int sig, err;
 
     sigemptyset(&set);
+    sigaddset(&set, SIGHUP);
     sigaddset(&set, SIGINT);
     sigaddset(&set, SIGTERM);
     pthread_mutex_unlock(&mtx_ctxsw);
index 4effdd647d661ba457f966c0fd7be9e2ff7880da..b911aaf5080ecdf0dc05b251dbd35b1728002b47 100644 (file)
@@ -43,6 +43,8 @@ char *privlog = "careless@invalid";
 /* Divine hosts and networks */
 char *privip = "127.0.0.1 ::1 ::ffff:127.0.0.1";
 
+int keep_journal = 0;
+
 int WORLD_X = 64;              /* World size - x */
 int WORLD_Y = 32;              /* World size - y */
 
index db7ce5a24127817601493dbfa0496de3f5c704b3..b3d4310ad8de2fdd18f584d3b1eb64f1c4821ffe 100644 (file)
@@ -50,6 +50,7 @@
 #include "subs.h"
 #include "common.h"
 #include "optlist.h"
+#include "journal.h"
 
 #if !defined(_WIN32)
 #include <unistd.h>
@@ -75,6 +76,7 @@ player_main(struct player *p)
     player = p;
     time(&player->lasttime);
     time(&player->curup);
+    journal_login();
     show_motd();
     if (init_nats() < 0) {
        pr("Server confused, try again later\n");
@@ -138,6 +140,7 @@ player_main(struct player *p)
        natp->nat_minused += 1;
     putnat(natp);
     pr("Bye-bye\n");
+    journal_logout();
 }
 
 static int
index 9a415da34a942c272f95c45f17018285a4d1eba3..a6c1d154c8deeb9b2acbcde7f02c368d112705d2 100644 (file)
@@ -37,6 +37,7 @@
 #include <stdio.h>
 #include "misc.h"
 #include "empio.h"
+#include "journal.h"
 #include "player.h"
 #include "empthread.h"
 
@@ -80,5 +81,6 @@ recvclient(char *cmd, int size)
     }
     if (player->aborted)
        return -2;
+    journal_input(cmd);
     return count;
 }
index d62421605aa26b899ef6464397679a678d186ca1..e7c3d797a02c42814d59650913cdb1ede18a691d 100644 (file)
@@ -34,6 +34,7 @@
 #include <config.h>
 
 #include "file.h"
+#include "journal.h"
 #include "prototypes.h"
 
 struct fileinit {
@@ -98,6 +99,8 @@ static void
 ef_open_srv(void)
 {
     int failed = 0;
+
+    failed |= journal_startup() < 0;
     failed |= !ef_open(EF_NATION, EFF_MEM);
     failed |= !ef_open(EF_SECTOR, EFF_MEM);
     failed |= !ef_open(EF_SHIP, EFF_MEM);
@@ -142,6 +145,7 @@ ef_close_srv(void)
     ef_close(EF_BMAP);
     ef_close(EF_LOST);
     ef_close(EF_REALM);
+    journal_shutdown();
 }
 
 static int
index c1d133b99caaf09c81fa0576e6c1bfe9b460cf60..ea1354d71204125ebed131e1a9e1f73f2a4b63a3 100644 (file)
@@ -46,6 +46,7 @@
 #include "update.h"
 #include "common.h"
 #include "optlist.h"
+#include "journal.h"
 #include "server.h"
 #include <stdlib.h>
 #if !defined(_WIN32)
@@ -92,6 +93,7 @@ update_main(void *unused)
      * happiness, and printing out the state of the nation)
      */
     logerror("production update (%d etus)", etu);
+    journal_update(etu);
     memset(pops, 0, sizeof(pops));
     memset(air_money, 0, sizeof(air_money));
     memset(sea_money, 0, sizeof(sea_money));
index 56bd3517e8ef1bb38d6ccf0535aa9cf07c213acb..525061c822f703b3c363cc635a8e16c6f3246ed6 100644 (file)
@@ -41,6 +41,7 @@
 #include <sys/ioctl.h>
 #endif
 #include <errno.h>
+#include <signal.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -57,6 +58,7 @@
 #include "misc.h"
 #include "nat.h"
 #include "file.h"
+#include "journal.h"
 #include "player.h"
 #include "empthread.h"
 #include "plane.h"
@@ -119,7 +121,7 @@ main(int argc, char **argv)
     int remove_service_set = 0;
 #endif
     char *config_file = NULL;
-    int op;
+    int op, sig;
 
 #ifdef _WIN32
 # define XOPTS "iI:rR:"
@@ -261,8 +263,19 @@ main(int argc, char **argv)
 #endif /* !_WIN32 */
     start_server(flags);
 
-    shutdwn(empth_wait_for_shutdown());
+    for (;;) {
+       sig = empth_wait_for_signal();
+#ifdef SIGHUP
+       if (sig == SIGHUP) {
+           journal_close();
+           journal_open();
+           continue;
+       }
+#endif
+       break;
+    }
 
+    shutdwn(sig);
     CANT_REACH();
     finish_server();
     return EXIT_SUCCESS;