summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2012-07-19 15:27:18 -0700
committerBen Longbons <b.r.longbons@gmail.com>2012-07-19 15:36:10 -0700
commitd9628b27abfb090d854f77073bc9a1870d804164 (patch)
tree185407724c8569d84d49f1c098faf38df0df88e4
parentcf14f8379f9315859d2d829f40c219e04fdd88f5 (diff)
downloadtmwa-d9628b27abfb090d854f77073bc9a1870d804164.tar.gz
tmwa-d9628b27abfb090d854f77073bc9a1870d804164.tar.bz2
tmwa-d9628b27abfb090d854f77073bc9a1870d804164.tar.xz
tmwa-d9628b27abfb090d854f77073bc9a1870d804164.zip
Hopefully make shutdown more clean.
-rw-r--r--src/common/core.c49
-rw-r--r--src/common/core.h2
-rw-r--r--src/ladmin/ladmin.c2
-rw-r--r--src/login/login.c9
-rw-r--r--src/map/map.c12
5 files changed, 40 insertions, 34 deletions
diff --git a/src/common/core.c b/src/common/core.c
index 2b1b67c..b08276c 100644
--- a/src/common/core.c
+++ b/src/common/core.c
@@ -15,19 +15,6 @@
extern int do_init (int, char **);
extern void term_func (void);
-static void chld_proc (int UNUSED)
-{
- wait(NULL);
-}
-static void sig_proc (int UNUSED)
-{
- term_func ();
- for (int i = 0; i < fd_max; i++)
- if (session[i])
- close (i);
- exit (0);
-}
-
// Added by Gabuzomeu
//
// This is an implementation of signal() using sigaction() for portability.
@@ -40,7 +27,12 @@ sigfunc compat_signal (int signo, sigfunc func)
struct sigaction sact, oact;
sact.sa_handler = func;
- sigemptyset (&sact.sa_mask);
+ sigfillset (&sact.sa_mask);
+ sigdelset(&sact.sa_mask, SIGSEGV);
+ sigdelset(&sact.sa_mask, SIGBUS);
+ sigdelset(&sact.sa_mask, SIGTRAP);
+ sigdelset(&sact.sa_mask, SIGILL);
+ sigdelset(&sact.sa_mask, SIGFPE);
sact.sa_flags = 0;
if (sigaction (signo, &sact, &oact) < 0)
@@ -49,8 +41,29 @@ sigfunc compat_signal (int signo, sigfunc func)
return oact.sa_handler;
}
+static void chld_proc (int UNUSED)
+{
+ wait(NULL);
+}
+static void sig_proc (int UNUSED)
+{
+ for (int i = 1; i < 31; ++i)
+ compat_signal(i, SIG_IGN);
+ term_func ();
+ _exit (0);
+}
+
bool runflag = true;
+/*
+ Note about fatal signals:
+
+ Under certain circumstances,
+ the following signals MUST not be ignored:
+ SIGFPE, SIGSEGV, SIGILL
+ Unless you use SA_SIGINFO and *carefully* check the origin,
+ that means they must be SIG_DFL.
+ */
int main (int argc, char **argv)
{
/// Note that getpid() and getppid() may be very close
@@ -58,6 +71,10 @@ int main (int argc, char **argv)
do_socket ();
+ do_init (argc, argv);
+ // set up exit handlers *after* the initialization has happened.
+ // This is because term_func is likely to depend on successful init.
+
compat_signal (SIGPIPE, SIG_IGN);
compat_signal (SIGTERM, sig_proc);
compat_signal (SIGINT, sig_proc);
@@ -68,8 +85,10 @@ int main (int argc, char **argv)
compat_signal (SIGBUS, SIG_DFL);
compat_signal (SIGTRAP, SIG_DFL);
compat_signal (SIGILL, SIG_DFL);
+ compat_signal (SIGFPE, SIG_DFL);
+
+ atexit (term_func);
- do_init (argc, argv);
while (runflag)
{
do_sendrecv (do_timer (gettick_nocache ()));
diff --git a/src/common/core.h b/src/common/core.h
index 6722ca2..44473e9 100644
--- a/src/common/core.h
+++ b/src/common/core.h
@@ -13,7 +13,7 @@ extern bool runflag;
extern int do_init (int, char **);
/// Cleanup function called whenever a signal kills us
-/// NOT currently called when exit() is called
+/// or when if we manage to exit() gracefully.
extern void term_func (void);
#endif // CORE_H
diff --git a/src/ladmin/ladmin.c b/src/ladmin/ladmin.c
index 1fb2804..3728799 100644
--- a/src/ladmin/ladmin.c
+++ b/src/ladmin/ladmin.c
@@ -6472,7 +6472,5 @@ int do_init (int argc, char **argv)
Connect_login_server ();
- atexit (term_func);
-
return 0;
}
diff --git a/src/login/login.c b/src/login/login.c
index 898729b..cd46049 100644
--- a/src/login/login.c
+++ b/src/login/login.c
@@ -1031,12 +1031,6 @@ void mmo_auth_sync (void)
return;
}
-void term_func (void)
-{
- mmo_auth_sync ();
-}
-
-
// We want to sync the DB to disk as little as possible as it's fairly
// resource intensive. therefore most player-triggerable events that
// update the account DB will not immideately trigger a save. Instead
@@ -4968,7 +4962,7 @@ void save_config_in_log (void)
//--------------------------------------
// Function called at exit of the server
//--------------------------------------
-void do_final (void)
+void term_func (void)
{
int i, fd;
@@ -5040,6 +5034,5 @@ int do_init (int argc, char **argv)
("The login-server is \033[1;32mready\033[0m (Server is listening on the port %d).\n\n",
login_port);
- atexit (do_final);
return 0;
}
diff --git a/src/map/map.c b/src/map/map.c
index 4f661af..d840486 100644
--- a/src/map/map.c
+++ b/src/map/map.c
@@ -1876,7 +1876,7 @@ static void map_set_logfile (char *filename)
gettimeofday (&tv, NULL);
map_start_logfile (tv.tv_sec);
- atexit (map_close_logfile);
+
MAP_LOG ("log-start v3");
}
@@ -2063,8 +2063,10 @@ static int cleanup_sub (struct block_list *bl, va_list ap)
* map鯖終了時処理
*------------------------------------------
*/
-void do_final (void)
+void term_func (void)
{
+ map_close_logfile ();
+
int map_id, i;
for (map_id = 0; map_id < map_num; map_id++)
@@ -2121,10 +2123,6 @@ int compare_item (struct item *a, struct item *b)
(a->card[2] == b->card[2]) && (a->card[3] == b->card[3]));
}
-// TODO move shutdown stuff here
-void term_func (void)
-{
-}
/*======================================================
* Map-Server Init and Command-line Arguments [Valaris]
*------------------------------------------------------
@@ -2163,8 +2161,6 @@ int do_init (int argc, char *argv[])
script_config_read (SCRIPT_CONF_NAME);
msg_config_read (MSG_CONF_NAME);
- atexit (do_final);
-
id_db = numdb_init ();
map_db = strdb_init (16);
nick_db = strdb_init (24);