[Box Backup-dev] COMMIT r654 - in box/chris/general/lib: common server

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Wed Jul 19 19:43:54 BST 2006


Author: chris
Date: 2006-07-19 18:43:52 +0000 (Wed, 19 Jul 2006)
New Revision: 654

Added:
   box/chris/general/lib/server/ServerControl.h
Modified:
   box/chris/general/lib/common/Test.h
Log:
* common/Test.h
* server/ServerControl.h
- Moved HupServer and KillServer to ServerControl.h, since on Win32 they
  depend on lib/server.


Modified: box/chris/general/lib/common/Test.h
===================================================================
--- box/chris/general/lib/common/Test.h	2006-07-19 08:48:29 UTC (rev 653)
+++ box/chris/general/lib/common/Test.h	2006-07-19 18:43:52 UTC (rev 654)
@@ -44,6 +44,8 @@
 // NOTE: The 0- bit is to allow this to work with stuff which has negative constants for flags (eg ConnectionException)
 #define TEST_CHECK_THROWS(statement, excepttype, subtype)									\
 	{																						\
+		printf("[Test] Expect an exception below: (" \
+			#excepttype "/" #subtype ")\n"); \
 		bool didthrow = false;																\
 		try																					\
 		{																					\
@@ -211,25 +213,6 @@
 #endif // WIN32
 }
 
-#ifndef WIN32
-
-inline bool HUPServer(int pid)
-{
-	if(pid == 0) return false;
-	return ::kill(pid, SIGHUP) != -1;
-}
-
-inline bool KillServer(int pid)
-{
-	if(pid == 0 || pid == -1) return false;
-	bool KilledOK = ::kill(pid, SIGTERM) != -1;
-	TEST_THAT(KilledOK);
-	::sleep(1);
-	return !ServerIsAlive(pid);
-}
-
-#endif // !WIN32
-
 inline void TestRemoteProcessMemLeaks(const char *filename)
 {
 #ifdef BOX_MEMORY_LEAK_TESTING

Added: box/chris/general/lib/server/ServerControl.h
===================================================================
--- box/chris/general/lib/server/ServerControl.h	2006-07-19 08:48:29 UTC (rev 653)
+++ box/chris/general/lib/server/ServerControl.h	2006-07-19 18:43:52 UTC (rev 654)
@@ -0,0 +1,143 @@
+#ifndef SERVER_CONTROL_H
+#define SERVER_CONTROL_H
+
+#ifdef WIN32
+
+#include "WinNamedPipeStream.h"
+#include "IOStreamGetLine.h"
+#include "BoxPortsAndFiles.h"
+
+static bool SendCommands(const std::string& rCmd)
+{
+	WinNamedPipeStream connection;
+
+	try
+	{
+		connection.Connect(BOX_NAMED_PIPE_NAME);
+	}
+	catch(...)
+	{
+		printf("Failed to connect to daemon control socket.\n");
+		return false;
+	}
+
+	// For receiving data
+	IOStreamGetLine getLine(connection);
+	
+	// Wait for the configuration summary
+	std::string configSummary;
+	if(!getLine.GetLine(configSummary))
+	{
+		printf("Failed to receive configuration summary from daemon\n");
+		return false;
+	}
+
+	// Was the connection rejected by the server?
+	if(getLine.IsEOF())
+	{
+		printf("Server rejected the connection.\n");
+		return false;
+	}
+
+	// Decode it
+	int autoBackup, updateStoreInterval, minimumFileAge, maxUploadWait;
+	if(::sscanf(configSummary.c_str(), "bbackupd: %d %d %d %d", 
+			&autoBackup, &updateStoreInterval, 
+			&minimumFileAge, &maxUploadWait) != 4)
+	{
+		printf("Config summary didn't decode\n");
+		return false;
+	}
+
+	std::string cmds;
+	bool expectResponse;
+
+	if (rCmd != "")
+	{
+		cmds = rCmd;
+		cmds += "\nquit\n";
+		expectResponse = true;
+	}
+	else
+	{
+		cmds = "quit\n";
+		expectResponse = false;
+	}
+	
+	connection.Write(cmds.c_str(), cmds.size());
+	
+	// Read the response
+	std::string line;
+	bool statusOk = !expectResponse;
+
+	while (expectResponse && !getLine.IsEOF() && getLine.GetLine(line))
+	{
+		// Is this an OK or error line?
+		if (line == "ok")
+		{
+			statusOk = true;
+		}
+		else if (line == "error")
+		{
+			printf("ERROR (%s)\n", rCmd.c_str());
+			break;
+		}
+		else
+		{
+			printf("WARNING: Unexpected response to command '%s': "
+				"%s", rCmd.c_str(), line.c_str());
+		}
+	}
+	
+	return statusOk;
+}
+
+inline bool HUPServer(int pid)
+{
+	return SendCommands("reload");
+}
+
+inline bool KillServer(int pid)
+{
+	HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, false, pid);
+	if (hProcess == NULL)
+	{
+		printf("Failed to open process %d: error %d\n",
+			pid, (int)GetLastError());
+		return false;
+	}
+
+	if (!TerminateProcess(hProcess, 1))
+	{
+		printf("Failed to terminate process %d: error %d\n",
+			pid, (int)GetLastError());
+		CloseHandle(hProcess);
+		return false;
+	}
+
+	CloseHandle(hProcess);
+
+	::sleep(1);
+	return !ServerIsAlive(pid);
+}
+
+#else // !WIN32
+
+inline bool HUPServer(int pid)
+{
+	if(pid == 0) return false;
+	return ::kill(pid, SIGHUP) != -1;
+}
+
+inline bool KillServer(int pid)
+{
+	if(pid == 0 || pid == -1) return false;
+	bool KilledOK = ::kill(pid, SIGTERM) != -1;
+	TEST_THAT(KilledOK);
+	::sleep(1);
+	return !ServerIsAlive(pid);
+}
+
+#endif // WIN32
+
+#endif // SERVER_CONTROL_H




More information about the Boxbackup-dev mailing list