17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ffbafc53Scomay * Common Development and Distribution License (the "License"). 6ffbafc53Scomay * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21ffbafc53Scomay 227c478bd9Sstevel@tonic-gate /* 23ffbafc53Scomay * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Console support for zones requires a significant infrastructure. The 317c478bd9Sstevel@tonic-gate * core pieces are contained in this file, but other portions of note 327c478bd9Sstevel@tonic-gate * are in the zlogin(1M) command, the zcons(7D) driver, and in the 337c478bd9Sstevel@tonic-gate * devfsadm(1M) misc_link generator. 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * Care is taken to make the console behave in an "intuitive" fashion for 367c478bd9Sstevel@tonic-gate * administrators. Essentially, we try as much as possible to mimic the 377c478bd9Sstevel@tonic-gate * experience of using a system via a tip line and system controller. 387c478bd9Sstevel@tonic-gate * 397c478bd9Sstevel@tonic-gate * The zone console architecture looks like this: 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * Global Zone | Non-Global Zone 427c478bd9Sstevel@tonic-gate * .--------------. | 437c478bd9Sstevel@tonic-gate * .-----------. | zoneadmd -z | | .--------. .---------. 447c478bd9Sstevel@tonic-gate * | zlogin -C | | myzone | | | ttymon | | syslogd | 457c478bd9Sstevel@tonic-gate * `-----------' `--------------' | `--------' `---------' 467c478bd9Sstevel@tonic-gate * | | | | | | | 477c478bd9Sstevel@tonic-gate * User | | | | | V V 487c478bd9Sstevel@tonic-gate * - - - - - - - - -|- - - -|- - - -|-|- - - - - - -|- - /dev/zconsole - - - 497c478bd9Sstevel@tonic-gate * Kernel V V | | | 507c478bd9Sstevel@tonic-gate * [AF_UNIX Socket] | `--------. .-------------' 517c478bd9Sstevel@tonic-gate * | | | 527c478bd9Sstevel@tonic-gate * | V V 537c478bd9Sstevel@tonic-gate * | +-----------+ 547c478bd9Sstevel@tonic-gate * | | ldterm, | 557c478bd9Sstevel@tonic-gate * | | etc. | 567c478bd9Sstevel@tonic-gate * | +-----------+ 577c478bd9Sstevel@tonic-gate * | +-[Anchor]--+ 587c478bd9Sstevel@tonic-gate * | | ptem | 597c478bd9Sstevel@tonic-gate * V +-----------+ 607c478bd9Sstevel@tonic-gate * +---master---+---slave---+ 617c478bd9Sstevel@tonic-gate * | | 627c478bd9Sstevel@tonic-gate * | zcons driver | 637c478bd9Sstevel@tonic-gate * | zonename="myzone" | 647c478bd9Sstevel@tonic-gate * +------------------------+ 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * There are basically three major tasks which the console subsystem in 677c478bd9Sstevel@tonic-gate * zoneadmd accomplishes: 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * - Setup and teardown of zcons driver instances. One zcons instance 707c478bd9Sstevel@tonic-gate * is maintained per zone; we take advantage of the libdevice APIs 717c478bd9Sstevel@tonic-gate * to online new instances of zcons as needed. Care is taken to 727c478bd9Sstevel@tonic-gate * prune and manage these appropriately; see init_console_dev() and 737c478bd9Sstevel@tonic-gate * destroy_console_dev(). The end result is the creation of the 747c478bd9Sstevel@tonic-gate * zcons(7D) instance and an open file descriptor to the master side. 757c478bd9Sstevel@tonic-gate * zcons instances are associated with zones via their zonename device 767c478bd9Sstevel@tonic-gate * property. This the console instance to persist across reboots, 777c478bd9Sstevel@tonic-gate * and while the zone is halted. 787c478bd9Sstevel@tonic-gate * 797c478bd9Sstevel@tonic-gate * - Initialization of the slave side of the console. This is 807c478bd9Sstevel@tonic-gate * accomplished by pushing various STREAMS modules onto the console. 817c478bd9Sstevel@tonic-gate * The ptem(7M) module gets special treatment, and is anchored into 827c478bd9Sstevel@tonic-gate * place using the I_ANCHOR facility. This is so that the zcons driver 837c478bd9Sstevel@tonic-gate * always has terminal semantics, as would a real hardware terminal. 847c478bd9Sstevel@tonic-gate * This means that ttymon(1M) works unmodified; at boot time, ttymon 857c478bd9Sstevel@tonic-gate * will do its own plumbing of the console stream, and will even 867c478bd9Sstevel@tonic-gate * I_POP modules off. Hence the anchor, which assures that ptem will 877c478bd9Sstevel@tonic-gate * never be I_POP'd. 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * - Acting as a server for 'zlogin -C' instances. When zlogin -C is 907c478bd9Sstevel@tonic-gate * run, zlogin connects to zoneadmd via unix domain socket. zoneadmd 917c478bd9Sstevel@tonic-gate * functions as a two-way proxy for console I/O, relaying user input 927c478bd9Sstevel@tonic-gate * to the master side of the console, and relaying output from the 937c478bd9Sstevel@tonic-gate * zone to the user. 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate #include <sys/types.h> 977c478bd9Sstevel@tonic-gate #include <sys/socket.h> 987c478bd9Sstevel@tonic-gate #include <sys/stat.h> 997c478bd9Sstevel@tonic-gate #include <sys/termios.h> 1007c478bd9Sstevel@tonic-gate #include <sys/zcons.h> 101*facf4a8dSllai1 #include <sys/mkdev.h> 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate #include <assert.h> 1047c478bd9Sstevel@tonic-gate #include <ctype.h> 1057c478bd9Sstevel@tonic-gate #include <errno.h> 1067c478bd9Sstevel@tonic-gate #include <fcntl.h> 1077c478bd9Sstevel@tonic-gate #include <stdarg.h> 1087c478bd9Sstevel@tonic-gate #include <stdio.h> 1097c478bd9Sstevel@tonic-gate #include <stdlib.h> 1107c478bd9Sstevel@tonic-gate #include <strings.h> 1117c478bd9Sstevel@tonic-gate #include <stropts.h> 1127c478bd9Sstevel@tonic-gate #include <thread.h> 1137c478bd9Sstevel@tonic-gate #include <ucred.h> 1147c478bd9Sstevel@tonic-gate #include <unistd.h> 1157c478bd9Sstevel@tonic-gate #include <zone.h> 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 1187c478bd9Sstevel@tonic-gate #include <libdevice.h> 1197c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate #include <syslog.h> 122*facf4a8dSllai1 #include <sys/modctl.h> 123*facf4a8dSllai1 #include <sys/fs/sdev_node.h> 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate #include "zoneadmd.h" 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate #define ZCONSNEX_DEVTREEPATH "/pseudo/zconsnex@1" 1287c478bd9Sstevel@tonic-gate #define ZCONSNEX_FILEPATH "/devices/pseudo/zconsnex@1" 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #define CONSOLE_SOCKPATH ZONES_TMPDIR "/%s.console_sock" 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate static int slavefd = -1; /* slave side of console */ 1337c478bd9Sstevel@tonic-gate static int serverfd = -1; /* console server unix domain socket fd */ 1343f2f09c1Sdp char boot_args[BOOTARGS_MAX]; 1353f2f09c1Sdp char bad_boot_arg[BOOTARGS_MAX]; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate static struct termios base_termios = { /* from init.c */ 1387c478bd9Sstevel@tonic-gate BRKINT|ICRNL|IXON|IMAXBEL, /* iflag */ 1397c478bd9Sstevel@tonic-gate OPOST|ONLCR|TAB3, /* oflag */ 1407c478bd9Sstevel@tonic-gate CS8|CREAD|B9600, /* cflag */ 1417c478bd9Sstevel@tonic-gate ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN, /* lflag */ 1427c478bd9Sstevel@tonic-gate CINTR, CQUIT, CERASE, CKILL, CEOF, 0, 0, 0, 1437c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 0, 0, 0, 1447c478bd9Sstevel@tonic-gate 0, 0, 0 1457c478bd9Sstevel@tonic-gate }; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* 1487c478bd9Sstevel@tonic-gate * The eventstream is a simple one-directional flow of messages from the 1497c478bd9Sstevel@tonic-gate * door server to the console subsystem, implemented with a pipe. 1507c478bd9Sstevel@tonic-gate * It is used to wake up the console poller when it needs to take action, 1517c478bd9Sstevel@tonic-gate * message the user, die off, etc. 1527c478bd9Sstevel@tonic-gate */ 1537c478bd9Sstevel@tonic-gate static int eventstream[2]; 1547c478bd9Sstevel@tonic-gate 1553f2f09c1Sdp 1563f2f09c1Sdp 1577c478bd9Sstevel@tonic-gate int 1587c478bd9Sstevel@tonic-gate eventstream_init() 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate if (pipe(eventstream) == -1) 1617c478bd9Sstevel@tonic-gate return (-1); 1627c478bd9Sstevel@tonic-gate return (0); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate void 1667c478bd9Sstevel@tonic-gate eventstream_write(zone_evt_t evt) 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate (void) write(eventstream[0], &evt, sizeof (evt)); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate static zone_evt_t 1727c478bd9Sstevel@tonic-gate eventstream_read(void) 1737c478bd9Sstevel@tonic-gate { 1747c478bd9Sstevel@tonic-gate zone_evt_t evt = Z_EVT_NULL; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate (void) read(eventstream[1], &evt, sizeof (evt)); 1777c478bd9Sstevel@tonic-gate return (evt); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* 1817c478bd9Sstevel@tonic-gate * count_console_devs() and its helper count_cb() do a walk of the 1827c478bd9Sstevel@tonic-gate * subtree of the device tree where zone console nodes are represented. 1837c478bd9Sstevel@tonic-gate * The goal is to count zone console instances already setup for a zone 1847c478bd9Sstevel@tonic-gate * with the given name. More than 1 is anomolous, and our caller will 1857c478bd9Sstevel@tonic-gate * have to deal with that if we find that's the case. 1867c478bd9Sstevel@tonic-gate * 1877c478bd9Sstevel@tonic-gate * Note: this algorithm is a linear search of nodes in the zconsnex subtree 1887c478bd9Sstevel@tonic-gate * of the device tree, and could be a scalability problem, but I don't see 1897c478bd9Sstevel@tonic-gate * how to avoid it. 1907c478bd9Sstevel@tonic-gate */ 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * cb_data is shared by count_cb and destroy_cb for simplicity. 1947c478bd9Sstevel@tonic-gate */ 1957c478bd9Sstevel@tonic-gate struct cb_data { 1967c478bd9Sstevel@tonic-gate zlog_t *zlogp; 1977c478bd9Sstevel@tonic-gate int found; 1987c478bd9Sstevel@tonic-gate int killed; 1997c478bd9Sstevel@tonic-gate }; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate static int 2027c478bd9Sstevel@tonic-gate count_cb(di_node_t node, void *arg) 2037c478bd9Sstevel@tonic-gate { 2047c478bd9Sstevel@tonic-gate struct cb_data *cb = (struct cb_data *)arg; 2057c478bd9Sstevel@tonic-gate char *prop_data; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename", 2087c478bd9Sstevel@tonic-gate &prop_data) != -1) { 2097c478bd9Sstevel@tonic-gate assert(prop_data != NULL); 2107c478bd9Sstevel@tonic-gate if (strcmp(prop_data, zone_name) == 0) { 2117c478bd9Sstevel@tonic-gate cb->found++; 2127c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate static int 2197c478bd9Sstevel@tonic-gate count_console_devs(zlog_t *zlogp) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate di_node_t root; 2227c478bd9Sstevel@tonic-gate struct cb_data cb; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate bzero(&cb, sizeof (cb)); 2257c478bd9Sstevel@tonic-gate cb.zlogp = zlogp; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) == 2287c478bd9Sstevel@tonic-gate DI_NODE_NIL) { 2297c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "di_init"); 2307c478bd9Sstevel@tonic-gate return (-1); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate (void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, count_cb); 2347c478bd9Sstevel@tonic-gate di_fini(root); 2357c478bd9Sstevel@tonic-gate return (cb.found); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* 2397c478bd9Sstevel@tonic-gate * destroy_console_devs() and its helper destroy_cb() tears down any console 2407c478bd9Sstevel@tonic-gate * instances associated with this zone. If things went very wrong, we 2417c478bd9Sstevel@tonic-gate * might have more than one console instance hanging around. This routine 2427c478bd9Sstevel@tonic-gate * hunts down and tries to remove all of them. Of course, if the console 2437c478bd9Sstevel@tonic-gate * is open, the instance will not detach, which is a potential issue. 2447c478bd9Sstevel@tonic-gate */ 2457c478bd9Sstevel@tonic-gate static int 2467c478bd9Sstevel@tonic-gate destroy_cb(di_node_t node, void *arg) 2477c478bd9Sstevel@tonic-gate { 2487c478bd9Sstevel@tonic-gate struct cb_data *cb = (struct cb_data *)arg; 2497c478bd9Sstevel@tonic-gate char *prop_data; 2507c478bd9Sstevel@tonic-gate char *tmp; 2517c478bd9Sstevel@tonic-gate char devpath[MAXPATHLEN]; 2527c478bd9Sstevel@tonic-gate devctl_hdl_t hdl; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename", 2557c478bd9Sstevel@tonic-gate &prop_data) == -1) 2567c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate assert(prop_data != NULL); 2597c478bd9Sstevel@tonic-gate if (strcmp(prop_data, zone_name) != 0) { 2607c478bd9Sstevel@tonic-gate /* this is the console for a different zone */ 2617c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate cb->found++; 2657c478bd9Sstevel@tonic-gate tmp = di_devfs_path(node); 2667c478bd9Sstevel@tonic-gate (void) snprintf(devpath, sizeof (devpath), "/devices/%s", tmp); 2677c478bd9Sstevel@tonic-gate di_devfs_path_free(tmp); 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate if ((hdl = devctl_device_acquire(devpath, 0)) == NULL) { 2707c478bd9Sstevel@tonic-gate zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, " 2717c478bd9Sstevel@tonic-gate "but it could not be controlled.", devpath); 2727c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate if (devctl_device_remove(hdl) == 0) { 2757c478bd9Sstevel@tonic-gate cb->killed++; 2767c478bd9Sstevel@tonic-gate } else { 2777c478bd9Sstevel@tonic-gate zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, " 2787c478bd9Sstevel@tonic-gate "but it could not be removed.", devpath); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate devctl_release(hdl); 2817c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate static int 2857c478bd9Sstevel@tonic-gate destroy_console_devs(zlog_t *zlogp) 2867c478bd9Sstevel@tonic-gate { 2877c478bd9Sstevel@tonic-gate di_node_t root; 2887c478bd9Sstevel@tonic-gate struct cb_data cb; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate bzero(&cb, sizeof (cb)); 2917c478bd9Sstevel@tonic-gate cb.zlogp = zlogp; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) == 2947c478bd9Sstevel@tonic-gate DI_NODE_NIL) { 2957c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "di_init"); 2967c478bd9Sstevel@tonic-gate return (-1); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate (void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, destroy_cb); 3007c478bd9Sstevel@tonic-gate if (cb.found > 1) { 3017c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "WARNING: multiple zone console " 3027c478bd9Sstevel@tonic-gate "instances detected for zone '%s'; %d of %d " 3037c478bd9Sstevel@tonic-gate "successfully removed.", 3047c478bd9Sstevel@tonic-gate zone_name, cb.killed, cb.found); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate di_fini(root); 3087c478bd9Sstevel@tonic-gate return (0); 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate /* 3127c478bd9Sstevel@tonic-gate * init_console_dev() drives the device-tree configuration of the zone 3137c478bd9Sstevel@tonic-gate * console device. The general strategy is to use the libdevice (devctl) 3147c478bd9Sstevel@tonic-gate * interfaces to instantiate a new zone console node. We do a lot of 3157c478bd9Sstevel@tonic-gate * sanity checking, and are careful to reuse a console if one exists. 3167c478bd9Sstevel@tonic-gate * 3177c478bd9Sstevel@tonic-gate * Once the device is in the device tree, we kick devfsadm via di_init_devs() 3187c478bd9Sstevel@tonic-gate * to ensure that the appropriate symlinks (to the master and slave console 3197c478bd9Sstevel@tonic-gate * devices) are placed in /dev in the global zone. 3207c478bd9Sstevel@tonic-gate */ 3217c478bd9Sstevel@tonic-gate static int 3227c478bd9Sstevel@tonic-gate init_console_dev(zlog_t *zlogp) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate devctl_hdl_t bus_hdl = NULL, dev_hdl = NULL; 3257c478bd9Sstevel@tonic-gate devctl_ddef_t ddef_hdl = NULL; 3267c478bd9Sstevel@tonic-gate di_devlink_handle_t dl = NULL; 3277c478bd9Sstevel@tonic-gate int rv = -1, ndevs; 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate /* 3307c478bd9Sstevel@tonic-gate * Don't re-setup console if it is working and ready already; just 3317c478bd9Sstevel@tonic-gate * skip ahead to making devlinks, which we do for sanity's sake. 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate ndevs = count_console_devs(zlogp); 3347c478bd9Sstevel@tonic-gate if (ndevs == 1) { 3357c478bd9Sstevel@tonic-gate goto devlinks; 3367c478bd9Sstevel@tonic-gate } else if (ndevs > 1 || ndevs == -1) { 3377c478bd9Sstevel@tonic-gate /* 3387c478bd9Sstevel@tonic-gate * For now, this seems like a reasonable but harsh punishment. 3397c478bd9Sstevel@tonic-gate * If needed, we could try to get clever and delete all but 3407c478bd9Sstevel@tonic-gate * the console which is pointed at by the current symlink. 3417c478bd9Sstevel@tonic-gate */ 3427c478bd9Sstevel@tonic-gate if (destroy_console_devs(zlogp) == -1) { 3437c478bd9Sstevel@tonic-gate goto error; 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * Time to make the consoles! 3497c478bd9Sstevel@tonic-gate */ 3507c478bd9Sstevel@tonic-gate if ((bus_hdl = devctl_bus_acquire(ZCONSNEX_FILEPATH, 0)) == NULL) { 3517c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "devctl_bus_acquire"); 3527c478bd9Sstevel@tonic-gate goto error; 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate if ((ddef_hdl = devctl_ddef_alloc("zcons", 0)) == NULL) { 3557c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to allocate ddef handle"); 3567c478bd9Sstevel@tonic-gate goto error; 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * Set three properties on this node; the first is the name of the 3607c478bd9Sstevel@tonic-gate * zone; the second is a flag which lets pseudo know that it is 3617c478bd9Sstevel@tonic-gate * OK to automatically allocate an instance # for this device; 3627c478bd9Sstevel@tonic-gate * the third tells the device framework not to auto-detach this 3637c478bd9Sstevel@tonic-gate * node-- we need the node to still be there when we ask devfsadmd 3647c478bd9Sstevel@tonic-gate * to make links, and when we need to open it. 3657c478bd9Sstevel@tonic-gate */ 3667c478bd9Sstevel@tonic-gate if (devctl_ddef_string(ddef_hdl, "zonename", zone_name) == -1) { 3677c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create zonename property"); 3687c478bd9Sstevel@tonic-gate goto error; 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate if (devctl_ddef_int(ddef_hdl, "auto-assign-instance", 1) == -1) { 3717c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create auto-assign-instance " 3727c478bd9Sstevel@tonic-gate "property"); 3737c478bd9Sstevel@tonic-gate goto error; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate if (devctl_ddef_int(ddef_hdl, "ddi-no-autodetach", 1) == -1) { 3767c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create ddi-no-auto-detach " 3777c478bd9Sstevel@tonic-gate "property"); 3787c478bd9Sstevel@tonic-gate goto error; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate if (devctl_bus_dev_create(bus_hdl, ddef_hdl, 0, &dev_hdl) == -1) { 3817c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create console node"); 3827c478bd9Sstevel@tonic-gate goto error; 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate devlinks: 3867c478bd9Sstevel@tonic-gate if ((dl = di_devlink_init("zcons", DI_MAKE_LINK)) != NULL) { 3877c478bd9Sstevel@tonic-gate (void) di_devlink_fini(&dl); 3887c478bd9Sstevel@tonic-gate } else { 3897c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to create devlinks"); 3907c478bd9Sstevel@tonic-gate goto error; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate rv = 0; 3947c478bd9Sstevel@tonic-gate error: 3957c478bd9Sstevel@tonic-gate if (ddef_hdl) 3967c478bd9Sstevel@tonic-gate devctl_ddef_free(ddef_hdl); 3977c478bd9Sstevel@tonic-gate if (bus_hdl) 3987c478bd9Sstevel@tonic-gate devctl_release(bus_hdl); 3997c478bd9Sstevel@tonic-gate if (dev_hdl) 4007c478bd9Sstevel@tonic-gate devctl_release(dev_hdl); 4017c478bd9Sstevel@tonic-gate return (rv); 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate /* 4057c478bd9Sstevel@tonic-gate * prep_console_slave() takes care of setting up the console slave device 4067c478bd9Sstevel@tonic-gate * (the side that the zone will eventually open). It is a helper for 4077c478bd9Sstevel@tonic-gate * init_console_slave(). 4087c478bd9Sstevel@tonic-gate * 4097c478bd9Sstevel@tonic-gate * We have to mknod and setup the console device; then the slave side is 4107c478bd9Sstevel@tonic-gate * opened, and the appropriate STREAMS modules are pushed on. A wrinkle is that 4117c478bd9Sstevel@tonic-gate * 'ptem' must be anchored in place (see streamio(7i) since we always want the 4127c478bd9Sstevel@tonic-gate * console to have terminal semantics. 4137c478bd9Sstevel@tonic-gate */ 4147c478bd9Sstevel@tonic-gate static int 415*facf4a8dSllai1 prep_console_slave(zlog_t *zlogp, char *devroot) 4167c478bd9Sstevel@tonic-gate { 4177c478bd9Sstevel@tonic-gate char slavename[MAXPATHLEN]; 4187c478bd9Sstevel@tonic-gate char zoneslavename[MAXPATHLEN]; 419*facf4a8dSllai1 char zonedev[MAXPATHLEN]; 420*facf4a8dSllai1 di_prof_t prof = NULL; 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate assert(slavefd == -1); 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate (void) snprintf(slavename, sizeof (slavename), 425*facf4a8dSllai1 "zcons/%s/%s", zone_name, ZCONS_SLAVE_NAME); 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate (void) snprintf(zoneslavename, sizeof (zoneslavename), 428*facf4a8dSllai1 "%s/dev/zconsole", devroot); 429*facf4a8dSllai1 430*facf4a8dSllai1 (void) snprintf(zonedev, sizeof (zonedev), 431*facf4a8dSllai1 "%s/dev", devroot); 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate /* 434*facf4a8dSllai1 * Specify zconsole as a name map in the dev profile 4357c478bd9Sstevel@tonic-gate */ 436*facf4a8dSllai1 if (di_prof_init(zonedev, &prof)) { 437*facf4a8dSllai1 zerror(zlogp, B_TRUE, "failed to initialize profile"); 4387c478bd9Sstevel@tonic-gate goto error; 4397c478bd9Sstevel@tonic-gate } 440*facf4a8dSllai1 441*facf4a8dSllai1 if (di_prof_add_map(prof, slavename, "zconsole")) { 442*facf4a8dSllai1 zerror(zlogp, B_TRUE, "failed to add zconsole map"); 4437c478bd9Sstevel@tonic-gate goto error; 4447c478bd9Sstevel@tonic-gate } 445*facf4a8dSllai1 446*facf4a8dSllai1 /* Send profile to kernel */ 447*facf4a8dSllai1 if (di_prof_commit(prof)) { 448*facf4a8dSllai1 zerror(zlogp, B_TRUE, "failed to commit profile"); 449*facf4a8dSllai1 goto error; 450*facf4a8dSllai1 } 451*facf4a8dSllai1 452*facf4a8dSllai1 di_prof_fini(prof); 453*facf4a8dSllai1 prof = NULL; 454*facf4a8dSllai1 4557c478bd9Sstevel@tonic-gate if ((slavefd = open(zoneslavename, O_RDWR | O_NOCTTY)) < 0) { 4567c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", zoneslavename); 457*facf4a8dSllai1 goto error; 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate /* 4617c478bd9Sstevel@tonic-gate * Just to make sure the whole stream is pristine. 4627c478bd9Sstevel@tonic-gate */ 4637c478bd9Sstevel@tonic-gate (void) ioctl(slavefd, I_FLUSH, FLUSHRW); 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate /* 4667c478bd9Sstevel@tonic-gate * Push hardware emulation (ptem) module; we would rather not, but 4677c478bd9Sstevel@tonic-gate * are forced to push ldterm and ttcompat here. Ultimately ttymon 4687c478bd9Sstevel@tonic-gate * will pop them off, so we ANCHOR only ptem. 4697c478bd9Sstevel@tonic-gate * 4707c478bd9Sstevel@tonic-gate * We need to use __I_PUSH_NOCTTY instead of I_PUSH here, otherwise 4717c478bd9Sstevel@tonic-gate * we'll end up having the slave device as *our* controling terminal. 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate if (ioctl(slavefd, __I_PUSH_NOCTTY, "ptem") == -1) { 4747c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to push ptem module"); 4757c478bd9Sstevel@tonic-gate goto error; 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate /* 4797c478bd9Sstevel@tonic-gate * Anchor the stream to prevent malicious or accidental I_POP of ptem. 4807c478bd9Sstevel@tonic-gate */ 4817c478bd9Sstevel@tonic-gate if (ioctl(slavefd, I_ANCHOR) == -1) { 4827c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to set stream anchor"); 4837c478bd9Sstevel@tonic-gate goto error; 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate if (ioctl(slavefd, __I_PUSH_NOCTTY, "ldterm") == -1) { 4877c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to push ldterm module"); 4887c478bd9Sstevel@tonic-gate goto error; 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate if (ioctl(slavefd, __I_PUSH_NOCTTY, "ttcompat") == -1) { 4917c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to push ttcompat module"); 4927c478bd9Sstevel@tonic-gate goto error; 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate /* 4967c478bd9Sstevel@tonic-gate * Setup default terminal settings 4977c478bd9Sstevel@tonic-gate */ 4987c478bd9Sstevel@tonic-gate if (tcsetattr(slavefd, TCSAFLUSH, &base_termios) == -1) { 4997c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to set base terminal settings"); 5007c478bd9Sstevel@tonic-gate goto error; 5017c478bd9Sstevel@tonic-gate } 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate return (0); 5047c478bd9Sstevel@tonic-gate error: 505*facf4a8dSllai1 if (slavefd != -1) 5067c478bd9Sstevel@tonic-gate (void) close(slavefd); 5077c478bd9Sstevel@tonic-gate slavefd = -1; 508*facf4a8dSllai1 if (prof) 509*facf4a8dSllai1 di_prof_fini(prof); 5107c478bd9Sstevel@tonic-gate return (-1); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate /* 5147c478bd9Sstevel@tonic-gate * init_console_slave() sets up the console slave device; the device node 5157c478bd9Sstevel@tonic-gate * itself has already been set up in the device tree; the primary job 5167c478bd9Sstevel@tonic-gate * here is to do some STREAMS plumbing (via prep_console_slave()) and then 5177c478bd9Sstevel@tonic-gate * to establish some symlinks. Eventually we should move that functionality 5187c478bd9Sstevel@tonic-gate * into devfsadm. 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate int 5217c478bd9Sstevel@tonic-gate init_console_slave(zlog_t *zlogp) 5227c478bd9Sstevel@tonic-gate { 523*facf4a8dSllai1 char devroot[MAXPATHLEN]; 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate if (slavefd != -1) 5267c478bd9Sstevel@tonic-gate return (0); 5277c478bd9Sstevel@tonic-gate 528*facf4a8dSllai1 if (zone_get_devroot(zone_name, devroot, sizeof (devroot)) != Z_OK) { 5297c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 5307c478bd9Sstevel@tonic-gate return (-1); 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 533*facf4a8dSllai1 if (prep_console_slave(zlogp, devroot) == -1) { 5347c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "could not prep console slave"); 5357c478bd9Sstevel@tonic-gate return (-1); 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate return (0); 5397c478bd9Sstevel@tonic-gate } 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate void 5427c478bd9Sstevel@tonic-gate destroy_console_slave(void) 5437c478bd9Sstevel@tonic-gate { 5447c478bd9Sstevel@tonic-gate (void) close(slavefd); 5457c478bd9Sstevel@tonic-gate slavefd = -1; 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate /* 5497c478bd9Sstevel@tonic-gate * Restore initial terminal attributes to the zone console. 5507c478bd9Sstevel@tonic-gate */ 5517c478bd9Sstevel@tonic-gate void 5527c478bd9Sstevel@tonic-gate reset_slave_terminal(zlog_t *zlogp) 5537c478bd9Sstevel@tonic-gate { 5547c478bd9Sstevel@tonic-gate assert(slavefd != -1); 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* Probably not fatal, so we drive on if it fails */ 5577c478bd9Sstevel@tonic-gate if (tcsetattr(slavefd, TCSAFLUSH, &base_termios) == -1) { 5587c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "WARNING: failed to set terminal " 5597c478bd9Sstevel@tonic-gate "settings."); 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate static int 5647c478bd9Sstevel@tonic-gate init_console_sock(zlog_t *zlogp) 5657c478bd9Sstevel@tonic-gate { 5667c478bd9Sstevel@tonic-gate int servfd; 5677c478bd9Sstevel@tonic-gate struct sockaddr_un servaddr; 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate bzero(&servaddr, sizeof (servaddr)); 5707c478bd9Sstevel@tonic-gate servaddr.sun_family = AF_UNIX; 5717c478bd9Sstevel@tonic-gate (void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path), 5727c478bd9Sstevel@tonic-gate CONSOLE_SOCKPATH, zone_name); 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate if ((servfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { 5757c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "console setup: could not create socket"); 5767c478bd9Sstevel@tonic-gate return (-1); 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate (void) unlink(servaddr.sun_path); 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate if (bind(servfd, (struct sockaddr *)&servaddr, 5817c478bd9Sstevel@tonic-gate sizeof (servaddr)) == -1) { 5827c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 5837c478bd9Sstevel@tonic-gate "console setup: could not bind to socket"); 5847c478bd9Sstevel@tonic-gate goto out; 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate if (listen(servfd, 4) == -1) { 5887c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 5897c478bd9Sstevel@tonic-gate "console setup: could not listen on socket"); 5907c478bd9Sstevel@tonic-gate goto out; 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate return (servfd); 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate out: 5957c478bd9Sstevel@tonic-gate (void) unlink(servaddr.sun_path); 5967c478bd9Sstevel@tonic-gate (void) close(servfd); 5977c478bd9Sstevel@tonic-gate return (-1); 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate static void 6017c478bd9Sstevel@tonic-gate destroy_console_sock(int servfd) 6027c478bd9Sstevel@tonic-gate { 6037c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), CONSOLE_SOCKPATH, zone_name); 6067c478bd9Sstevel@tonic-gate (void) unlink(path); 6077c478bd9Sstevel@tonic-gate (void) shutdown(servfd, SHUT_RDWR); 6087c478bd9Sstevel@tonic-gate (void) close(servfd); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate /* 6127c478bd9Sstevel@tonic-gate * Read the "ident" string from the client's descriptor; this routine also 6137c478bd9Sstevel@tonic-gate * tolerates being called with pid=NULL, for times when you want to "eat" 6147c478bd9Sstevel@tonic-gate * the ident string from a client without saving it. 6157c478bd9Sstevel@tonic-gate */ 6167c478bd9Sstevel@tonic-gate static int 6177c478bd9Sstevel@tonic-gate get_client_ident(int clifd, pid_t *pid, char *locale, size_t locale_len) 6187c478bd9Sstevel@tonic-gate { 6197c478bd9Sstevel@tonic-gate char buf[BUFSIZ], *bufp; 6207c478bd9Sstevel@tonic-gate size_t buflen = sizeof (buf); 6217c478bd9Sstevel@tonic-gate char c = '\0'; 6227c478bd9Sstevel@tonic-gate int i = 0, r; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate /* "eat up the ident string" case, for simplicity */ 6257c478bd9Sstevel@tonic-gate if (pid == NULL) { 6267c478bd9Sstevel@tonic-gate assert(locale == NULL && locale_len == 0); 6277c478bd9Sstevel@tonic-gate while (read(clifd, &c, 1) == 1) { 6287c478bd9Sstevel@tonic-gate if (c == '\n') 6297c478bd9Sstevel@tonic-gate return (0); 6307c478bd9Sstevel@tonic-gate } 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate bzero(buf, sizeof (buf)); 6347c478bd9Sstevel@tonic-gate while ((buflen > 1) && (r = read(clifd, &c, 1)) == 1) { 6357c478bd9Sstevel@tonic-gate buflen--; 6367c478bd9Sstevel@tonic-gate if (c == '\n') 6377c478bd9Sstevel@tonic-gate break; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate buf[i] = c; 6407c478bd9Sstevel@tonic-gate i++; 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate if (r == -1) 6437c478bd9Sstevel@tonic-gate return (-1); 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * We've filled the buffer, but still haven't seen \n. Keep eating 6477c478bd9Sstevel@tonic-gate * until we find it; we don't expect this to happen, but this is 6487c478bd9Sstevel@tonic-gate * defensive. 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate if (c != '\n') { 6517c478bd9Sstevel@tonic-gate while ((r = read(clifd, &c, sizeof (c))) > 0) 6527c478bd9Sstevel@tonic-gate if (c == '\n') 6537c478bd9Sstevel@tonic-gate break; 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate /* 6577c478bd9Sstevel@tonic-gate * Parse buffer for message of the form: IDENT <pid> <locale> 6587c478bd9Sstevel@tonic-gate */ 6597c478bd9Sstevel@tonic-gate bufp = buf; 6607c478bd9Sstevel@tonic-gate if (strncmp(bufp, "IDENT ", 6) != 0) 6617c478bd9Sstevel@tonic-gate return (-1); 6627c478bd9Sstevel@tonic-gate bufp += 6; 6637c478bd9Sstevel@tonic-gate errno = 0; 6647c478bd9Sstevel@tonic-gate *pid = strtoll(bufp, &bufp, 10); 6657c478bd9Sstevel@tonic-gate if (errno != 0) 6667c478bd9Sstevel@tonic-gate return (-1); 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate while (*bufp != '\0' && isspace(*bufp)) 6697c478bd9Sstevel@tonic-gate bufp++; 6707c478bd9Sstevel@tonic-gate (void) strlcpy(locale, bufp, locale_len); 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate return (0); 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate static int 6767c478bd9Sstevel@tonic-gate accept_client(int servfd, pid_t *pid, char *locale, size_t locale_len) 6777c478bd9Sstevel@tonic-gate { 6787c478bd9Sstevel@tonic-gate int connfd; 6797c478bd9Sstevel@tonic-gate struct sockaddr_un cliaddr; 6807c478bd9Sstevel@tonic-gate socklen_t clilen; 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate clilen = sizeof (cliaddr); 6837c478bd9Sstevel@tonic-gate connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen); 6847c478bd9Sstevel@tonic-gate if (connfd == -1) 6857c478bd9Sstevel@tonic-gate return (-1); 6867c478bd9Sstevel@tonic-gate if (get_client_ident(connfd, pid, locale, locale_len) == -1) { 6877c478bd9Sstevel@tonic-gate (void) shutdown(connfd, SHUT_RDWR); 6887c478bd9Sstevel@tonic-gate (void) close(connfd); 6897c478bd9Sstevel@tonic-gate return (-1); 6907c478bd9Sstevel@tonic-gate } 6917c478bd9Sstevel@tonic-gate (void) write(connfd, "OK\n", 3); 6927c478bd9Sstevel@tonic-gate return (connfd); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate static void 6967c478bd9Sstevel@tonic-gate reject_client(int servfd, pid_t clientpid) 6977c478bd9Sstevel@tonic-gate { 6987c478bd9Sstevel@tonic-gate int connfd; 6997c478bd9Sstevel@tonic-gate struct sockaddr_un cliaddr; 7007c478bd9Sstevel@tonic-gate socklen_t clilen; 7017c478bd9Sstevel@tonic-gate char nak[MAXPATHLEN]; 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate clilen = sizeof (cliaddr); 7047c478bd9Sstevel@tonic-gate connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen); 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate /* 7077c478bd9Sstevel@tonic-gate * After hear its ident string, tell client to get lost. 7087c478bd9Sstevel@tonic-gate */ 7097c478bd9Sstevel@tonic-gate if (get_client_ident(connfd, NULL, NULL, 0) == 0) { 7107c478bd9Sstevel@tonic-gate (void) snprintf(nak, sizeof (nak), "%lu\n", 7117c478bd9Sstevel@tonic-gate clientpid); 7127c478bd9Sstevel@tonic-gate (void) write(connfd, nak, strlen(nak)); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate (void) shutdown(connfd, SHUT_RDWR); 7157c478bd9Sstevel@tonic-gate (void) close(connfd); 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate static void 7197c478bd9Sstevel@tonic-gate event_message(int clifd, char *clilocale, zone_evt_t evt) 7207c478bd9Sstevel@tonic-gate { 7213f2f09c1Sdp char *str, *lstr = NULL; 7223f2f09c1Sdp char lmsg[BUFSIZ]; 7233f2f09c1Sdp char outbuf[BUFSIZ]; 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate if (clifd == -1) 7267c478bd9Sstevel@tonic-gate return; 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate switch (evt) { 7297c478bd9Sstevel@tonic-gate case Z_EVT_ZONE_BOOTING: 7303f2f09c1Sdp if (*boot_args == '\0') { 7313f2f09c1Sdp str = "NOTICE: Zone booting up"; 7323f2f09c1Sdp break; 7333f2f09c1Sdp } 7343f2f09c1Sdp /*LINTED*/ 7353f2f09c1Sdp (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale, 7363f2f09c1Sdp "NOTICE: Zone booting up with arguments: %s"), boot_args); 7373f2f09c1Sdp lstr = lmsg; 7387c478bd9Sstevel@tonic-gate break; 7397c478bd9Sstevel@tonic-gate case Z_EVT_ZONE_READIED: 7403f2f09c1Sdp str = "NOTICE: Zone readied"; 7417c478bd9Sstevel@tonic-gate break; 7427c478bd9Sstevel@tonic-gate case Z_EVT_ZONE_HALTED: 7433f2f09c1Sdp str = "NOTICE: Zone halted"; 7447c478bd9Sstevel@tonic-gate break; 7457c478bd9Sstevel@tonic-gate case Z_EVT_ZONE_REBOOTING: 7463f2f09c1Sdp if (*boot_args == '\0') { 7473f2f09c1Sdp str = "NOTICE: Zone rebooting"; 7483f2f09c1Sdp break; 7493f2f09c1Sdp } 7503f2f09c1Sdp /*LINTED*/ 7513f2f09c1Sdp (void) snprintf(lmsg, sizeof (lmsg), localize_msg(clilocale, 7523f2f09c1Sdp "NOTICE: Zone rebooting with arguments: %s"), boot_args); 7533f2f09c1Sdp lstr = lmsg; 7547c478bd9Sstevel@tonic-gate break; 7557c478bd9Sstevel@tonic-gate case Z_EVT_ZONE_UNINSTALLING: 7563f2f09c1Sdp str = "NOTICE: Zone is being uninstalled. Disconnecting..."; 7577c478bd9Sstevel@tonic-gate break; 758ffbafc53Scomay case Z_EVT_ZONE_BOOTFAILED: 7593f2f09c1Sdp str = "NOTICE: Zone boot failed"; 7603f2f09c1Sdp break; 7613f2f09c1Sdp case Z_EVT_ZONE_BADARGS: 7623f2f09c1Sdp /*LINTED*/ 7633f2f09c1Sdp (void) snprintf(lmsg, sizeof (lmsg), 7643f2f09c1Sdp localize_msg(clilocale, 7653f2f09c1Sdp "WARNING: Ignoring invalid boot arguments: %s"), 7663f2f09c1Sdp bad_boot_arg); 7673f2f09c1Sdp lstr = lmsg; 768ffbafc53Scomay break; 7697c478bd9Sstevel@tonic-gate default: 7707c478bd9Sstevel@tonic-gate return; 7717c478bd9Sstevel@tonic-gate } 7723f2f09c1Sdp 7733f2f09c1Sdp if (lstr == NULL) 7743f2f09c1Sdp lstr = localize_msg(clilocale, str); 7753f2f09c1Sdp (void) snprintf(outbuf, sizeof (outbuf), "\r\n[%s]\r\n", lstr); 7763f2f09c1Sdp (void) write(clifd, outbuf, strlen(outbuf)); 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate /* 7807c478bd9Sstevel@tonic-gate * Check to see if the client at the other end of the socket is still 7817c478bd9Sstevel@tonic-gate * alive; we know it is not if it throws EPIPE at us when we try to write 7827c478bd9Sstevel@tonic-gate * an otherwise harmless 0-length message to it. 7837c478bd9Sstevel@tonic-gate */ 7847c478bd9Sstevel@tonic-gate static int 7857c478bd9Sstevel@tonic-gate test_client(int clifd) 7867c478bd9Sstevel@tonic-gate { 7877c478bd9Sstevel@tonic-gate if ((write(clifd, "", 0) == -1) && errno == EPIPE) 7887c478bd9Sstevel@tonic-gate return (-1); 7897c478bd9Sstevel@tonic-gate return (0); 7907c478bd9Sstevel@tonic-gate } 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate /* 7937c478bd9Sstevel@tonic-gate * This routine drives the console I/O loop. It polls for input from the 7947c478bd9Sstevel@tonic-gate * master side of the console (output to the console), and from the client 7957c478bd9Sstevel@tonic-gate * (input from the console user). Additionally, it polls on the server fd, 7967c478bd9Sstevel@tonic-gate * and disconnects any clients that might try to hook up with the zone while 7977c478bd9Sstevel@tonic-gate * the console is in use. 7987c478bd9Sstevel@tonic-gate * 7997c478bd9Sstevel@tonic-gate * When the client first calls us up, it is expected to send a line giving 8007c478bd9Sstevel@tonic-gate * its "identity"; this consists of the string 'IDENT <pid> <locale>'. 8017c478bd9Sstevel@tonic-gate * This is so that we can report that the console is busy along with 8027c478bd9Sstevel@tonic-gate * some diagnostics about who has it busy; the locale is used so that 8037c478bd9Sstevel@tonic-gate * asynchronous messages about zone state (like the NOTICE: zone halted 8047c478bd9Sstevel@tonic-gate * messages) can be output in the user's locale. 8057c478bd9Sstevel@tonic-gate */ 8067c478bd9Sstevel@tonic-gate static void 8077c478bd9Sstevel@tonic-gate do_console_io(zlog_t *zlogp, int consfd, int servfd) 8087c478bd9Sstevel@tonic-gate { 8097c478bd9Sstevel@tonic-gate struct pollfd pollfds[4]; 8107c478bd9Sstevel@tonic-gate char ibuf[BUFSIZ]; 8117c478bd9Sstevel@tonic-gate int cc, ret; 8127c478bd9Sstevel@tonic-gate int clifd = -1; 8137c478bd9Sstevel@tonic-gate int pollerr = 0; 8147c478bd9Sstevel@tonic-gate char clilocale[MAXPATHLEN]; 8157c478bd9Sstevel@tonic-gate pid_t clipid = 0; 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate /* console side, watch for read events */ 8187c478bd9Sstevel@tonic-gate pollfds[0].fd = consfd; 8197c478bd9Sstevel@tonic-gate pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | 8207c478bd9Sstevel@tonic-gate POLLPRI | POLLERR | POLLHUP | POLLNVAL; 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate /* client side, watch for read events */ 8237c478bd9Sstevel@tonic-gate pollfds[1].fd = clifd; 8247c478bd9Sstevel@tonic-gate pollfds[1].events = pollfds[0].events; 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate /* the server socket; watch for events (new connections) */ 8277c478bd9Sstevel@tonic-gate pollfds[2].fd = servfd; 8287c478bd9Sstevel@tonic-gate pollfds[2].events = pollfds[0].events; 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate /* the eventstram; watch for events (e.g.: zone halted) */ 8317c478bd9Sstevel@tonic-gate pollfds[3].fd = eventstream[1]; 8327c478bd9Sstevel@tonic-gate pollfds[3].events = pollfds[0].events; 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate for (;;) { 8357c478bd9Sstevel@tonic-gate pollfds[0].revents = pollfds[1].revents = 0; 8367c478bd9Sstevel@tonic-gate pollfds[2].revents = pollfds[3].revents = 0; 8377c478bd9Sstevel@tonic-gate 8387c478bd9Sstevel@tonic-gate ret = poll(pollfds, 8397c478bd9Sstevel@tonic-gate sizeof (pollfds) / sizeof (struct pollfd), -1); 8407c478bd9Sstevel@tonic-gate if (ret == -1 && errno != EINTR) { 8417c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "poll failed"); 8427c478bd9Sstevel@tonic-gate /* we are hosed, close connection */ 8437c478bd9Sstevel@tonic-gate break; 8447c478bd9Sstevel@tonic-gate } 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate /* event from console side */ 8477c478bd9Sstevel@tonic-gate if (pollfds[0].revents) { 8487c478bd9Sstevel@tonic-gate if (pollfds[0].revents & 8497c478bd9Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { 8507c478bd9Sstevel@tonic-gate errno = 0; 8517c478bd9Sstevel@tonic-gate cc = read(consfd, ibuf, BUFSIZ); 8527c478bd9Sstevel@tonic-gate if (cc <= 0 && (errno != EINTR) && 8537c478bd9Sstevel@tonic-gate (errno != EAGAIN)) 8547c478bd9Sstevel@tonic-gate break; 8557c478bd9Sstevel@tonic-gate /* 8567c478bd9Sstevel@tonic-gate * Lose I/O if no one is listening 8577c478bd9Sstevel@tonic-gate */ 8587c478bd9Sstevel@tonic-gate if (clifd != -1 && cc > 0) 8597c478bd9Sstevel@tonic-gate (void) write(clifd, ibuf, cc); 8607c478bd9Sstevel@tonic-gate } else { 8617c478bd9Sstevel@tonic-gate pollerr = pollfds[0].revents; 8627c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 8637c478bd9Sstevel@tonic-gate "closing connection with (console) " 8647c478bd9Sstevel@tonic-gate "pollerr %d\n", pollerr); 8657c478bd9Sstevel@tonic-gate break; 8667c478bd9Sstevel@tonic-gate } 8677c478bd9Sstevel@tonic-gate } 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate /* event from client side */ 8707c478bd9Sstevel@tonic-gate if (pollfds[1].revents) { 8717c478bd9Sstevel@tonic-gate if (pollfds[1].revents & 8727c478bd9Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { 8737c478bd9Sstevel@tonic-gate errno = 0; 8747c478bd9Sstevel@tonic-gate cc = read(clifd, ibuf, BUFSIZ); 8757c478bd9Sstevel@tonic-gate if (cc <= 0 && (errno != EINTR) && 8767c478bd9Sstevel@tonic-gate (errno != EAGAIN)) 8777c478bd9Sstevel@tonic-gate break; 8787c478bd9Sstevel@tonic-gate (void) write(consfd, ibuf, cc); 8797c478bd9Sstevel@tonic-gate } else { 8807c478bd9Sstevel@tonic-gate pollerr = pollfds[1].revents; 8817c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 8827c478bd9Sstevel@tonic-gate "closing connection with (client) " 8837c478bd9Sstevel@tonic-gate "pollerr %d\n", pollerr); 8847c478bd9Sstevel@tonic-gate break; 8857c478bd9Sstevel@tonic-gate } 8867c478bd9Sstevel@tonic-gate } 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate /* event from server socket */ 8897c478bd9Sstevel@tonic-gate if (pollfds[2].revents && 8907c478bd9Sstevel@tonic-gate (pollfds[2].revents & (POLLIN | POLLRDNORM))) { 8917c478bd9Sstevel@tonic-gate if (clifd != -1) { 8927c478bd9Sstevel@tonic-gate /* 8937c478bd9Sstevel@tonic-gate * Test the client to see if it is really 8947c478bd9Sstevel@tonic-gate * still alive. If it has died but we 8957c478bd9Sstevel@tonic-gate * haven't yet detected that, we might 8967c478bd9Sstevel@tonic-gate * deny a legitimate connect attempt. If it 8977c478bd9Sstevel@tonic-gate * is dead, we break out; once we tear down 8987c478bd9Sstevel@tonic-gate * the old connection, the new connection 8997c478bd9Sstevel@tonic-gate * will happen. 9007c478bd9Sstevel@tonic-gate */ 9017c478bd9Sstevel@tonic-gate if (test_client(clifd) == -1) { 9027c478bd9Sstevel@tonic-gate break; 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate /* we're already handling a client */ 9057c478bd9Sstevel@tonic-gate reject_client(servfd, clipid); 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate } else if ((clifd = accept_client(servfd, &clipid, 9097c478bd9Sstevel@tonic-gate clilocale, sizeof (clilocale))) != -1) { 9107c478bd9Sstevel@tonic-gate pollfds[1].fd = clifd; 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate } else { 9137c478bd9Sstevel@tonic-gate break; 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * Watch for events on the eventstream. This is how we get 9197c478bd9Sstevel@tonic-gate * notified of the zone halting, etc. It provides us a 9207c478bd9Sstevel@tonic-gate * "wakeup" from poll when important things happen, which 9217c478bd9Sstevel@tonic-gate * is good. 9227c478bd9Sstevel@tonic-gate */ 9237c478bd9Sstevel@tonic-gate if (pollfds[3].revents) { 9247c478bd9Sstevel@tonic-gate int evt = eventstream_read(); 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * After we drain out the event, if we aren't servicing 9277c478bd9Sstevel@tonic-gate * a console client, we hop back out to our caller, 9287c478bd9Sstevel@tonic-gate * which will check to see if it is time to shutdown 9297c478bd9Sstevel@tonic-gate * the daemon, or if we should take another console 9307c478bd9Sstevel@tonic-gate * service lap. 9317c478bd9Sstevel@tonic-gate */ 9327c478bd9Sstevel@tonic-gate if (clifd == -1) { 9337c478bd9Sstevel@tonic-gate break; 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate event_message(clifd, clilocale, evt); 9367c478bd9Sstevel@tonic-gate /* 9377c478bd9Sstevel@tonic-gate * Special handling for the message that the zone is 9387c478bd9Sstevel@tonic-gate * uninstalling; we boot the client, then break out 9397c478bd9Sstevel@tonic-gate * of this function. When we return to the 9407c478bd9Sstevel@tonic-gate * serve_console loop, we will see that the zone is 9417c478bd9Sstevel@tonic-gate * in a state < READY, and so zoneadmd will shutdown. 9427c478bd9Sstevel@tonic-gate */ 9437c478bd9Sstevel@tonic-gate if (evt == Z_EVT_ZONE_UNINSTALLING) { 9447c478bd9Sstevel@tonic-gate break; 9457c478bd9Sstevel@tonic-gate } 9467c478bd9Sstevel@tonic-gate } 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate } 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate if (clifd != -1) { 9517c478bd9Sstevel@tonic-gate (void) shutdown(clifd, SHUT_RDWR); 9527c478bd9Sstevel@tonic-gate (void) close(clifd); 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate int 9577c478bd9Sstevel@tonic-gate init_console(zlog_t *zlogp) 9587c478bd9Sstevel@tonic-gate { 9597c478bd9Sstevel@tonic-gate if (init_console_dev(zlogp) == -1) { 9607c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 9617c478bd9Sstevel@tonic-gate "console setup: device initialization failed"); 9627c478bd9Sstevel@tonic-gate return (-1); 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate if ((serverfd = init_console_sock(zlogp)) == -1) { 9667c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 9677c478bd9Sstevel@tonic-gate "console setup: socket initialization failed"); 9687c478bd9Sstevel@tonic-gate return (-1); 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate return (0); 9717c478bd9Sstevel@tonic-gate } 9727c478bd9Sstevel@tonic-gate 9737c478bd9Sstevel@tonic-gate /* 9747c478bd9Sstevel@tonic-gate * serve_console() is the master loop for driving console I/O. It is also the 9757c478bd9Sstevel@tonic-gate * routine which is ultimately responsible for "pulling the plug" on zoneadmd 9767c478bd9Sstevel@tonic-gate * when it realizes that the daemon should shut down. 9777c478bd9Sstevel@tonic-gate * 9787c478bd9Sstevel@tonic-gate * The rules for shutdown are: there must be no console client, and the zone 9797c478bd9Sstevel@tonic-gate * state must be < ready. However, we need to give things a chance to actually 9807c478bd9Sstevel@tonic-gate * get going when the daemon starts up-- otherwise the daemon would immediately 9817c478bd9Sstevel@tonic-gate * exit on startup if the zone was in the installed state, so we first drop 9827c478bd9Sstevel@tonic-gate * into the do_console_io() loop in order to give *something* a chance to 9837c478bd9Sstevel@tonic-gate * happen. 9847c478bd9Sstevel@tonic-gate */ 9857c478bd9Sstevel@tonic-gate void 9867c478bd9Sstevel@tonic-gate serve_console(zlog_t *zlogp) 9877c478bd9Sstevel@tonic-gate { 9887c478bd9Sstevel@tonic-gate int masterfd; 9897c478bd9Sstevel@tonic-gate zone_state_t zstate; 9907c478bd9Sstevel@tonic-gate char conspath[MAXPATHLEN]; 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate (void) snprintf(conspath, sizeof (conspath), 9937c478bd9Sstevel@tonic-gate "/dev/zcons/%s/%s", zone_name, ZCONS_MASTER_NAME); 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate for (;;) { 9967c478bd9Sstevel@tonic-gate masterfd = open(conspath, O_RDWR|O_NONBLOCK|O_NOCTTY); 9977c478bd9Sstevel@tonic-gate if (masterfd == -1) { 9987c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open console master"); 9997c478bd9Sstevel@tonic-gate (void) mutex_lock(&lock); 10007c478bd9Sstevel@tonic-gate goto death; 10017c478bd9Sstevel@tonic-gate } 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate /* 10047c478bd9Sstevel@tonic-gate * Setting RPROTDIS on the stream means that the control 10057c478bd9Sstevel@tonic-gate * portion of messages received (which we don't care about) 10067c478bd9Sstevel@tonic-gate * will be discarded by the stream head. If we allowed such 10077c478bd9Sstevel@tonic-gate * messages, we wouldn't be able to use read(2), as it fails 10087c478bd9Sstevel@tonic-gate * (EBADMSG) when a message with a control element is received. 10097c478bd9Sstevel@tonic-gate */ 10107c478bd9Sstevel@tonic-gate if (ioctl(masterfd, I_SRDOPT, RNORM|RPROTDIS) == -1) { 10117c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to set options on " 10127c478bd9Sstevel@tonic-gate "console master"); 10137c478bd9Sstevel@tonic-gate (void) mutex_lock(&lock); 10147c478bd9Sstevel@tonic-gate goto death; 10157c478bd9Sstevel@tonic-gate } 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate do_console_io(zlogp, masterfd, serverfd); 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate /* 10207c478bd9Sstevel@tonic-gate * We would prefer not to do this, but hostile zone processes 10217c478bd9Sstevel@tonic-gate * can cause the stream to become tainted, and reads will 10227c478bd9Sstevel@tonic-gate * fail. So, in case something has gone seriously ill, 10237c478bd9Sstevel@tonic-gate * we dismantle the stream and reopen the console when we 10247c478bd9Sstevel@tonic-gate * take another lap. 10257c478bd9Sstevel@tonic-gate */ 10267c478bd9Sstevel@tonic-gate (void) close(masterfd); 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate (void) mutex_lock(&lock); 10297c478bd9Sstevel@tonic-gate /* 10307c478bd9Sstevel@tonic-gate * We need to set death_throes (see below) atomically with 10317c478bd9Sstevel@tonic-gate * respect to noticing that (a) we have no console client and 10327c478bd9Sstevel@tonic-gate * (b) the zone is not installed. Otherwise we could get a 10337c478bd9Sstevel@tonic-gate * request to boot during this time. Once we set death_throes, 10347c478bd9Sstevel@tonic-gate * any incoming door stuff will be turned away. 10357c478bd9Sstevel@tonic-gate */ 10367c478bd9Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) == Z_OK) { 10377c478bd9Sstevel@tonic-gate if (zstate < ZONE_STATE_READY) 10387c478bd9Sstevel@tonic-gate goto death; 10397c478bd9Sstevel@tonic-gate } else { 10407c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 10417c478bd9Sstevel@tonic-gate "unable to determine state of zone"); 10427c478bd9Sstevel@tonic-gate goto death; 10437c478bd9Sstevel@tonic-gate } 10447c478bd9Sstevel@tonic-gate /* 10457c478bd9Sstevel@tonic-gate * Even if zone_get_state() fails, stay conservative, and 10467c478bd9Sstevel@tonic-gate * take another lap. 10477c478bd9Sstevel@tonic-gate */ 10487c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 10497c478bd9Sstevel@tonic-gate } 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate death: 10527c478bd9Sstevel@tonic-gate assert(MUTEX_HELD(&lock)); 10537c478bd9Sstevel@tonic-gate in_death_throes = B_TRUE; 10547c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lock); 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate destroy_console_sock(serverfd); 10577c478bd9Sstevel@tonic-gate (void) destroy_console_devs(zlogp); 10587c478bd9Sstevel@tonic-gate } 1059