0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SshSocketHandler.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2014 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #ifndef Tools_cluster_SshSocketHandler_h
28 #define Tools_cluster_SshSocketHandler_h
29 
30 #include "SshOutputCollector.h"
31 
32 #include <AsyncComm/Comm.h>
33 #include <AsyncComm/CommAddress.h>
35 
36 #include <Common/PageArena.h>
37 
38 #include <libssh/callbacks.h>
39 #include <libssh/libssh.h>
40 
41 #include <cerrno>
42 #include <chrono>
43 #include <condition_variable>
44 #include <cstdio>
45 #include <cstdlib>
46 #include <cstring>
47 #include <iostream>
48 #include <memory>
49 #include <mutex>
50 #include <ostream>
51 #include <string>
52 #include <tuple>
53 
54 namespace Hypertable {
55 
58 
61  public:
62 
67  SshSocketHandler(const std::string &hostname);
68 
70  virtual ~SshSocketHandler();
71 
82  virtual bool handle(int sd, int events);
83 
87  virtual void deregister(int sd);
88 
91  virtual int poll_interest(int sd) { return m_poll_interest; }
92 
101  void log_callback(ssh_session session, int priority, const char *message);
102 
111  int auth_callback(const char *prompt, char *buf, size_t len,
112  int echo, int verify);
113 
117  void connect_status_callback(float status);
118 
123  void global_request_callback(ssh_session session, ssh_message message);
124 
130  void set_exit_status(int exit_status);
131 
141  bool wait_for_connection(std::chrono::system_clock::time_point deadline);
142 
149  bool issue_command(const std::string &command);
150 
160 
164  void cancel();
165 
172  void dump_log(std::ostream &out);
173 
181  void set_terminal_output(bool val);
182 
186  static void enable_debug();
187 
194  static void set_libssh_verbosity(const std::string &value);
195 
198  const std::string hostname() const { return m_hostname; }
199 
200  private:
201 
206  bool verify_knownhost();
207 
212  void write_to_stdout(const char *output, size_t len);
213 
218  void write_to_stderr(const char *output, size_t len);
219 
224  bool socket_has_data();
225 
227  static bool ms_debug_enabled;
228 
231 
234 
236  std::condition_variable m_cond;
237 
240 
242  std::string m_hostname;
243 
245  ssh_session m_ssh_session;
246 
249 
251  ssh_channel m_channel;
252 
254  int m_state {};
255 
257  int m_sd {};
258 
261 
263  std::string m_error;
264 
266  ssh_callbacks_struct m_callbacks;
267 
269  ssh_channel_callbacks_struct m_channel_callbacks;
270 
272  std::string m_command;
273 
276 
279 
282 
285 
288 
290  bool m_cancelled {};
291 
294 
297 
300 
303 
306 
309 
312 
313  };
314 
316  typedef std::shared_ptr<SshSocketHandler> SshSocketHandlerPtr;
317 
319 
320 } // namespace Hypertable
321 
322 #endif // Tools_cluster_SshSocketHandler_h
void set_exit_status(int exit_status)
libssh exit status callback This function sets m_command_exit_status to exit_status, sets m_command_exit_status_is_set to true, and then signals m_cond.
static std::mutex mutex
Definition: Logger.cc:43
void connect_status_callback(float status)
libssh connection status callback.
Collects buffers of output from execution of remote SSH command.
std::shared_ptr< SshSocketHandler > SshSocketHandlerPtr
Smart pointer to SshSocketHandler.
std::condition_variable m_cond
Condition variable signalling connection and command completion.
virtual ~SshSocketHandler()
Destructor.
void write_to_stderr(const char *output, size_t len)
Writes output to stderr Writes output to stderr, prefixing each line with '[' hostname ']'...
SshOutputCollector::Buffer m_stderr_buffer
Current stderr output buffer.
PageArena memory allocator.
std::string m_command
Current command being issued.
SshOutputCollector m_stderr_collector
Output collector for stderr.
Declarations for RawSocketHandler.
bool m_line_prefix_needed_stderr
Line prefix needs to be emitted on next stderr output.
int m_sd
Socket descriptor.
bool issue_command(const std::string &command)
Asynchronously issues a command.
SshOutputCollector m_stdout_collector
Output collector for stdout.
SshOutputCollector m_log_collector
Output collector for logging.
Declarations for CommAddress.
SshOutputCollector::Buffer m_stdout_buffer
Current stdout output buffer.
int auth_callback(const char *prompt, char *buf, size_t len, int echo, int verify)
libssh authorization callback.
std::mutex m_mutex
Mutex for serialzing access to members
int m_state
Current handler state.
SshSocketHandler(const std::string &hostname)
Constructor.
Abstract base class for application raw socket handlers registered with AsyncComm.
std::string m_error
Error message
bool m_line_prefix_needed_stdout
Line prefix needs to be emitted on next stdout output.
bool wait_for_connection(std::chrono::system_clock::time_point deadline)
Waits for connection establishment.
static int ms_libssh_verbosity
Libssh logging verbosity level.
virtual bool handle(int sd, int events)
Socket event handler function.
virtual int poll_interest(int sd)
Returns current polling interest.
bool status(ContextPtr &context, Timer &timer, Status &status)
Runs a status check on the master.
Definition: Utility.cc:408
Fixed-size buffer to hold a portion of output.
bool m_channel_is_eof
Flag indicating that current channel is EOF.
const std::string hostname() const
Returns hostname.
Comm * m_comm
Pointer to comm layer.
virtual void deregister(int sd)
Deregisters socket.
bool verify_knownhost()
Verifies host with public key method.
Declarations for SshOutputCollector.
static void set_libssh_verbosity(const std::string &value)
Sets libssh logging verbosity level.
ssh_channel m_channel
libssh channel object
std::string m_hostname
Name of host to connect to.
ssh_callbacks_struct m_callbacks
libssh callbacks
int m_poll_interest
Current polling interest.
Hypertable definitions
bool m_command_exit_status_is_set
Flag indicating that the exit status has been set.
Entry point to AsyncComm service.
Definition: Comm.h:61
void cancel()
Cancels outstanding connection establishment or command execution.
bool m_terminal_output
Redirect output to terminal.
static void enable_debug()
Enables debug logging output.
bool m_cancelled
Flag indicating that outstanding operations should be cancelled.
Declarations for Comm.
SshOutputCollector::Buffer m_log_buffer
Current logging output buffer.
Raw socket handler for ssh protocol driver.
void log_callback(ssh_session session, int priority, const char *message)
Writes log messages to logging output collector.
CommAddress m_comm_address
Address of connection.
int m_command_exit_status
Command exit status.
void global_request_callback(ssh_session session, ssh_message message)
libssh global request callback.
void write_to_stdout(const char *output, size_t len)
Writes output to stdout Writes output to stdout, prefixing each line with '[' hostname ']'...
bool wait_for_command_completion()
Waits for command completion.
void dump_log(std::ostream &out)
Writes collected log messages to output stream.
ssh_session m_ssh_session
libssh sesison object
Address abstraction to hold either proxy name or IPv4:port address.
Definition: CommAddress.h:52
static bool ms_debug_enabled
Flag for enabling debugging output.
bool socket_has_data()
Determines if data available on socket for reading Checks socket descriptor m_sd to see if there is a...
ssh_channel_callbacks_struct m_channel_callbacks
libssh channel callbacks
void set_terminal_output(bool val)
Tells handler to send collected output subsequent output to terminal If val is true, sends any collected stdout or stderr output to terminal and sets m_terminal_output to true which causes any subsequent output collected to be sent to the terminal.