Skip to content

Commit

Permalink
sending SIGUSR1 output to emitters terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
velnias75 committed Feb 25, 2015
1 parent 6360c30 commit 891150b
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 12 deletions.
5 changes: 5 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand Down
6 changes: 4 additions & 2 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion netmaumau.xinetd.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/client/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 2 additions & 2 deletions src/server/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
62 changes: 56 additions & 6 deletions src/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "logger.h"
#include "gameconfig.h"
#include "serverplayer.h"
#include "ttynamecheckdir.h"
#include "serverconnection.h"
#include "servereventhandler.h"

Expand All @@ -74,7 +75,7 @@
#endif

#ifndef DP_GROUP
#define DP_GROUP "nogroup"
#define DP_GROUP "tty"
#endif

namespace {
Expand Down Expand Up @@ -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)) {

This comment has been minimized.

Copy link
@velnias75

velnias75 Feb 25, 2015

Author Owner

See #29

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<dev_t>(tty_nr), "/dev/pts"))) {
p = NetMauMau::Server::ttynameCheckDir(static_cast<dev_t>(tty_nr), "/dev");
}
}

std::ofstream out(p ? p : "/dev/null");

free(p);

if(out.is_open()) {

Expand Down Expand Up @@ -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();
}
}

Expand Down
91 changes: 91 additions & 0 deletions src/server/ttynamecheckdir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2015 by Heiko Schäfer <heiko@rangun.de>
*
* 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 <http://www.gnu.org/licenses/>.
*
* Based upon: http://man7.org/tlpi/code/online/book/tty/ttyname.c.html
*/

#ifndef _WIN32

#include <cstdlib>
#include <cstring>
#include <cstdio>

#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

#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<char *>(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<char *>(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;
41 changes: 41 additions & 0 deletions src/server/ttynamecheckdir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2015 by Heiko Schäfer <heiko@rangun.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#ifndef NETMAUMAU_SERVER_TTYNAMECHECKDIR_H
#define NETMAUMAU_SERVER_TTYNAMECHECKDIR_H

#ifndef _WIN32

#include <sys/types.h>

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;

0 comments on commit 891150b

Please sign in to comment.