1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  * Copyright 2015 Joyent, Inc.
  26  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
  27  */
  28 
  29 /*
  30  * Console support for zones requires a significant infrastructure.  The
  31  * core pieces are contained in this file, but other portions of note
  32  * are in the zlogin(1M) command, the zcons(7D) driver, and in the
  33  * devfsadm(1M) misc_link generator.
  34  *
  35  * Care is taken to make the console behave in an "intuitive" fashion for
  36  * administrators.  Essentially, we try as much as possible to mimic the
  37  * experience of using a system via a tip line and system controller.
  38  *
  39  * The zone console architecture looks like this:
  40  *
  41  *                                      Global Zone | Non-Global Zone
  42  *                        .--------------.          |
  43  *        .-----------.   | zoneadmd -z  |          | .--------. .---------.
  44  *        | zlogin -C |   |     myzone   |          | | ttymon | | syslogd |
  45  *        `-----------'   `--------------'          | `--------' `---------'
  46  *                  |       |       | |             |      |       |
  47  *  User            |       |       | |             |      V       V
  48  * - - - - - - - - -|- - - -|- - - -|-|- - - - - - -|- - /dev/zconsole - - -
  49  *  Kernel          V       V       | |                        |
  50  *               [AF_UNIX Socket]   | `--------. .-------------'
  51  *                                  |          | |
  52  *                                  |          V V
  53  *                                  |     +-----------+
  54  *                                  |     |  ldterm,  |
  55  *                                  |     |   etc.    |
  56  *                                  |     +-----------+
  57  *                                  |     +-[Anchor]--+
  58  *                                  |     |   ptem    |
  59  *                                  V     +-----------+
  60  *                           +---master---+---slave---+
  61  *                           |                        |
  62  *                           |      zcons driver      |
  63  *                           |    zonename="myzone"   |
  64  *                           +------------------------+
  65  *
  66  * There are basically two major tasks which the console subsystem in
  67  * zoneadmd accomplishes:
  68  *
  69  * - Setup and teardown of zcons driver instances.  One zcons instance
  70  *   is maintained per zone; we take advantage of the libdevice APIs
  71  *   to online new instances of zcons as needed.  Care is taken to
  72  *   prune and manage these appropriately; see init_console_dev() and
  73  *   destroy_console_dev().  The end result is the creation of the
  74  *   zcons(7D) instance and an open file descriptor to the master side.
  75  *   zcons instances are associated with zones via their zonename device
  76  *   property.  This the console instance to persist across reboots,
  77  *   and while the zone is halted.
  78  *
  79  * - Acting as a server for 'zlogin -C' instances.  When zlogin -C is
  80  *   run, zlogin connects to zoneadmd via unix domain socket.  zoneadmd
  81  *   functions as a two-way proxy for console I/O, relaying user input
  82  *   to the master side of the console, and relaying output from the
  83  *   zone to the user.
  84  */
  85 
  86 #include <sys/types.h>
  87 #include <sys/socket.h>
  88 #include <sys/stat.h>
  89 #include <sys/termios.h>
  90 #include <sys/zcons.h>
  91 #include <sys/mkdev.h>
  92 
  93 #include <assert.h>
  94 #include <ctype.h>
  95 #include <errno.h>
  96 #include <fcntl.h>
  97 #include <stdarg.h>
  98 #include <stdio.h>
  99 #include <stdlib.h>
 100 #include <strings.h>
 101 #include <stropts.h>
 102 #include <thread.h>
 103 #include <ucred.h>
 104 #include <unistd.h>
 105 #include <zone.h>
 106 
 107 #include <libdevinfo.h>
 108 #include <libdevice.h>
 109 #include <libzonecfg.h>
 110 
 111 #include <syslog.h>
 112 #include <sys/modctl.h>
 113 
 114 #include "zoneadmd.h"
 115 
 116 #define ZCONSNEX_DEVTREEPATH    "/pseudo/zconsnex@1"
 117 #define ZCONSNEX_FILEPATH       "/devices/pseudo/zconsnex@1"
 118 
 119 #define CONSOLE_SOCKPATH        ZONES_TMPDIR "/%s.console_sock"
 120 
 121 #define ZCONS_RETRY             10
 122 
 123 static int      serverfd = -1;  /* console server unix domain socket fd */
 124 char boot_args[BOOTARGS_MAX];
 125 
 126 /*
 127  * The eventstream is a simple one-directional flow of messages from the
 128  * door server to the console subsystem, implemented with a pipe.
 129  * It is used to wake up the console poller when it needs to take action,
 130  * message the user, die off, etc.
 131  */
 132 static int eventstream[2];
 133 
 134 /* flag used to cope with race creating master zcons devlink */
 135 static boolean_t master_zcons_failed = B_FALSE;
 136 /* flag to track if we've seen a state change when there is no master zcons */
 137 static boolean_t state_changed = B_FALSE;
 138 
 139 int
 140 eventstream_init()
 141 {
 142         if (pipe(eventstream) == -1)
 143                 return (-1);
 144         return (0);
 145 }
 146 
 147 void
 148 eventstream_write(zone_evt_t evt)
 149 {
 150         (void) write(eventstream[0], &evt, sizeof (evt));
 151 }
 152 
 153 static zone_evt_t
 154 eventstream_read(void)
 155 {
 156         zone_evt_t evt = Z_EVT_NULL;
 157 
 158         (void) read(eventstream[1], &evt, sizeof (evt));
 159         return (evt);
 160 }
 161 
 162 /*
 163  * count_console_devs() and its helper count_cb() do a walk of the
 164  * subtree of the device tree where zone console nodes are represented.
 165  * The goal is to count zone console instances already setup for a zone
 166  * with the given name.  More than 1 is anomolous, and our caller will
 167  * have to deal with that if we find that's the case.
 168  *
 169  * Note: this algorithm is a linear search of nodes in the zconsnex subtree
 170  * of the device tree, and could be a scalability problem, but I don't see
 171  * how to avoid it.
 172  */
 173 
 174 /*
 175  * cb_data is shared by count_cb and destroy_cb for simplicity.
 176  */
 177 struct cb_data {
 178         zlog_t *zlogp;
 179         int found;
 180         int killed;
 181 };
 182 
 183 static int
 184 count_cb(di_node_t node, void *arg)
 185 {
 186         struct cb_data *cb = (struct cb_data *)arg;
 187         char *prop_data;
 188 
 189         if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename",
 190             &prop_data) != -1) {
 191                 assert(prop_data != NULL);
 192                 if (strcmp(prop_data, zone_name) == 0) {
 193                         cb->found++;
 194                         return (DI_WALK_CONTINUE);
 195                 }
 196         }
 197         return (DI_WALK_CONTINUE);
 198 }
 199 
 200 static int
 201 count_console_devs(zlog_t *zlogp)
 202 {
 203         di_node_t root;
 204         struct cb_data cb;
 205 
 206         bzero(&cb, sizeof (cb));
 207         cb.zlogp = zlogp;
 208 
 209         if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) ==
 210             DI_NODE_NIL) {
 211                 zerror(zlogp, B_TRUE, "%s failed", "di_init");
 212                 return (-1);
 213         }
 214 
 215         (void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, count_cb);
 216         di_fini(root);
 217         return (cb.found);
 218 }
 219 
 220 /*
 221  * destroy_console_devs() and its helper destroy_cb() tears down any console
 222  * instances associated with this zone.  If things went very wrong, we
 223  * might have more than one console instance hanging around.  This routine
 224  * hunts down and tries to remove all of them.  Of course, if the console
 225  * is open, the instance will not detach, which is a potential issue.
 226  */
 227 static int
 228 destroy_cb(di_node_t node, void *arg)
 229 {
 230         struct cb_data *cb = (struct cb_data *)arg;
 231         char *prop_data;
 232         char *tmp;
 233         char devpath[MAXPATHLEN];
 234         devctl_hdl_t hdl;
 235 
 236         if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename",
 237             &prop_data) == -1)
 238                 return (DI_WALK_CONTINUE);
 239 
 240         assert(prop_data != NULL);
 241         if (strcmp(prop_data, zone_name) != 0) {
 242                 /* this is the console for a different zone */
 243                 return (DI_WALK_CONTINUE);
 244         }
 245 
 246         cb->found++;
 247         tmp = di_devfs_path(node);
 248         (void) snprintf(devpath, sizeof (devpath), "/devices/%s", tmp);
 249         di_devfs_path_free(tmp);
 250 
 251         if ((hdl = devctl_device_acquire(devpath, 0)) == NULL) {
 252                 zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, "
 253                     "but it could not be controlled.", devpath);
 254                 return (DI_WALK_CONTINUE);
 255         }
 256         if (devctl_device_remove(hdl) == 0) {
 257                 cb->killed++;
 258         } else {
 259                 zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, "
 260                     "but it could not be removed.", devpath);
 261         }
 262         devctl_release(hdl);
 263         return (DI_WALK_CONTINUE);
 264 }
 265 
 266 static int
 267 destroy_console_devs(zlog_t *zlogp)
 268 {
 269         char conspath[MAXPATHLEN];
 270         di_node_t root;
 271         struct cb_data cb;
 272         int masterfd;
 273         int slavefd;
 274 
 275         /*
 276          * Signal the master side to release its handle on the slave side by
 277          * issuing a ZC_RELEASESLAVE ioctl.
 278          */
 279         (void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
 280             zone_name, ZCONS_MASTER_NAME);
 281         if ((masterfd = open(conspath, O_RDWR | O_NOCTTY)) != -1) {
 282                 (void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
 283                     zone_name, ZCONS_SLAVE_NAME);
 284                 if ((slavefd = open(conspath, O_RDWR | O_NOCTTY)) != -1) {
 285                         if (ioctl(masterfd, ZC_RELEASESLAVE,
 286                             (caddr_t)(intptr_t)slavefd) != 0)
 287                                 zerror(zlogp, B_TRUE, "WARNING: error while "
 288                                     "releasing slave handle of zone console for"
 289                                     " %s", zone_name);
 290                         (void) close(slavefd);
 291                 } else {
 292                         zerror(zlogp, B_TRUE, "WARNING: could not open slave "
 293                             "side of zone console for %s to release slave "
 294                             "handle", zone_name);
 295                 }
 296                 (void) close(masterfd);
 297         } else {
 298                 zerror(zlogp, B_TRUE, "WARNING: could not open master side of "
 299                     "zone console for %s to release slave handle", zone_name);
 300         }
 301 
 302         bzero(&cb, sizeof (cb));
 303         cb.zlogp = zlogp;
 304 
 305         if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) ==
 306             DI_NODE_NIL) {
 307                 zerror(zlogp, B_TRUE, "%s failed", "di_init");
 308                 return (-1);
 309         }
 310 
 311         (void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, destroy_cb);
 312         if (cb.found > 1) {
 313                 zerror(zlogp, B_FALSE, "WARNING: multiple zone console "
 314                     "instances detected for zone '%s'; %d of %d "
 315                     "successfully removed.",
 316                     zone_name, cb.killed, cb.found);
 317         }
 318 
 319         di_fini(root);
 320         return (0);
 321 }
 322 
 323 /*
 324  * init_console_dev() drives the device-tree configuration of the zone
 325  * console device.  The general strategy is to use the libdevice (devctl)
 326  * interfaces to instantiate a new zone console node.  We do a lot of
 327  * sanity checking, and are careful to reuse a console if one exists.
 328  *
 329  * Once the device is in the device tree, we kick devfsadm via di_init_devs()
 330  * to ensure that the appropriate symlinks (to the master and slave console
 331  * devices) are placed in /dev in the global zone.
 332  */
 333 static int
 334 init_console_dev(zlog_t *zlogp)
 335 {
 336         char conspath[MAXPATHLEN];
 337         devctl_hdl_t bus_hdl = NULL;
 338         devctl_hdl_t dev_hdl = NULL;
 339         devctl_ddef_t ddef_hdl = NULL;
 340         di_devlink_handle_t dl = NULL;
 341         int rv = -1;
 342         int ndevs;
 343         int masterfd;
 344         int slavefd;
 345         int i;
 346 
 347         /*
 348          * Don't re-setup console if it is working and ready already; just
 349          * skip ahead to making devlinks, which we do for sanity's sake.
 350          */
 351         ndevs = count_console_devs(zlogp);
 352         if (ndevs == 1) {
 353                 goto devlinks;
 354         } else if (ndevs > 1 || ndevs == -1) {
 355                 /*
 356                  * For now, this seems like a reasonable but harsh punishment.
 357                  * If needed, we could try to get clever and delete all but
 358                  * the console which is pointed at by the current symlink.
 359                  */
 360                 if (destroy_console_devs(zlogp) == -1) {
 361                         goto error;
 362                 }
 363         }
 364 
 365         /*
 366          * Time to make the consoles!
 367          */
 368         if ((bus_hdl = devctl_bus_acquire(ZCONSNEX_FILEPATH, 0)) == NULL) {
 369                 zerror(zlogp, B_TRUE, "%s failed", "devctl_bus_acquire");
 370                 goto error;
 371         }
 372         if ((ddef_hdl = devctl_ddef_alloc("zcons", 0)) == NULL) {
 373                 zerror(zlogp, B_TRUE, "failed to allocate ddef handle");
 374                 goto error;
 375         }
 376         /*
 377          * Set three properties on this node; the first is the name of the
 378          * zone; the second is a flag which lets pseudo know that it is
 379          * OK to automatically allocate an instance # for this device;
 380          * the third tells the device framework not to auto-detach this
 381          * node-- we need the node to still be there when we ask devfsadmd
 382          * to make links, and when we need to open it.
 383          */
 384         if (devctl_ddef_string(ddef_hdl, "zonename", zone_name) == -1) {
 385                 zerror(zlogp, B_TRUE, "failed to create zonename property");
 386                 goto error;
 387         }
 388         if (devctl_ddef_int(ddef_hdl, "auto-assign-instance", 1) == -1) {
 389                 zerror(zlogp, B_TRUE, "failed to create auto-assign-instance "
 390                     "property");
 391                 goto error;
 392         }
 393         if (devctl_ddef_int(ddef_hdl, "ddi-no-autodetach", 1) == -1) {
 394                 zerror(zlogp, B_TRUE, "failed to create ddi-no-auto-detach "
 395                     "property");
 396                 goto error;
 397         }
 398         if (devctl_bus_dev_create(bus_hdl, ddef_hdl, 0, &dev_hdl) == -1) {
 399                 zerror(zlogp, B_TRUE, "failed to create console node");
 400                 goto error;
 401         }
 402 
 403 devlinks:
 404         if ((dl = di_devlink_init("zcons", DI_MAKE_LINK)) != NULL) {
 405                 (void) di_devlink_fini(&dl);
 406         } else {
 407                 zerror(zlogp, B_TRUE, "failed to create devlinks");
 408                 goto error;
 409         }
 410 
 411         /*
 412          * Open the master side of the console and issue the ZC_HOLDSLAVE ioctl,
 413          * which will cause the master to retain a reference to the slave.
 414          * This prevents ttymon from blowing through the slave's STREAMS anchor.
 415          *
 416          * In very rare cases the open returns ENOENT if devfs doesn't have
 417          * everything setup yet due to heavy zone startup load. Wait for
 418          * 1 sec. and retry a few times. Even if we can't setup the zone's
 419          * console, we still go ahead and boot the zone.
 420          */
 421         (void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
 422             zone_name, ZCONS_MASTER_NAME);
 423         if ((masterfd = open(conspath, O_RDWR | O_NOCTTY)) == -1) {
 424                 zerror(zlogp, B_TRUE, "ERROR: could not open master side of "
 425                     "zone console for %s to acquire slave handle", zone_name);
 426                 master_zcons_failed = B_TRUE;
 427         }
 428         (void) snprintf(conspath, sizeof (conspath), "/dev/zcons/%s/%s",
 429             zone_name, ZCONS_SLAVE_NAME);
 430         for (i = 0; i < ZCONS_RETRY; i++) {
 431                 slavefd = open(conspath, O_RDWR | O_NOCTTY);
 432                 if (slavefd >= 0 || errno != ENOENT)
 433                         break;
 434                 (void) sleep(1);
 435         }
 436         if (slavefd == -1)
 437                 zerror(zlogp, B_TRUE, "ERROR: could not open slave side of zone"
 438                     " console for %s to acquire slave handle", zone_name);
 439 
 440         /*
 441          * This ioctl can occasionally return ENXIO if devfs doesn't have
 442          * everything plumbed up yet due to heavy zone startup load. Wait for
 443          * 1 sec. and retry a few times before we fail to boot the zone.
 444          */
 445         if (masterfd != -1 && slavefd != -1) {
 446                 for (i = 0; i < ZCONS_RETRY; i++) {
 447                         if (ioctl(masterfd, ZC_HOLDSLAVE,
 448                             (caddr_t)(intptr_t)slavefd) == 0) {
 449                                 rv = 0;
 450                                 break;
 451                         } else if (errno != ENXIO) {
 452                                 break;
 453                         }
 454                         (void) sleep(1);
 455                 }
 456                 if (rv != 0)
 457                         zerror(zlogp, B_TRUE, "ERROR: error while acquiring "
 458                             "slave handle of zone console for %s", zone_name);
 459         }
 460 
 461         if (slavefd != -1)
 462                 (void) close(slavefd);
 463         if (masterfd != -1)
 464                 (void) close(masterfd);
 465 
 466 error:
 467         if (ddef_hdl)
 468                 devctl_ddef_free(ddef_hdl);
 469         if (bus_hdl)
 470                 devctl_release(bus_hdl);
 471         if (dev_hdl)
 472                 devctl_release(dev_hdl);
 473         return (rv);
 474 }
 475 
 476 static int
 477 init_console_sock(zlog_t *zlogp)
 478 {
 479         int servfd;
 480         struct sockaddr_un servaddr;
 481 
 482         bzero(&servaddr, sizeof (servaddr));
 483         servaddr.sun_family = AF_UNIX;
 484         (void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path),
 485             CONSOLE_SOCKPATH, zone_name);
 486 
 487         if ((servfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
 488                 zerror(zlogp, B_TRUE, "console setup: could not create socket");
 489                 return (-1);
 490         }
 491         (void) unlink(servaddr.sun_path);
 492 
 493         if (bind(servfd, (struct sockaddr *)&servaddr,
 494             sizeof (servaddr)) == -1) {
 495                 zerror(zlogp, B_TRUE,
 496                     "console setup: could not bind to socket");
 497                 goto out;
 498         }
 499 
 500         if (listen(servfd, 4) == -1) {
 501                 zerror(zlogp, B_TRUE,
 502                     "console setup: could not listen on socket");
 503                 goto out;
 504         }
 505         return (servfd);
 506 
 507 out:
 508         (void) unlink(servaddr.sun_path);
 509         (void) close(servfd);
 510         return (-1);
 511 }
 512 
 513 static void
 514 destroy_console_sock(int servfd)
 515 {
 516         char path[MAXPATHLEN];
 517 
 518         (void) snprintf(path, sizeof (path), CONSOLE_SOCKPATH, zone_name);
 519         (void) unlink(path);
 520         (void) shutdown(servfd, SHUT_RDWR);
 521         (void) close(servfd);
 522 }
 523 
 524 /*
 525  * Read the "ident" string from the client's descriptor; this routine also
 526  * tolerates being called with pid=NULL, for times when you want to "eat"
 527  * the ident string from a client without saving it.
 528  */
 529 static int
 530 get_client_ident(int clifd, pid_t *pid, char *locale, size_t locale_len,
 531     int *disconnect)
 532 {
 533         char buf[BUFSIZ], *bufp;
 534         size_t buflen = sizeof (buf);
 535         char c = '\0';
 536         int i = 0, r;
 537         ucred_t *cred = NULL;
 538 
 539         /* "eat up the ident string" case, for simplicity */
 540         if (pid == NULL) {
 541                 assert(locale == NULL && locale_len == 0);
 542                 while (read(clifd, &c, 1) == 1) {
 543                         if (c == '\n')
 544                                 return (0);
 545                 }
 546         }
 547 
 548         bzero(buf, sizeof (buf));
 549         while ((buflen > 1) && (r = read(clifd, &c, 1)) == 1) {
 550                 buflen--;
 551                 if (c == '\n')
 552                         break;
 553 
 554                 buf[i] = c;
 555                 i++;
 556         }
 557         if (r == -1)
 558                 return (-1);
 559 
 560         /*
 561          * We've filled the buffer, but still haven't seen \n.  Keep eating
 562          * until we find it; we don't expect this to happen, but this is
 563          * defensive.
 564          */
 565         if (c != '\n') {
 566                 while ((r = read(clifd, &c, sizeof (c))) > 0)
 567                         if (c == '\n')
 568                                 break;
 569         }
 570 
 571         if (getpeerucred(clifd, &cred) == 0) {
 572                 *pid = ucred_getpid((const ucred_t *)cred);
 573                 ucred_free(cred);
 574         } else {
 575                 return (-1);
 576         }
 577 
 578         /*
 579          * Parse buffer for message of the form:
 580          * IDENT <locale> <disconnect flag>
 581          */
 582         bufp = buf;
 583         if (strncmp(bufp, "IDENT ", 6) != 0)
 584                 return (-1);
 585         bufp += 6;
 586         errno = 0;
 587 
 588         while (*bufp != '\0' && isspace(*bufp))
 589                 bufp++;
 590         buflen = strlen(bufp) - 1;
 591         *disconnect = atoi(&bufp[buflen]);
 592         bufp[buflen - 1] = '\0';
 593         (void) strlcpy(locale, bufp, locale_len);
 594 
 595         return (0);
 596 }
 597 
 598 static int
 599 accept_client(int servfd, pid_t *pid, char *locale, size_t locale_len,
 600     int *disconnect)
 601 {
 602         int connfd;
 603         struct sockaddr_un cliaddr;
 604         socklen_t clilen;
 605 
 606         clilen = sizeof (cliaddr);
 607         connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
 608         if (connfd == -1)
 609                 return (-1);
 610         if (get_client_ident(connfd, pid, locale, locale_len,
 611             disconnect) == -1) {
 612                 (void) shutdown(connfd, SHUT_RDWR);
 613                 (void) close(connfd);
 614                 return (-1);
 615         }
 616         (void) write(connfd, "OK\n", 3);
 617         return (connfd);
 618 }
 619 
 620 static void
 621 reject_client(int servfd, pid_t clientpid)
 622 {
 623         int connfd;
 624         struct sockaddr_un cliaddr;
 625         socklen_t clilen;
 626         char nak[MAXPATHLEN];
 627 
 628         clilen = sizeof (cliaddr);
 629         connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
 630 
 631         /*
 632          * After hear its ident string, tell client to get lost.
 633          */
 634         if (get_client_ident(connfd, NULL, NULL, 0, NULL) == 0) {
 635                 (void) snprintf(nak, sizeof (nak), "%lu\n",
 636                     clientpid);
 637                 (void) write(connfd, nak, strlen(nak));
 638         }
 639         (void) shutdown(connfd, SHUT_RDWR);
 640         (void) close(connfd);
 641 }
 642 
 643 static void
 644 event_message(int clifd, char *clilocale, zone_evt_t evt, int dflag)
 645 {
 646         char *str, *lstr = NULL;
 647         char lmsg[BUFSIZ];
 648         char outbuf[BUFSIZ];
 649 
 650         if (clifd == -1)
 651                 return;
 652 
 653         switch (evt) {
 654         case Z_EVT_ZONE_BOOTING:
 655                 if (*boot_args == '\0') {
 656                         str = "NOTICE: Zone booting up";
 657                         break;
 658                 }
 659                 /*LINTED*/
 660                 (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
 661                     "NOTICE: Zone booting up with arguments: %s"), boot_args);
 662                 lstr = lmsg;
 663                 break;
 664         case Z_EVT_ZONE_READIED:
 665                 str = "NOTICE: Zone readied";
 666                 break;
 667         case Z_EVT_ZONE_HALTED:
 668                 if (dflag)
 669                         str = "NOTICE: Zone halted.  Disconnecting...";
 670                 else
 671                         str = "NOTICE: Zone halted";
 672                 break;
 673         case Z_EVT_ZONE_REBOOTING:
 674                 if (*boot_args == '\0') {
 675                         str = "NOTICE: Zone rebooting";
 676                         break;
 677                 }
 678                 /*LINTED*/
 679                 (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale,
 680                     "NOTICE: Zone rebooting with arguments: %s"), boot_args);
 681                 lstr = lmsg;
 682                 break;
 683         case Z_EVT_ZONE_UNINSTALLING:
 684                 str = "NOTICE: Zone is being uninstalled.  Disconnecting...";
 685                 break;
 686         case Z_EVT_ZONE_BOOTFAILED:
 687                 if (dflag)
 688                         str = "NOTICE: Zone boot failed.  Disconnecting...";
 689                 else
 690                         str = "NOTICE: Zone boot failed";
 691                 break;
 692         default:
 693                 return;
 694         }
 695 
 696         if (lstr == NULL)
 697                 lstr = localize_msg(clilocale, str);
 698         (void) snprintf(outbuf, sizeof (outbuf), "\r\n[%s]\r\n", lstr);
 699         (void) write(clifd, outbuf, strlen(outbuf));
 700 }
 701 
 702 /*
 703  * Check to see if the client at the other end of the socket is still
 704  * alive; we know it is not if it throws EPIPE at us when we try to write
 705  * an otherwise harmless 0-length message to it.
 706  */
 707 static int
 708 test_client(int clifd)
 709 {
 710         if ((write(clifd, "", 0) == -1) && errno == EPIPE)
 711                 return (-1);
 712         return (0);
 713 }
 714 
 715 /*
 716  * This routine drives the console I/O loop.  It polls for input from the
 717  * master side of the console (output to the console), and from the client
 718  * (input from the console user).  Additionally, it polls on the server fd,
 719  * and disconnects any clients that might try to hook up with the zone while
 720  * the console is in use.
 721  *
 722  * When the client first calls us up, it is expected to send a line giving
 723  * its "identity"; this consists of the string 'IDENT <pid> <locale>'.
 724  * This is so that we can report that the console is busy along with
 725  * some diagnostics about who has it busy; the locale is used so that
 726  * asynchronous messages about zone state (like the NOTICE: zone halted
 727  * messages) can be output in the user's locale.
 728  */
 729 static void
 730 do_console_io(zlog_t *zlogp, int consfd, int servfd)
 731 {
 732         struct pollfd pollfds[4];
 733         char ibuf[BUFSIZ];
 734         int cc, ret;
 735         int clifd = -1;
 736         int pollerr = 0;
 737         char clilocale[MAXPATHLEN];
 738         pid_t clipid = 0;
 739         int disconnect = 0;
 740 
 741         /* console side, watch for read events */
 742         pollfds[0].fd = consfd;
 743         pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND |
 744             POLLPRI | POLLERR | POLLHUP | POLLNVAL;
 745 
 746         /* client side, watch for read events */
 747         pollfds[1].fd = clifd;
 748         pollfds[1].events = pollfds[0].events;
 749 
 750         /* the server socket; watch for events (new connections) */
 751         pollfds[2].fd = servfd;
 752         pollfds[2].events = pollfds[0].events;
 753 
 754         /* the eventstram; watch for events (e.g.: zone halted) */
 755         pollfds[3].fd = eventstream[1];
 756         pollfds[3].events = pollfds[0].events;
 757 
 758         for (;;) {
 759                 pollfds[0].revents = pollfds[1].revents = 0;
 760                 pollfds[2].revents = pollfds[3].revents = 0;
 761 
 762                 ret = poll(pollfds,
 763                     sizeof (pollfds) / sizeof (struct pollfd), -1);
 764                 if (ret == -1 && errno != EINTR) {
 765                         zerror(zlogp, B_TRUE, "poll failed");
 766                         /* we are hosed, close connection */
 767                         break;
 768                 }
 769 
 770                 /* event from console side */
 771                 if (pollfds[0].revents) {
 772                         if (pollfds[0].revents &
 773                             (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
 774                                 errno = 0;
 775                                 cc = read(consfd, ibuf, BUFSIZ);
 776                                 if (cc <= 0 && (errno != EINTR) &&
 777                                     (errno != EAGAIN))
 778                                         break;
 779                                 /*
 780                                  * Lose I/O if no one is listening
 781                                  */
 782                                 if (clifd != -1 && cc > 0)
 783                                         (void) write(clifd, ibuf, cc);
 784                         } else {
 785                                 pollerr = pollfds[0].revents;
 786                                 zerror(zlogp, B_FALSE,
 787                                     "closing connection with (console) "
 788                                     "pollerr %d\n", pollerr);
 789                                 break;
 790                         }
 791                 }
 792 
 793                 /* event from client side */
 794                 if (pollfds[1].revents) {
 795                         if (pollfds[1].revents &
 796                             (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
 797                                 errno = 0;
 798                                 cc = read(clifd, ibuf, BUFSIZ);
 799                                 if (cc <= 0 && (errno != EINTR) &&
 800                                     (errno != EAGAIN))
 801                                         break;
 802                                 (void) write(consfd, ibuf, cc);
 803                         } else {
 804                                 pollerr = pollfds[1].revents;
 805                                 zerror(zlogp, B_FALSE,
 806                                     "closing connection with (client) "
 807                                     "pollerr %d\n", pollerr);
 808                                 break;
 809                         }
 810                 }
 811 
 812                 /* event from server socket */
 813                 if (pollfds[2].revents &&
 814                     (pollfds[2].revents & (POLLIN | POLLRDNORM))) {
 815                         if (clifd != -1) {
 816                                 /*
 817                                  * Test the client to see if it is really
 818                                  * still alive.  If it has died but we
 819                                  * haven't yet detected that, we might
 820                                  * deny a legitimate connect attempt.  If it
 821                                  * is dead, we break out; once we tear down
 822                                  * the old connection, the new connection
 823                                  * will happen.
 824                                  */
 825                                 if (test_client(clifd) == -1) {
 826                                         break;
 827                                 }
 828                                 /* we're already handling a client */
 829                                 reject_client(servfd, clipid);
 830 
 831 
 832                         } else if ((clifd = accept_client(servfd, &clipid,
 833                             clilocale, sizeof (clilocale),
 834                             &disconnect)) != -1) {
 835                                 pollfds[1].fd = clifd;
 836 
 837                         } else {
 838                                 break;
 839                         }
 840                 }
 841 
 842                 /*
 843                  * Watch for events on the eventstream.  This is how we get
 844                  * notified of the zone halting, etc.  It provides us a
 845                  * "wakeup" from poll when important things happen, which
 846                  * is good.
 847                  */
 848                 if (pollfds[3].revents) {
 849                         int evt = eventstream_read();
 850                         /*
 851                          * After we drain out the event, if we aren't servicing
 852                          * a console client, we hop back out to our caller,
 853                          * which will check to see if it is time to shutdown
 854                          * the daemon, or if we should take another console
 855                          * service lap.
 856                          */
 857                         if (clifd == -1) {
 858                                 break;
 859                         }
 860                         event_message(clifd, clilocale, evt, disconnect);
 861                         /*
 862                          * Special handling for the message that the zone is
 863                          * uninstalling; we boot the client, then break out
 864                          * of this function.  When we return to the
 865                          * serve_console loop, we will see that the zone is
 866                          * in a state < READY, and so zoneadmd will shutdown.
 867                          */
 868                         if (evt == Z_EVT_ZONE_UNINSTALLING) {
 869                                 break;
 870                         }
 871                         /*
 872                          * Diconnect if -C and -d options were specified and
 873                          * zone was halted or failed to boot.
 874                          */
 875                         if ((evt == Z_EVT_ZONE_HALTED ||
 876                             evt == Z_EVT_ZONE_BOOTFAILED) && disconnect) {
 877                                 break;
 878                         }
 879                 }
 880 
 881         }
 882 
 883         if (clifd != -1) {
 884                 (void) shutdown(clifd, SHUT_RDWR);
 885                 (void) close(clifd);
 886         }
 887 }
 888 
 889 int
 890 init_console(zlog_t *zlogp)
 891 {
 892         if (init_console_dev(zlogp) == -1) {
 893                 zerror(zlogp, B_FALSE,
 894                     "console setup: device initialization failed");
 895         }
 896 
 897         if ((serverfd = init_console_sock(zlogp)) == -1) {
 898                 zerror(zlogp, B_FALSE,
 899                     "console setup: socket initialization failed");
 900                 return (-1);
 901         }
 902         return (0);
 903 }
 904 
 905 /*
 906  * Maintain a simple flag that tracks if we have seen at least one state
 907  * change. This is currently only used to handle the special case where we are
 908  * running without a console device, which is what normally drives shutdown.
 909  */
 910 void
 911 zcons_statechanged()
 912 {
 913         state_changed = B_TRUE;
 914 }
 915 
 916 /*
 917  * serve_console() is the master loop for driving console I/O.  It is also the
 918  * routine which is ultimately responsible for "pulling the plug" on zoneadmd
 919  * when it realizes that the daemon should shut down.
 920  *
 921  * The rules for shutdown are: there must be no console client, and the zone
 922  * state must be < ready.  However, we need to give things a chance to actually
 923  * get going when the daemon starts up-- otherwise the daemon would immediately
 924  * exit on startup if the zone was in the installed state, so we first drop
 925  * into the do_console_io() loop in order to give *something* a chance to
 926  * happen.
 927  */
 928 void
 929 serve_console(zlog_t *zlogp)
 930 {
 931         int masterfd;
 932         zone_state_t zstate;
 933         char conspath[MAXPATHLEN];
 934         static boolean_t cons_warned = B_FALSE;
 935 
 936         (void) snprintf(conspath, sizeof (conspath),
 937             "/dev/zcons/%s/%s", zone_name, ZCONS_MASTER_NAME);
 938 
 939         for (;;) {
 940                 masterfd = open(conspath, O_RDWR|O_NONBLOCK|O_NOCTTY);
 941                 if (masterfd == -1) {
 942                         if (master_zcons_failed) {
 943                                 /*
 944                                  * If we don't have a console and the zone is
 945                                  * not shutting down, there may have been a
 946                                  * race/failure with devfs while creating the
 947                                  * console. In this case we want to leave the
 948                                  * zone up, even without a console, so
 949                                  * periodically recheck.
 950                                  */
 951                                 int i;
 952 
 953                                 /*
 954                                  * In the normal flow of this loop, we use
 955                                  * do_console_io to give things a chance to get
 956                                  * going first. However, in this case we can't
 957                                  * use that, so we have to wait for at least
 958                                  * one state change before checking the state.
 959                                  */
 960                                 for (i = 0; i < 60; i++) {
 961                                         if (state_changed)
 962                                                 break;
 963                                         (void) sleep(1);
 964                                 }
 965 
 966                                 if (i < 60 && zone_get_state(zone_name,
 967                                     &zstate) == Z_OK &&
 968                                     (zstate == ZONE_STATE_READY ||
 969                                     zstate == ZONE_STATE_RUNNING)) {
 970                                         if (!cons_warned) {
 971                                                 zerror(zlogp, B_FALSE,
 972                                                     "WARNING: missing zone "
 973                                                     "console for %s",
 974                                                     zone_name);
 975                                                 cons_warned = B_TRUE;
 976                                         }
 977                                         (void) sleep(ZCONS_RETRY);
 978                                         continue;
 979                                 }
 980                         }
 981 
 982                         zerror(zlogp, B_TRUE, "failed to open console master");
 983                         (void) mutex_lock(&lock);
 984                         goto death;
 985                 }
 986 
 987                 /*
 988                  * Setting RPROTDIS on the stream means that the control
 989                  * portion of messages received (which we don't care about)
 990                  * will be discarded by the stream head.  If we allowed such
 991                  * messages, we wouldn't be able to use read(2), as it fails
 992                  * (EBADMSG) when a message with a control element is received.
 993                  */
 994                 if (ioctl(masterfd, I_SRDOPT, RNORM|RPROTDIS) == -1) {
 995                         zerror(zlogp, B_TRUE, "failed to set options on "
 996                             "console master");
 997                         (void) mutex_lock(&lock);
 998                         goto death;
 999                 }
1000 
1001                 do_console_io(zlogp, masterfd, serverfd);
1002 
1003                 /*
1004                  * We would prefer not to do this, but hostile zone processes
1005                  * can cause the stream to become tainted, and reads will
1006                  * fail.  So, in case something has gone seriously ill,
1007                  * we dismantle the stream and reopen the console when we
1008                  * take another lap.
1009                  */
1010                 (void) close(masterfd);
1011 
1012                 (void) mutex_lock(&lock);
1013                 /*
1014                  * We need to set death_throes (see below) atomically with
1015                  * respect to noticing that (a) we have no console client and
1016                  * (b) the zone is not installed.  Otherwise we could get a
1017                  * request to boot during this time.  Once we set death_throes,
1018                  * any incoming door stuff will be turned away.
1019                  */
1020                 if (zone_get_state(zone_name, &zstate) == Z_OK) {
1021                         if (zstate < ZONE_STATE_READY)
1022                                 goto death;
1023                 } else {
1024                         zerror(zlogp, B_FALSE,
1025                             "unable to determine state of zone");
1026                         goto death;
1027                 }
1028                 /*
1029                  * Even if zone_get_state() fails, stay conservative, and
1030                  * take another lap.
1031                  */
1032                 (void) mutex_unlock(&lock);
1033         }
1034 
1035 death:
1036         assert(MUTEX_HELD(&lock));
1037         in_death_throes = B_TRUE;
1038         (void) mutex_unlock(&lock);
1039 
1040         destroy_console_sock(serverfd);
1041         (void) destroy_console_devs(zlogp);
1042 }