phyp_driver.c 39.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/*
 * Copyright IBM Corp. 2009
 *
 * phyp_driver.c: ssh layer to access Power Hypervisors
 *
 * Authors:
 *  Eduardo Otubo <otubo at linux.vnet.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <config.h>

#include <sys/types.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
37 38 39 40 41
#include <libssh2.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

#include "internal.h"
#include "util.h"
#include "datatypes.h"
#include "buf.h"
#include "memory.h"
#include "logging.h"
#include "driver.h"
#include "libvirt/libvirt.h"
#include "virterror_internal.h"
#include "uuid.h"
#include "domain_conf.h"

#include "phyp_driver.h"

#define VIR_FROM_THIS VIR_FROM_PHYP

D
Daniel Veillard 已提交
59
#define PHYP_CMD_DEBUG VIR_DEBUG("COMMAND:%s\n",cmd);
60

61 62
static int escape_specialcharacters(char *src, char *dst, size_t dstlen);

63 64 65 66 67 68 69 70
/*
 * URI: phyp://user@[hmc|ivm]/managed_system
 * */

static virDrvOpenStatus
phypOpen(virConnectPtr conn,
         virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
{
71
    LIBSSH2_SESSION *session = NULL;
72 73
    ConnectionData *connection_data = NULL;
    char *string;
74
    size_t len = 0;
75
    uuid_dbPtr uuid_db = NULL;
76
    int internal_socket;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    if (!conn || !conn->uri)
        return VIR_DRV_OPEN_DECLINED;

    if (conn->uri->scheme == NULL || STRNEQ(conn->uri->scheme, "phyp"))
        return VIR_DRV_OPEN_DECLINED;

    if (conn->uri->server == NULL) {
        virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                      VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                      _("Missing server name in phyp:// URI"));
        return VIR_DRV_OPEN_ERROR;
    }

    if (conn->uri->path == NULL) {
        virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                      VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                      _("Missing path name in phyp:// URI"));
        return VIR_DRV_OPEN_ERROR;
    }

98 99 100 101 102 103 104 105 106 107
    if (VIR_ALLOC(uuid_db) < 0) {
        virReportOOMError(conn);
        goto failure;
    }

    if (VIR_ALLOC(connection_data) < 0) {
        virReportOOMError(conn);
        goto failure;
    }

108 109 110
    len = strlen(conn->uri->path) + 1;

    if (VIR_ALLOC_N(string, len) < 0) {
111 112 113 114
        virReportOOMError(conn);
        goto failure;
    }

115
    if (escape_specialcharacters(conn->uri->path, string, len) == -1) {
116 117 118
        virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                      VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                      _("Error parsing 'path'. Invalid characters."));
119
        goto failure;
120 121
    }

122
    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
123 124 125
        virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                      VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                      _("Error while opening SSH session."));
126
        goto failure;
127
    }
128
    //conn->uri->path = string;
129 130 131 132 133 134 135 136 137 138 139
    connection_data->session = session;
    connection_data->auth = auth;

    uuid_db->nlpars = 0;
    uuid_db->lpars = NULL;

    conn->privateData = uuid_db;
    conn->networkPrivateData = connection_data;
    init_uuid_db(conn);

    return VIR_DRV_OPEN_SUCCESS;
140

141
  failure:
142 143 144 145 146
    VIR_FREE(uuid_db);
    VIR_FREE(connection_data);
    VIR_FREE(string);

    return VIR_DRV_OPEN_ERROR;
147 148 149 150 151 152
}

static int
phypClose(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
153
    LIBSSH2_SESSION *session = connection_data->session;
154

155 156
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
157 158 159 160 161

    VIR_FREE(connection_data);
    return 0;
}

162 163 164
LIBSSH2_SESSION *
openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
               int *internal_socket)
165
{
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    LIBSSH2_SESSION *session;
    const char *hostname = conn->uri->server;
    const char *username = conn->uri->user;
    const char *password = NULL;
    int sock;
    int rc;

    struct addrinfo *ai = NULL, *cur;
    struct addrinfo hints;
    int ret;

    memset (&hints, '\0', sizeof (hints));
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

    ret = getaddrinfo (hostname, "22", &hints, &ai);
    if (ret != 0) {
        virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                      VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0,
                      _("Error while getting %s address info"),
                      hostname);
188 189 190
        goto err;
    }

