phyp_driver.c 57.5 KB
Newer Older
1 2

/*
3
 * Copyright (C) 2010 Red Hat, Inc.
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
 * 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>
29
#include <sys/stat.h>
30 31 32 33 34 35 36 37
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
38 39 40 41 42
#include <libssh2.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
43 44 45
#include <fcntl.h>
#include <sys/utsname.h>
#include <domain_event.h>
46 47

#include "internal.h"
48
#include "authhelper.h"
49 50 51 52 53 54 55 56 57 58
#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"
59
#include "nodeinfo.h"
60 61 62 63 64

#include "phyp_driver.h"

#define VIR_FROM_THIS VIR_FROM_PHYP

65 66
#define PHYP_ERROR(code, ...)                                                 \
    virReportErrorHelper(NULL, VIR_FROM_PHYP, code, __FILE__, __FUNCTION__,   \
67
                         __LINE__, __VA_ARGS__)
68

69 70 71 72 73 74 75 76
/*
 * URI: phyp://user@[hmc|ivm]/managed_system
 * */

static virDrvOpenStatus
phypOpen(virConnectPtr conn,
         virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
{
77
    LIBSSH2_SESSION *session = NULL;
78
    ConnectionData *connection_data = NULL;
79
    char *string = NULL;
80
    size_t len = 0;
81
    int internal_socket;
82 83 84 85
    uuid_tablePtr uuid_table = NULL;
    phyp_driverPtr phyp_driver = NULL;
    char *char_ptr;
    char *managed_system;
86 87 88 89 90 91 92 93

    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) {
94
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
95
                   "%s", _("Missing server name in phyp:// URI"));
96 97 98
        return VIR_DRV_OPEN_ERROR;
    }

99
    if (conn->uri->path == NULL) {
100
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
101
                   "%s", _("Missing managed system name in phyp:// URI"));
102 103 104
        return VIR_DRV_OPEN_ERROR;
    }

105
    if (VIR_ALLOC(phyp_driver) < 0) {
106
        virReportOOMError();
107 108 109 110
        goto failure;
    }

    if (VIR_ALLOC(uuid_table) < 0) {
111
        virReportOOMError();
112 113 114 115
        goto failure;
    }

    if (VIR_ALLOC(connection_data) < 0) {
116
        virReportOOMError();
117 118 119
        goto failure;
    }

120 121 122
    len = strlen(conn->uri->path) + 1;

    if (VIR_ALLOC_N(string, len) < 0) {
123
        virReportOOMError();
124 125 126
        goto failure;
    }

127 128 129 130 131 132 133
    /* need to shift one byte in order to remove the first "/" of URI component */
    if (conn->uri->path[0] == '/')
        managed_system = strdup(conn->uri->path + 1);
    else
        managed_system = strdup(conn->uri->path);

    if (!managed_system) {
134
        virReportOOMError();
135 136 137 138 139 140 141 142 143 144 145
        goto failure;
    }

    /* 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';

146
    if (escape_specialcharacters(conn->uri->path, string, len) == -1) {
147
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
148
                   "%s", _("Error parsing 'path'. Invalid characters."));
149
        goto failure;
150 151
    }

152
    if ((session = openSSHSession(conn, auth, &internal_socket)) == NULL) {
153
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
154
                   "%s", _("Error while opening SSH session."));
155
        goto failure;
156
    }
157

158 159
    connection_data->session = session;

160 161 162 163 164 165
    uuid_table->nlpars = 0;
    uuid_table->lpars = NULL;

    phyp_driver->managed_system = managed_system;
    phyp_driver->uuid_table = uuid_table;
    if ((phyp_driver->caps = phypCapsInit()) == NULL) {
166
        virReportOOMError();
167 168
        goto failure;
    }
169

170
    conn->privateData = phyp_driver;
171
    conn->networkPrivateData = connection_data;
172 173 174 175 176
    if (phypUUIDTable_Init(conn) == -1)
        goto failure;

    if ((phyp_driver->vios_id = phypGetVIOSPartitionID(conn)) == -1)
        goto failure;
177 178

    return VIR_DRV_OPEN_SUCCESS;
179

180
  failure:
181 182 183 184 185 186 187 188 189 190 191 192 193
    if (phyp_driver != NULL) {
        virCapabilitiesFree(phyp_driver->caps);
        VIR_FREE(phyp_driver->managed_system);
        VIR_FREE(phyp_driver);
    }

    phypUUIDTable_Free(uuid_table);

    if (session != NULL) {
        libssh2_session_disconnect(session, "Disconnecting...");
        libssh2_session_free(session);
    }

194 195 196 197
    VIR_FREE(connection_data);
    VIR_FREE(string);

    return VIR_DRV_OPEN_ERROR;
198 199 200 201 202 203
}

static int
phypClose(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
204
    phyp_driverPtr phyp_driver = conn->privateData;
205
    LIBSSH2_SESSION *session = connection_data->session;
206

207 208
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
209

210
    virCapabilitiesFree(phyp_driver->caps);
211
    phypUUIDTable_Free(phyp_driver->uuid_table);
212 213
    VIR_FREE(phyp_driver->managed_system);
    VIR_FREE(phyp_driver);
214 215 216 217
    VIR_FREE(connection_data);
    return 0;
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

static int
phypIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Phyp uses an SSH tunnel, so is always encrypted */
    return 1;
}


static int
phypIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Phyp uses an SSH tunnel, so is always secure */
    return 1;
}


235 236 237
LIBSSH2_SESSION *
openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
               int *internal_socket)
238
{
239 240
    LIBSSH2_SESSION *session;
    const char *hostname = conn->uri->server;
241 242
    char *username = NULL;
    char *password = NULL;
243 244 245 246 247
    int sock;
    int rc;
    struct addrinfo *ai = NULL, *cur;
    struct addrinfo hints;
    int ret;
248 249
    char *pubkey = NULL;
    char *pvtkey = NULL;
250
    char *userhome = virGetUserDirectory(geteuid());
251 252 253 254 255 256
    struct stat pvt_stat, pub_stat;

    if (userhome == NULL)
        goto err;

    if (virAsprintf(&pubkey, "%s/.ssh/id_rsa.pub", userhome) < 0) {
257
        virReportOOMError();
258 259 260 261
        goto err;
    }

    if (virAsprintf(&pvtkey, "%s/.ssh/id_rsa", userhome) < 0) {
262
        virReportOOMError();
263 264
        goto err;
    }
265

266 267 268 269 270 271 272 273 274
    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);

        if (username == NULL) {
            virReportOOMError();
            goto err;
        }
    } else {
        if (auth == NULL || auth->cb == NULL) {
275
            PHYP_ERROR(VIR_ERR_AUTH_FAILED,
276 277 278 279 280 281 282
                       "%s", _("No authentication callback provided."));
            goto err;
        }

        username = virRequestUsername(auth, NULL, conn->uri->server);

        if (username == NULL) {
283
            PHYP_ERROR(VIR_ERR_AUTH_FAILED, "%s", _("Username request failed"));
284 285 286 287
            goto err;
        }
    }

288
    memset(&hints, 0, sizeof(hints));
289 290 291 292
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

293
    ret = getaddrinfo(hostname, "22", &hints, &ai);
