From 748579a668de5df68c8839a84e9700ae9fd1fcc8 Mon Sep 17 00:00:00 2001 From: Steven Edwards Date: Sat, 7 Feb 2009 15:21:15 +0000 Subject: [PATCH] - Reimplement TerminateShell function using WaitForSingleObject to avoid some of the excessive usage of Sleep. Let me know if you spot a bug. svn path=/trunk/; revision=39457 --- .../applications/sysutils/telnetd/telnetd.c | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/rosapps/applications/sysutils/telnetd/telnetd.c b/rosapps/applications/sysutils/telnetd/telnetd.c index d8d0e985c8a..882e272aade 100644 --- a/rosapps/applications/sysutils/telnetd/telnetd.c +++ b/rosapps/applications/sysutils/telnetd/telnetd.c @@ -17,19 +17,14 @@ * - Unify Debugging output and return StatusCodes */ -#include -#include - #include "telnetd.h" #define telnetd_printf printf - #if 0 -extern void syslog (int priority, const char *fmt, ...); - -int telnetd_printf(const char *format, ...) +static inline int telnetd_printf(const char *format, ...); { - syslog (6, format); + printf(format,...); + syslog (6, format); } #endif @@ -37,7 +32,6 @@ int telnetd_printf(const char *format, ...) static BOOLEAN bShutdown = 0; static BOOLEAN bSocketInterfaceInitialised = 0; - static int sock; /* In the future, some options might be passed here to handle @@ -50,6 +44,9 @@ static int sock; */ int main(int argc, char **argv) { + printf("Attempting to start Simple TelnetD\n"); + +// DetectPlatform(); SetConsoleCtrlHandler(Cleanup, 1); if (!StartSocketInterface()) @@ -143,7 +140,6 @@ static void UserLogin(int client_socket) if (client == NULL) ErrorExit("failed to allocate memory for client"); - client->socket = client_socket; CreateThread(NULL, 0, UserLoginThread, client, 0, &threadID); } @@ -574,35 +570,52 @@ static DWORD WINAPI ReadFromPipeThread(LPVOID data) /* TerminateShell */ static void TerminateShell(client_t *client) { - DWORD exitCode; - DWORD dwWritten; - char stop[] = "\003\r\nexit\r\n"; /* Ctrl-C + exit */ + DWORD exitCode; + DWORD dwWritten; + char stop[] = "\003\r\nexit\r\n"; /* Ctrl-C + exit */ - GetExitCodeProcess(client->hProcess, &exitCode); - if (exitCode == STILL_ACTIVE) { - telnetd_printf("user shell still active, send Ctrl-Break to group-id %lu\n", client->dwProcessId ); + GetExitCodeProcess(client->hProcess, &exitCode); - if (!GenerateConsoleCtrlEvent( CTRL_BREAK_EVENT, client->dwProcessId )) - telnetd_printf("Failed to send Ctrl_break\n"); + if (exitCode == STILL_ACTIVE) + { + HANDLE hEvent = NULL; + DWORD dwWaitResult; - Sleep(500); + telnetd_printf("user shell still active, send Ctrl-Break to group-id %lu\n", client->dwProcessId ); - if (!GenerateConsoleCtrlEvent( CTRL_C_EVENT, client->dwProcessId )) - telnetd_printf("Failed to send Ctrl_C\n"); + hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - Sleep(500); + if (hEvent == NULL) + printf("CreateEvent error\n"); - if (!WriteFile(client->hChildStdinWr, stop, sizeof(stop), &dwWritten, NULL)) - telnetd_printf("Error writing to pipe\n"); + if (!GenerateConsoleCtrlEvent( CTRL_BREAK_EVENT, client->dwProcessId )) + telnetd_printf("Failed to send Ctrl_break\n"); - Sleep(500); + if (!GenerateConsoleCtrlEvent( CTRL_C_EVENT, client->dwProcessId )) + telnetd_printf("Failed to send Ctrl_C\n"); - GetExitCodeProcess(client->hProcess, &exitCode); - if (exitCode == STILL_ACTIVE) { - telnetd_printf("user shell still active, attempt to terminate it now...\n"); - TerminateProcess(client->hProcess, 0); + if (!WriteFile(client->hChildStdinWr, stop, sizeof(stop), &dwWritten, NULL)) + telnetd_printf("Error writing to pipe\n"); + + /* wait for our handler to be called */ + dwWaitResult=WaitForSingleObject(hEvent, 500); + + if (WAIT_FAILED==dwWaitResult) + telnetd_printf("WaitForSingleObject failed\n"); + + GetExitCodeProcess(client->hProcess, &exitCode); + if (exitCode == STILL_ACTIVE) + { + telnetd_printf("user shell still active, attempt to terminate it now...\n"); + + if (hEvent != NULL) + { + if (!CloseHandle(hEvent)) + telnetd_printf("CloseHandle"); + } + TerminateProcess(client->hProcess, 0); + } } - } } /* ErrorExit */ -- 2.17.1