diff options
author | Christophe Fergeau <cfergeau@redhat.com> | 2012-10-02 22:50:46 +0200 |
---|---|---|
committer | Christophe Fergeau <cfergeau@redhat.com> | 2012-10-02 23:10:06 +0200 |
commit | 8b47b54c7a08612c9c0fd6fa44d9d0ed46a85d83 (patch) | |
tree | 3d9f8dbcdc2b206a2f7e8219bf0ecb7c80a71075 | |
parent | 60c53563604bbf33096f968f1058c8d5f3750d6d (diff) |
Move SPICEController::StartClient to SpiceController
This method is OS-specific, this commit moves it to SpiceController
because this class has most of the OS-specific code.
-rw-r--r-- | SPICEConsoleAPI.cpp | 82 | ||||
-rw-r--r-- | SPICEConsoleAPI.h | 7 | ||||
-rw-r--r-- | controller.cpp | 87 | ||||
-rw-r--r-- | controller.h | 5 |
4 files changed, 94 insertions, 87 deletions
diff --git a/SPICEConsoleAPI.cpp b/SPICEConsoleAPI.cpp index f2d6609..d617568 100644 --- a/SPICEConsoleAPI.cpp +++ b/SPICEConsoleAPI.cpp @@ -34,8 +34,6 @@ * ***** END LICENSE BLOCK ***** */ #include <sys/stat.h> -#include <sys/types.h> -#include <sys/wait.h> #include <glib.h> @@ -91,57 +89,6 @@ bool SPICEConsoleAPI::initSocket(void) return true; } -bool SPICEConsoleAPI::startClient(void) -{ - /* use a pipe for the children to wait until it gets tracked */ - int pipe_fds[2] = { -1, -1 }; - if (pipe(pipe_fds) < 0) { - perror("spice-xpi system error"); - return false; - } - - m_pid_controller = fork(); - if (m_pid_controller == 0) - { - setpgrp(); - - close(pipe_fds[1]); - pipe_fds[1] = -1; - - char c; - if (read(pipe_fds[0], &c, 1) != 0) - g_critical("Error while reading on pipe: %s", g_strerror(errno)); - - close(pipe_fds[0]); - pipe_fds[0] = -1; - - execl("/usr/libexec/spice-xpi-client", "/usr/libexec/spice-xpi-client", NULL); - g_message("failed to run spice-xpi-client, running spicec instead"); - - // TODO: temporary fallback for backward compatibility - execl("/usr/bin/spicec", "/usr/bin/spicec", "--controller", NULL); - g_critical("ERROR failed to run spicec fallback"); - - exit(EXIT_FAILURE); - } - else - { - g_debug("child pid: %"G_GUINT64_FORMAT, (guint64)m_pid_controller); - - close(pipe_fds[0]); - pipe_fds[0] = -1; - - pthread_t controller_thread_id; - pthread_create(&controller_thread_id, NULL, ControllerWaitHelper, - reinterpret_cast<void*>(this)); - - close(pipe_fds[1]); - pipe_fds[1] = -1; - - return true; - } -} - bool SPICEConsoleAPI::createTrustStore(void) { // create trust store filename @@ -215,7 +162,7 @@ void SPICEConsoleAPI::connect() return; } - if (!this->startClient()) { + if (!m_external_controller.StartClient()) { g_critical("failed to start SPICE client"); return; } @@ -270,8 +217,7 @@ void SPICEConsoleAPI::show() void SPICEConsoleAPI::disconnect() { - if (m_pid_controller > 0) - kill(-m_pid_controller, SIGTERM); + m_external_controller.StopClient(); } void SPICEConsoleAPI::SetLanguageStrings(const std::string §ion, @@ -389,27 +335,3 @@ template<typename T> void SPICEConsoleAPI::SendValue(uint32_t id, } SendMsg(id, value); } - -void *SPICEConsoleAPI::ControllerWaitHelper(void *opaque) -{ - SPICEConsoleAPI *fake_this = reinterpret_cast<SPICEConsoleAPI *>(opaque); - if (!fake_this) - return NULL; - - int exit_code; - waitpid(fake_this->m_pid_controller, &exit_code, 0); - g_debug("child finished, pid: %"G_GUINT64_FORMAT, (guint64)exit_code); - - fake_this->m_connected_status = fake_this->m_external_controller.TranslateRC(exit_code); - if (!getenv("SPICE_XPI_DEBUG")) - { - fake_this->fire_disconnected(exit_code); - fake_this->m_external_controller.Disconnect(); - } - - unlink(fake_this->m_trust_store_file.c_str()); - fake_this->m_trust_store_file.clear(); - fake_this->m_pid_controller = -1; - return NULL; -} - diff --git a/SPICEConsoleAPI.h b/SPICEConsoleAPI.h index efb2e86..09e0b4e 100644 --- a/SPICEConsoleAPI.h +++ b/SPICEConsoleAPI.h @@ -61,8 +61,7 @@ public: SPICEConsoleAPI(const SPICEConsolePtr& plugin, const FB::BrowserHostPtr& host) : m_plugin(plugin), m_host(host), - m_connected_status(-2), - m_pid_controller(-1) + m_connected_status(-2) { registerMethod("connect", make_method(this, &SPICEConsoleAPI::connect)); registerMethod("show", make_method(this, &SPICEConsoleAPI::show)); @@ -139,7 +138,6 @@ private: std::map<std::string, std::string> m_languages; std::string m_usb_filter; int m_connected_status; - pid_t m_pid_controller; SpiceController m_external_controller; std::string m_tmp_dir; std::string m_trust_store_file; @@ -156,10 +154,7 @@ private: void SendMsg(uint32_t id, uint32_t value); template<typename T> void SendValue(uint32_t id, std::string attributeName); - static void *ControllerWaitHelper(void *opaque); - bool initSocket(void); - bool startClient(void); bool createTrustStore(void); }; diff --git a/controller.cpp b/controller.cpp index b912c27..328f03e 100644 --- a/controller.cpp +++ b/controller.cpp @@ -51,14 +51,17 @@ extern "C" { # include <unistd.h> # include <fcntl.h> # include <sys/socket.h> +# include <sys/types.h> # include <sys/un.h> +# include <sys/wait.h> } #include "rederrorcodes.h" #include "controller.h" SpiceController::SpiceController(): - m_client_socket(-1) + m_client_socket(-1), + m_pid_controller(-1) { } @@ -190,3 +193,85 @@ int SpiceController::TranslateRC(int nRC) } } +void SpiceController::StopClient() +{ + if (m_pid_controller > 0) + kill(-m_pid_controller, SIGTERM); +} + +bool SpiceController::StartClient(void) +{ + /* use a pipe for the children to wait until it gets tracked */ + int pipe_fds[2] = { -1, -1 }; + if (pipe(pipe_fds) < 0) { + perror("spice-xpi system error"); + return false; + } + + m_pid_controller = fork(); + if (m_pid_controller == 0) + { + setpgrp(); + + close(pipe_fds[1]); + pipe_fds[1] = -1; + + char c; + if (read(pipe_fds[0], &c, 1) != 0) + g_critical("Error while reading on pipe: %s", g_strerror(errno)); + + close(pipe_fds[0]); + pipe_fds[0] = -1; + + execl("/usr/libexec/spice-xpi-client", "/usr/libexec/spice-xpi-client", NULL); + g_message("failed to run spice-xpi-client, running spicec instead"); + + // TODO: temporary fallback for backward compatibility + execl("/usr/bin/spicec", "/usr/bin/spicec", "--controller", NULL); + g_critical("ERROR failed to run spicec fallback"); + + exit(EXIT_FAILURE); + } + else + { + g_debug("child pid: %"G_GUINT64_FORMAT, (guint64)m_pid_controller); + + close(pipe_fds[0]); + pipe_fds[0] = -1; + + pthread_t controller_thread_id; + pthread_create(&controller_thread_id, NULL, ControllerWaitHelper, + reinterpret_cast<void*>(this)); + + close(pipe_fds[1]); + pipe_fds[1] = -1; + + return true; + } +} + +void *SpiceController::ControllerWaitHelper(void *opaque) +{ + SpiceController *fake_this = reinterpret_cast<SpiceController *>(opaque); + if (!fake_this) + return NULL; + + int exit_code; + waitpid(fake_this->m_pid_controller, &exit_code, 0); + g_debug("child finished, pid: %"G_GUINT64_FORMAT, (guint64)exit_code); + + if (!getenv("SPICE_XPI_DEBUG")) + { + fake_this->Disconnect(); + } + + /* FIXME: callback(exit_code); + unlink(fake_this->m_trust_store_file.c_str()); + fake_this->m_trust_store_file.clear(); + fake_this->m_connected_status = fake_this->m_external_controller.TranslateRC(exit_code); + if (!getenv("SPICE_XPI_DEBUG")) + fake_this->fire_disconnected(exit_code); + */ + fake_this->m_pid_controller = -1; + return NULL; +} diff --git a/controller.h b/controller.h index 07c04c7..c8a96e3 100644 --- a/controller.h +++ b/controller.h @@ -72,6 +72,8 @@ public: ~SpiceController(); void SetFilename(const std::string &name); + bool StartClient(void); + void StopClient(void); int Connect(); int Connect(int nRetries); void Disconnect(); @@ -82,6 +84,9 @@ public: private: int m_client_socket; std::string m_name; + pid_t m_pid_controller; + + static void *ControllerWaitHelper(void *opaque); }; #endif // SPICE_CONTROLLER_H |