191 192 193 194 195 196 197 198 199 200
    cur = ai;
    while (cur != NULL) {
        sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
        if (sock >= 0) {
            if (connect (sock, cur->ai_addr, cur->ai_addrlen) == 0) {
                goto connected;
            }
            close (sock);
        }
        cur = cur->ai_next;
201 202
    }

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                  VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0,
                  _("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;
228 229
    }

230 231 232 233 234 235 236 237 238 239
    /* 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) {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        int i;
        int hasPassphrase = 0;

        virConnectCredential creds[] = {
            {VIR_CRED_PASSPHRASE, "password", "Password", NULL, NULL, 0},
        };

        if (!auth || !auth->cb) {
            virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                          VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                          _("No authentication callback provided."));
            goto err;
        }

        for (i = 0; i < auth->ncredtype; i++) {
            if (auth->credtype[i] == VIR_CRED_PASSPHRASE)
                hasPassphrase = 1;
        }

        if (!hasPassphrase) {
            virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                          VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                          _("Required credentials are not supported."));
            goto err;
        }

        int res =
            (auth->cb) (creds, ARRAY_CARDINALITY(creds), auth->cbdata);

        if (res < 0) {
            virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
                          VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                          _("Unable to fetch credentials."));
            goto err;
        }

276
        if (creds[0].result) {
277
            password = creds[0].result;
278
        } else {
279
            virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
280 281 282 283
                          VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                          _("Unable to get password certificates"));
            libssh2_session_disconnect(session, "Disconnecting...");
            libssh2_session_free(session);
284 285 286
            goto err;
        }

287 288 289 290
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
               LIBSSH2_ERROR_EAGAIN) ;
291

292
        if (rc) {
293
            virRaiseError(conn, NULL, NULL, 0, VIR_FROM_PHYP,
294 295 296 297
                          VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s",
                          _("Authentication failed"));
            libssh2_session_disconnect(session, "Disconnecting");
            libssh2_session_free(session);
298 299 300
            goto err;
        } else
            goto exit;
301
    }
302 303 304 305 306 307 308 309 310 311
  err:
    return NULL;

  exit:
    return session;
}

/* this functions is the layer that manipulates the ssh channel itself
 * and executes the commands on the remote machine */
static char *
312
phypExec(LIBSSH2_SESSION * session, char *cmd, int *exit_status,
313 314
         virConnectPtr conn)
{
315 316
    LIBSSH2_CHANNEL *channel;
    ConnectionData *connection_data = conn->networkPrivateData;
317
    virBuffer tex_ret = VIR_BUFFER_INITIALIZER;
318 319 320 321 322 323 324 325 326 327 328 329
    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);
    }
330

331
    if (channel == NULL) {
332 333 334
        goto err;
    }

335 336 337
    while ((rc = libssh2_channel_exec(channel, cmd)) ==
           LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
338 339
    }

340
    if (rc != 0) {
341 342 343
        goto err;
    }

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    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;
        }
    }
363

364
    exitcode = 127;
365

366 367 368
    while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
    }
369

370 371
    if (rc == 0) {
        exitcode = libssh2_channel_get_exit_status(channel);
372 373
    }

374 375 376 377 378
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
    goto exit;

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
  err:
    (*exit_status) = SSH_CMD_ERR;
    char *cleanup_buf = virBufferContentAndReset(&tex_ret);

    VIR_FREE(cleanup_buf);
    return NULL;

  exit:
    if (virBufferError(&tex_ret)) {
        virReportOOMError(conn);
        return NULL;
    }
    return virBufferContentAndReset(&tex_ret);
}

/* return the lpar_id given a name and a managed system name */
static int
396
phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
397 398 399 400 401 402 403 404 405 406 407 408 409
              const char *name, virConnectPtr conn)
{
    int exit_status = 0;
    int lpar_id = 0;
    char *char_ptr;
    char *cmd;

    if (virAsprintf(&cmd,
                    "lssyscfg -r lpar -m %s --filter lpar_names=%s -F lpar_id",
                    managed_system, name) < 0) {
        virReportOOMError(conn);
        goto err;
    }
410
    PHYP_CMD_DEBUG;
411

412
    const char *ret = phypExec(session, cmd, &exit_status, conn);
413

414
    if (exit_status < 0 || ret == NULL)
415 416
        goto err;

417
    if (virStrToLong_i(ret, &char_ptr, 10, &lpar_id) == -1)
418 419 420 421 422 423 424 425 426 427 428 429
        goto err;

    VIR_FREE(cmd);
    return lpar_id;

  err:
    VIR_FREE(cmd);
    return -1;
}

