From 1a72d7c5377a1395e623b68558a59ece9c277c98 Mon Sep 17 00:00:00 2001 From: joonicks Date: Wed, 12 Dec 2018 14:30:01 +0100 Subject: [PATCH] safer sessionsaving --- VERSIONS | 3 +++ src/core.c | 5 ++++- src/defines.h | 10 ++++++++++ src/io.c | 2 +- src/main.c | 8 +++++--- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/VERSIONS b/VERSIONS index 02173b3..9602ccf 100644 --- a/VERSIONS +++ b/VERSIONS @@ -1,5 +1,8 @@ 3.1 -- WORK IN PROGRESS (~May, 2018) + * Changed: Session config is saved to "filename.sessiontemp" and once the save is complete + its moved to "filename.session", just in case something happens that might create a corrupt + session file (eg, SIGSEGV). * Added: SERVERSILENCETIMEOUT, maximum idle timer for server connection. in case of lost FIN packets * Fixed: Compiles clean with -std=c89 * Added: $guid to esay variables diff --git a/src/core.c b/src/core.c index ae3002a..0366b09 100644 --- a/src/core.c +++ b/src/core.c @@ -148,7 +148,8 @@ int write_session(void) UniVar *varval; int j,sf; - if ((sf = open(SESSIONFILE,O_WRONLY|O_CREAT|O_TRUNC,NEWFILEMODE)) < 0) + /* save to filename.sessiontemp at first, in case SIGSEGV happens */ + if ((sf = open(SESSIONTEMPFILE,O_WRONLY|O_CREAT|O_TRUNC,NEWFILEMODE)) < 0) return(FALSE); #ifdef RAWDNS @@ -311,6 +312,8 @@ int write_session(void) } close(sf); + unlink(SESSIONFILE); + rename(SESSIONTEMPFILE,SESSIONFILE); return(TRUE); } diff --git a/src/defines.h b/src/defines.h index 633f47d..e253e13 100644 --- a/src/defines.h +++ b/src/defines.h @@ -30,6 +30,16 @@ #define LS /* nothing */ #endif /* LIBRARY */ +/* + * startup == + */ +#define STARTUP_RUNNING 0 +#define STARTUP_ECHOTOCONSOLE (startup > 1 && startup < 1000) +#define STARTUP_COMMANDANDEXIT 3 +#define STARTUP_TESTRUN 6 +#define STARTUP_NORMALSTART 1000 +#define STARTUP_SIGSEGV 31337 + /* * Channel Crap */ diff --git a/src/io.c b/src/io.c index 55983f9..71d81c9 100644 --- a/src/io.c +++ b/src/io.c @@ -355,7 +355,7 @@ void to_user_q(const char *target, const char *format, ...) stringcat(message,"\n"); - if (startup > 0) + if (STARTUP_ECHOTOCONSOLE) { write(1,message,strlen(message)); return; diff --git a/src/main.c b/src/main.c index f46ed5f..2ea05bf 100644 --- a/src/main.c +++ b/src/main.c @@ -448,6 +448,7 @@ void sig_segv(int crap, siginfo_t *si, void *uap) greg_t *rsp,*rip; /* general registers */ time(&now); + startup = STARTUP_SIGSEGV; debug("(sigsegv) trying to access "mx_pfmt"\n",(mx_ptr)si->si_addr); mctx = &((ucontext_t *)uap)->uc_mcontext; @@ -919,6 +920,7 @@ int main(int argc, char **argv, char **envp) #endif uptime = time(&now); + startup = STARTUP_NORMALSTART; if ((getuid() == 0) || (geteuid() == 0)) { @@ -1060,7 +1062,7 @@ int main(int argc, char **argv, char **envp) to_file(1,"error: Missing argument for -e \n"); _exit(0); case 't': - startup = 666; + startup = STARTUP_TESTRUN; break; case 'f': if (opt[2] != 0) @@ -1276,12 +1278,12 @@ int main(int argc, char **argv, char **envp) monitor_fs(executable); #endif - if (startup == 666) + if (startup == STARTUP_TESTRUN) { to_file(1,"init: test run completed, exiting...\n"); _exit(0); } - startup = FALSE; + startup = STARTUP_RUNNING; #ifdef DEBUG debug("(main) entering doit()...\n"); #endif