diff options
Diffstat (limited to 'src/common/core.cpp')
-rw-r--r-- | src/common/core.cpp | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/src/common/core.cpp b/src/common/core.cpp index db26e31..994de93 100644 --- a/src/common/core.cpp +++ b/src/common/core.cpp @@ -1,15 +1,18 @@ -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <signal.h> +#include "core.hpp" + #include <sys/wait.h> -#include "core.hpp" +#include <unistd.h> + +#include <csignal> +#include <cstdlib> +#include <ctime> + +#include "random.hpp" #include "socket.hpp" #include "timer.hpp" -#include "version.hpp" -#include "mt_rand.hpp" -#include "nullpo.hpp" + +#include "../poison.hpp" // Added by Gabuzomeu // @@ -17,13 +20,14 @@ // (sigaction() is POSIX; signal() is not.) Taken from Stevens' _Advanced // Programming in the UNIX Environment_. // -typedef void (*sigfunc)(int); -static sigfunc compat_signal (int signo, sigfunc func) +typedef void(*sigfunc)(int); +static +sigfunc compat_signal(int signo, sigfunc func) { struct sigaction sact, oact; sact.sa_handler = func; - sigfillset (&sact.sa_mask); + sigfillset(&sact.sa_mask); sigdelset(&sact.sa_mask, SIGSEGV); sigdelset(&sact.sa_mask, SIGBUS); sigdelset(&sact.sa_mask, SIGTRAP); @@ -31,22 +35,24 @@ static sigfunc compat_signal (int signo, sigfunc func) sigdelset(&sact.sa_mask, SIGFPE); sact.sa_flags = 0; - if (sigaction (signo, &sact, &oact) < 0) + if (sigaction(signo, &sact, &oact) < 0) return SIG_ERR; return oact.sa_handler; } -static void chld_proc (int UNUSED) +static +void chld_proc(int) { wait(NULL); } -static void sig_proc (int UNUSED) +static __attribute__((noreturn)) +void sig_proc(int) { for (int i = 1; i < 31; ++i) compat_signal(i, SIG_IGN); - term_func (); - _exit (0); + term_func(); + _exit(0); } bool runflag = true; @@ -60,34 +66,35 @@ bool runflag = true; Unless you use SA_SIGINFO and *carefully* check the origin, that means they must be SIG_DFL. */ -int main (int argc, char **argv) +int main(int argc, char **argv) { - /// Note that getpid() and getppid() may be very close - mt_seed (time (NULL) ^ (getpid () << 16) ^ (getppid () << 8)); - - do_socket (); + do_socket(); - do_init (argc, argv); + 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); - compat_signal (SIGCHLD, chld_proc); + compat_signal(SIGPIPE, SIG_IGN); + compat_signal(SIGTERM, sig_proc); + compat_signal(SIGINT, sig_proc); + compat_signal(SIGCHLD, chld_proc); // Signal to create coredumps by system when necessary (crash) - compat_signal (SIGSEGV, SIG_DFL); - compat_signal (SIGBUS, SIG_DFL); - compat_signal (SIGTRAP, SIG_DFL); - compat_signal (SIGILL, SIG_DFL); - compat_signal (SIGFPE, SIG_DFL); + compat_signal(SIGSEGV, SIG_DFL); + compat_signal(SIGBUS, SIG_DFL); + compat_signal(SIGTRAP, SIG_DFL); + compat_signal(SIGILL, SIG_DFL); + compat_signal(SIGFPE, SIG_DFL); - atexit (term_func); + atexit(term_func); while (runflag) { - do_sendrecv (do_timer (gettick_nocache ())); - do_parsepacket (); + // TODO - if timers take a long time to run, this + // may wait too long in sendrecv + tick_t now = milli_clock::now(); + interval_t next = do_timer(now); + do_sendrecv(next); + do_parsepacket(); } } |