/* return the lpar name given a lpar_id and a managed system name */
static char *
430
phypGetLparNAME(LIBSSH2_SESSION * session, const char *managed_system,
431 432 433 434 435 436 437 438 439 440 441
                unsigned int lpar_id, virConnectPtr conn)
{
    char *cmd;
    int exit_status = 0;

    if (virAsprintf(&cmd,
                    "lssyscfg -r lpar -m %s --filter lpar_ids=%d -F name",
                    managed_system, lpar_id) < 0) {
        virReportOOMError(conn);
        goto err;
    }
442
    PHYP_CMD_DEBUG;
443

444
    char *ret = phypExec(session, cmd, &exit_status, conn);
445

446
    if (ret == NULL)
447 448
        goto err;

449
    char *char_ptr = strchr(ret, '\n');
450 451 452 453

    if (char_ptr)
        *char_ptr = '\0';

454
    if (exit_status < 0 || ret == NULL)
455 456 457
        goto err;

    VIR_FREE(cmd);
458
    return ret;
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

  err:
    VIR_FREE(cmd);
    return NULL;
}


/* Search into the uuid_db for a lpar_uuid given a lpar_id
 * and a managed system name
 *
 * return:  0 - record found
 *         -1 - not found
 * */
int
phypGetLparUUID(unsigned char *uuid, int lpar_id, virConnectPtr conn)
{
    uuid_dbPtr uuid_db = conn->privateData;
    lparPtr *lpars = uuid_db->lpars;
    unsigned int i = 0;

    for (i = 0; i < uuid_db->nlpars; i++) {
        if (lpars[i]->id == lpar_id) {
            memmove(uuid, lpars[i]->uuid, VIR_UUID_BUFLEN);
            return 0;
        }
    }

    return -1;
}

/*
 * type:
 * 0 - maxmem
 * 1 - memory
 * */
unsigned long
phypGetLparMem(virConnectPtr conn, const char *managed_system, int lpar_id,
               int type)
{
    ConnectionData *connection_data = conn->networkPrivateData;
499
    LIBSSH2_SESSION *session = connection_data->session;
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    char *cmd;
    char *char_ptr;
    int memory = 0;
    int exit_status = 0;

    if (type != 1 && type != 0)
        goto err;

    if (type) {
        if (virAsprintf(&cmd,
                        "lshwres -m %s -r mem --level lpar -F curr_mem --filter lpar_ids=%d",
                        managed_system, lpar_id) < 0) {
            virReportOOMError(conn);
            goto err;
        }
    } else {
        if (virAsprintf(&cmd,
                        "lshwres -m %s -r mem --level lpar -F curr_max_mem --filter lpar_ids=%d",
                        managed_system, lpar_id) < 0) {
            virReportOOMError(conn);
            goto err;
        }
    }
523
    PHYP_CMD_DEBUG;
524

525
    char *ret = phypExec(session, cmd, &exit_status, conn);
526

527
    if (ret == NULL)
528 529
        goto err;

530
    char *mem_char_ptr = strchr(ret, '\n');
531 532 533 534 535 536 537

    if (mem_char_ptr)
        *mem_char_ptr = '\0';

    if (exit_status < 0)
        goto err;

538
    if (virStrToLong_i(ret, &char_ptr, 10, &memory) == -1)
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
        goto err;

    VIR_FREE(cmd);
    return memory;

  err:
    VIR_FREE(cmd);
    return 0;

}

unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
{
    ConnectionData *connection_data = conn->networkPrivateData;
554
    LIBSSH2_SESSION *session = connection_data->session;
555 556 557 558 559 560 561 562 563 564
    char *cmd;
    int exit_status = 0;
    int vcpus = 0;

    if (virAsprintf(&cmd,
                    "lshwres -m %s -r proc --level lpar -F curr_procs --filter lpar_ids=%d",
                    managed_system, lpar_id) < 0) {
        virReportOOMError(conn);
        goto err;
    }
565 566
    PHYP_CMD_DEBUG;
    char *ret = phypExec(session, cmd, &exit_status, conn);
567

568
    if (ret == NULL)
569 570
        goto err;

571
    char *char_ptr = strchr(ret, '\n');
572 573 574 575

    if (char_ptr)
        *char_ptr = '\0';

576
    if (virStrToLong_i(ret, &char_ptr, 10, &vcpus) == -1)
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
        goto err;

    if (exit_status < 0)
        goto err;

    VIR_FREE(cmd);
    return (unsigned long) vcpus;

  err:
    VIR_FREE(cmd);
    return 0;
}

