提交 191053b5 编写于 作者: E Eduardo Otubo 提交者: Daniel Veillard

Switch Power Hypervisor to libssh2

* configure.in src/Makefile.am: change detection and flags
* src/phyp/phyp_driver.c src/phyp/phyp_driver.h: connection now
  need to be done as part of the driver code, cleaned up by DV
上级 02d9cef3
...@@ -187,8 +187,8 @@ AC_ARG_WITH([uml], ...@@ -187,8 +187,8 @@ AC_ARG_WITH([uml],
[ --with-uml add UML support (on)],[],[with_uml=check]) [ --with-uml add UML support (on)],[],[with_uml=check])
AC_ARG_WITH([openvz], AC_ARG_WITH([openvz],
[ --with-openvz add OpenVZ support (on)],[],[with_openvz=yes]) [ --with-openvz add OpenVZ support (on)],[],[with_openvz=yes])
AC_ARG_WITH([libssh], AC_ARG_WITH([libssh2],
[ --with-libssh=[PFX] libssh location],[],[with_libssh=yes]) [ --with-libssh2=[PFX] libssh2 location],[],[with_libssh2=yes])
AC_ARG_WITH([phyp], AC_ARG_WITH([phyp],
[ --with-phyp=[PFX] add PHYP support (on)],[],[with_phyp=check]) [ --with-phyp=[PFX] add PHYP support (on)],[],[with_phyp=check])
AC_ARG_WITH([vbox], AC_ARG_WITH([vbox],
...@@ -866,45 +866,45 @@ dnl ...@@ -866,45 +866,45 @@ dnl
dnl libssh checks dnl libssh checks
dnl dnl
if test "$with_libssh" != "yes" -a "$with_libssh" != "no"; then if test "$with_libssh2" != "yes" -a "$with_libssh2" != "no"; then
libssh_path="$with_libssh" libssh2_path="$with_libssh2"
elif test "$with_libssh" = "yes"; then elif test "$with_libssh2" = "yes"; then
libssh_path="/usr/local/lib/" libssh2_path="/usr/local/lib/"
elif test "$with_libssh" = "no"; then elif test "$with_libssh2" = "no"; then
with_phyp="no"; with_phyp="no";
fi fi
if test "$with_phyp" = "check"; then if test "$with_phyp" = "check"; then
AC_CHECK_LIB([ssh],[ssh_new],[ AC_CHECK_LIB([ssh2],[libssh2_session_startup],[
LIBSSH_LIBS="$LIBSSH_LIBS -lssh -L$libssh_path" LIBSSH2_LIBS="$LIBSSH2_LIBS -lssh2 -L$libssh2_path"
AC_SUBST([LIBSSH_LIBS])],[ AC_SUBST([LIBSSH2_LIBS])],[
with_phyp="no" with_phyp="no"
with_libssh="no"; with_libssh2="no";
],[]) ],[])
if test "$with_phyp" != "no"; then if test "$with_phyp" != "no"; then
AC_CHECK_HEADERS([libssh/libssh.h],[ AC_CHECK_HEADERS([libssh2.h],[
with_phyp="yes" with_phyp="yes"
LIBSSH_CFLAGS="-I/usr/local/include/libssh" LIBSSH2_CFLAGS="-I/usr/local/include"
AC_SUBST([LIBSSH_CFLAGS]) AC_SUBST([LIBSSH2_CFLAGS])
AC_DEFINE_UNQUOTED([WITH_PHYP], 1, AC_DEFINE_UNQUOTED([WITH_PHYP], 1,
[whether IBM HMC / IVM driver is enabled]) [whether IBM HMC / IVM driver is enabled])
],[ ],[
with_phyp="no" with_phyp="no"
with_libssh="no"; with_libssh2="no";
],[]) ],[])
fi fi
elif test "$with_phyp" = "yes"; then elif test "$with_phyp" = "yes"; then
AC_CHECK_LIB([ssh],[ssh_new],[ AC_CHECK_LIB([ssh2],[libssh2_session_startup],[
LIBSSH_LIBS="$LIBSSH_LIBS -lssh -L$libssh_path" LIBSSH2_LIBS="$LIBSSH2_LIBS -lssh2 -L$libssh2_path"
AC_SUBST([LIBSSH_LIBS])],[ AC_SUBST([LIBSSH2_LIBS])],[
AC_MSG_ERROR([You must install the libssh to compile Phype driver.]) AC_MSG_ERROR([You must install the libssh2 to compile Phype driver.])
]) ])
AC_CHECK_HEADERS([libssh/libssh.h],[ AC_CHECK_HEADERS([libssh2.h],[
LIBSSH_CFLAGS="-I/usr/local/include/libssh" LIBSSH2_CFLAGS="-I/usr/local/include"
AC_SUBST([LIBSSH_CFLAGS])],[ AC_SUBST([LIBSSH2_CFLAGS])],[
AC_MSG_ERROR([Cannot find libssh headers.Is libssh installed ?]) AC_MSG_ERROR([Cannot find libssh2 headers.Is libssh2 installed ?])
],[]) ],[])
AC_DEFINE_UNQUOTED([WITH_PHYP], 1, AC_DEFINE_UNQUOTED([WITH_PHYP], 1,
[whether IBM HMC / IVM driver is enabled]) [whether IBM HMC / IVM driver is enabled])
...@@ -1699,10 +1699,10 @@ AC_MSG_NOTICE([ libcurl: $LIBCURL_CFLAGS $LIBCURL_LIBS]) ...@@ -1699,10 +1699,10 @@ AC_MSG_NOTICE([ libcurl: $LIBCURL_CFLAGS $LIBCURL_LIBS])
else else
AC_MSG_NOTICE([ libcurl: no]) AC_MSG_NOTICE([ libcurl: no])
fi fi
if test "$with_libssh" != "no" ; then if test "$with_libssh2" != "no" ; then
AC_MSG_NOTICE([ libssh: $LIBSSH_CFLAGS $LIBSSH_LIBS]) AC_MSG_NOTICE([ libssh2: $LIBSSH2_CFLAGS $LIBSSH2_LIBS])
else else
AC_MSG_NOTICE([ libssh: no]) AC_MSG_NOTICE([ libssh2: no])
fi fi
AC_MSG_NOTICE([ gnutls: $GNUTLS_CFLAGS $GNUTLS_LIBS]) AC_MSG_NOTICE([ gnutls: $GNUTLS_CFLAGS $GNUTLS_LIBS])
if test "$with_sasl" != "no" ; then if test "$with_sasl" != "no" ; then
......
...@@ -6,7 +6,7 @@ INCLUDES = \ ...@@ -6,7 +6,7 @@ INCLUDES = \
-I@top_srcdir@/include \ -I@top_srcdir@/include \
-I@top_srcdir@/qemud \ -I@top_srcdir@/qemud \
$(LIBXML_CFLAGS) \ $(LIBXML_CFLAGS) \
$(LIBSSH_CFLAGS) \ $(LIBSSH2_CFLAGS) \
$(XEN_CFLAGS) \ $(XEN_CFLAGS) \
$(SELINUX_CFLAGS) \ $(SELINUX_CFLAGS) \
$(DRIVER_MODULE_CFLAGS) \ $(DRIVER_MODULE_CFLAGS) \
...@@ -308,8 +308,8 @@ else ...@@ -308,8 +308,8 @@ else
noinst_LTLIBRARIES += libvirt_driver_phyp.la noinst_LTLIBRARIES += libvirt_driver_phyp.la
libvirt_la_LIBADD += libvirt_driver_phyp.la libvirt_la_LIBADD += libvirt_driver_phyp.la
endif endif
libvirt_driver_phyp_la_LDFLAGS = $(LIBSSH_LIBS) libvirt_driver_phyp_la_LDFLAGS = $(LIBSSH2_LIBS)
libvirt_driver_phyp_la_CFLAGS = $(LIBSSH_CFLAGS) libvirt_driver_phyp_la_CFLAGS = $(LIBSSH2_CFLAGS)
libvirt_driver_phyp_la_SOURCES = $(PHYP_DRIVER_SOURCES) libvirt_driver_phyp_la_SOURCES = $(PHYP_DRIVER_SOURCES)
endif endif
......
...@@ -34,8 +34,11 @@ ...@@ -34,8 +34,11 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <libssh2.h>
#include <libssh/libssh.h> #include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include "internal.h" #include "internal.h"
#include "util.h" #include "util.h"
...@@ -53,6 +56,10 @@ ...@@ -53,6 +56,10 @@
#define VIR_FROM_THIS VIR_FROM_PHYP #define VIR_FROM_THIS VIR_FROM_PHYP
#ifdef ENABLE_DEBUG
#define PHYP_CMD_DEBUG fprintf(stdout,"%s:%d - COMMAND:%s\n",__FUNCTION__,__LINE__,cmd);
#endif
static int escape_specialcharacters(char *src, char *dst, size_t dstlen); static int escape_specialcharacters(char *src, char *dst, size_t dstlen);
/* /*
...@@ -63,11 +70,12 @@ static virDrvOpenStatus ...@@ -63,11 +70,12 @@ static virDrvOpenStatus
phypOpen(virConnectPtr conn, phypOpen(virConnectPtr conn,
virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
{ {
SSH_SESSION *session = NULL; LIBSSH2_SESSION *session = NULL;
ConnectionData *connection_data = NULL; ConnectionData *connection_data = NULL;
char *string; char *string;
size_t len = 0; size_t len = 0;
uuid_dbPtr uuid_db = NULL; uuid_dbPtr uuid_db = NULL;
int internal_socket;
if (!conn || !conn->uri) if (!conn || !conn->uri)
return VIR_DRV_OPEN_DECLINED; return VIR_DRV_OPEN_DECLINED;
...@@ -113,15 +121,13 @@ phypOpen(virConnectPtr conn, ...@@ -113,15 +121,13 @@ phypOpen(virConnectPtr conn,
goto failure; goto failure;
} }
if ((session = openSSHSession(conn, auth)) == NULL) { if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
_("Error while opening SSH session.")); _("Error while opening SSH session."));
goto failure; goto failure;
} }
//conn->uri->path = string;
VIR_FREE(conn->uri->path);
conn->uri->path = string;
connection_data->session = session; connection_data->session = session;
connection_data->auth = auth; connection_data->auth = auth;
...@@ -134,7 +140,7 @@ phypOpen(virConnectPtr conn, ...@@ -134,7 +140,7 @@ phypOpen(virConnectPtr conn,
return VIR_DRV_OPEN_SUCCESS; return VIR_DRV_OPEN_SUCCESS;
failure: failure:
VIR_FREE(uuid_db); VIR_FREE(uuid_db);
VIR_FREE(connection_data); VIR_FREE(connection_data);
VIR_FREE(string); VIR_FREE(string);
...@@ -146,63 +152,95 @@ static int ...@@ -146,63 +152,95 @@ static int
phypClose(virConnectPtr conn) phypClose(virConnectPtr conn)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
ssh_disconnect(ssh_session); libssh2_session_disconnect(session, "Disconnecting...");
libssh2_session_free(session);
VIR_FREE(connection_data); VIR_FREE(connection_data);
return 0; return 0;
} }
SSH_SESSION * LIBSSH2_SESSION *
openSSHSession(virConnectPtr conn, virConnectAuthPtr auth) openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
int *internal_socket)
{ {
SSH_SESSION *session; LIBSSH2_SESSION *session;
SSH_OPTIONS *opt; const char *hostname = conn->uri->server;
char *user = conn->uri->user; const char *username = conn->uri->user;
char *host = conn->uri->server; const char *password = NULL;
int ssh_auth = 0; int sock;
char *banner; int rc;
int port = 22;
char *password; struct addrinfo *ai = NULL, *cur;
struct addrinfo hints;
if (conn->uri->port) int ret;
port = conn->uri->port;
memset (&hints, '\0', sizeof (hints));
session = ssh_new(); hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
opt = ssh_options_new(); hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
/*setting some ssh options */
ssh_options_set_host(opt, host); ret = getaddrinfo (hostname, "22", &hints, &ai);
ssh_options_set_port(opt, port); if (ret != 0) {
ssh_options_set_username(opt, user); virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
ssh_set_options(session, opt); VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0,
_("Error while getting %s address info"),
/*starting ssh connection */ hostname);
if (ssh_connect(session)) {
virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, VIR_ERR_ERROR,
NULL, NULL, NULL, 0, 0, "%s",
_("Connection failed."));
ssh_disconnect(session);
ssh_finalize();
goto err; goto err;
} }
/*trying to use pub key */ cur = ai;
if ((ssh_auth = while (cur != NULL) {
ssh_userauth_autopubkey(session, NULL)) == SSH_AUTH_ERROR) { sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
VIR_WARN("%s", "Authentication with public key failed."); if (sock >= 0) {
if (connect (sock, cur->ai_addr, cur->ai_addrlen) == 0) {
goto connected;
}
close (sock);
}
cur = cur->ai_next;
} }
if ((banner = ssh_get_issue_banner(session))) { virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
VIR_INFO("%s", banner); VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0,
VIR_FREE(banner); _("Failed to connect to %s"), hostname);
freeaddrinfo (ai);
goto err;
connected:
(*internal_socket) = sock;
/* Create a session instance */
session = libssh2_session_init();
if (!session)
goto err;
/* tell libssh2 we want it all done non-blocking */
libssh2_session_set_blocking(session, 0);
while ((rc = libssh2_session_startup(session, sock)) ==
LIBSSH2_ERROR_EAGAIN) ;
if (rc) {
virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
_("Failure establishing SSH session."));
goto err;
} }
if (ssh_auth != SSH_AUTH_SUCCESS) { /* Trying authentication by pubkey */
while ((rc =
libssh2_userauth_publickey_fromfile(session, username,
"/home/user/"
".ssh/id_rsa.pub",
"/home/user/"
".ssh/id_rsa",
password)) ==
LIBSSH2_ERROR_EAGAIN) ;
if (rc) {
int i; int i;
int hasPassphrase = 0; int hasPassphrase = 0;
int auth_check = 0;
virConnectCredential creds[] = { virConnectCredential creds[] = {
{VIR_CRED_PASSPHRASE, "password", "Password", NULL, NULL, 0}, {VIR_CRED_PASSPHRASE, "password", "Password", NULL, NULL, 0},
...@@ -237,34 +275,32 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth) ...@@ -237,34 +275,32 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth)
goto err; goto err;
} }
if (creds[0].result) if (creds[0].result) {
password = creds[0].result; password = creds[0].result;
else { } else {
virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
_("Unable to get password certificate: %s"), _("Unable to get password certificates"));
ssh_get_error(session)); libssh2_session_disconnect(session, "Disconnecting...");
ssh_disconnect(session); libssh2_session_free(session);
goto err; goto err;
} }
char *username = user; while ((rc =
libssh2_userauth_password(session, username,
auth_check = ssh_userauth_password(session, username, password); password)) ==
memset(password, 0, strlen(password)); LIBSSH2_ERROR_EAGAIN) ;
if (auth_check != SSH_AUTH_SUCCESS) { if (rc) {
virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
_("Authentication failed: %s"), _("Authentication failed"));
ssh_get_error(session)); libssh2_session_disconnect(session, "Disconnecting");
ssh_disconnect(session); libssh2_session_free(session);
goto err; goto err;
} else } else
goto exit; goto exit;
} else }
goto exit;
err: err:
return NULL; return NULL;
...@@ -275,56 +311,73 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth) ...@@ -275,56 +311,73 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth)
/* this functions is the layer that manipulates the ssh channel itself /* this functions is the layer that manipulates the ssh channel itself
* and executes the commands on the remote machine */ * and executes the commands on the remote machine */
static char * static char *
phypExec(SSH_SESSION * session, char *cmd, int *exit_status, phypExec(LIBSSH2_SESSION * session, char *cmd, int *exit_status,
virConnectPtr conn) virConnectPtr conn)
{ {
CHANNEL *channel = channel_new(session); LIBSSH2_CHANNEL *channel;
ConnectionData *connection_data = conn->networkPrivateData;
virBuffer tex_ret = VIR_BUFFER_INITIALIZER; virBuffer tex_ret = VIR_BUFFER_INITIALIZER;
char buf[4096] = { 0 }; char buffer[0x4000] = { 0 };
int ret = 0; int exitcode;
int bytecount = 0;
int sock = connection_data->sock;
int rc = 0;
/* Exec non-blocking on the remove host */
while ((channel = libssh2_channel_open_session(session)) == NULL &&
libssh2_session_last_error(session, NULL, NULL, 0) ==
LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
}
if (channel_open_session(channel) == SSH_ERROR) { if (channel == NULL) {
virRaiseError(NULL, NULL, NULL, 0, VIR_FROM_PHYP,
VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
_("Unable to open a SSH channel."));
goto err; goto err;
} }
if (channel_request_exec(channel, cmd) == SSH_ERROR) { while ((rc = libssh2_channel_exec(channel, cmd)) ==
virRaiseError(NULL, NULL, NULL, 0, VIR_FROM_PHYP, LIBSSH2_ERROR_EAGAIN) {
VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", waitsocket(sock, session);
_("Unable to execute remote command."));
goto err;
} }
if (channel_send_eof(channel) == SSH_ERROR) { if (rc != 0) {
virRaiseError(NULL, NULL, NULL, 0, VIR_FROM_PHYP,
VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
_("Unable to send EOF."));
goto err; goto err;
} }
while (channel && channel_is_open(channel)) { for (;;) {
ret = channel_read(channel, buf, sizeof(buf), 0); /* loop until we block */
if (ret < 0) do {
goto err; rc = libssh2_channel_read(channel, buffer, sizeof(buffer));
if (rc > 0) {
if (ret == 0) { bytecount += rc;
channel_send_eof(channel); virBufferVSprintf(&tex_ret, "%s", buffer);
if (channel_get_exit_status(channel) == -1) }
goto err; }
while (rc > 0);
/* this is due to blocking that would occur otherwise so we loop on
* this condition */
if (rc == LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session);
} else {
break;
}
}
if (channel_close(channel) == SSH_ERROR) exitcode = 127;
goto err;
channel_free(channel); while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
channel = NULL; waitsocket(sock, session);
goto exit; }
}
virBufferAdd(&tex_ret, (const char *) &buf, ret); if (rc == 0) {
exitcode = libssh2_channel_get_exit_status(channel);
} }
(*exit_status) = exitcode;
libssh2_channel_free(channel);
channel = NULL;
goto exit;
err: err:
(*exit_status) = SSH_CMD_ERR; (*exit_status) = SSH_CMD_ERR;
char *cleanup_buf = virBufferContentAndReset(&tex_ret); char *cleanup_buf = virBufferContentAndReset(&tex_ret);
...@@ -342,7 +395,7 @@ phypExec(SSH_SESSION * session, char *cmd, int *exit_status, ...@@ -342,7 +395,7 @@ phypExec(SSH_SESSION * session, char *cmd, int *exit_status,
/* return the lpar_id given a name and a managed system name */ /* return the lpar_id given a name and a managed system name */
static int static int
phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system, phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
const char *name, virConnectPtr conn) const char *name, virConnectPtr conn)
{ {
int exit_status = 0; int exit_status = 0;
...@@ -356,13 +409,14 @@ phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system, ...@@ -356,13 +409,14 @@ phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system,
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
const char *tex_ret = phypExec(ssh_session, cmd, &exit_status, conn); const char *ret = phypExec(session, cmd, &exit_status, conn);
if (exit_status < 0 || tex_ret == NULL) if (exit_status < 0 || ret == NULL)
goto err; goto err;
if (virStrToLong_i(tex_ret, &char_ptr, 10, &lpar_id) == -1) if (virStrToLong_i(ret, &char_ptr, 10, &lpar_id) == -1)
goto err; goto err;
VIR_FREE(cmd); VIR_FREE(cmd);
...@@ -375,7 +429,7 @@ phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system, ...@@ -375,7 +429,7 @@ phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system,
/* return the lpar name given a lpar_id and a managed system name */ /* return the lpar name given a lpar_id and a managed system name */
static char * static char *
phypGetLparNAME(SSH_SESSION * ssh_session, const char *managed_system, phypGetLparNAME(LIBSSH2_SESSION * session, const char *managed_system,
unsigned int lpar_id, virConnectPtr conn) unsigned int lpar_id, virConnectPtr conn)
{ {
char *cmd; char *cmd;
...@@ -387,22 +441,23 @@ phypGetLparNAME(SSH_SESSION * ssh_session, const char *managed_system, ...@@ -387,22 +441,23 @@ phypGetLparNAME(SSH_SESSION * ssh_session, const char *managed_system,
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *lpar_name = phypExec(ssh_session, cmd, &exit_status, conn); char *ret = phypExec(session, cmd, &exit_status, conn);
if (lpar_name == NULL) if (ret == NULL)
goto err; goto err;
char *char_ptr = strchr(lpar_name, '\n'); char *char_ptr = strchr(ret, '\n');
if (char_ptr) if (char_ptr)
*char_ptr = '\0'; *char_ptr = '\0';
if (exit_status < 0 || lpar_name == NULL) if (exit_status < 0 || ret == NULL)
goto err; goto err;
VIR_FREE(cmd); VIR_FREE(cmd);
return lpar_name; return ret;
err: err:
VIR_FREE(cmd); VIR_FREE(cmd);
...@@ -443,7 +498,7 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id, ...@@ -443,7 +498,7 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
int type) int type)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *cmd; char *cmd;
char *char_ptr; char *char_ptr;
int memory = 0; int memory = 0;
...@@ -467,13 +522,14 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id, ...@@ -467,13 +522,14 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
goto err; goto err;
} }
} }
PHYP_CMD_DEBUG;
char *tex_ret = phypExec(ssh_session, cmd, &exit_status, conn); char *ret = phypExec(session, cmd, &exit_status, conn);
if (tex_ret == NULL) if (ret == NULL)
goto err; goto err;
char *mem_char_ptr = strchr(tex_ret, '\n'); char *mem_char_ptr = strchr(ret, '\n');
if (mem_char_ptr) if (mem_char_ptr)
*mem_char_ptr = '\0'; *mem_char_ptr = '\0';
...@@ -481,7 +537,7 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id, ...@@ -481,7 +537,7 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
if (exit_status < 0) if (exit_status < 0)
goto err; goto err;
if (virStrToLong_i(tex_ret, &char_ptr, 10, &memory) == -1) if (virStrToLong_i(ret, &char_ptr, 10, &memory) == -1)
goto err; goto err;
VIR_FREE(cmd); VIR_FREE(cmd);
...@@ -497,7 +553,7 @@ unsigned long ...@@ -497,7 +553,7 @@ unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id) phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *cmd; char *cmd;
int exit_status = 0; int exit_status = 0;
int vcpus = 0; int vcpus = 0;
...@@ -508,17 +564,18 @@ phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id) ...@@ -508,17 +564,18 @@ phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
char *tex_ret = phypExec(ssh_session, cmd, &exit_status, conn); PHYP_CMD_DEBUG;
char *ret = phypExec(session, cmd, &exit_status, conn);
if (tex_ret == NULL) if (ret == NULL)
goto err; goto err;
char *char_ptr = strchr(tex_ret, '\n'); char *char_ptr = strchr(ret, '\n');
if (char_ptr) if (char_ptr)
*char_ptr = '\0'; *char_ptr = '\0';
if (virStrToLong_i(tex_ret, &char_ptr, 10, &vcpus) == -1) if (virStrToLong_i(ret, &char_ptr, 10, &vcpus) == -1)
goto err; goto err;
if (exit_status < 0) if (exit_status < 0)
...@@ -537,7 +594,7 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system, ...@@ -537,7 +594,7 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
const char *lpar_name) const char *lpar_name)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *cmd; char *cmd;
char *char_ptr; char *char_ptr;
int remote_slot = 0; int remote_slot = 0;
...@@ -549,12 +606,13 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system, ...@@ -549,12 +606,13 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
char *tex_ret = phypExec(ssh_session, cmd, &exit_status, conn); PHYP_CMD_DEBUG;
char *ret = phypExec(session, cmd, &exit_status, conn);
if (tex_ret == NULL) if (ret == NULL)
goto err; goto err;
char *char_ptr2 = strchr(tex_ret, '\n'); char *char_ptr2 = strchr(ret, '\n');
if (char_ptr2) if (char_ptr2)
*char_ptr2 = '\0'; *char_ptr2 = '\0';
...@@ -562,7 +620,7 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system, ...@@ -562,7 +620,7 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
if (exit_status < 0) if (exit_status < 0)
goto err; goto err;
if (virStrToLong_i(tex_ret, &char_ptr, 10, &remote_slot) == -1) if (virStrToLong_i(ret, &char_ptr, 10, &remote_slot) == -1)
goto err; goto err;
VIR_FREE(cmd); VIR_FREE(cmd);
...@@ -578,7 +636,7 @@ phypGetBackingDevice(virConnectPtr conn, const char *managed_system, ...@@ -578,7 +636,7 @@ phypGetBackingDevice(virConnectPtr conn, const char *managed_system,
char *lpar_name) char *lpar_name)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *cmd; char *cmd;
int remote_slot = 0; int remote_slot = 0;
int exit_status = 0; int exit_status = 0;
...@@ -593,8 +651,9 @@ phypGetBackingDevice(virConnectPtr conn, const char *managed_system, ...@@ -593,8 +651,9 @@ phypGetBackingDevice(virConnectPtr conn, const char *managed_system,
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *ret = phypExec(ssh_session, cmd, &exit_status, conn); char *ret = phypExec(session, cmd, &exit_status, conn);
if (ret == NULL) if (ret == NULL)
goto err; goto err;
...@@ -639,7 +698,7 @@ int ...@@ -639,7 +698,7 @@ int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id) phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *cmd; char *cmd;
int exit_status = 0; int exit_status = 0;
char *char_ptr = NULL; char *char_ptr = NULL;
...@@ -664,8 +723,9 @@ phypGetLparState(virConnectPtr conn, unsigned int lpar_id) ...@@ -664,8 +723,9 @@ phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *ret = phypExec(ssh_session, cmd, &exit_status, conn); char *ret = phypExec(session, cmd, &exit_status, conn);
if (ret == NULL) if (ret == NULL)
goto err; goto err;
...@@ -697,7 +757,7 @@ int ...@@ -697,7 +757,7 @@ int
phypDiskType(virConnectPtr conn, char *backing_device) phypDiskType(virConnectPtr conn, char *backing_device)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *cmd; char *cmd;
int exit_status = 0; int exit_status = 0;
...@@ -707,8 +767,9 @@ phypDiskType(virConnectPtr conn, char *backing_device) ...@@ -707,8 +767,9 @@ phypDiskType(virConnectPtr conn, char *backing_device)
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *ret = phypExec(ssh_session, cmd, &exit_status, conn); char *ret = phypExec(session, cmd, &exit_status, conn);
if (ret == NULL) if (ret == NULL)
goto err; goto err;
...@@ -746,7 +807,7 @@ static int ...@@ -746,7 +807,7 @@ static int
phypNumDomainsGeneric(virConnectPtr conn, unsigned int type) phypNumDomainsGeneric(virConnectPtr conn, unsigned int type)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
int exit_status = 0; int exit_status = 0;
int ndom = 0; int ndom = 0;
char *char_ptr; char *char_ptr;
...@@ -780,8 +841,9 @@ phypNumDomainsGeneric(virConnectPtr conn, unsigned int type) ...@@ -780,8 +841,9 @@ phypNumDomainsGeneric(virConnectPtr conn, unsigned int type)
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *ret = phypExec(ssh_session, cmd, &exit_status, conn); char *ret = phypExec(session, cmd, &exit_status, conn);
if (exit_status < 0 || ret == NULL) if (exit_status < 0 || ret == NULL)
goto err; goto err;
...@@ -821,7 +883,7 @@ phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids, ...@@ -821,7 +883,7 @@ phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids,
unsigned int type) unsigned int type)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *managed_system = conn->uri->path; char *managed_system = conn->uri->path;
int exit_status = 0; int exit_status = 0;
int got = 0; int got = 0;
...@@ -857,21 +919,22 @@ phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids, ...@@ -857,21 +919,22 @@ phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids,
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
char *domains = phypExec(ssh_session, cmd, &exit_status, conn); PHYP_CMD_DEBUG;
char *ret = phypExec(session, cmd, &exit_status, conn);
/* I need to parse the textual return in order to get the domains */ /* I need to parse the textual return in order to get the ret */
if (exit_status < 0 || domains == NULL || got == 0) if (exit_status < 0 || ret == NULL)
goto err; goto err;
else { else {
while (got < nids) { while (got < nids) {
if (domains[i] == '\n') { if (ret[i] == '\n') {
if (virStrToLong_i(id_c, &char_ptr, 10, &ids[got]) == -1) if (virStrToLong_i(id_c, &char_ptr, 10, &ids[got]) == -1)
return 0; return 0;
memset(id_c, 0, 10); memset(id_c, 0, 10);
j = 0; j = 0;
got++; got++;
} else { } else {
id_c[j] = domains[i]; id_c[j] = ret[i];
j++; j++;
} }
i++; i++;
...@@ -896,7 +959,7 @@ static int ...@@ -896,7 +959,7 @@ static int
phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames) phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *managed_system = conn->uri->path; char *managed_system = conn->uri->path;
int exit_status = 0; int exit_status = 0;
int got = 0; int got = 0;
...@@ -923,8 +986,9 @@ phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames) ...@@ -923,8 +986,9 @@ phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
virReportOOMError(conn); virReportOOMError(conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *ret = phypExec(ssh_session, cmd, &exit_status, conn); char *ret = phypExec(session, cmd, &exit_status, conn);
if (VIR_ALLOC(domains) < 0) if (VIR_ALLOC(domains) < 0)
virReportOOMError(conn); virReportOOMError(conn);
...@@ -968,7 +1032,7 @@ static virDomainPtr ...@@ -968,7 +1032,7 @@ static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name) phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
int lpar_id = 0; int lpar_id = 0;
char *managed_system = conn->uri->path; char *managed_system = conn->uri->path;
...@@ -989,7 +1053,7 @@ phypDomainLookupByName(virConnectPtr conn, const char *lpar_name) ...@@ -989,7 +1053,7 @@ phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
if (char_ptr) if (char_ptr)
*char_ptr = '\0'; *char_ptr = '\0';
lpar_id = phypGetLparID(ssh_session, managed_system, lpar_name, conn); lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
if (lpar_id < 0) if (lpar_id < 0)
goto err; goto err;
...@@ -1013,7 +1077,7 @@ static virDomainPtr ...@@ -1013,7 +1077,7 @@ static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id) phypDomainLookupByID(virConnectPtr conn, int lpar_id)
{ {
ConnectionData *connection_data = conn->networkPrivateData; ConnectionData *connection_data = conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
char *managed_system = conn->uri->path; char *managed_system = conn->uri->path;
int exit_status = 0; int exit_status = 0;
...@@ -1034,7 +1098,7 @@ phypDomainLookupByID(virConnectPtr conn, int lpar_id) ...@@ -1034,7 +1098,7 @@ phypDomainLookupByID(virConnectPtr conn, int lpar_id)
if (char_ptr) if (char_ptr)
*char_ptr = '\0'; *char_ptr = '\0';
char *lpar_name = phypGetLparNAME(ssh_session, managed_system, lpar_id, char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
conn); conn);
if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1) if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
...@@ -1062,7 +1126,7 @@ static char * ...@@ -1062,7 +1126,7 @@ static char *
phypDomainDumpXML(virDomainPtr dom, int flags) phypDomainDumpXML(virDomainPtr dom, int flags)
{ {
ConnectionData *connection_data = dom->conn->networkPrivateData; ConnectionData *connection_data = dom->conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
virDomainDefPtr def = NULL; virDomainDefPtr def = NULL;
char *ret = NULL; char *ret = NULL;
char *managed_system = dom->conn->uri->path; char *managed_system = dom->conn->uri->path;
...@@ -1089,7 +1153,7 @@ phypDomainDumpXML(virDomainPtr dom, int flags) ...@@ -1089,7 +1153,7 @@ phypDomainDumpXML(virDomainPtr dom, int flags)
def->virtType = VIR_DOMAIN_VIRT_PHYP; def->virtType = VIR_DOMAIN_VIRT_PHYP;
def->id = dom->id; def->id = dom->id;
char *lpar_name = phypGetLparNAME(ssh_session, managed_system, def->id, char *lpar_name = phypGetLparNAME(session, managed_system, def->id,
dom->conn); dom->conn);
if (lpar_name == NULL) { if (lpar_name == NULL) {
...@@ -1136,7 +1200,7 @@ static int ...@@ -1136,7 +1200,7 @@ static int
phypDomainResume(virDomainPtr dom) phypDomainResume(virDomainPtr dom)
{ {
ConnectionData *connection_data = dom->conn->networkPrivateData; ConnectionData *connection_data = dom->conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *managed_system = dom->conn->uri->path; char *managed_system = dom->conn->uri->path;
int exit_status = 0; int exit_status = 0;
char *char_ptr = NULL; char *char_ptr = NULL;
...@@ -1161,8 +1225,9 @@ phypDomainResume(virDomainPtr dom) ...@@ -1161,8 +1225,9 @@ phypDomainResume(virDomainPtr dom)
virReportOOMError(dom->conn); virReportOOMError(dom->conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *ret = phypExec(ssh_session, cmd, &exit_status, dom->conn); char *ret = phypExec(session, cmd, &exit_status, dom->conn);
err: err:
VIR_FREE(cmd); VIR_FREE(cmd);
...@@ -1175,7 +1240,7 @@ static int ...@@ -1175,7 +1240,7 @@ static int
phypDomainShutdown(virDomainPtr dom) phypDomainShutdown(virDomainPtr dom)
{ {
ConnectionData *connection_data = dom->conn->networkPrivateData; ConnectionData *connection_data = dom->conn->networkPrivateData;
SSH_SESSION *ssh_session = connection_data->session; LIBSSH2_SESSION *session = connection_data->session;
char *managed_system = dom->conn->uri->path; char *managed_system = dom->conn->uri->path;
int exit_status = 0; int exit_status = 0;
char *char_ptr = NULL; char *char_ptr = NULL;
...@@ -1200,8 +1265,9 @@ phypDomainShutdown(virDomainPtr dom) ...@@ -1200,8 +1265,9 @@ phypDomainShutdown(virDomainPtr dom)
virReportOOMError(dom->conn); virReportOOMError(dom->conn);
goto err; goto err;
} }
PHYP_CMD_DEBUG;
char *ret = phypExec(ssh_session, cmd, &exit_status, dom->conn); char *ret = phypExec(session, cmd, &exit_status, dom->conn);
err: err:
VIR_FREE(cmd); VIR_FREE(cmd);
...@@ -1392,3 +1458,34 @@ escape_specialcharacters(char *src, char *dst, size_t dstlen) ...@@ -1392,3 +1458,34 @@ escape_specialcharacters(char *src, char *dst, size_t dstlen)
return 0; return 0;
} }
int
waitsocket(int socket_fd, LIBSSH2_SESSION * session)
{
struct timeval timeout;
int rc;
fd_set fd;
fd_set *writefd = NULL;
fd_set *readfd = NULL;
int dir;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
FD_ZERO(&fd);
FD_SET(socket_fd, &fd);
/* now make sure we wait in the correct direction */
dir = libssh2_session_block_directions(session);
if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
readfd = &fd;
if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
writefd = &fd;
rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
return rc;
}
#include <config.h> #include <config.h>
#include <libssh/libssh.h> #include <libssh2.h>
#define LPAR_EXEC_ERR -1 #define LPAR_EXEC_ERR -1
#define SSH_CONN_ERR -2 /* error while trying to connect to remote host */ #define SSH_CONN_ERR -2 /* error while trying to connect to remote host */
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
typedef struct _ConnectionData ConnectionData; typedef struct _ConnectionData ConnectionData;
typedef ConnectionData *ConnectionDataPtr; typedef ConnectionData *ConnectionDataPtr;
struct _ConnectionData { struct _ConnectionData {
SSH_SESSION *session; LIBSSH2_SESSION *session;
virConnectAuthPtr auth; virConnectAuthPtr auth;
int sock;
}; };
/* This is the lpar (domain) struct that relates /* This is the lpar (domain) struct that relates
...@@ -42,8 +43,6 @@ void stripPath(char *striped_path, char *path); ...@@ -42,8 +43,6 @@ void stripPath(char *striped_path, char *path);
void stripNewline(char *striped_string, char *string); void stripNewline(char *striped_string, char *string);
int buffer_add_u8(struct buffer_struct *buffer, u8 data);
int phypGetLparState(virConnectPtr conn, unsigned int lpar_id); int phypGetLparState(virConnectPtr conn, unsigned int lpar_id);
unsigned long phypGetLparMem(virConnectPtr conn, unsigned long phypGetLparMem(virConnectPtr conn,
...@@ -61,4 +60,6 @@ char *phypGetBackingDevice(virConnectPtr conn, const char *managed_system, ...@@ -61,4 +60,6 @@ char *phypGetBackingDevice(virConnectPtr conn, const char *managed_system,
int phypDiskType(virConnectPtr conn, char *backing_device); int phypDiskType(virConnectPtr conn, char *backing_device);
SSH_SESSION *openSSHSession(virConnectPtr conn, virConnectAuthPtr auth); LIBSSH2_SESSION *openSSHSession(virConnectPtr conn, virConnectAuthPtr auth, int *internal_socket);
int waitsocket(int socket_fd, LIBSSH2_SESSION * session);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册