294
    if (ret != 0) {
295
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
296
                   _("Error while getting %s address info"), hostname);
297 298 299
        goto err;
    }

300 301 302 303
    cur = ai;
    while (cur != NULL) {
        sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
        if (sock >= 0) {
304
            if (connect(sock, cur->ai_addr, cur->ai_addrlen) == 0) {
305 306
                goto connected;
            }
307
            close(sock);
308 309
        }
        cur = cur->ai_next;
310 311
    }

312
    PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
313
               _("Failed to connect to %s"), hostname);
314
    freeaddrinfo(ai);
315 316
    goto err;

317
  connected:
318 319 320 321 322 323 324 325 326 327 328 329 330 331

    (*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) {
332
        PHYP_ERROR(VIR_ERR_INTERNAL_ERROR,
333
                   "%s", _("Failure establishing SSH session."));
334
        goto disconnect;
335 336
    }

337
    /* Trying authentication by pubkey */
338 339
    if (stat(pvtkey, &pvt_stat) || stat(pubkey, &pub_stat)) {
        rc = LIBSSH2_ERROR_SOCKET_NONE;
340
        goto keyboard_interactive;
341
    }
342

343 344
    while ((rc =
            libssh2_userauth_publickey_fromfile(session, username,
345 346 347
                                                pubkey,
                                                pvtkey,
                                                NULL)) ==
348
           LIBSSH2_ERROR_EAGAIN) ;
349

350
  keyboard_interactive:
351 352 353
    if (rc == LIBSSH2_ERROR_SOCKET_NONE
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED
        || rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED) {
354
        if (auth == NULL || auth->cb == NULL) {
355
            PHYP_ERROR(VIR_ERR_AUTH_FAILED,
356
                       "%s", _("No authentication callback provided."));
357
            goto disconnect;
358 359
        }

360
        password = virRequestPassword(auth, username, conn->uri->server);
361

362
        if (password == NULL) {
363
            PHYP_ERROR(VIR_ERR_AUTH_FAILED, "%s", _("Password request failed"));
364
            goto disconnect;
365 366
        }

367 368 369 370
        while ((rc =
                libssh2_userauth_password(session, username,
                                          password)) ==
               LIBSSH2_ERROR_EAGAIN) ;
371

372
        if (rc) {
373
            PHYP_ERROR(VIR_ERR_AUTH_FAILED,
374
                       "%s", _("Authentication failed"));
375
            goto disconnect;
376 377
        } else
            goto exit;
378 379 380 381 382 383 384

    } else if (rc == LIBSSH2_ERROR_NONE) {
        goto exit;

    } else if (rc == LIBSSH2_ERROR_ALLOC || rc == LIBSSH2_ERROR_SOCKET_SEND
               || rc == LIBSSH2_ERROR_SOCKET_TIMEOUT) {
        goto err;
385
    }
386

387 388 389
  disconnect:
    libssh2_session_disconnect(session, "Disconnecting...");
    libssh2_session_free(session);
390
  err:
391 392 393
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
394
    VIR_FREE(username);
395
    VIR_FREE(password);
396 397 398
    return NULL;

  exit:
399 400 401
    VIR_FREE(userhome);
    VIR_FREE(pubkey);
    VIR_FREE(pvtkey);
402
    VIR_FREE(username);
403
    VIR_FREE(password);
404 405 406 407 408 409
    return session;
}

/* this functions is the layer that manipulates the ssh channel itself
 * and executes the commands on the remote machine */
static char *
410
phypExec(LIBSSH2_SESSION * session, char *cmd, int *exit_status,
411 412
         virConnectPtr conn)
{
413 414
    LIBSSH2_CHANNEL *channel;
    ConnectionData *connection_data = conn->networkPrivateData;
415
    virBuffer tex_ret = VIR_BUFFER_INITIALIZER;
416 417 418 419 420 421 422 423 424 425 426 427
    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);
    }
428

429
    if (channel == NULL) {
430 431 432
        goto err;
    }

433 434 435
    while ((rc = libssh2_channel_exec(channel, cmd)) ==
           LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
436 437
    }

438
    if (rc != 0) {
439 440 441
        goto err;
    }

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    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;
        }
    }
461

462
    exitcode = 127;
463

464 465 466
    while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
        waitsocket(sock, session);
    }
467

468 469
    if (rc == 0) {
        exitcode = libssh2_channel_get_exit_status(channel);
470 471
    }

472 473 474 475 476
    (*exit_status) = exitcode;
    libssh2_channel_free(channel);
    channel = NULL;
    goto exit;

477 478
  err:
    (*exit_status) = SSH_CMD_ERR;
479
    virBufferFreeAndReset(&tex_ret);
480 481 482 483
    return NULL;

  exit:
    if (virBufferError(&tex_ret)) {
484
        virBufferFreeAndReset(&tex_ret);
485
        virReportOOMError();
486 487 488 489 490 491 492
        return NULL;
    }
    return virBufferContentAndReset(&tex_ret);
}

/* return the lpar_id given a name and a managed system name */
static int
493
phypGetLparID(LIBSSH2_SESSION * session, const char *managed_system,
494 495 496 497 498
              const char *name, virConnectPtr conn)
{
    int exit_status = 0;
    int lpar_id = 0;
    char *char_ptr;
499 500
    char *cmd = NULL;
    char *ret = NULL;
501 502 503 504

    if (virAsprintf(&cmd,
                    "lssyscfg -r lpar -m %s --filter lpar_names=%s -F lpar_id",
                    managed_system, name) < 0) {
505
        virReportOOMError();
506 507 508
        goto err;
    }

509
    ret = phypExec(session, cmd, &exit_status, conn);
510

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

514
    if (virStrToLong_i(ret, &char_ptr, 10, &lpar_id) == -1)
515 516 517
        goto err;

    VIR_FREE(cmd);
518
    VIR_FREE(ret);
519 520 521 522
    return lpar_id;

  err:
    VIR_FREE(cmd);
523
    VIR_FREE(ret);
524 525 526 527 528
    return -1;
}

/* return the lpar name given a lpar_id and a managed system name */
static char *
529
phypGetLparNAME(LIBSSH2_SESSION * session, const char *managed_system,
530 531
                unsigned int lpar_id, virConnectPtr conn)
{
532 533
    char *cmd = NULL;
    char *ret = NULL;
534 535 536 537 538
    int exit_status = 0;

    if (virAsprintf(&cmd,
                    "lssyscfg -r lpar -m %s --filter lpar_ids=%d -F name",
                    managed_system, lpar_id) < 0) {
539
        virReportOOMError();
540 541 542
        goto err;
    }

543
    ret = phypExec(session, cmd, &exit_status, conn);
544

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

548
    char *char_ptr = strchr(ret, '\n');
549 550 551 552 553

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

    VIR_FREE(cmd);
554
    return ret;
555 556 557

  err:
    VIR_FREE(cmd);
558
    VIR_FREE(ret);
559 560 561 562
    return NULL;
}


563
/* Search into the uuid_table for a lpar_uuid given a lpar_id
564 565 566 567 568 569 570 571
 * and a managed system name
 *
 * return:  0 - record found
 *         -1 - not found
 * */