int
phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
                  const char *lpar_name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
595
    LIBSSH2_SESSION *session = connection_data->session;
596 597 598 599 600 601 602 603 604 605 606
    char *cmd;
    char *char_ptr;
    int remote_slot = 0;
    int exit_status = 0;

    if (virAsprintf(&cmd,
                    "lshwres -m %s -r virtualio --rsubtype scsi -F remote_slot_num --filter lpar_names=%s",
                    managed_system, lpar_name) < 0) {
        virReportOOMError(conn);
        goto err;
    }
607 608
    PHYP_CMD_DEBUG;
    char *ret = phypExec(session, cmd, &exit_status, conn);
609

610
    if (ret == NULL)
611 612
        goto err;

613
    char *char_ptr2 = strchr(ret, '\n');
614 615 616 617 618 619 620

    if (char_ptr2)
        *char_ptr2 = '\0';

    if (exit_status < 0)
        goto err;

621
    if (virStrToLong_i(ret, &char_ptr, 10, &remote_slot) == -1)
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
        goto err;

    VIR_FREE(cmd);
    return remote_slot;

  err:
    VIR_FREE(cmd);
    return 0;
}

char *
phypGetBackingDevice(virConnectPtr conn, const char *managed_system,
                     char *lpar_name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
637
    LIBSSH2_SESSION *session = connection_data->session;
638 639 640 641 642 643 644 645 646 647 648 649 650 651
    char *cmd;
    int remote_slot = 0;
    int exit_status = 0;

    if ((remote_slot =
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == 0)
        goto err;

    if (virAsprintf(&cmd,
                    "lshwres -m %s -r virtualio --rsubtype scsi -F backing_devices --filter slots=%d",
                    managed_system, remote_slot) < 0) {
        virReportOOMError(conn);
        goto err;
    }
652
    PHYP_CMD_DEBUG;
653

654
    char *ret = phypExec(session, cmd, &exit_status, conn);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

    if (ret == NULL)
        goto err;

    /* here is a little trick to deal returns of this kind:
     *
     * 0x8100000000000000//lv01
     *
     * the information we really need is only lv01, so we
     * need to skip a lot of things on the string.
     * */
    char *backing_device = strchr(ret, '/');

    if (backing_device) {
        backing_device++;
        if (backing_device[0] == '/')
            backing_device++;
        else
            goto err;
    } else {
        backing_device = ret;
    }

    char *char_ptr = strchr(backing_device, '\n');

    if (char_ptr)
        *char_ptr = '\0';

    if (exit_status < 0 || backing_device == NULL)
        goto err;

    VIR_FREE(cmd);
    return backing_device;

  err:
    VIR_FREE(cmd);
    return NULL;

}

int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
{
    ConnectionData *connection_data = conn->networkPrivateData;
699
    LIBSSH2_SESSION *session = connection_data->session;
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    char *cmd;
    int exit_status = 0;
    char *char_ptr = NULL;
    char *managed_system = conn->uri->path;

    /* need to shift one byte in order to remove the first "/" of URI component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */

    char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    if (virAsprintf(&cmd,
                    "lssyscfg -r lpar -m %s -F state --filter lpar_ids=%d",
                    managed_system, lpar_id) < 0) {
        virReportOOMError(conn);
        goto err;
    }
724
    PHYP_CMD_DEBUG;
725

726
    char *ret = phypExec(session, cmd, &exit_status, conn);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

    if (ret == NULL)
        goto err;

    char_ptr = strchr(ret, '\n');

    if (char_ptr)
        *char_ptr = '\0';

    if (exit_status < 0 || ret == NULL)
        goto err;

    VIR_FREE(cmd);
    if (STREQ(ret, "Running"))
        return VIR_DOMAIN_RUNNING;
    else if (STREQ(ret, "Not Activated"))
        return VIR_DOMAIN_SHUTOFF;
    else if (STREQ(ret, "Shutting Down"))
        return VIR_DOMAIN_SHUTDOWN;
    else
        goto err;

  err:
    VIR_FREE(cmd);
    return VIR_DOMAIN_NOSTATE;
}

int
phypDiskType(virConnectPtr conn, char *backing_device)
{
    ConnectionData *connection_data = conn->networkPrivateData;
758
    LIBSSH2_SESSION *session = connection_data->session;
759 760 761 762 763 764 765 766 767
    char *cmd;
    int exit_status = 0;

    if (virAsprintf(&cmd,
                    "ioscli lssp -field name type -fmt , -all|grep %s|sed -e 's/^.*,//g'",
                    backing_device) < 0) {
        virReportOOMError(conn);
        goto err;
    }
768
    PHYP_CMD_DEBUG;
769

770
    char *ret = phypExec(session, cmd, &exit_status, conn);
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

    if (ret == NULL)
        goto err;

    char *char_ptr = strchr(ret, '\n');

    if (char_ptr)
        *char_ptr = '\0';

    if (exit_status < 0 || ret == NULL)
        goto err;

    VIR_FREE(cmd);
    if (STREQ(ret, "LVPOOL"))
        return VIR_DOMAIN_DISK_TYPE_BLOCK;
    else if (STREQ(ret, "FBPOOL"))
        return VIR_DOMAIN_DISK_TYPE_FILE;
    else
        goto err;

  err:
    VIR_FREE(cmd);
    return -1;
}

/* This is a generic function that won't be used directly by
 * libvirt api. The function returns the number of domains
 * in different states: Running, Not Activated and all:
 *
 * type: 0 - Running
 *       1 - Not Activated
 *       * - All
 * */
static int
phypNumDomainsGeneric(virConnectPtr conn, unsigned int type)
{
    ConnectionData *connection_data = conn->networkPrivateData;
808
    LIBSSH2_SESSION *session = connection_data->session;
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    int exit_status = 0;
    int ndom = 0;
    char *char_ptr;
    char *cmd;
    char *managed_system = conn->uri->path;
    const char *state;

    if (type == 0)
        state = "|grep Running";
    else if (type == 1)
        state = "|grep \"Not Activated\"";
    else
        state = " ";

    /* need to shift one byte in order to remove the first "/" of URI component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */

    char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    if (virAsprintf(&cmd,
                    "lssyscfg -r lpar -m %s -F lpar_id,state %s |grep -c ^[0-9]*",
                    managed_system, state) < 0) {
        virReportOOMError(conn);
        goto err;
    }
842
    PHYP_CMD_DEBUG;
843

844
    char *ret = phypExec(session, cmd, &exit_status, conn);
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883

    if (exit_status < 0 || ret == NULL)
        goto err;

    if (virStrToLong_i(ret, &char_ptr, 10, &ndom) == -1)
        goto err;

    VIR_FREE(cmd);
    return ndom;

  err:
    VIR_FREE(cmd);
    return 0;
}

static int
phypNumDefinedDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 1);
}

