phyp_driver.c 56.9 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 38
#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>
39 40 41 42 43
#include <libssh2.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
44 45 46
#include <fcntl.h>
#include <sys/utsname.h>
#include <domain_event.h>
47 48

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

#include "phyp_driver.h"

#define VIR_FROM_THIS VIR_FROM_PHYP

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

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

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

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

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

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

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

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

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

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

128 129 130 131 132 133 134
    /* 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) {
135
        virReportOOMError();
136 137 138 139 140 141 142 143 144 145 146
        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';

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

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

159 160
    connection_data->session = session;

161 162 163 164 165 166
    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) {
167
        virReportOOMError();
168 169
        goto failure;
    }
170

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

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

    return VIR_DRV_OPEN_SUCCESS;
180

181
  failure:
182 183 184 185 186 187 188 189 190 191 192 193 194
    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);
    }

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

    return VIR_DRV_OPEN_ERROR;
199 200 201 202 203 204
}

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

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

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

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

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


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

    if (userhome == NULL)
        goto err;

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

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

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    if (conn->uri->user != NULL) {
        username = strdup(conn->uri->user);

        if (username == NULL) {
            virReportOOMError();
            goto err;
        }
    } else {
        if (auth == NULL || auth->cb == NULL) {
            PHYP_ERROR(conn, VIR_ERR_AUTH_FAILED,
                       "%s", _("No authentication callback provided."));
            goto err;
        }

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

        if (username == NULL) {
            PHYP_ERROR(conn, VIR_ERR_AUTH_FAILED, "Username request failed");
            goto err;
        }
    }

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

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

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

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

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

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

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

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

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

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

363 364
        if (password == NULL) {
            PHYP_ERROR(conn, VIR_ERR_AUTH_FAILED, "Password request failed");
365
            goto disconnect;
366 367
        }

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

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

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

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

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

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

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

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

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

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

463
    exitcode = 127;
464

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

}

unsigned long
phypGetLparCPU(virConnectPtr conn, const char *managed_system, int lpar_id)
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
{
    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)
668 669
{
    ConnectionData *connection_data = conn->networkPrivateData;
670
    LIBSSH2_SESSION *session = connection_data->session;
671 672
    char *cmd = NULL;
    char *ret = NULL;
673
    char *char_ptr;
674 675 676
    int exit_status = 0;
    int vcpus = 0;

677 678 679 680 681
    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) {
682
            virReportOOMError();
683 684 685 686 687 688 689
            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) {
690
            virReportOOMError();
691 692
            goto err;
        }
693
    }
694
    ret = phypExec(session, cmd, &exit_status, conn);
695

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        backing_device = strdup(char_ptr);

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

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

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

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

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

}

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

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

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

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

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

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

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

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

875 876 877 878 879 880
int
phypGetVIOSPartitionID(virConnectPtr conn)
{
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
881 882
    char *cmd = NULL;
    char *ret = NULL;
883 884 885 886 887 888 889 890
    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) {
891
        virReportOOMError();
892 893 894
        goto err;
    }

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

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

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

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

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

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

    if (virAsprintf(&cmd,
928 929 930
                    "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) {
931
        virReportOOMError();
932
        goto cleanup;
933 934
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1062 1063
    /* I need to parse the textual return in order to get the ret */
    if (exit_status < 0 || ret == NULL)
1064 1065 1066
        goto err;
    else {
        while (got < nids) {
1067 1068 1069 1070 1071 1072 1073
            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;
                }
1074 1075 1076 1077
                memset(id_c, 0, 10);
                j = 0;
                got++;
            } else {
1078
                id_c[j] = ret[i];
1079 1080 1081 1082 1083 1084 1085
                j++;
            }
            i++;
        }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    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;
1190
    phyp_driverPtr phyp_driver = conn->privateData;
1191
    LIBSSH2_SESSION *session = connection_data->session;
1192
    virDomainPtr dom = NULL;
1193
    char *managed_system = phyp_driver->managed_system;
1194
    int exit_status = 0;
1195
    unsigned char lpar_uuid[VIR_UUID_BUFLEN];
1196

1197
    char *lpar_name = phypGetLparNAME(session, managed_system, lpar_id,
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
                                      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;
1223
    phyp_driverPtr phyp_driver = dom->conn->privateData;
1224
    LIBSSH2_SESSION *session = connection_data->session;
1225
    virDomainDef def;
1226
    char *managed_system = phyp_driver->managed_system;
1227

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

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

1233
    char *lpar_name = phypGetLparNAME(session, managed_system, def.id,
1234 1235 1236 1237 1238 1239 1240
                                      dom->conn);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

static int
phypDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info)
{
1341 1342
    phyp_driverPtr phyp_driver = dom->conn->privateData;
    char *managed_system = phyp_driver->managed_system;
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

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

1361 1362 1363 1364 1365 1366 1367 1368
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;
1369 1370
    char *cmd = NULL;
    char *ret = NULL;
1371 1372 1373 1374

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

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

    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;

1414
    if (!(def = virDomainDefParseString(phyp_driver->caps, xml,
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
                                        VIR_DOMAIN_XML_SECURE)))
        goto err;

    /* checking if this name already exists on this system */
    if (phypGetLparID(session, managed_system, def->name, conn) == -1) {
        VIR_WARN("%s", "LPAR name already exists.");
        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) {
            VIR_WARN("%s", "LPAR ID or UUID already exists.");
            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);
1445 1446
    if (dom)
        virUnrefDomain(dom);
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
    return NULL;
}

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

    if ((xml = virCapabilitiesFormatXML(phyp_driver->caps)) == NULL)