int
phypGetLparUUID(unsigned char *uuid, int lpar_id, virConnectPtr conn)
{
572 573 574
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
575 576
    unsigned int i = 0;

577
    for (i = 0; i < uuid_table->nlpars; i++) {
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
        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;
597
    LIBSSH2_SESSION *session = connection_data->session;
598 599
    char *cmd = NULL;
    char *ret = NULL;
600 601 602 603 604 605 606 607 608
    char *char_ptr;
    int memory = 0;
    int exit_status = 0;

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

    if (type) {
        if (virAsprintf(&cmd,
609 610
                        "lshwres -m %s -r mem --level lpar -F curr_mem "
                        "--filter lpar_ids=%d",
611
                        managed_system, lpar_id) < 0) {
612
            virReportOOMError();
613 614 615 616
            goto err;
        }
    } else {
        if (virAsprintf(&cmd,
617 618
                        "lshwres -m %s -r mem --level lpar -F "
                        "curr_max_mem --filter lpar_ids=%d",
619
                        managed_system, lpar_id) < 0) {
620
            virReportOOMError();
621 622 623 624
            goto err;
        }
    }

625
    ret = phypExec(session, cmd, &exit_status, conn);
626

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

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

632 633
    if (char_ptr)
        *char_ptr = '\0';
634

635
    if (virStrToLong_i(ret, &char_ptr, 10, &memory) == -1)
636 637 638
        goto err;

    VIR_FREE(cmd);
639
    VIR_FREE(ret);
640 641 642 643
    return memory;

  err:
    VIR_FREE(cmd);
644
    VIR_FREE(ret);
645 646 647 648 649 650
    return 0;

}

unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
{
    return phypGetLparCPUGeneric(conn, managed_system, lpar_id, 0);
}

static int
phypGetLparCPUMAX(virDomainPtr dom)
{
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;

    return phypGetLparCPUGeneric(dom->conn, managed_system, dom->id, 1);
}

unsigned long
phypGetLparCPUGeneric(virConnectPtr conn, const char *managed_system,
                      int lpar_id, int type)
667 668
{
    ConnectionData *connection_data = conn->networkPrivateData;
669
    LIBSSH2_SESSION *session = connection_data->session;
670 671
    char *cmd = NULL;
    char *ret = NULL;
672
    char *char_ptr;
673 674 675
    int exit_status = 0;
    int vcpus = 0;

676 677 678 679 680
    if (type) {
        if (virAsprintf(&cmd,
                        "lshwres -m %s -r proc --level lpar -F "
                        "curr_max_procs --filter lpar_ids=%d",
                        managed_system, lpar_id) < 0) {
681
            virReportOOMError();
682 683 684 685 686 687 688
            goto err;
        }
    } else {
        if (virAsprintf(&cmd,
                        "lshwres -m %s -r proc --level lpar -F "
                        "curr_procs --filter lpar_ids=%d",
                        managed_system, lpar_id) < 0) {
689
            virReportOOMError();
690 691
            goto err;
        }
692
    }
693
    ret = phypExec(session, cmd, &exit_status, conn);
694

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

698
    char_ptr = strchr(ret, '\n');
699 700 701 702

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

703
    if (virStrToLong_i(ret, &char_ptr, 10, &vcpus) == -1)
704 705 706
        goto err;

    VIR_FREE(cmd);
707
    VIR_FREE(ret);
708 709 710 711
    return (unsigned long) vcpus;

  err:
    VIR_FREE(cmd);
712
    VIR_FREE(ret);
713 714 715 716 717 718 719 720
    return 0;
}

int
phypGetRemoteSlot(virConnectPtr conn, const char *managed_system,
                  const char *lpar_name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
721
    LIBSSH2_SESSION *session = connection_data->session;
722 723
    char *cmd = NULL;
    char *ret = NULL;
724 725 726 727 728
    char *char_ptr;
    int remote_slot = 0;
    int exit_status = 0;

    if (virAsprintf(&cmd,
729 730
                    "lshwres -m %s -r virtualio --rsubtype scsi -F "
                    "remote_slot_num --filter lpar_names=%s",
731
                    managed_system, lpar_name) < 0) {
732
        virReportOOMError();
733 734
        goto err;
    }
735
    ret = phypExec(session, cmd, &exit_status, conn);
736

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

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

742 743
    if (char_ptr)
        *char_ptr = '\0';
744

745
    if (virStrToLong_i(ret, &char_ptr, 10, &remote_slot) == -1)
746 747 748
        goto err;

    VIR_FREE(cmd);
749
    VIR_FREE(ret);
750 751 752 753
    return remote_slot;

  err:
    VIR_FREE(cmd);
754
    VIR_FREE(ret);
755
    return -1;
756 757 758 759 760 761 762
}

char *
phypGetBackingDevice(virConnectPtr conn, const char *managed_system,
                     char *lpar_name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
763
    LIBSSH2_SESSION *session = connection_data->session;
764 765
    char *cmd = NULL;
    char *ret = NULL;
766 767
    int remote_slot = 0;
    int exit_status = 0;
768 769
    char *char_ptr;
    char *backing_device = NULL;
770 771

    if ((remote_slot =
772
         phypGetRemoteSlot(conn, managed_system, lpar_name)) == -1)
773 774 775
        goto err;

    if (virAsprintf(&cmd,
776 777
                    "lshwres -m %s -r virtualio --rsubtype scsi -F "
                    "backing_devices --filter slots=%d",
778
                    managed_system, remote_slot) < 0) {
779
        virReportOOMError();
780 781 782
        goto err;
    }

783
    ret = phypExec(session, cmd, &exit_status, conn);
784

785
    if (exit_status < 0 || ret == NULL)
786 787 788 789 790 791 792 793 794
        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.
     * */
795
    char_ptr = strchr(ret, '/');
796

797 798 799 800
    if (char_ptr) {
        char_ptr++;
        if (char_ptr[0] == '/')
            char_ptr++;
801 802
        else
            goto err;
803 804 805 806

        backing_device = strdup(char_ptr);

        if (backing_device == NULL) {
807
            virReportOOMError();
808 809
            goto err;
        }
810 811
    } else {
        backing_device = ret;
812
        ret = NULL;
813 814
    }

815
    char_ptr = strchr(backing_device, '\n');
816 817 818 819 820

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

    VIR_FREE(cmd);
821
    VIR_FREE(ret);
822 823 824 825
    return backing_device;

  err:
    VIR_FREE(cmd);
826
    VIR_FREE(ret);
827 828 829 830 831 832 833 834
    return NULL;

}

int
phypGetLparState(virConnectPtr conn, unsigned int lpar_id)
{
    ConnectionData *connection_data = conn->networkPrivateData;
835
    phyp_driverPtr phyp_driver = conn->privateData;
836
    LIBSSH2_SESSION *session = connection_data->session;
837 838
    char *cmd = NULL;
    char *ret = NULL;
839 840
    int exit_status = 0;
    char *char_ptr = NULL;
841
    char *managed_system = phyp_driver->managed_system;
842
    int state = VIR_DOMAIN_NOSTATE;
843 844 845 846

    if (virAsprintf(&cmd,
                    "lssyscfg -r lpar -m %s -F state --filter lpar_ids=%d",
                    managed_system, lpar_id) < 0) {
847
        virReportOOMError();
848
        goto cleanup;
849 850
    }

851
    ret = phypExec(session, cmd, &exit_status, conn);
852

853 854
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
855 856 857 858 859 860 861

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

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

    if (STREQ(ret, "Running"))
862
        state = VIR_DOMAIN_RUNNING;
863
    else if (STREQ(ret, "Not Activated"))
864
        state = VIR_DOMAIN_SHUTOFF;
865
    else if (STREQ(ret, "Shutting Down"))
866
        state = VIR_DOMAIN_SHUTDOWN;
867

868
  cleanup:
869
    VIR_FREE(cmd);
870 871
    VIR_FREE(ret);
    return state;
872 873
}

874 875 876 877 878 879
int
phypGetVIOSPartitionID(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
880 881
    char *cmd = NULL;
    char *ret = NULL;
882 883 884 885 886 887 888 889
    int exit_status = 0;
    int id = -1;
    char *char_ptr;
    char *managed_system = phyp_driver->managed_system;

    if (virAsprintf(&cmd,
                    "lssyscfg -m %s -r lpar -F lpar_id,lpar_env|grep "
                    "vioserver|sed -s 's/,.*$//g'", managed_system) < 0) {
890
        virReportOOMError();
891 892 893
        goto err;
    }

894
    ret = phypExec(session, cmd, &exit_status, conn);
895 896 897 898 899 900 901

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

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

902 903
    VIR_FREE(cmd);
    VIR_FREE(ret);
904 905 906 907
    return id;

  err:
    VIR_FREE(cmd);
908
    VIR_FREE(ret);
909 910 911
    return -1;
}

912 913 914
int
phypDiskType(virConnectPtr conn, char *backing_device)
{
915
    phyp_driverPtr phyp_driver = conn->privateData;
916
    ConnectionData *connection_data = conn->networkPrivateData;
917
    LIBSSH2_SESSION *session = connection_data->session;
918 919
    char *cmd = NULL;
    char *ret = NULL;
920
    int exit_status = 0;
921 922 923
    char *char_ptr;
    char *managed_system = phyp_driver->managed_system;
    int vios_id = phyp_driver->vios_id;
924
    int disk_type = -1;
925 926

    if (virAsprintf(&cmd,
927 928 929
                    "viosvrcmd -m %s -p %d -c \"lssp -field name type "
                    "-fmt , -all|grep %s|sed -e 's/^.*,//g'\"",
                    managed_system, vios_id, backing_device) < 0) {
930
        virReportOOMError();
931
        goto cleanup;
932 933
    }

934
    ret = phypExec(session, cmd, &exit_status, conn);
935

936 937
    if (exit_status < 0 || ret == NULL)
        goto cleanup;
938

939
    char_ptr = strchr(ret, '\n');
940 941 942 943 944

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

    if (STREQ(ret, "LVPOOL"))
945
        disk_type = VIR_DOMAIN_DISK_TYPE_BLOCK;
946
    else if (STREQ(ret, "FBPOOL"))
947
        disk_type = VIR_DOMAIN_DISK_TYPE_FILE;
948

949
  cleanup:
950
    VIR_FREE(cmd);
951 952
    VIR_FREE(ret);
    return disk_type;
953 954 955 956 957 958 959 960 961 962 963 964 965 966
}

/* 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;
967
    phyp_driverPtr phyp_driver = conn->privateData;
968
    LIBSSH2_SESSION *session = connection_data->session;
969 970 971
    int exit_status = 0;
    int ndom = 0;
    char *char_ptr;
972 973
    char *cmd = NULL;
    char *ret = NULL;
974
    char *managed_system = phyp_driver->managed_system;
975 976 977 978 979 980 981 982 983 984
    const char *state;

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

    if (virAsprintf(&cmd,
985 986
                    "lssyscfg -r lpar -m %s -F lpar_id,state %s |grep -c "
                    "^[0-9]*", managed_system, state) < 0) {
987
        virReportOOMError();
988 989 990
        goto err;
    }

991
    ret = phypExec(session, cmd, &exit_status, conn);
992 993 994 995 996 997 998 999

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

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

    VIR_FREE(cmd);
1000
    VIR_FREE(ret);
1001 1002 1003 1004
    return ndom;

  err:
    VIR_FREE(cmd);
1005
    VIR_FREE(ret);
1006
    return -1;
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
}

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
1026
 *       1 - all
1027 1028 1029 1030 1031 1032
 * */
static int
phypListDomainsGeneric(virConnectPtr conn, int *ids, int nids,
                       unsigned int type)
{
    ConnectionData *connection_data = conn->networkPrivateData;
1033
    phyp_driverPtr phyp_driver = conn->privateData;
1034
    LIBSSH2_SESSION *session = connection_data->session;
1035
    char *managed_system = phyp_driver->managed_system;
1036 1037 1038 1039 1040
    int exit_status = 0;
    int got = 0;
    char *char_ptr;
    unsigned int i = 0, j = 0;
    char id_c[10];
1041 1042
    char *cmd = NULL;
    char *ret = NULL;
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    const char *state;

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

    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) {
1056
        virReportOOMError();
1057 1058
        goto err;
    }
1059
    ret = phypExec(session, cmd, &exit_status, conn);
1060

1061 1062
    /* I need to parse the textual return in order to get the ret */
    if (exit_status < 0 || ret == NULL)
1063 1064 1065
        goto err;
    else {
        while (got < nids) {
1066 1067 1068 1069 1070 1071 1072
            if (ret[i] == '\0')
                break;
            else if (ret[i] == '\n') {
                if (virStrToLong_i(id_c, &char_ptr, 10, &ids[got]) == -1) {
                    VIR_ERROR("Cannot parse number from '%s'", id_c);
                    goto err;
                }
1073 1074 1075 1076
                memset(id_c, 0, 10);
                j = 0;
                got++;
            } else {
1077
                id_c[j] = ret[i];
1078 1079 1080 1081 1082 1083 1084
                j++;
            }
            i++;
        }
    }

    VIR_FREE(cmd);
1085
    VIR_FREE(ret);
1086 1087 1088 1089
    return got;

  err:
    VIR_FREE(cmd);
1090
    VIR_FREE(ret);
1091
    return -1;
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
}

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;
1104
    phyp_driverPtr phyp_driver = conn->privateData;