static int
phypNumDomains(virConnectPtr conn)
{
    return phypNumDomainsGeneric(conn, 0);
}

/* This is a generic function that won't be used directly by
 * libvirt api. The function returns the ids of domains
 * in different states: Running, and all:
 *
 * type: 0 - Running
 *       * - all
 * */
static int
phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids,
                       unsigned int type)
{
    ConnectionData *connection_data = conn->networkPrivateData;
884
    LIBSSH2_SESSION *session = connection_data->session;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    char *managed_system = conn->uri->path;
    int exit_status = 0;
    int got = 0;
    char *char_ptr;
    unsigned int i = 0, j = 0;
    char id_c[10];
    char *cmd;
    const char *state;

    if (type == 0)
        state = "|grep Running";
    else
        state = " ";

    /* need to shift one byte in order to remove the first "/" of URI component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    memset(id_c, 0, 10);

    if (virAsprintf
        (&cmd,
         "lssyscfg -r lpar -m %s -F lpar_id,state %s | sed -e 's/,.*$//g'",
         managed_system, state) < 0) {
        virReportOOMError(conn);
        goto err;
    }
920 921
    PHYP_CMD_DEBUG;
    char *ret = phypExec(session, cmd, &exit_status, conn);
922

923 924
    /* I need to parse the textual return in order to get the ret */
    if (exit_status < 0 || ret == NULL)
925 926 927
        goto err;
    else {
        while (got < nids) {
928
            if (ret[i] == '\n') {
929 930 931 932 933 934
                if (virStrToLong_i(id_c, &char_ptr, 10, &ids[got]) == -1)
                    return 0;
                memset(id_c, 0, 10);
                j = 0;
                got++;
            } else {
935
                id_c[j] = ret[i];
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
                j++;
            }
            i++;
        }
    }

    VIR_FREE(cmd);
    return got;

  err:
    VIR_FREE(cmd);
    return 0;
}

