Skip to content

Commit

Permalink
using posix timers for idle shutdown in inetd mode
Browse files Browse the repository at this point in the history
  • Loading branch information
velnias75 committed Jan 23, 2015
1 parent ef6493b commit 1c8508a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fi
# Checks for libraries.
PKG_CHECK_MODULES([POPT], [popt >= 1.10])
AC_CHECK_LIB([magic], [magic_open])
AC_CHECK_LIB([rt], [timer_create])
PKG_CHECK_MODULES([GSL], [gsl >= 1.9],
[ AM_CONDITIONAL([GSL], [ true ])
echo "[[ENVIRONMENT]]
Expand Down Expand Up @@ -131,6 +132,7 @@ AC_C_INLINE
# Checks for library functions.
AC_CHECK_FUNCS([mkdir])
AC_CHECK_FUNCS([initstate])
AC_CHECK_FUNCS([strdup])
AC_CHECK_FUNCS([strtoul])
AC_CHECK_FUNCS([memset])
AC_CHECK_FUNCS([socket])
Expand Down
61 changes: 59 additions & 2 deletions src/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,27 @@ int main(int argc, const char **argv) {
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);

#ifdef HAVE_LIBRT

timer_t timerid;
struct sigevent sev;
struct itimerspec its;

if(inetd) {

sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &timerid;

if(timer_create(CLOCK_REALTIME, &sev, &timerid) != -1) {
sigaction(SIGRTMIN, &sa, NULL);
} else {
logWarning("Could not create timer for idle shutdown");
}
}

#endif

NetMauMau::DB::SQLite::getInstance();

if(!dropPrivileges(user, grp)) {
Expand Down Expand Up @@ -607,15 +628,51 @@ int main(int argc, const char **argv) {
if(cs == Server::Game::ACCEPTED_READY) {

refuse = true;
#ifdef HAVE_LIBRT

if(inetd) {

its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;

if(timer_settime(timerid, 0, &its, NULL) == -1) {
logWarning("Could not disarm idle timer");
} else {
logInfo(NetMauMau::Common::Logger::time(TIMEFORMAT) <<
"Idle timer disarmed");
}
}

#endif

game.start(ultimate);

#ifdef HAVE_LIBRT

if(!inetd) {
#endif
updatePlayerCap(caps, game.getPlayerCount(), con,
aiOpponent);
refuse = false;
#ifdef HAVE_LIBRT
} else {
break;

its.it_value.tv_sec = 60;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;

if(timer_settime(timerid, 0, &its, NULL) == -1) {
logWarning("Could not arm idle timer");
} else {
logInfo(NetMauMau::Common::Logger::time(TIMEFORMAT) <<
"Idle timer armed");
}
}

#endif
refuse = false;
}

} else {
Expand Down

0 comments on commit 1c8508a

Please sign in to comment.