From 0de660756306b48a3e61d2012dc04c1ec2b2fa5d Mon Sep 17 00:00:00 2001 From: kcwu Date: Sat, 13 Jun 2009 17:46:14 +0000 Subject: * CPULIMIT macro is deprecated * new CPULIMIT_PER_DAY macro: cpu limit control by day git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4600 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/cal.c | 4 ++-- mbbsd/gamble.c | 2 +- mbbsd/mbbsd.c | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/mbbsd/cal.c b/mbbsd/cal.c index 932b385d..1abc5421 100644 --- a/mbbsd/cal.c +++ b/mbbsd/cal.c @@ -675,8 +675,8 @@ p_sysinfo(void) (long int)ru.ru_utime.tv_usec, (long int)ru.ru_stime.tv_sec, (long int)ru.ru_stime.tv_usec); -#ifdef CPULIMIT - prints(" (limit %d secs)", (int)(CPULIMIT * 60)); +#ifdef CPULIMIT_PER_DAY + prints(" (limit %d secs per day)", CPULIMIT_PER_DAY); #endif outs("\n特別參數:" #ifdef CRITICAL_MEMORY diff --git a/mbbsd/gamble.c b/mbbsd/gamble.c index 0b17a9c6..93388e1f 100644 --- a/mbbsd/gamble.c +++ b/mbbsd/gamble.c @@ -298,7 +298,7 @@ openticket(int bid) close(0); close(1); setproctitle("open ticket"); -#ifdef CPULIMIT +#ifdef CPULIMIT_PER_DAY { struct rlimit rml; rml.rlim_cur = RLIM_INFINITY; diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c index e8881b99..bd2a7944 100644 --- a/mbbsd/mbbsd.c +++ b/mbbsd/mbbsd.c @@ -362,6 +362,35 @@ abort_bbs_debug(int sig) exit(0); } +#ifdef CPULIMIT_PER_DAY +static void +signal_xcpu_handler(int sig) +{ + static time_t last_time_exceeded = 0; + struct rlimit rml; + int margin_for_handler = 5; + bool give_more_time = true; + + // 不用 (time(0) - login_start_time) 來平均, 避免用好幾天之後突然狂吃 cpu 的狀況. + if (last_time_exceeded != 0 && time(0) - last_time_exceeded < 86400) + give_more_time = false; + + getrlimit(RLIMIT_CPU, &rml); + + // reach hard limit + if (rml.rlim_cur + CPULIMIT_PER_DAY + margin_for_handler > rml.rlim_max) + give_more_time = false; + + if (!give_more_time) { + abort_bbs_debug(sig); + assert(0); // shout not reach here + } + + rml.rlim_cur += CPULIMIT_PER_DAY; + setrlimit(RLIMIT_CPU, &rml); +} +#endif + /* 登錄 BBS 程式 */ static void mysrand(void) @@ -1411,10 +1440,10 @@ do_term_init(enum TermMode term_mode, int w, int h) static int start_client(struct ProgramOption *option) { -#ifdef CPULIMIT +#ifdef CPULIMIT_PER_DAY struct rlimit rml; - rml.rlim_cur = CPULIMIT * 60 - 5; - rml.rlim_max = CPULIMIT * 60; + getrlimit(RLIMIT_CPU, &rml); + rml.rlim_cur = CPULIMIT_PER_DAY; setrlimit(RLIMIT_CPU, &rml); #endif @@ -1435,7 +1464,9 @@ start_client(struct ProgramOption *option) Signal(SIGFPE, abort_bbs_debug); Signal(SIGBUS, abort_bbs_debug); Signal(SIGSEGV, abort_bbs_debug); - Signal(SIGXCPU, abort_bbs_debug); +#ifdef CPULIMIT_PER_DAY + Signal(SIGXCPU, signal_xcpu_handler); +#endif signal_restart(SIGUSR1, talk_request); signal_restart(SIGUSR2, write_request); -- cgit v1.2.3