static int
phypListDomains(virConnectPtr conn, int *ids, int nids)
{
    return phypListDomainsGeneric(conn, ids, nids, 0);
}

static int
phypListDefinedDomains(virConnectPtr conn, char **const names, int nnames)
{
    ConnectionData *connection_data = conn->networkPrivateData;
960
    LIBSSH2_SESSION *session = connection_data->session;
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
    char *managed_system = conn->uri->path;
    int exit_status = 0;
    int got = 0;
    char *char_ptr = NULL;
    char *cmd;
    char *domains;

    /* need to shift one byte in order to remove the first "/" of URI component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    if (virAsprintf
        (&cmd,
         "lssyscfg -r lpar -m %s -F name,state | grep \"Not Activated\" | sed -e 's/,.*$//g'",
         managed_system) < 0) {
        virReportOOMError(conn);
        goto err;
    }
987
    PHYP_CMD_DEBUG;
988

989
    char *ret = phypExec(session, cmd, &exit_status, conn);
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

    if (VIR_ALLOC(domains) < 0)
        virReportOOMError(conn);

    domains = strdup(ret);
    if (!domains)
        goto err;

    char *char_ptr2 = NULL;
    /* I need to parse the textual return in order to get the domains */
    if (exit_status < 0 || domains == NULL)
        goto err;
    else {
        while (got < nnames) {
            char_ptr2 = strchr(domains, '\n');

            if (char_ptr2) {
                *char_ptr2 = '\0';
                if (!strdup(domains))
                    goto err;
                names[got] = strdup(domains);
                char_ptr2++;
                domains = char_ptr2;
                got++;
            }
        }
    }

    VIR_FREE(domains);
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return got;

  err:
    VIR_FREE(domains);
    VIR_FREE(ret);
    return 0;
}