1105
    LIBSSH2_SESSION *session = connection_data->session;
1106
    char *managed_system = phyp_driver->managed_system;
1107 1108
    int exit_status = 0;
    int got = 0;
1109 1110 1111 1112 1113
    int i;
    char *cmd = NULL;
    char *ret = NULL;
    char *domains = NULL;
    char *char_ptr2 = NULL;
1114 1115 1116

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

1123
    ret = phypExec(session, cmd, &exit_status, conn);
1124 1125

    /* I need to parse the textual return in order to get the domains */
1126
    if (exit_status < 0 || ret == NULL)
1127 1128
        goto err;
    else {
1129 1130
        domains = ret;

1131 1132 1133 1134 1135
        while (got < nnames) {
            char_ptr2 = strchr(domains, '\n');

            if (char_ptr2) {
                *char_ptr2 = '\0';
1136
                if ((names[got++] = strdup(domains)) == NULL) {
1137
                    virReportOOMError();
1138
                    goto err;
1139
                }
1140 1141
                char_ptr2++;
                domains = char_ptr2;
1142 1143
            } else
                break;
1144 1145 1146 1147 1148 1149 1150 1151
        }
    }

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

  err:
1152 1153 1154
    for (i = 0; i < got; i++)
        VIR_FREE(names[i]);
    VIR_FREE(cmd);
