summaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2007-03-22 23:48:12 +0000
committerChris Wilson <chris+github@qwirx.com>2007-03-22 23:48:12 +0000
commitb9fec05c7692aeb993e67b50e6f0b5f8249325d0 (patch)
tree8157bd45cb61cc98aebd3f6158c41243ebd83730 /lib/common
parent1c28c07ef9651fa381b6469dc541a12891f8ba18 (diff)
Move lib/common/ServerControl.h to lib/server where it belongs, since
it uses server functions (WinNamedPipeStream on win32) (refs #3)
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/ServerControl.h177
1 files changed, 0 insertions, 177 deletions
diff --git a/lib/common/ServerControl.h b/lib/common/ServerControl.h
deleted file mode 100644
index a8477900..00000000
--- a/lib/common/ServerControl.h
+++ /dev/null
@@ -1,177 +0,0 @@
-#ifndef SERVER_CONTROL_H
-#define SERVER_CONTROL_H
-
-#ifdef WIN32
-
-#include "WinNamedPipeStream.h"
-#include "IOStreamGetLine.h"
-#include "BoxPortsAndFiles.h"
-#include "Test.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 KillServerInternal(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);
- return true;
-}
-
-#else // !WIN32
-
-inline bool HUPServer(int pid)
-{
- if(pid == 0) return false;
- return ::kill(pid, SIGHUP) == 0;
-}
-
-inline bool KillServerInternal(int pid)
-{
- if(pid == 0 || pid == -1) return false;
- bool killed = (::kill(pid, SIGTERM) == 0);
- TEST_THAT(killed);
- return killed;
-}
-
-#endif // WIN32
-
-inline bool KillServer(int pid)
-{
- if (!KillServerInternal(pid))
- {
- return false;
- }
-
- for (int i = 0; i < 30; i++)
- {
- if (!ServerIsAlive(pid)) break;
- ::sleep(1);
- if (!ServerIsAlive(pid)) break;
-
- if (i == 0)
- {
- printf("waiting for server to die");
- }
-
- printf(".");
- fflush(stdout);
- }
-
- if (!ServerIsAlive(pid))
- {
- printf("done.\n");
- }
- else
- {
- printf("failed!\n");
- }
-
- fflush(stdout);
-
- return !ServerIsAlive(pid);
-}
-
-#endif // SERVER_CONTROL_H