static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
1033
    LIBSSH2_SESSION *session = connection_data->session;
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    virDomainPtr dom = NULL;
    int lpar_id = 0;
    char *managed_system = conn->uri->path;
    unsigned char *lpar_uuid = NULL;

    if (VIR_ALLOC_N(lpar_uuid, VIR_UUID_BUFLEN) < 0)
        virReportOOMError(dom->conn);

    /* need to shift one byte in order to remove the first "/" of uri component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char *char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

1054
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    if (lpar_id < 0)
        goto err;

    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        goto err;

    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    VIR_FREE(lpar_uuid);
    return dom;

  err:
    VIR_FREE(lpar_uuid);
    return NULL;
}

static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id)
{
    ConnectionData *connection_data = conn->networkPrivateData;
1078
    LIBSSH2_SESSION *session = connection_data->session;
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    virDomainPtr dom = NULL;
    char *managed_system = conn->uri->path;
    int exit_status = 0;
    unsigned char *lpar_uuid = NULL;

    if (VIR_ALLOC_N(lpar_uuid, VIR_UUID_BUFLEN) < 0)
        virReportOOMError(dom->conn);

    /* need to shift one byte in order to remove the first "/" of uri component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char *char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

1099
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
                                      conn);

    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
        goto err;

    if (exit_status < 0)
        goto err;

    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    VIR_FREE(lpar_name);
    VIR_FREE(lpar_uuid);
    return dom;

  err:
    VIR_FREE(lpar_name);
    VIR_FREE(lpar_uuid);
    return NULL;
}

static char *
phypDomainDumpXML(virDomainPtr dom, int flags)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
1127
    LIBSSH2_SESSION *session = connection_data->session;
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    virDomainDefPtr def = NULL;
    char *ret = NULL;
    char *managed_system = dom->conn->uri->path;
    unsigned char *lpar_uuid = NULL;

    if (VIR_ALLOC_N(lpar_uuid, VIR_UUID_BUFLEN) < 0)
        virReportOOMError(dom->conn);

    if (VIR_ALLOC(def) < 0)
        virReportOOMError(dom->conn);

    /* need to shift one byte in order to remove the first "/" of uri component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char *char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    def->virtType = VIR_DOMAIN_VIRT_PHYP;
    def->id = dom->id;

1154
    char *lpar_name = phypGetLparNAME(session, managed_system, def->id,
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
                                      dom->conn);

    if (lpar_name == NULL) {
        VIR_ERROR("%s", "Unable to determine domain's name.");
        goto err;
    }

    if (phypGetLparUUID(lpar_uuid, dom->id, dom->conn) == -1) {
        VIR_ERROR("%s", "Unable to generate random uuid.");
        goto err;
    }

    if (!memcpy(def->uuid, lpar_uuid, VIR_UUID_BUFLEN)) {
        VIR_ERROR("%s", "Unable to generate random uuid.");
        goto err;
    }

    if ((def->maxmem =
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0) {
        VIR_ERROR("%s", "Unable to determine domain's max memory.");
        goto err;
    }

    if ((def->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
        VIR_ERROR("%s", "Unable to determine domain's memory.");
        goto err;
    }

    if ((def->vcpus =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
        VIR_ERROR("%s", "Unable to determine domain's CPU.");
        goto err;
    }

    ret = virDomainDefFormat(dom->conn, def, flags);

  err:
    VIR_FREE(def);
    return ret;
}

static int
phypDomainResume(virDomainPtr dom)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
1201
    LIBSSH2_SESSION *session = connection_data->session;
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    char *managed_system = dom->conn->uri->path;
    int exit_status = 0;
    char *char_ptr = NULL;
    char *cmd;

    /* need to shift one byte in order to remove the first "/" of URI component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    if (virAsprintf
        (&cmd,
         "chsysstate -m %s -r lpar -o on --id %d -f %s",
         managed_system, dom->id, dom->name) < 0) {
        virReportOOMError(dom->conn);
        goto err;
    }
1226
    PHYP_CMD_DEBUG;
1227

1228
    char *ret = phypExec(session, cmd, &exit_status, dom->conn);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

  err:
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return 0;

}

static int
phypDomainShutdown(virDomainPtr dom)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
1241
    LIBSSH2_SESSION *session = connection_data->session;
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    char *managed_system = dom->conn->uri->path;
    int exit_status = 0;
    char *char_ptr = NULL;
    char *cmd;

    /* need to shift one byte in order to remove the first "/" of URI component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    if (virAsprintf
        (&cmd,
         "chsysstate -m %s -r lpar -o shutdown --id %d",
         managed_system, dom->id) < 0) {
        virReportOOMError(dom->conn);
        goto err;
    }
1266
    PHYP_CMD_DEBUG;
1267

1268
    char *ret = phypExec(session, cmd, &exit_status, dom->conn);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

  err:
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return 0;

}

static int
phypDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
    char *managed_system = dom->conn->uri->path;

    /* need to shift one byte in order to remove the first "/" of uri component */
    if (managed_system[0] == '/')
        managed_system++;

    /* here we are handling only the first component of the path,
     * so skipping the second:
     * */
    char *char_ptr = strchr(managed_system, '/');

    if (char_ptr)
        *char_ptr = '\0';

    info->state = phypGetLparState(dom->conn, dom->id);

    if ((info->maxMem =
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0)
        VIR_WARN("%s", "Unable to determine domain's max memory.");

    if ((info->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0)
        VIR_WARN("%s", "Unable to determine domain's memory.");

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        VIR_WARN("%s", "Unable to determine domain's CPU.");

    return 0;

}

virDriver phypDriver = {
    VIR_DRV_PHYP,
    "PHYP",
    phypOpen,                   /* open */
    phypClose,                  /* close */
    NULL,                       /* supports_feature */
    NULL,                       /* type */
    NULL,                       /* version */
    NULL,                       /* getHostname */
    NULL,                       /* getMaxVcpus */
    NULL,                       /* nodeGetInfo */
    NULL,                       /* getCapabilities */
    phypListDomains,            /* listDomains */
    phypNumDomains,             /* numOfDomains */
    NULL,                       /* domainCreateXML */
    phypDomainLookupByID,       /* domainLookupByID */
    NULL,                       /* domainLookupByUUID */
    phypDomainLookupByName,     /* domainLookupByName */
    NULL,                       /* domainSuspend */
    phypDomainResume,           /* domainResume */
    phypDomainShutdown,         /* domainShutdown */
    NULL,                       /* domainReboot */
    NULL,                       /* domainDestroy */
    NULL,                       /* domainGetOSType */
    NULL,                       /* domainGetMaxMemory */
    NULL,                       /* domainSetMaxMemory */
    NULL,                       /* domainSetMemory */
    phypDomainGetInfo,          /* domainGetInfo */
    NULL,                       /* domainSave */
    NULL,                       /* domainRestore */
    NULL,                       /* domainCoreDump */
    NULL,                       /* domainSetVcpus */
    NULL,                       /* domainPinVcpu */
    NULL,                       /* domainGetVcpus */
    NULL,                       /* domainGetMaxVcpus */
    NULL,                       /* domainGetSecurityLabel */
    NULL,                       /* nodeGetSecurityModel */
    phypDomainDumpXML,          /* domainDumpXML */
1350 1351
    NULL,                       /* domainXMLFromNative */
    NULL,                       /* domainXMLToNative */
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    phypListDefinedDomains,     /* listDefinedDomains */
    phypNumDefinedDomains,      /* numOfDefinedDomains */
    NULL,                       /* domainCreate */
    NULL,                       /* domainDefineXML */
    NULL,                       /* domainUndefine */
    NULL,                       /* domainAttachDevice */
    NULL,                       /* domainDetachDevice */
    NULL,                       /* domainGetAutostart */
    NULL,                       /* domainSetAutostart */
    NULL,                       /* domainGetSchedulerType */
    NULL,                       /* domainGetSchedulerParameters */
    NULL,                       /* domainSetSchedulerParameters */
    NULL,                       /* domainMigratePrepare */
    NULL,                       /* domainMigratePerform */
    NULL,                       /* domainMigrateFinish */
    NULL,                       /* domainBlockStats */
    NULL,                       /* domainInterfaceStats */
    NULL,                       /* domainBlockPeek */
    NULL,                       /* domainMemoryPeek */
    NULL,                       /* nodeGetCellsFreeMemory */
    NULL,                       /* getFreeMemory */
    NULL,                       /* domainEventRegister */
    NULL,                       /* domainEventDeregister */
    NULL,                       /* domainMigratePrepare2 */
    NULL,                       /* domainMigrateFinish2 */
    NULL,                       /* nodeDeviceDettach */
    NULL,                       /* nodeDeviceReAttach */
    NULL,                       /* nodeDeviceReset */
};

int
phypRegister(void)
{
    virRegisterDriver(&phypDriver);
    return 0;
}

void
init_uuid_db(virConnectPtr conn)
{
    uuid_dbPtr uuid_db;
    int nids = 0;
    int *ids = NULL;
    unsigned int i = 0;

    if ((nids = phypNumDomainsGeneric(conn, 2)) == 0)
        goto exit;

    if (VIR_ALLOC_N(ids, nids) < 0)
        virReportOOMError(conn);

    if (VIR_ALLOC(uuid_db) < 0)
        virReportOOMError(conn);

    if (phypListDomainsGeneric(conn, ids, nids, 1) == 0)
        goto exit;

    uuid_db = conn->privateData;
    uuid_db->nlpars = nids;

    if (VIR_ALLOC_N(uuid_db->lpars, uuid_db->nlpars) >= 0) {
        for (i = 0; i < uuid_db->nlpars; i++) {
            if (VIR_ALLOC(uuid_db->lpars[i]) < 0)
                virReportOOMError(conn);
            uuid_db->lpars[i]->id = ids[i];

            if (virUUIDGenerate(uuid_db->lpars[i]->uuid) < 0)
                VIR_WARN("%s %d", "Unable to generate UUID for domain",
                         ids[i]);
        }
    }
  exit:
    VIR_FREE(ids);
    return;
}

1428 1429
static int
escape_specialcharacters(char *src, char *dst, size_t dstlen)
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
{
    size_t len = strlen(src);
    char temp_buffer[len];
    unsigned int i = 0, j = 0;
    if (len == 0)
        return -1;

    for (i = 0; i < len; i++) {
        switch (src[i]) {
            case '&': case ';': case '`': case '@':
            case '"': case '|': case '*': case '?':
            case '~': case '<': case '>': case '^':
            case '(': case ')': case '[': case ']':
            case '{': case '}': case '$': case '%':
            case '#': case '\\': case '\n': case '\r':
            case '\t':
                continue;
            default:
                temp_buffer[j] = src[i];
                j++;
        }
    }
    temp_buffer[j] = '\0';

C
Chris Lalancette 已提交
1454
    if (virStrcpy(dst, temp_buffer, dstlen) == NULL)
1455 1456 1457 1458
        return -1;

    return 0;
}
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489

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;
}