1155
    VIR_FREE(ret);
1156
    return -1;
1157 1158 1159 1160 1161 1162
}

static virDomainPtr
phypDomainLookupByName(virConnectPtr conn, const char *lpar_name)
{
    ConnectionData *connection_data = conn->networkPrivateData;
1163
    phyp_driverPtr phyp_driver = conn->privateData;
1164
    LIBSSH2_SESSION *session = connection_data->session;
1165 1166
    virDomainPtr dom = NULL;
    int lpar_id = 0;
1167
    char *managed_system = phyp_driver->managed_system;
1168
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
1169

1170
    lpar_id = phypGetLparID(session, managed_system, lpar_name, conn);
1171
    if (lpar_id == -1)
1172
        return NULL;
1173 1174

    if (phypGetLparUUID(lpar_uuid, lpar_id, conn) == -1)
1175
        return NULL;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

    dom = virGetDomain(conn, lpar_name, lpar_uuid);

    if (dom)
        dom->id = lpar_id;

    return dom;
}

static virDomainPtr
phypDomainLookupByID(virConnectPtr conn, int lpar_id)
{
    ConnectionData *connection_data = conn->networkPrivateData;
1189
    phyp_driverPtr phyp_driver = conn->privateData;
1190
    LIBSSH2_SESSION *session = connection_data->session;
1191
    virDomainPtr dom = NULL;
1192
    char *managed_system = phyp_driver->managed_system;
1193
    int exit_status = 0;
1194
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
1195

1196
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
                                      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);
    return dom;

  err:
    VIR_FREE(lpar_name);
    return NULL;
}

static char *
phypDomainDumpXML(virDomainPtr dom, int flags)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
1222
    phyp_driverPtr phyp_driver = dom->conn->privateData;
1223
    LIBSSH2_SESSION *session = connection_data->session;
1224
    virDomainDef def;
1225
    char *managed_system = phyp_driver->managed_system;
1226

1227
    memset(&def, 0, sizeof(virDomainDef));
1228

1229 1230
    def.virtType = VIR_DOMAIN_VIRT_PHYP;
    def.id = dom->id;
1231

1232
    char *lpar_name = phypGetLparNAME(session, managed_system, def.id,
1233 1234 1235
                                      dom->conn);

    if (lpar_name == NULL) {
1236
        VIR_ERROR0(_("Unable to determine domain's name."));
1237 1238 1239
        goto err;
    }

1240
    if (phypGetLparUUID(def.uuid, dom->id, dom->conn) == -1) {
1241
        VIR_ERROR0(_("Unable to generate random uuid."));
1242 1243 1244
        goto err;
    }

1245
    if ((def.maxmem =
1246
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0) {
1247
        VIR_ERROR0(_("Unable to determine domain's max memory."));
1248 1249 1250
        goto err;
    }

1251
    if ((def.memory =
1252
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0) {
1253
        VIR_ERROR0(_("Unable to determine domain's memory."));
1254 1255 1256
        goto err;
    }

1257
    if ((def.vcpus =
1258
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0) {
1259
        VIR_ERROR0(_("Unable to determine domain's CPU."));
1260 1261 1262
        goto err;
    }

1263
    return virDomainDefFormat(&def, flags);
1264 1265 1266

  err:
    return NULL;
1267 1268 1269 1270 1271 1272
}

static int
phypDomainResume(virDomainPtr dom)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
1273
    phyp_driverPtr phyp_driver = dom->conn->privateData;
1274
    LIBSSH2_SESSION *session = connection_data->session;
1275
    char *managed_system = phyp_driver->managed_system;
1276
    int exit_status = 0;
1277 1278
    char *cmd = NULL;
    char *ret = NULL;
1279 1280 1281 1282 1283

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

1288
    ret = phypExec(session, cmd, &exit_status, dom->conn);
1289

1290 1291 1292
    if (exit_status < 0)
        goto err;

1293 1294 1295 1296
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return 0;

1297 1298 1299 1300
  err:
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return -1;
1301 1302 1303 1304 1305 1306
}

static int
phypDomainShutdown(virDomainPtr dom)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
1307
    phyp_driverPtr phyp_driver = dom->conn->privateData;
1308
    LIBSSH2_SESSION *session = connection_data->session;
1309
    char *managed_system = phyp_driver->managed_system;
1310
    int exit_status = 0;
1311 1312
    char *cmd = NULL;
    char *ret = NULL;
1313 1314 1315 1316 1317

    if (virAsprintf
        (&cmd,
         "chsysstate -m %s -r lpar -o shutdown --id %d",
         managed_system, dom->id) < 0) {
1318
        virReportOOMError();
1319 1320 1321
        goto err;
    }

1322
    ret = phypExec(session, cmd, &exit_status, dom->conn);
1323

1324 1325 1326
    if (exit_status < 0)
        goto err;

1327 1328 1329 1330
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return 0;

1331 1332 1333 1334
  err:
    VIR_FREE(cmd);
    VIR_FREE(ret);
    return 0;
1335 1336 1337 1338 1339
}

static int
phypDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
1340 1341
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;
1342 1343 1344 1345 1346

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

    if ((info->maxMem =
         phypGetLparMem(dom->conn, managed_system, dom->id, 0)) == 0)
1347
        VIR_WARN0("Unable to determine domain's max memory.");
1348 1349 1350

    if ((info->memory =
         phypGetLparMem(dom->conn, managed_system, dom->id, 1)) == 0)
1351
        VIR_WARN0("Unable to determine domain's memory.");
1352 1353 1354

    if ((info->nrVirtCpu =
         phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
1355
        VIR_WARN0("Unable to determine domain's CPU.");
1356 1357 1358 1359

    return 0;
}

1360 1361 1362 1363 1364 1365 1366 1367
static int
phypDomainDestroy(virDomainPtr dom)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
1368 1369
    char *cmd = NULL;
    char *ret = NULL;
1370 1371 1372 1373

    if (virAsprintf
        (&cmd,
         "rmsyscfg -m %s -r lpar --id %d", managed_system, dom->id) < 0) {
1374
        virReportOOMError();
1375 1376 1377
        goto err;
    }

1378
    ret = phypExec(session, cmd, &exit_status, dom->conn);
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

    if (exit_status < 0)
        goto err;

    if (phypUUIDTable_RemLpar(dom->conn, dom->id) == -1)
        goto err;

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

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

}

