Hello friends, This is the new version of phyp driver now using libssh2. I also did some other changes: * Added some debug information. Sometimes its worth to know which command is being executed and where. So I added the PHYP_CMD_DEBUG macro. * All the connection startup is now "hard coded" in libssh2. For example, I need to open the socket and start the connection all by myself. Not a big deal, but in a near future I am planning to send a patch to libssh2 to encapsulate all these procedures into functions, hence, less code, probably less bugs :) * There is a lot of "out of scope" functions in the bottom of the file. In the next patch I may put all those together in a phyp_config.[ch] in order to get things clearer. What's up next: * The problem of handling the UUID in a centralized way still remains. I started to handle this by opening a sftp connection to the HMC system and transfer my UUID database. But there is an odd behavior that makes the driver connect OR NOT to some HMC systems. * CPU management: this feature planned to be released in this version is not ready yet due to the issue above. * Storage management. Vacations: I'll be on vacations from 26th/august to 14th/september. I'll check my emails in the meanwhile, but no so often. Anyway, any comments on this patch are always welcome :) []'s -- Eduardo Otubo Software Engineer Linux Technology Center IBM Systems & Technology Group Mobile: +55 19 8135 0885 otubo@xxxxxxxxxxxxxxxxxx
diff --git a/configure.in b/configure.in index d28c44a..4e933f8 100644 --- a/configure.in +++ b/configure.in @@ -187,8 +187,8 @@ AC_ARG_WITH([uml], [ --with-uml add UML support (on)],[],[with_uml=check]) AC_ARG_WITH([openvz], [ --with-openvz add OpenVZ support (on)],[],[with_openvz=yes]) -AC_ARG_WITH([libssh], -[ --with-libssh=[PFX] libssh location],[],[with_libssh=yes]) +AC_ARG_WITH([libssh2], +[ --with-libssh2=[PFX] libssh2 location],[],[with_libssh2=yes]) AC_ARG_WITH([phyp], [ --with-phyp=[PFX] add PHYP support (on)],[],[with_phyp=check]) AC_ARG_WITH([vbox], @@ -866,45 +866,45 @@ dnl dnl libssh checks dnl -if test "$with_libssh" != "yes" -a "$with_libssh" != "no"; then - libssh_path="$with_libssh" -elif test "$with_libssh" = "yes"; then - libssh_path="/usr/local/lib/" -elif test "$with_libssh" = "no"; then +if test "$with_libssh2" != "yes" -a "$with_libssh2" != "no"; then + libssh2_path="$with_libssh2" +elif test "$with_libssh2" = "yes"; then + libssh2_path="/usr/local/lib/" +elif test "$with_libssh2" = "no"; then with_phyp="no"; fi if test "$with_phyp" = "check"; then - AC_CHECK_LIB([ssh],[ssh_new],[ - LIBSSH_LIBS="$LIBSSH_LIBS -lssh -L$libssh_path" - AC_SUBST([LIBSSH_LIBS])],[ + AC_CHECK_LIB([ssh2],[libssh2_session_startup],[ + LIBSSH2_LIBS="$LIBSSH2_LIBS -lssh2 -L$libssh2_path" + AC_SUBST([LIBSSH2_LIBS])],[ with_phyp="no" - with_libssh="no"; + with_libssh2="no"; ],[]) if test "$with_phyp" != "no"; then - AC_CHECK_HEADERS([libssh/libssh.h],[ + AC_CHECK_HEADERS([libssh2.h],[ with_phyp="yes" - LIBSSH_CFLAGS="-I/usr/local/include/libssh" - AC_SUBST([LIBSSH_CFLAGS]) + LIBSSH2_CFLAGS="-I/usr/local/include" + AC_SUBST([LIBSSH2_CFLAGS]) AC_DEFINE_UNQUOTED([WITH_PHYP], 1, [whether IBM HMC / IVM driver is enabled]) ],[ with_phyp="no" - with_libssh="no"; + with_libssh2="no"; ],[]) fi elif test "$with_phyp" = "yes"; then - AC_CHECK_LIB([ssh],[ssh_new],[ - LIBSSH_LIBS="$LIBSSH_LIBS -lssh -L$libssh_path" - AC_SUBST([LIBSSH_LIBS])],[ - AC_MSG_ERROR([You must install the libssh to compile Phype driver.]) + AC_CHECK_LIB([ssh2],[libssh2_session_startup],[ + LIBSSH2_LIBS="$LIBSSH2_LIBS -lssh2 -L$libssh2_path" + AC_SUBST([LIBSSH2_LIBS])],[ + AC_MSG_ERROR([You must install the libssh2 to compile Phype driver.]) ]) - AC_CHECK_HEADERS([libssh/libssh.h],[ - LIBSSH_CFLAGS="-I/usr/local/include/libssh" - AC_SUBST([LIBSSH_CFLAGS])],[ - AC_MSG_ERROR([Cannot find libssh headers.Is libssh installed ?]) + AC_CHECK_HEADERS([libssh2.h],[ + LIBSSH2_CFLAGS="-I/usr/local/include" + AC_SUBST([LIBSSH2_CFLAGS])],[ + AC_MSG_ERROR([Cannot find libssh2 headers.Is libssh2 installed ?]) ],[]) AC_DEFINE_UNQUOTED([WITH_PHYP], 1, [whether IBM HMC / IVM driver is enabled]) @@ -1699,10 +1699,10 @@ AC_MSG_NOTICE([ libcurl: $LIBCURL_CFLAGS $LIBCURL_LIBS]) else AC_MSG_NOTICE([ libcurl: no]) fi -if test "$with_libssh" != "no" ; then -AC_MSG_NOTICE([ libssh: $LIBSSH_CFLAGS $LIBSSH_LIBS]) +if test "$with_libssh2" != "no" ; then +AC_MSG_NOTICE([ libssh2: $LIBSSH2_CFLAGS $LIBSSH2_LIBS]) else -AC_MSG_NOTICE([ libssh: no]) +AC_MSG_NOTICE([ libssh2: no]) fi AC_MSG_NOTICE([ gnutls: $GNUTLS_CFLAGS $GNUTLS_LIBS]) if test "$with_sasl" != "no" ; then diff --git a/src/Makefile.am b/src/Makefile.am index 9567490..c7f6299 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,7 +6,7 @@ INCLUDES = \ -I@top_srcdir@/include \ -I@top_srcdir@/qemud \ $(LIBXML_CFLAGS) \ - $(LIBSSH_CFLAGS) \ + $(LIBSSH2_CFLAGS) \ $(XEN_CFLAGS) \ $(SELINUX_CFLAGS) \ $(DRIVER_MODULE_CFLAGS) \ @@ -307,8 +307,8 @@ else noinst_LTLIBRARIES += libvirt_driver_phyp.la libvirt_la_LIBADD += libvirt_driver_phyp.la endif -libvirt_driver_phyp_la_LDFLAGS = $(LIBSSH_LIBS) -libvirt_driver_phyp_la_CFLAGS = $(LIBSSH_CFLAGS) +libvirt_driver_phyp_la_LDFLAGS = $(LIBSSH2_LIBS) +libvirt_driver_phyp_la_CFLAGS = $(LIBSSH2_CFLAGS) libvirt_driver_phyp_la_SOURCES = $(PHYP_DRIVER_SOURCES) endif diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c index 3100144..861a9c8 100644 --- a/src/phyp/phyp_driver.c +++ b/src/phyp/phyp_driver.c @@ -34,8 +34,11 @@ #include <unistd.h> #include <errno.h> #include <stdio.h> - -#include <libssh/libssh.h> +#include <libssh2.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <sys/socket.h> +#include <netdb.h> #include "internal.h" #include "util.h" @@ -53,6 +56,10 @@ #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); /* @@ -63,11 +70,12 @@ static virDrvOpenStatus phypOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) { - SSH_SESSION *session = NULL; + LIBSSH2_SESSION *session = NULL; ConnectionData *connection_data = NULL; char *string; size_t len = 0; uuid_dbPtr uuid_db = NULL; + int internal_socket; if (!conn || !conn->uri) return VIR_DRV_OPEN_DECLINED; @@ -113,15 +121,13 @@ phypOpen(virConnectPtr conn, goto failure; } - if ((session = openSSHSession(conn, auth)) == NULL) { + if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) { virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", _("Error while opening SSH session.")); goto failure; } - - VIR_FREE(conn->uri->path); - conn->uri->path = string; + //conn->uri->path = string; connection_data->session = session; connection_data->auth = auth; @@ -134,7 +140,7 @@ phypOpen(virConnectPtr conn, return VIR_DRV_OPEN_SUCCESS; -failure: + failure: VIR_FREE(uuid_db); VIR_FREE(connection_data); VIR_FREE(string); @@ -146,63 +152,81 @@ static int phypClose(virConnectPtr conn) { 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); return 0; } -SSH_SESSION * -openSSHSession(virConnectPtr conn, virConnectAuthPtr auth) +LIBSSH2_SESSION * +openSSHSession(virConnectPtr conn, virConnectAuthPtr auth, + int *internal_socket) { - SSH_SESSION *session; - SSH_OPTIONS *opt; - char *user = conn->uri->user; - char *host = conn->uri->server; - int ssh_auth = 0; - char *banner; + LIBSSH2_SESSION *session; + const char *hostname = conn->uri->server; + const char *username = conn->uri->user; + const char *password = NULL; + int sock; + struct sockaddr_in sin; + int rc; int port = 22; - char *password; - - if (conn->uri->port) - port = conn->uri->port; - - session = ssh_new(); - opt = ssh_options_new(); - - /*setting some ssh options */ - ssh_options_set_host(opt, host); - ssh_options_set_port(opt, port); - ssh_options_set_username(opt, user); - ssh_set_options(session, opt); - - /*starting ssh connection */ - 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; + struct hostent *hp; + struct in_addr **pptr; + + sock = socket(AF_INET, SOCK_STREAM, 0); + + if ((hp = gethostbyname(hostname)) == NULL) { + return NULL; + } + + pptr = (struct in_addr **) hp->h_addr_list; + if (*pptr != NULL) { + sin.sin_addr.s_addr = inet_addr(inet_ntoa(**(pptr))); } - /*trying to use pub key */ - if ((ssh_auth = - ssh_userauth_autopubkey(session, NULL)) == SSH_AUTH_ERROR) { - VIR_WARN("%s", "Authentication with public key failed."); + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + if (connect(sock, (struct sockaddr *) (&sin), + sizeof(struct sockaddr_in)) != 0) { + virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, + VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", + _("Error while opening socket.")); + goto err; } + (*internal_socket) = sock; - if ((banner = ssh_get_issue_banner(session))) { - VIR_INFO("%s", banner); - VIR_FREE(banner); + /* 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 hasPassphrase = 0; - int auth_check = 0; virConnectCredential creds[] = { {VIR_CRED_PASSPHRASE, "password", "Password", NULL, NULL, 0}, @@ -237,34 +261,32 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth) goto err; } - if (creds[0].result) + if (creds[0].result) { password = creds[0].result; - else { + } else { virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, - VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, - _("Unable to get password certificate: %s"), - ssh_get_error(session)); - ssh_disconnect(session); + VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", + _("Unable to get password certificates")); + libssh2_session_disconnect(session, "Disconnecting..."); + libssh2_session_free(session); goto err; } - char *username = user; - - auth_check = ssh_userauth_password(session, username, password); - memset(password, 0, strlen(password)); + while ((rc = + libssh2_userauth_password(session, username, + password)) == + LIBSSH2_ERROR_EAGAIN) ; - if (auth_check != SSH_AUTH_SUCCESS) { + if (rc) { virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP, - VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, - _("Authentication failed: %s"), - ssh_get_error(session)); - ssh_disconnect(session); + VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", + _("Authentication failed")); + libssh2_session_disconnect(session, "Disconnecting"); + libssh2_session_free(session); goto err; } else goto exit; - } else - goto exit; - + } err: return NULL; @@ -275,56 +297,73 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth) /* this functions is the layer that manipulates the ssh channel itself * and executes the commands on the remote machine */ static char * -phypExec(SSH_SESSION * session, char *cmd, int *exit_status, +phypExec(LIBSSH2_SESSION * session, char *cmd, int *exit_status, virConnectPtr conn) { - CHANNEL *channel = channel_new(session); + LIBSSH2_CHANNEL *channel; + ConnectionData *connection_data = conn->networkPrivateData; virBuffer tex_ret = VIR_BUFFER_INITIALIZER; - char buf[4096] = { 0 }; - int ret = 0; + char buffer[0x4000] = { 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) { - virRaiseError(NULL, NULL, NULL, 0, VIR_FROM_PHYP, - VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", - _("Unable to open a SSH channel.")); + if (channel == NULL) { goto err; } - if (channel_request_exec(channel, cmd) == SSH_ERROR) { - virRaiseError(NULL, NULL, NULL, 0, VIR_FROM_PHYP, - VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", - _("Unable to execute remote command.")); - goto err; + while ((rc = libssh2_channel_exec(channel, cmd)) == + LIBSSH2_ERROR_EAGAIN) { + waitsocket(sock, session); } - if (channel_send_eof(channel) == SSH_ERROR) { - virRaiseError(NULL, NULL, NULL, 0, VIR_FROM_PHYP, - VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", - _("Unable to send EOF.")); + if (rc != 0) { goto err; } - while (channel && channel_is_open(channel)) { - ret = channel_read(channel, buf, sizeof(buf), 0); - if (ret < 0) - goto err; - - if (ret == 0) { - channel_send_eof(channel); - if (channel_get_exit_status(channel) == -1) - goto err; + for (;;) { + /* loop until we block */ + do { + rc = libssh2_channel_read(channel, buffer, sizeof(buffer)); + if (rc > 0) { + bytecount += rc; + virBufferVSprintf(&tex_ret, "%s", buffer); + } + } + 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) - goto err; + exitcode = 127; - channel_free(channel); - channel = NULL; - goto exit; - } + while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) { + waitsocket(sock, session); + } - 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: (*exit_status) = SSH_CMD_ERR; char *cleanup_buf = virBufferContentAndReset(&tex_ret); @@ -342,7 +381,7 @@ phypExec(SSH_SESSION * session, char *cmd, int *exit_status, /* return the lpar_id given a name and a managed system name */ static int -phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system, +phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system, const char *name, virConnectPtr conn) { int exit_status = 0; @@ -356,13 +395,14 @@ phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system, virReportOOMError(conn); 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; - if (virStrToLong_i(tex_ret, &char_ptr, 10, &lpar_id) == -1) + if (virStrToLong_i(ret, &char_ptr, 10, &lpar_id) == -1) goto err; VIR_FREE(cmd); @@ -375,7 +415,7 @@ phypGetLparID(SSH_SESSION * ssh_session, const char *managed_system, /* return the lpar name given a lpar_id and a managed system name */ 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) { char *cmd; @@ -387,22 +427,23 @@ phypGetLparNAME(SSH_SESSION * ssh_session, const char *managed_system, virReportOOMError(conn); 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; - char *char_ptr = strchr(lpar_name, '\n'); + char *char_ptr = strchr(ret, '\n'); if (char_ptr) *char_ptr = '\0'; - if (exit_status < 0 || lpar_name == NULL) + if (exit_status < 0 || ret == NULL) goto err; VIR_FREE(cmd); - return lpar_name; + return ret; err: VIR_FREE(cmd); @@ -443,7 +484,7 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id, int type) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *cmd; char *char_ptr; int memory = 0; @@ -467,13 +508,14 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id, 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; - char *mem_char_ptr = strchr(tex_ret, '\n'); + char *mem_char_ptr = strchr(ret, '\n'); if (mem_char_ptr) *mem_char_ptr = '\0'; @@ -481,7 +523,7 @@ phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id, if (exit_status < 0) goto err; - if (virStrToLong_i(tex_ret, &char_ptr, 10, &memory) == -1) + if (virStrToLong_i(ret, &char_ptr, 10, &memory) == -1) goto err; VIR_FREE(cmd); @@ -497,7 +539,7 @@ unsigned long phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *cmd; int exit_status = 0; int vcpus = 0; @@ -508,17 +550,18 @@ phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id) virReportOOMError(conn); 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; - char *char_ptr = strchr(tex_ret, '\n'); + char *char_ptr = strchr(ret, '\n'); if (char_ptr) *char_ptr = '\0'; - if (virStrToLong_i(tex_ret, &char_ptr, 10, &vcpus) == -1) + if (virStrToLong_i(ret, &char_ptr, 10, &vcpus) == -1) goto err; if (exit_status < 0) @@ -537,7 +580,7 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system, const char *lpar_name) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *cmd; char *char_ptr; int remote_slot = 0; @@ -549,12 +592,13 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system, virReportOOMError(conn); 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; - char *char_ptr2 = strchr(tex_ret, '\n'); + char *char_ptr2 = strchr(ret, '\n'); if (char_ptr2) *char_ptr2 = '\0'; @@ -562,7 +606,7 @@ phypGetRemoteSlot(virConnectPtr conn, const char *managed_system, if (exit_status < 0) 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; VIR_FREE(cmd); @@ -578,7 +622,7 @@ phypGetBackingDevice(virConnectPtr conn, const char *managed_system, char *lpar_name) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *cmd; int remote_slot = 0; int exit_status = 0; @@ -593,8 +637,9 @@ phypGetBackingDevice(virConnectPtr conn, const char *managed_system, virReportOOMError(conn); 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) goto err; @@ -639,7 +684,7 @@ int phypGetLparState(virConnectPtr conn, unsigned int lpar_id) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *cmd; int exit_status = 0; char *char_ptr = NULL; @@ -664,8 +709,9 @@ phypGetLparState(virConnectPtr conn, unsigned int lpar_id) virReportOOMError(conn); 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) goto err; @@ -697,7 +743,7 @@ int phypDiskType(virConnectPtr conn, char *backing_device) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *cmd; int exit_status = 0; @@ -707,8 +753,9 @@ phypDiskType(virConnectPtr conn, char *backing_device) virReportOOMError(conn); 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) goto err; @@ -746,7 +793,7 @@ static int phypNumDomainsGeneric(virConnectPtr conn, unsigned int type) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; int exit_status = 0; int ndom = 0; char *char_ptr; @@ -780,8 +827,9 @@ phypNumDomainsGeneric(virConnectPtr conn, unsigned int type) virReportOOMError(conn); 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) goto err; @@ -821,7 +869,7 @@ phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids, unsigned int type) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *managed_system = conn->uri->path; int exit_status = 0; int got = 0; @@ -857,21 +905,22 @@ phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids, virReportOOMError(conn); 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 */ - if (exit_status < 0 || domains == NULL || got == 0) + /* I need to parse the textual return in order to get the ret */ + if (exit_status < 0 || ret == NULL) goto err; else { while (got < nids) { - if (domains[i] == '\n') { + if (ret[i] == '\n') { if (virStrToLong_i(id_c, &char_ptr, 10, &ids[got]) == -1) return 0; memset(id_c, 0, 10); j = 0; got++; } else { - id_c[j] = domains[i]; + id_c[j] = ret[i]; j++; } i++; @@ -896,7 +945,7 @@ static int phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; char *managed_system = conn->uri->path; int exit_status = 0; int got = 0; @@ -923,8 +972,9 @@ phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames) virReportOOMError(conn); 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) virReportOOMError(conn); @@ -968,7 +1018,7 @@ static virDomainPtr phypDomainLookupByName(virConnectPtr conn, const char *lpar_name) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; virDomainPtr dom = NULL; int lpar_id = 0; char *managed_system = conn->uri->path; @@ -989,7 +1039,7 @@ phypDomainLookupByName(virConnectPtr conn, const char *lpar_name) if (char_ptr) *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) goto err; @@ -1013,7 +1063,7 @@ static virDomainPtr phypDomainLookupByID(virConnectPtr conn, int lpar_id) { ConnectionData *connection_data = conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; virDomainPtr dom = NULL; char *managed_system = conn->uri->path; int exit_status = 0; @@ -1034,7 +1084,7 @@ phypDomainLookupByID(virConnectPtr conn, int lpar_id) if (char_ptr) *char_ptr = '\0'; - char *lpar_name = phypGetLparNAME(ssh_session, managed_system, lpar_id, + char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id, conn); if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1) @@ -1062,7 +1112,7 @@ static char * phypDomainDumpXML(virDomainPtr dom, int flags) { ConnectionData *connection_data = dom->conn->networkPrivateData; - SSH_SESSION *ssh_session = connection_data->session; + LIBSSH2_SESSION *session = connection_data->session; virDomainDefPtr def = NULL; char *ret = NULL; char *managed_system = dom->conn->uri->path; @@ -1089,7 +1139,7 @@ phypDomainDumpXML(virDomainPtr dom, int flags) def->virtType = VIR_DOMAIN_VIRT_PHYP; 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); if (lpar_name == NULL) { @@ -1136,7 +1186,7 @@ static int phypDomainResume(virDomainPtr dom) { 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; int exit_status = 0; char *char_ptr = NULL; @@ -1161,8 +1211,9 @@ phypDomainResume(virDomainPtr dom) virReportOOMError(dom->conn); 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: VIR_FREE(cmd); @@ -1175,7 +1226,7 @@ static int phypDomainShutdown(virDomainPtr dom) { 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; int exit_status = 0; char *char_ptr = NULL; @@ -1200,8 +1251,9 @@ phypDomainShutdown(virDomainPtr dom) virReportOOMError(dom->conn); 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: VIR_FREE(cmd); @@ -1392,3 +1444,34 @@ escape_specialcharacters(char *src, char *dst, size_t dstlen) 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; +} diff --git a/src/phyp/phyp_driver.h b/src/phyp/phyp_driver.h index f16b6fe..ee2ec32 100644 --- a/src/phyp/phyp_driver.h +++ b/src/phyp/phyp_driver.h @@ -1,5 +1,5 @@ #include <config.h> -#include <libssh/libssh.h> +#include <libssh2.h> #define LPAR_EXEC_ERR -1 #define SSH_CONN_ERR -2 /* error while trying to connect to remote host */ @@ -8,8 +8,9 @@ typedef struct _ConnectionData ConnectionData; typedef ConnectionData *ConnectionDataPtr; struct _ConnectionData { - SSH_SESSION *session; + LIBSSH2_SESSION *session; virConnectAuthPtr auth; + int sock; }; /* This is the lpar (domain) struct that relates @@ -42,8 +43,6 @@ void stripPath(char *striped_path, char *path); 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); unsigned long phypGetLparMem(virConnectPtr conn, @@ -61,4 +60,6 @@ char *phypGetBackingDevice(virConnectPtr conn, const char *managed_system, 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);
-- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list