754
755 if (retstr != NULL) {
756 if ((*retstr = malloc(1024)) == NULL) {
757 zerror(zlogp, B_FALSE, "out of memory");
758 return (-1);
759 }
760 inbuf = *retstr;
761 rd_cnt = 0;
762 } else {
763 inbuf = buf;
764 }
765
766 file = popen(cmdbuf, "r");
767 if (file == NULL) {
768 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
769 return (-1);
770 }
771
772 while (fgets(inbuf, 1024, file) != NULL) {
773 if (retstr == NULL) {
774 if (zlogp != &logsys)
775 zerror(zlogp, B_FALSE, "%s", inbuf);
776 } else {
777 char *p;
778
779 rd_cnt += 1024 - 1;
780 if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) {
781 zerror(zlogp, B_FALSE, "out of memory");
782 (void) pclose(file);
783 return (-1);
784 }
785
786 *retstr = p;
787 inbuf = *retstr + rd_cnt;
788 }
789 }
790 status = pclose(file);
791
792 if (WIFSIGNALED(status)) {
793 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
794 "signal %d", cmdbuf, WTERMSIG(status));
795 return (-1);
1460 abort();
1461 zerror(zlogp, B_FALSE, "zone is already unmounted");
1462 rval = 0;
1463 break;
1464 }
1465 break;
1466
1467 case ZONE_STATE_READY:
1468 switch (cmd) {
1469 case Z_READY:
1470 /*
1471 * We could have two clients racing to ready this
1472 * zone; the second client loses, but his request
1473 * doesn't fail, since the zone is now in the desired
1474 * state.
1475 */
1476 zerror(zlogp, B_FALSE, "zone is already ready");
1477 rval = 0;
1478 break;
1479 case Z_BOOT:
1480 (void) strlcpy(boot_args, zargp->bootbuf,
1481 sizeof (boot_args));
1482 eventstream_write(Z_EVT_ZONE_BOOTING);
1483 rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
1484 audit_put_record(zlogp, uc, rval, "boot");
1485 if (rval != 0) {
1486 bringup_failure_recovery = B_TRUE;
1487 (void) zone_halt(zlogp, B_FALSE, B_TRUE,
1488 zstate);
1489 eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1490 }
1491 boot_args[0] = '\0';
1492 break;
1493 case Z_HALT:
1494 if (kernelcall) /* Invalid; can't happen */
1495 abort();
1496 if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1497 != 0)
1498 break;
1499 eventstream_write(Z_EVT_ZONE_HALTED);
1500 break;
1501 case Z_SHUTDOWN:
1502 case Z_REBOOT:
1503 case Z_NOTE_UNINSTALLING:
1504 case Z_MOUNT:
1505 case Z_UNMOUNT:
1506 if (kernelcall) /* Invalid; can't happen */
1507 abort();
1508 zerror(zlogp, B_FALSE, "%s operation is invalid "
1509 "for zones in state '%s'", z_cmd_name(cmd),
1510 zone_state_str(zstate));
1511 rval = -1;
1512 break;
1513 }
1514 break;
1515
1516 case ZONE_STATE_MOUNTED:
1517 switch (cmd) {
1518 case Z_UNMOUNT:
1519 if (kernelcall) /* Invalid; can't happen */
1520 abort();
1521 rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate);
1522 if (rval == 0) {
1523 eventstream_write(Z_EVT_ZONE_HALTED);
1524 (void) sema_post(&scratch_sem);
1532 zone_state_str(zstate));
1533 rval = -1;
1534 break;
1535 }
1536 break;
1537
1538 case ZONE_STATE_RUNNING:
1539 case ZONE_STATE_SHUTTING_DOWN:
1540 case ZONE_STATE_DOWN:
1541 switch (cmd) {
1542 case Z_READY:
1543 if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1544 != 0)
1545 break;
1546 if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0)
1547 eventstream_write(Z_EVT_ZONE_READIED);
1548 else
1549 eventstream_write(Z_EVT_ZONE_HALTED);
1550 break;
1551 case Z_BOOT:
1552 /*
1553 * We could have two clients racing to boot this
1554 * zone; the second client loses, but his request
1555 * doesn't fail, since the zone is now in the desired
1556 * state.
1557 */
1558 zerror(zlogp, B_FALSE, "zone is already booted");
1559 rval = 0;
1560 break;
1561 case Z_HALT:
1562 if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1563 != 0)
1564 break;
1565 eventstream_write(Z_EVT_ZONE_HALTED);
1566 break;
1567 case Z_REBOOT:
1568 (void) strlcpy(boot_args, zargp->bootbuf,
1569 sizeof (boot_args));
1570 eventstream_write(Z_EVT_ZONE_REBOOTING);
1571 if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1579 eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1580 boot_args[0] = '\0';
1581 break;
1582 }
1583 rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
1584 audit_put_record(zlogp, uc, rval, "reboot");
1585 if (rval != 0) {
1586 (void) zone_halt(zlogp, B_FALSE, B_TRUE,
1587 zstate);
1588 eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1589 }
1590 boot_args[0] = '\0';
1591 break;
1592 case Z_SHUTDOWN:
1593 if ((rval = zone_graceful_shutdown(zlogp)) == 0) {
1594 wait_shut = B_TRUE;
1595 }
1596 break;
1597 case Z_NOTE_UNINSTALLING:
1598 case Z_MOUNT:
1599 case Z_UNMOUNT:
1600 zerror(zlogp, B_FALSE, "%s operation is invalid "
1601 "for zones in state '%s'", z_cmd_name(cmd),
1602 zone_state_str(zstate));
1603 rval = -1;
1604 break;
1605 }
1606 break;
1607 default:
1608 abort();
1609 }
1610
1611 /*
1612 * Because the state of the zone may have changed, we make sure
1613 * to wake the console poller, which is in charge of initiating
1614 * the shutdown procedure as necessary.
1615 */
1616 eventstream_write(Z_EVT_NULL);
1617
1618 out:
|
754
755 if (retstr != NULL) {
756 if ((*retstr = malloc(1024)) == NULL) {
757 zerror(zlogp, B_FALSE, "out of memory");
758 return (-1);
759 }
760 inbuf = *retstr;
761 rd_cnt = 0;
762 } else {
763 inbuf = buf;
764 }
765
766 file = popen(cmdbuf, "r");
767 if (file == NULL) {
768 zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
769 return (-1);
770 }
771
772 while (fgets(inbuf, 1024, file) != NULL) {
773 if (retstr == NULL) {
774 if (zlogp != &logsys) {
775 int last = strlen(inbuf) - 1;
776
777 if (inbuf[last] == '\n')
778 inbuf[last] = '\0';
779 zerror(zlogp, B_FALSE, "%s", inbuf);
780 }
781 } else {
782 char *p;
783
784 rd_cnt += 1024 - 1;
785 if ((p = realloc(*retstr, rd_cnt + 1024)) == NULL) {
786 zerror(zlogp, B_FALSE, "out of memory");
787 (void) pclose(file);
788 return (-1);
789 }
790
791 *retstr = p;
792 inbuf = *retstr + rd_cnt;
793 }
794 }
795 status = pclose(file);
796
797 if (WIFSIGNALED(status)) {
798 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
799 "signal %d", cmdbuf, WTERMSIG(status));
800 return (-1);
1465 abort();
1466 zerror(zlogp, B_FALSE, "zone is already unmounted");
1467 rval = 0;
1468 break;
1469 }
1470 break;
1471
1472 case ZONE_STATE_READY:
1473 switch (cmd) {
1474 case Z_READY:
1475 /*
1476 * We could have two clients racing to ready this
1477 * zone; the second client loses, but his request
1478 * doesn't fail, since the zone is now in the desired
1479 * state.
1480 */
1481 zerror(zlogp, B_FALSE, "zone is already ready");
1482 rval = 0;
1483 break;
1484 case Z_BOOT:
1485 case Z_FORCEBOOT:
1486 (void) strlcpy(boot_args, zargp->bootbuf,
1487 sizeof (boot_args));
1488 eventstream_write(Z_EVT_ZONE_BOOTING);
1489 rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
1490 audit_put_record(zlogp, uc, rval, "boot");
1491 if (rval != 0) {
1492 bringup_failure_recovery = B_TRUE;
1493 (void) zone_halt(zlogp, B_FALSE, B_TRUE,
1494 zstate);
1495 eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1496 }
1497 boot_args[0] = '\0';
1498 break;
1499 case Z_HALT:
1500 if (kernelcall) /* Invalid; can't happen */
1501 abort();
1502 if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1503 != 0)
1504 break;
1505 eventstream_write(Z_EVT_ZONE_HALTED);
1506 break;
1507 case Z_SHUTDOWN:
1508 case Z_REBOOT:
1509 case Z_NOTE_UNINSTALLING:
1510 case Z_MOUNT:
1511 case Z_FORCEMOUNT:
1512 case Z_UNMOUNT:
1513 if (kernelcall) /* Invalid; can't happen */
1514 abort();
1515 zerror(zlogp, B_FALSE, "%s operation is invalid "
1516 "for zones in state '%s'", z_cmd_name(cmd),
1517 zone_state_str(zstate));
1518 rval = -1;
1519 break;
1520 }
1521 break;
1522
1523 case ZONE_STATE_MOUNTED:
1524 switch (cmd) {
1525 case Z_UNMOUNT:
1526 if (kernelcall) /* Invalid; can't happen */
1527 abort();
1528 rval = zone_halt(zlogp, B_TRUE, B_FALSE, zstate);
1529 if (rval == 0) {
1530 eventstream_write(Z_EVT_ZONE_HALTED);
1531 (void) sema_post(&scratch_sem);
1539 zone_state_str(zstate));
1540 rval = -1;
1541 break;
1542 }
1543 break;
1544
1545 case ZONE_STATE_RUNNING:
1546 case ZONE_STATE_SHUTTING_DOWN:
1547 case ZONE_STATE_DOWN:
1548 switch (cmd) {
1549 case Z_READY:
1550 if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1551 != 0)
1552 break;
1553 if ((rval = zone_ready(zlogp, Z_MNT_BOOT, zstate)) == 0)
1554 eventstream_write(Z_EVT_ZONE_READIED);
1555 else
1556 eventstream_write(Z_EVT_ZONE_HALTED);
1557 break;
1558 case Z_BOOT:
1559 case Z_FORCEBOOT:
1560 /*
1561 * We could have two clients racing to boot this
1562 * zone; the second client loses, but his request
1563 * doesn't fail, since the zone is now in the desired
1564 * state.
1565 */
1566 zerror(zlogp, B_FALSE, "zone is already booted");
1567 rval = 0;
1568 break;
1569 case Z_HALT:
1570 if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE, zstate))
1571 != 0)
1572 break;
1573 eventstream_write(Z_EVT_ZONE_HALTED);
1574 break;
1575 case Z_REBOOT:
1576 (void) strlcpy(boot_args, zargp->bootbuf,
1577 sizeof (boot_args));
1578 eventstream_write(Z_EVT_ZONE_REBOOTING);
1579 if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE, zstate))
1587 eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1588 boot_args[0] = '\0';
1589 break;
1590 }
1591 rval = zone_bootup(zlogp, zargp->bootbuf, zstate);
1592 audit_put_record(zlogp, uc, rval, "reboot");
1593 if (rval != 0) {
1594 (void) zone_halt(zlogp, B_FALSE, B_TRUE,
1595 zstate);
1596 eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1597 }
1598 boot_args[0] = '\0';
1599 break;
1600 case Z_SHUTDOWN:
1601 if ((rval = zone_graceful_shutdown(zlogp)) == 0) {
1602 wait_shut = B_TRUE;
1603 }
1604 break;
1605 case Z_NOTE_UNINSTALLING:
1606 case Z_MOUNT:
1607 case Z_FORCEMOUNT:
1608 case Z_UNMOUNT:
1609 zerror(zlogp, B_FALSE, "%s operation is invalid "
1610 "for zones in state '%s'", z_cmd_name(cmd),
1611 zone_state_str(zstate));
1612 rval = -1;
1613 break;
1614 }
1615 break;
1616 default:
1617 abort();
1618 }
1619
1620 /*
1621 * Because the state of the zone may have changed, we make sure
1622 * to wake the console poller, which is in charge of initiating
1623 * the shutdown procedure as necessary.
1624 */
1625 eventstream_write(Z_EVT_NULL);
1626
1627 out:
|