static virDomainPtr
phypDomainCreateAndStart(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags ATTRIBUTE_UNUSED)
{

    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    virDomainDefPtr def = NULL;
    virDomainPtr dom = NULL;
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    lparPtr *lpars = uuid_table->lpars;
    unsigned int i = 0;
    char *managed_system = phyp_driver->managed_system;

1413
    if (!(def = virDomainDefParseString(phyp_driver->caps, xml,
1414 1415 1416 1417 1418
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

    /* checking if this name already exists on this system */
    if (phypGetLparID(session, managed_system, def->name, conn) == -1) {
1419
        VIR_WARN0("LPAR name already exists.");
1420 1421 1422 1423 1424 1425
        goto err;
    }

    /* checking if ID or UUID already exists on this system */
    for (i = 0; i < uuid_table->nlpars; i++) {
        if (lpars[i]->id == def->id || lpars[i]->uuid == def->uuid) {
1426
            VIR_WARN0("LPAR ID or UUID already exists.");
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
            goto err;
        }
    }

    if ((dom = virGetDomain(conn, def->name, def->uuid)) == NULL)
        goto err;

    if (phypBuildLpar(conn, def) == -1)
        goto err;

    if (phypDomainResume(dom) == -1)
        goto err;

    return dom;

  err:
    virDomainDefFree(def);
1444 1445
    if (dom)
        virUnrefDomain(dom);
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
    return NULL;
}

static char *
phypConnectGetCapabilities(virConnectPtr conn)
{
    phyp_driverPtr phyp_driver = conn->privateData;
    char *xml;

    if ((xml = virCapabilitiesFormatXML(phyp_driver->caps)) == NULL)
1456
        virReportOOMError();
1457 1458 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 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512

    return xml;
}

virCapsPtr
phypCapsInit(void)
{
    struct utsname utsname;
    virCapsPtr caps;
    virCapsGuestPtr guest;

    uname(&utsname);

    if ((caps = virCapabilitiesNew(utsname.machine, 0, 0)) == NULL)
        goto no_memory;

    /* Some machines have problematic NUMA toplogy causing
     * unexpected failures. We don't want to break the QEMU
     * driver in this scenario, so log errors & carry on
     */
    if (nodeCapsInitNUMA(caps) < 0) {
        virCapabilitiesFreeNUMAInfo(caps);
        VIR_WARN0
            ("Failed to query host NUMA topology, disabling NUMA capabilities");
    }

    /* XXX shouldn't 'borrow' KVM's prefix */
    virCapabilitiesSetMacPrefix(caps, (unsigned char[]) {
                                0x52, 0x54, 0x00});

    if ((guest = virCapabilitiesAddGuest(caps,
                                         "linux",
                                         utsname.machine,
                                         sizeof(int) == 4 ? 32 : 8,
                                         NULL, NULL, 0, NULL)) == NULL)
        goto no_memory;

    if (virCapabilitiesAddGuestDomain(guest,
                                      "phyp", NULL, NULL, 0, NULL) == NULL)
        goto no_memory;

    return caps;

  no_memory:
    virCapabilitiesFree(caps);
    return NULL;
}

static int
phypDomainSetCPU(virDomainPtr dom, unsigned int nvcpus)
{
    ConnectionData *connection_data = dom->conn->networkPrivateData;
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
    int exit_status = 0;
1513 1514
    char *cmd = NULL;
    char *ret = NULL;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
    char operation;
    unsigned long ncpus = 0;
    unsigned int amount = 0;

    if ((ncpus = phypGetLparCPU(dom->conn, managed_system, dom->id)) == 0)
        goto err;

    if (nvcpus > phypGetLparCPUMAX(dom)) {
        VIR_ERROR("%s",
                  "You are trying to set a number of CPUs bigger than "
                  "the max possible..");
        goto err;
    }

    if (ncpus > nvcpus) {
        operation = 'r';
        amount = nvcpus - ncpus;
    } else if (ncpus < nvcpus) {
        operation = 'a';
        amount = nvcpus - ncpus;
    } else
        goto exit;

    if (virAsprintf
        (&cmd,
         "chhwres -r proc -m %s --id %d -o %c --procunits %d 2>&1 |sed"
         "-e 's/^.*\\([0-9]\\+.[0-9]\\+\\).*$/\\1/g'",
         managed_system, dom->id, operation, amount) < 0) {
1543
        virReportOOMError();
1544 1545 1546
        goto err;
    }

1547
    ret = phypExec(session, cmd, &exit_status, dom->conn);
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575

    if (exit_status < 0) {
        VIR_ERROR("%s",
                  "Possibly you don't have IBM Tools installed in your LPAR."
                  "Contact your support to enable this feature.");
        goto err;
    }

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

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

}

virDriver phypDriver = {
    VIR_DRV_PHYP,
    "PHYP",
    phypOpen,                   /* open */
    phypClose,                  /* close */
    NULL,                       /* supports_feature */
    NULL,                       /* type */
    NULL,                       /* version */
1576
    NULL,                       /* libvirtVersion (impl. in libvirt.c) */
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
    NULL,                       /* getHostname */
    NULL,                       /* getMaxVcpus */
    NULL,                       /* nodeGetInfo */
    phypConnectGetCapabilities, /* getCapabilities */
    phypListDomains,            /* listDomains */
    phypNumDomains,             /* numOfDomains */
    phypDomainCreateAndStart,   /* domainCreateXML */
    phypDomainLookupByID,       /* domainLookupByID */
    NULL,                       /* domainLookupByUUID */
    phypDomainLookupByName,     /* domainLookupByName */
    NULL,                       /* domainSuspend */
    phypDomainResume,           /* domainResume */
    phypDomainShutdown,         /* domainShutdown */
    NULL,                       /* domainReboot */
    phypDomainDestroy,          /* domainDestroy */
    NULL,                       /* domainGetOSType */
    NULL,                       /* domainGetMaxMemory */
    NULL,                       /* domainSetMaxMemory */
    NULL,                       /* domainSetMemory */
1596 1597 1598 1599
    phypDomainGetInfo,          /* domainGetInfo */
    NULL,                       /* domainSave */
    NULL,                       /* domainRestore */
    NULL,                       /* domainCoreDump */
1600
    phypDomainSetCPU,           /* domainSetVcpus */
1601 1602
    NULL,                       /* domainPinVcpu */
    NULL,                       /* domainGetVcpus */
1603
    phypGetLparCPUMAX,          /* domainGetMaxVcpus */
1604 1605 1606
    NULL,                       /* domainGetSecurityLabel */
    NULL,                       /* nodeGetSecurityModel */
    phypDomainDumpXML,          /* domainDumpXML */
1607 1608
    NULL,                       /* domainXMLFromNative */
    NULL,                       /* domainXMLToNative */
1609 1610 1611 1612 1613 1614
    phypListDefinedDomains,     /* listDefinedDomains */
    phypNumDefinedDomains,      /* numOfDefinedDomains */
    NULL,                       /* domainCreate */
    NULL,                       /* domainDefineXML */
    NULL,                       /* domainUndefine */
    NULL,                       /* domainAttachDevice */
1615
    NULL,                       /* domainAttachDeviceFlags */
1616
    NULL,                       /* domainDetachDevice */
1617
    NULL,                       /* domainDetachDeviceFlags */
1618
    NULL,                       /* domainUpdateDeviceFlags */
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
    NULL,                       /* domainGetAutostart */
    NULL,                       /* domainSetAutostart */
    NULL,                       /* domainGetSchedulerType */
    NULL,                       /* domainGetSchedulerParameters */
    NULL,                       /* domainSetSchedulerParameters */
    NULL,                       /* domainMigratePrepare */
    NULL,                       /* domainMigratePerform */
    NULL,                       /* domainMigrateFinish */
    NULL,                       /* domainBlockStats */
    NULL,                       /* domainInterfaceStats */
1629
    NULL,                       /* domainMemoryStats */
1630 1631
    NULL,                       /* domainBlockPeek */
    NULL,                       /* domainMemoryPeek */
1632
    NULL,                       /* domainGetBlockInfo */
1633 1634 1635 1636 1637 1638 1639 1640 1641
    NULL,                       /* nodeGetCellsFreeMemory */
    NULL,                       /* getFreeMemory */
    NULL,                       /* domainEventRegister */
    NULL,                       /* domainEventDeregister */
    NULL,                       /* domainMigratePrepare2 */
    NULL,                       /* domainMigrateFinish2 */
    NULL,                       /* nodeDeviceDettach */
    NULL,                       /* nodeDeviceReAttach */
    NULL,                       /* nodeDeviceReset */
C
Chris Lalancette 已提交
1642
    NULL,                       /* domainMigratePrepareTunnel */
1643 1644
    phypIsEncrypted,            /* isEncrypted */
    phypIsSecure,               /* isSecure */
1645 1646
    NULL,                       /* domainIsActive */
    NULL,                       /* domainIsPersistent */
J
Jiri Denemark 已提交
1647
    NULL,                       /* cpuCompare */
1648
    NULL,                       /* cpuBaseline */
1649
    NULL, /* domainGetJobInfo */
1650
    NULL, /* domainAbortJob */
1651
    NULL, /* domainMigrateSetMaxDowntime */
1652 1653
    NULL, /* domainEventRegisterAny */
    NULL, /* domainEventDeregisterAny */
1654 1655 1656
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665
    NULL, /* domainSnapshotCreateXML */
    NULL, /* domainSnapshotDumpXML */
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
1666 1667 1668
};

int
1669
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
1670
{
1671 1672 1673 1674
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
1675 1676
    char *cmd = NULL;
    char *ret = NULL;
1677 1678 1679 1680 1681 1682 1683 1684 1685
    int exit_status = 0;

    if (virAsprintf
        (&cmd,
         "mksyscfg -m %s -r lpar -p %s -i min_mem=%d,desired_mem=%d,"
         "max_mem=%d,desired_procs=%d,virtual_scsi_adapters=%s",
         managed_system, def->name, (int) def->memory,
         (int) def->memory, (int) def->maxmem, (int) def->vcpus,
         def->disks[0]->src) < 0) {
1686
        virReportOOMError();
1687 1688 1689
        goto err;
    }

1690
    ret = phypExec(session, cmd, &exit_status, conn);
1691 1692 1693 1694 1695 1696 1697

    if (exit_status < 0) {
        VIR_ERROR("%s\"%s\"", "Unable to create LPAR. Reason: ", ret);
        goto err;
    }

    if (phypUUIDTable_AddLpar(conn, def->uuid, def->id) == -1) {
1698
        VIR_ERROR0(_("Unable to add LPAR to the table"));
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
        goto err;
    }

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

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

int
phypUUIDTable_RemLpar(virConnectPtr conn, int id)
{
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;

    for (i = 0; i <= uuid_table->nlpars; i++) {
        if (uuid_table->lpars[i]->id == id) {
            uuid_table->lpars[i]->id = -1;
1722
            memset(uuid_table->lpars[i]->uuid, 0, VIR_UUID_BUFLEN);
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
        }
    }

    if (phypUUIDTable_WriteFile(conn) == -1)
        goto err;

    if (phypUUIDTable_Push(conn) == -1)
        goto err;

    return 0;

  err:
    return -1;
}

int
phypUUIDTable_AddLpar(virConnectPtr conn, unsigned char *uuid, int id)
{
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;

    uuid_table->nlpars++;
    unsigned int i = uuid_table->nlpars;
    i--;

    if (VIR_REALLOC_N(uuid_table->lpars, uuid_table->nlpars) < 0) {
1749
        virReportOOMError();
1750 1751 1752 1753
        goto err;
    }

    if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
1754
        virReportOOMError();
1755 1756 1757 1758
        goto err;
    }

    uuid_table->lpars[i]->id = id;
1759
    memmove(uuid_table->lpars[i]->uuid, uuid, VIR_UUID_BUFLEN);
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

    if (phypUUIDTable_WriteFile(conn) == -1)
        goto err;

    if (phypUUIDTable_Push(conn) == -1)
        goto err;

    return 0;

  err:
    return -1;
}

int
phypUUIDTable_ReadFile(virConnectPtr conn)
{
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;
    int fd = -1;
    char local_file[] = "./uuid_table";
    int rc = 0;
1782
    int id;
1783 1784

    if ((fd = open(local_file, O_RDONLY)) == -1) {
1785
        VIR_WARN0("Unable to write information to local file.");
1786 1787 1788 1789 1790 1791 1792
        goto err;
    }

    /* Creating a new data base and writing to local file */
    if (VIR_ALLOC_N(uuid_table->lpars, uuid_table->nlpars) >= 0) {
        for (i = 0; i < uuid_table->nlpars; i++) {

1793
            rc = read(fd, &id, sizeof(int));
1794
            if (rc == sizeof(int)) {
1795
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
1796
                    virReportOOMError();
1797 1798
                    goto err;
                }
1799
                uuid_table->lpars[i]->id = id;
1800
            } else {
1801
                VIR_WARN0("Unable to read from information to local file.");
1802 1803 1804 1805 1806
                goto err;
            }

            rc = read(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN);
            if (rc != VIR_UUID_BUFLEN) {
1807
                VIR_WARN0("Unable to read information to local file.");
1808 1809 1810
                goto err;
            }
        }
1811
    } else
1812
        virReportOOMError();
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835

    close(fd);
    return 0;

  err:
    close(fd);
    return -1;
}

int
phypUUIDTable_WriteFile(virConnectPtr conn)
{
    phyp_driverPtr phyp_driver = conn->privateData;
    uuid_tablePtr uuid_table = phyp_driver->uuid_table;
    unsigned int i = 0;
    int fd = -1;
    char local_file[] = "./uuid_table";

    if ((fd = creat(local_file, 0755)) == -1)
        goto err;

    for (i = 0; i < uuid_table->nlpars; i++) {
        if (safewrite(fd, &uuid_table->lpars[i]->id,
1836
                      sizeof(uuid_table->lpars[i]->id)) !=
1837
            sizeof(uuid_table->lpars[i]->id)) {
1838
            VIR_ERROR0(_("Unable to write information to local file."));
1839 1840 1841 1842 1843
            goto err;
        }

        if (safewrite(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN) !=
            VIR_UUID_BUFLEN) {
1844
            VIR_ERROR0(_("Unable to write information to local file."));
1845 1846 1847 1848 1849
            goto err;
        }
    }

    close(fd);
1850
    return 0;
1851 1852 1853 1854

  err:
    close(fd);
    return -1;
1855 1856
}

1857 1858
int
phypUUIDTable_Init(virConnectPtr conn)
1859
{
1860 1861
    uuid_tablePtr uuid_table;
    phyp_driverPtr phyp_driver;
1862 1863 1864 1865
    int nids = 0;
    int *ids = NULL;
    unsigned int i = 0;

1866
    if ((nids = phypNumDomainsGeneric(conn, 2)) < 0)
1867
        goto err;
1868

1869 1870 1871 1872
    /* exit early if there are no domains */
    if (nids == 0)
        return 0;

1873
    if (VIR_ALLOC_N(ids, nids) < 0) {
1874
        virReportOOMError();
1875 1876
        goto err;
    }
1877

1878
    if ((nids = phypListDomainsGeneric(conn, ids, nids, 1)) < 0)
1879 1880
        goto err;

1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
    /* exit early if there are no domains */
    /* FIXME: phypNumDomainsGeneric() returned > 0 but phypListDomainsGeneric()
     *        returned 0. indicates this an error condition?
     *        an even stricter check would be to treat
     *
     *          phypNumDomainsGeneric() != phypListDomainsGeneric()
     *
     *        as an error */
    if (nids == 0) {
        VIR_FREE(ids);
        return 0;
    }

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
    phyp_driver = conn->privateData;
    uuid_table = phyp_driver->uuid_table;
    uuid_table->nlpars = nids;

    /* try to get the table from server */
    if (phypUUIDTable_Pull(conn) == -1) {
        /* file not found in the server, creating a new one */
        if (VIR_ALLOC_N(uuid_table->lpars, uuid_table->nlpars) >= 0) {
            for (i = 0; i < uuid_table->nlpars; i++) {
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
1904
                    virReportOOMError();
1905 1906 1907 1908 1909
                    goto err;
                }
                uuid_table->lpars[i]->id = ids[i];

                if (virUUIDGenerate(uuid_table->lpars[i]->uuid) < 0)
1910
                    VIR_WARN("Unable to generate UUID for domain %d", ids[i]);
1911
            }
1912
        } else {
1913
            virReportOOMError();
1914
            goto err;
1915
        }
1916 1917 1918 1919 1920 1921 1922 1923 1924

        if (phypUUIDTable_WriteFile(conn) == -1)
            goto err;

        if (phypUUIDTable_Push(conn) == -1)
            goto err;
    } else {
        if (phypUUIDTable_ReadFile(conn) == -1)
            goto err;
1925
        goto exit;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
    }

  exit:
    VIR_FREE(ids);
    return 0;

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

1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
void
phypUUIDTable_Free(uuid_tablePtr uuid_table)
{
    int i;

    if (uuid_table == NULL)
        return;

    for (i = 0; i < uuid_table->nlpars; i++)
        VIR_FREE(uuid_table->lpars[i]);

    VIR_FREE(uuid_table->lpars);
    VIR_FREE(uuid_table);
}

1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
int
phypUUIDTable_Push(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    LIBSSH2_CHANNEL *channel = NULL;
    struct stat local_fileinfo;
    char buffer[1024];
    int rc = 0;
    FILE *fd;
    size_t nread, sent;
    char *ptr;
    char remote_file[] = "/home/hscroot/libvirt_uuid_table";
    char local_file[] = "./uuid_table";

    if (stat(local_file, &local_fileinfo) == -1) {
        VIR_WARN0("Unable to stat local file.");
        goto err;
    }
1971

1972 1973 1974 1975
    if (!(fd = fopen(local_file, "rb"))) {
        VIR_WARN0("Unable to open local file.");
        goto err;
    }
1976

1977 1978 1979 1980 1981
    do {
        channel =
            libssh2_scp_send(session, remote_file,
                             0x1FF & local_fileinfo.st_mode,
                             (unsigned long) local_fileinfo.st_size);
1982

1983 1984 1985 1986 1987 1988 1989 1990
        if ((!channel) && (libssh2_session_last_errno(session) !=
                           LIBSSH2_ERROR_EAGAIN))
            goto err;
    } while (!channel);

    do {
        nread = fread(buffer, 1, sizeof(buffer), fd);
        if (nread <= 0) {
1991 1992 1993 1994 1995 1996 1997
            if (feof(fd)) {
                /* end of file */
                break;
            } else {
                VIR_ERROR("Failed to read from '%s'", local_file);
                goto err;
            }
1998
        }
1999 2000 2001 2002 2003 2004 2005 2006
        ptr = buffer;
        sent = 0;

        do {
            /* write the same data over and over, until error or completion */
            rc = libssh2_channel_write(channel, ptr, nread);
            if (LIBSSH2_ERROR_EAGAIN == rc) {   /* must loop around */
                continue;
2007
            } else if (rc > 0) {
2008 2009 2010
                /* rc indicates how many bytes were written this time */
                sent += rc;
            }
2011 2012
            ptr += sent;
            nread -= sent;
2013 2014 2015 2016 2017 2018 2019 2020 2021
        } while (rc > 0 && sent < nread);
    } while (1);

    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
2022
    }
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
    return 0;

  err:
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    return -1;
}

int
phypUUIDTable_Pull(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    LIBSSH2_SESSION *session = connection_data->session;
    LIBSSH2_CHANNEL *channel = NULL;
    struct stat fileinfo;
    char buffer[1024];
    int rc = 0;
    int fd;
    int got = 0;
    int amount = 0;
    int total = 0;
    int sock = 0;
    char remote_file[] = "/home/hscroot/libvirt_uuid_table";
    char local_file[] = "./uuid_table";

    /* Trying to stat the remote file. */
    do {
        channel = libssh2_scp_recv(session, remote_file, &fileinfo);

        if (!channel) {
            if (libssh2_session_last_errno(session) !=
                LIBSSH2_ERROR_EAGAIN) {
                goto err;;
            } else {
                waitsocket(sock, session);
            }
        }
    } while (!channel);

    /* Creating a new data base based on remote file */
    if ((fd = creat(local_file, 0755)) == -1)
        goto err;

    /* Request a file via SCP */
    while (got < fileinfo.st_size) {
        do {
            amount = sizeof(buffer);

            if ((fileinfo.st_size - got) < amount) {
                amount = fileinfo.st_size - got;
            }

            rc = libssh2_channel_read(channel, buffer, amount);
            if (rc > 0) {
                if (safewrite(fd, buffer, rc) != rc)
2083
                    VIR_WARN0("Unable to write information to local file.");
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102

                got += rc;
                total += rc;
            }
        } while (rc > 0);

        if ((rc == LIBSSH2_ERROR_EAGAIN)
            && (got < fileinfo.st_size)) {
            /* this is due to blocking that would occur otherwise
             * so we loop on this condition */

            waitsocket(sock, session);  /* now we wait */
            continue;
        }
        break;
    }
    close(fd);
    goto exit;

2103
  exit:
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    return 0;

  err:
    if (channel) {
        libssh2_channel_send_eof(channel);
        libssh2_channel_wait_eof(channel);
        libssh2_channel_wait_closed(channel);
        libssh2_channel_free(channel);
        channel = NULL;
    }
    return -1;
2122 2123
}

2124
int
2125
escape_specialcharacters(char *src, char *dst, size_t dstlen)
2126 2127 2128 2129 2130 2131 2132 2133 2134
{
    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]) {
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
            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':
2159 2160 2161 2162 2163 2164 2165 2166 2167
            case '\t':
                continue;
            default:
                temp_buffer[j] = src[i];
                j++;
        }
    }
    temp_buffer[j] = '\0';

C
Chris Lalancette 已提交
2168
    if (virStrcpy(dst, temp_buffer, dstlen) == NULL)
2169 2170 2171 2172
        return -1;

    return 0;
}
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183

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;

2184 2185
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000;
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203

    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;
}
2204 2205 2206 2207 2208 2209 2210

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