1457
        virReportOOMError();
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 1513

    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;
1514 1515
    char *cmd = NULL;
    char *ret = NULL;
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 1543
    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) {
1544
        virReportOOMError();
1545 1546 1547
        goto err;
    }

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

    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 */
1577
    NULL,                       /* libvirtVersion (impl. in libvirt.c) */
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
    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 */
1597 1598 1599 1600
    phypDomainGetInfo,          /* domainGetInfo */
    NULL,                       /* domainSave */
    NULL,                       /* domainRestore */
    NULL,                       /* domainCoreDump */
1601
    phypDomainSetCPU,           /* domainSetVcpus */
1602 1603
    NULL,                       /* domainPinVcpu */
    NULL,                       /* domainGetVcpus */
1604
    phypGetLparCPUMAX,          /* domainGetMaxVcpus */
1605 1606 1607
    NULL,                       /* domainGetSecurityLabel */
    NULL,                       /* nodeGetSecurityModel */
    phypDomainDumpXML,          /* domainDumpXML */
1608 1609
    NULL,                       /* domainXMLFromNative */
    NULL,                       /* domainXMLToNative */
1610 1611 1612 1613 1614 1615
    phypListDefinedDomains,     /* listDefinedDomains */
    phypNumDefinedDomains,      /* numOfDefinedDomains */
    NULL,                       /* domainCreate */
    NULL,                       /* domainDefineXML */
    NULL,                       /* domainUndefine */
    NULL,                       /* domainAttachDevice */
1616
    NULL,                       /* domainAttachDeviceFlags */
1617
    NULL,                       /* domainDetachDevice */
1618
    NULL,                       /* domainDetachDeviceFlags */
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 1632 1633 1634 1635 1636 1637 1638 1639 1640
    NULL,                       /* domainBlockPeek */
    NULL,                       /* domainMemoryPeek */
    NULL,                       /* nodeGetCellsFreeMemory */
    NULL,                       /* getFreeMemory */
    NULL,                       /* domainEventRegister */
    NULL,                       /* domainEventDeregister */
    NULL,                       /* domainMigratePrepare2 */
    NULL,                       /* domainMigrateFinish2 */
    NULL,                       /* nodeDeviceDettach */
    NULL,                       /* nodeDeviceReAttach */
    NULL,                       /* nodeDeviceReset */
C
Chris Lalancette 已提交
1641
    NULL,                       /* domainMigratePrepareTunnel */
1642 1643 1644 1645
    phypIsEncrypted,
    phypIsSecure,
    NULL,                       /* domainIsActive */
    NULL,                       /* domainIsPersistent */
J
Jiri Denemark 已提交
1646
    NULL,                       /* cpuCompare */
1647
    NULL,                       /* cpuBaseline */
1648
    NULL, /* domainGetJobInfo */
1649
    NULL, /* domainAbortJob */
1650 1651 1652
};

int
1653
phypBuildLpar(virConnectPtr conn, virDomainDefPtr def)
1654
{
1655 1656 1657 1658
    ConnectionData *connection_data = conn->networkPrivateData;
    phyp_driverPtr phyp_driver = conn->privateData;
    LIBSSH2_SESSION *session = connection_data->session;
    char *managed_system = phyp_driver->managed_system;
1659 1660
    char *cmd = NULL;
    char *ret = NULL;
1661 1662 1663 1664 1665 1666 1667 1668 1669
    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) {
1670
        virReportOOMError();
1671 1672 1673
        goto err;
    }

1674
    ret = phypExec(session, cmd, &exit_status, conn);
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

    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) {
        VIR_ERROR("%s", "Unable to add LPAR to the table");
        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;
1706
            memset(uuid_table->lpars[i]->uuid, 0, VIR_UUID_BUFLEN);
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
        }
    }

    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) {
1733
        virReportOOMError();
1734 1735 1736 1737
        goto err;
    }

    if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
1738
        virReportOOMError();
1739 1740 1741 1742
        goto err;
    }

    uuid_table->lpars[i]->id = id;
1743
    memmove(uuid_table->lpars[i]->uuid, uuid, VIR_UUID_BUFLEN);
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765

    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;
