From 891150b21a598d8f885cf537c233944dedc6e49f Mon Sep 17 00:00:00 2001 From: velnias75 Date: Wed, 25 Feb 2015 05:28:49 +0100 Subject: [PATCH] sending SIGUSR1 output to emitters terminal --- configure.ac | 5 ++ debian/rules | 6 ++- netmaumau.xinetd.in | 2 +- src/client/Makefile.am | 2 +- src/server/Makefile.am | 4 +- src/server/main.cpp | 62 ++++++++++++++++++++--- src/server/ttynamecheckdir.cpp | 91 ++++++++++++++++++++++++++++++++++ src/server/ttynamecheckdir.h | 41 +++++++++++++++ 8 files changed, 201 insertions(+), 12 deletions(-) create mode 100644 src/server/ttynamecheckdir.cpp create mode 100644 src/server/ttynamecheckdir.h diff --git a/configure.ac b/configure.ac index b2aecf3..f590577 100644 --- a/configure.ac +++ b/configure.ac @@ -130,6 +130,7 @@ AC_CHECK_HEADERS([sys/socket.h]) # Checks for typedefs, structures, and compiler characteristics. AX_CXX_GCC_ABI_DEMANGLE AC_CHECK_TYPES([ptrdiff_t]) +AC_CHECK_MEMBERS([struct stat.st_rdev]) AC_TYPE_SIZE_T AC_TYPE_SSIZE_T AC_TYPE_UINT16_T @@ -139,6 +140,10 @@ AC_TYPE_PID_T AC_C_INLINE # Checks for library functions. +AS_IF([test "x$cross_compiling" != "xyes"], [ + AC_FUNC_MALLOC + AC_FUNC_REALLOC +]) AC_CHECK_FUNCS([mkdir]) AC_CHECK_FUNCS([initstate]) AC_CHECK_FUNCS([strdup]) diff --git a/debian/rules b/debian/rules index b58b5b5..1dd7c7b 100755 --- a/debian/rules +++ b/debian/rules @@ -31,8 +31,10 @@ include /usr/share/cdbs/1/rules/autoreconf.mk # Add here any variable or target overrides you need. -DEB_CONFIGURE_EXTRA_FLAGS = --bindir=\$${prefix}/games --enable-cli-client --enable-ai-image=/usr/share/pixmaps/debian-logo.png \ - --enable-ai-name="`lsb_release -sc | sed 's/.*/\u&/'`" --localstatedir=/var/games +DEB_CONFIGURE_EXTRA_FLAGS = --bindir=\$${prefix}/games --enable-cli-client \ + --enable-ai-image=/usr/share/pixmaps/debian-logo.png \ + --enable-ai-name="`lsb_release -sc | sed 's/.*/\u&/'`" \ + --localstatedir=/var/games DEB_MAKE_CHECK_TARGET = check INT_INSTALL_TARGET = install-strip DEB_DH_BUILDDEB_ARGS_DEFAULT := -- -z9 -Zxz diff --git a/netmaumau.xinetd.in b/netmaumau.xinetd.in index beb6dc3..3eac141 100644 --- a/netmaumau.xinetd.in +++ b/netmaumau.xinetd.in @@ -7,7 +7,7 @@ service netmaumau protocol = tcp user = root server = @bindir@/nmm-server - server_args = --inetd + server_args = --inetd --group=tty type = UNLISTED wait = yes instances = 1 diff --git a/src/client/Makefile.am b/src/client/Makefile.am index 2e1dddc..803975f 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -13,7 +13,7 @@ libnetmaumauclient_private_la_SOURCES = base64bridge.cpp clientcardfactory.cpp libnetmaumauclient_la_CXXFLAGS = -I$(top_srcdir)/src/common -I$(top_srcdir)/src/include libnetmaumauclient_la_SOURCES = abstractclient.cpp abstractclientv05impl.cpp \ capabilitiesexception.cpp clientconnection.cpp clientconnectionimpl.cpp \ - connectionrejectedexception.cpp interceptederrorexception.cpp gamerunningexception.cpp \ + connectionrejectedexception.cpp gamerunningexception.cpp interceptederrorexception.cpp \ nonetmaumauserverexception.cpp playerlistexception.cpp protocolerrorexception.cpp \ scoresexception.cpp shutdownexception.cpp timeoutexception.cpp versionmismatchexception.cpp libnetmaumauclient_la_LDFLAGS = -nodefaultlibs -nostartfiles -no-undefined \ diff --git a/src/server/Makefile.am b/src/server/Makefile.am index 82e6389..e81e3ca 100644 --- a/src/server/Makefile.am +++ b/src/server/Makefile.am @@ -18,11 +18,11 @@ MAINTAINERCLEANFILES = $(bin_PROGRAMS).1 DISTCLEANFILES = $(man1_MANS) noinst_HEADERS = gameconfig.h game.h serverconnection.h servereventhandler.h \ - serverplayerexception.h serverplayer.h + serverplayerexception.h serverplayer.h ttynamecheckdir.h libnmm_server_private_la_CXXFLAGS = $(NO_RTTI_FLAGS) -I$(top_srcdir)/src/include \ -I$(top_srcdir)/src/engine -libnmm_server_private_la_SOURCES = gameconfig.cpp +libnmm_server_private_la_SOURCES = gameconfig.cpp ttynamecheckdir.cpp nmm_server_CPPFLAGS = $(GSL) diff --git a/src/server/main.cpp b/src/server/main.cpp index 0ad56a7..3ad9170 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -66,6 +66,7 @@ #include "logger.h" #include "gameconfig.h" #include "serverplayer.h" +#include "ttynamecheckdir.h" #include "serverconnection.h" #include "servereventhandler.h" @@ -74,7 +75,7 @@ #endif #ifndef DP_GROUP -#define DP_GROUP "nogroup" +#define DP_GROUP "tty" #endif namespace { @@ -212,11 +213,60 @@ void sh_interrupt(int) { void sh_dump(int, siginfo_t *info, void *) { - char pp[PATH_MAX] = ""; + char *p = NULL; - if(info) std::snprintf(pp, PATH_MAX, "/proc/%d/fd/1", info->si_pid); + if(info) { - std::ofstream out(*pp ? pp : "/dev/null"); + char sp[PATH_MAX] = ""; + +#ifndef __OpenBSD__ + std::snprintf(sp, PATH_MAX, "/proc/%d/stat", info->si_pid); +#else + std::snprintf(sp, PATH_MAX, "/proc/%d/status", info->si_pid); +#endif + + int tty_nr = 0; + + FILE *spf; + + if((spf = std::fopen(sp, "r"))) { + + int iDummy; + +#ifndef __OpenBSD__ + + char cDummy, *sDummy; + + // cppcheck-suppress invalidscanf_libc + // cppcheck-suppress invalidscanf + if(std::fscanf(spf, "%d %ms %c %d %d %d %d", &iDummy, &sDummy, &cDummy, &iDummy, + &iDummy, &iDummy, &tty_nr)) {} + + free(sDummy); +#else + char sDevice[20], sCmd[256]; + + // cppcheck-suppress invalidscanf_libc + // cppcheck-suppress invalidscanf + if(std::fscanf(spf, "%255s %d %d %d %d %19s", sCmd, &iDummy, &iDummy, &iDummy, + &iDummy, sDevice)) { + logDebug("BSD emitter: " << sCmd); // why (swapper) and not (kill)? + logDebug("BSD tty device: " << sDevice); // why (-1,-1)? + } + +#endif + + std::fclose(spf); + } + + if(!(p = NetMauMau::Server::ttynameCheckDir(static_cast(tty_nr), "/dev/pts"))) { + p = NetMauMau::Server::ttynameCheckDir(static_cast(tty_nr), "/dev"); + } + } + + std::ofstream out(p ? p : "/dev/null"); + + free(p); if(out.is_open()) { @@ -260,8 +310,8 @@ void sh_dump(int, siginfo_t *info, void *) { << NetMauMau::DB::SQLite::getInstance().getServedGames() << std::endl; } - out.flush(); - out.close(); + //out.flush(); + //out.close(); } } diff --git a/src/server/ttynamecheckdir.cpp b/src/server/ttynamecheckdir.cpp new file mode 100644 index 0000000..0b5a5c9 --- /dev/null +++ b/src/server/ttynamecheckdir.cpp @@ -0,0 +1,91 @@ +/* + * Copyright 2015 by Heiko Schäfer + * + * This file is part of NetMauMau. + * + * NetMauMau is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * NetMauMau is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NetMauMau. If not, see . + * + * Based upon: http://man7.org/tlpi/code/online/book/tty/ttyname.c.html + */ + +#ifndef _WIN32 + +#include +#include +#include + +#include +#include +#include + +#include "ttynamecheckdir.h" + +char *NetMauMau::Server::ttynameCheckDir(dev_t ttyNr, const char *devDir) { + + struct dirent *dent; + static char *ttyPath; + std::size_t ttyLen = 0; + struct stat devStat; + + if(ttyLen == 0) { + + ttyPath = static_cast(malloc(50)); + + if(!ttyPath) return NULL; + + ttyLen = 50; + } + + DIR *dirh = opendir(devDir); + + if(!dirh) return NULL; + + bool found = false; + + // cppcheck-suppress nonreentrantFunctionsreaddir + while((dent = readdir(dirh))) { + + const std::size_t requiredLen = strlen(devDir) + strlen(dent->d_name) + 2; + + if(requiredLen > ttyLen) { + + char *nTtyPath = static_cast(realloc(ttyPath, requiredLen)); + + if(nTtyPath) { + ttyPath = nTtyPath; + } else { + break; + } + + ttyLen = requiredLen; + } + + std::snprintf(ttyPath, ttyLen, "%s/%s", devDir, dent->d_name); + + if(stat(ttyPath, &devStat) == -1) continue; + + if(S_ISCHR(devStat.st_mode) && ttyNr == devStat.st_rdev) { + found = true; + break; + } + } + + closedir(dirh); + + return found ? ttyPath : NULL; +} + +#endif + +// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4; diff --git a/src/server/ttynamecheckdir.h b/src/server/ttynamecheckdir.h new file mode 100644 index 0000000..a9b7bef --- /dev/null +++ b/src/server/ttynamecheckdir.h @@ -0,0 +1,41 @@ +/* + * Copyright 2015 by Heiko Schäfer + * + * This file is part of NetMauMau. + * + * NetMauMau is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * NetMauMau is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NetMauMau. If not, see . + */ + +#ifndef NETMAUMAU_SERVER_TTYNAMECHECKDIR_H +#define NETMAUMAU_SERVER_TTYNAMECHECKDIR_H + +#ifndef _WIN32 + +#include + +namespace NetMauMau { + +namespace Server { + +char *ttynameCheckDir(dev_t ttyNr, const char *devDir); + +} + +} + +#endif + +#endif /* NETMAUMAU_SERVER_TTYNAMECHECKDIR_H */ + +// kate: indent-mode cstyle; indent-width 4; replace-tabs off; tab-width 4;