1766
    int id;
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776

    if ((fd = open(local_file, O_RDONLY)) == -1) {
        VIR_WARN("%s", "Unable to write information to local file.");
        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++) {

1777
            rc = read(fd, &id, sizeof(int));
1778
            if (rc == sizeof(int)) {
1779
                if (VIR_ALLOC(uuid_table->lpars[i]) < 0) {
1780
                    virReportOOMError();
1781 1782
                    goto err;
                }
1783
                uuid_table->lpars[i]->id = id;
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
            } else {
                VIR_WARN("%s",
                         "Unable to read from information to local file.");
                goto err;
            }

            rc = read(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN);
            if (rc != VIR_UUID_BUFLEN) {
                VIR_WARN("%s",
                         "Unable to read information to local file.");
                goto err;
            }
        }
1797
    } else
1798
        virReportOOMError();
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821

    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,
1822
                      sizeof(uuid_table->lpars[i]->id)) !=
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
            sizeof(uuid_table->lpars[i]->id)) {
            VIR_ERROR("%s", "Unable to write information to local file.");
            goto err;
        }

        if (safewrite(fd, uuid_table->lpars[i]->uuid, VIR_UUID_BUFLEN) !=
            VIR_UUID_BUFLEN) {
            VIR_ERROR("%s", "Unable to write information to local file.");
            goto err;
        }
    }

    close(fd);
1836
    return 0;
1837 1838 1839 1840

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

1843 1844
int
phypUUIDTable_Init(virConnectPtr conn)
1845
{
1846 1847
    uuid_tablePtr uuid_table;
    phyp_driverPtr phyp_driver;
1848 1849 1850 1851
    int nids = 0;
    int *ids = NULL;
    unsigned int i = 0;

1852
    if ((nids = phypNumDomainsGeneric(conn, 2)) < 0)
1853
        goto err;
1854

1855 1856 1857 1858
    /* exit early if there are no domains */
    if (nids == 0)
        return 0;

1859
    if (VIR_ALLOC_N(ids, nids) < 0) {
1860
        virReportOOMError();
1861 1862
        goto err;
    }
1863

1864
    if ((nids = phypListDomainsGeneric(conn, ids, nids, 1)) < 0)
1865 1866
        goto err;

1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
    /* 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;
    }

1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
    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) {
1890
                    virReportOOMError();
1891 1892 1893 1894 1895 1896 1897 1898
                    goto err;
                }
                uuid_table->lpars[i]->id = ids[i];

                if (virUUIDGenerate(uuid_table->lpars[i]->uuid) < 0)
                    VIR_WARN("%s %d", "Unable to generate UUID for domain",
                             ids[i]);
            }
1899
        } else {
1900
            virReportOOMError();
1901
            goto err;
1902
        }
1903 1904 1905 1906 1907 1908 1909 1910 1911

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

        if (phypUUIDTable_Push(conn) == -1)
            goto err;
    } else {
        if (phypUUIDTable_ReadFile(conn) == -1)
            goto err;
1912
        goto exit;
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
    }

  exit:
    VIR_FREE(ids);
    return 0;

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

1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
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);
}

1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
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;
    }
1958

1959 1960 1961 1962
    if (!(fd = fopen(local_file, "rb"))) {
        VIR_WARN0("Unable to open local file.");
        goto err;
    }
1963

1964 1965 1966 1967 1968
    do {
        channel =
            libssh2_scp_send(session, remote_file,
                             0x1FF & local_fileinfo.st_mode,
                             (unsigned long) local_fileinfo.st_size);
1969

1970 1971 1972 1973 1974 1975 1976 1977
        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) {
1978 1979 1980 1981 1982 1983 1984
            if (feof(fd)) {
                /* end of file */
                break;
            } else {
                VIR_ERROR("Failed to read from '%s'", local_file);
                goto err;
            }
1985
        }
1986 1987 1988 1989 1990 1991 1992 1993
        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;
1994
            } else if (rc > 0) {
1995 1996 1997
                /* rc indicates how many bytes were written this time */
                sent += rc;
            }
1998 1999
            ptr += sent;
            nread -= sent;
2000 2001 2002 2003 2004 2005 2006 2007 2008
        } 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;
2009
    }
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 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 2083 2084 2085 2086 2087 2088 2089 2090
    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)
                    VIR_WARN("%s",
                             "Unable to write information to local file.");

                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;

2091
  exit:
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    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;
2110 2111
}

2112
int
2113
escape_specialcharacters(char *src, char *dst, size_t dstlen)
2114 2115 2116 2117 2118 2119 2120 2121 2122
{
    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]) {
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
            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':
2147 2148 2149 2150 2151 2152 2153 2154 2155
            case '\t':
                continue;
            default:
                temp_buffer[j] = src[i];
                j++;
        }
    }
    temp_buffer[j] = '\0';

C
Chris Lalancette 已提交
2156
    if (virStrcpy(dst, temp_buffer, dstlen) == NULL)
2157 2158 2159 2160
        return -1;

    return 0;
}
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171

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;

2172 2173
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000;
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191

    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;
}
2192 2193 2194 2195 2196 2197 2198

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