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 5fea9cb91Slq150181 * Common Development and Distribution License (the "License"). 6fea9cb91Slq150181 * 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 */ 21fea9cb91Slq150181 227c478bd9Sstevel@tonic-gate /* 2319397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * Indirect console driver for Sun. 297c478bd9Sstevel@tonic-gate * 307c478bd9Sstevel@tonic-gate * Redirects all I/O to the device designated as the underlying "hardware" 317c478bd9Sstevel@tonic-gate * console, as given by the value of rconsvp. The implementation assumes that 327c478bd9Sstevel@tonic-gate * rconsvp denotes a STREAMS device; the assumption is justified since 337c478bd9Sstevel@tonic-gate * consoles must be capable of effecting tty semantics. 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * rconsvp is set in autoconf.c:consconfig(), based on information obtained 367c478bd9Sstevel@tonic-gate * from the EEPROM. 377c478bd9Sstevel@tonic-gate * 387c478bd9Sstevel@tonic-gate * XXX: The driver still needs to be converted to use ANSI C consistently 397c478bd9Sstevel@tonic-gate * throughout. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/open.h> 447c478bd9Sstevel@tonic-gate #include <sys/param.h> 457c478bd9Sstevel@tonic-gate #include <sys/systm.h> 467c478bd9Sstevel@tonic-gate #include <sys/signal.h> 477c478bd9Sstevel@tonic-gate #include <sys/cred.h> 487c478bd9Sstevel@tonic-gate #include <sys/user.h> 497c478bd9Sstevel@tonic-gate #include <sys/proc.h> 507c478bd9Sstevel@tonic-gate #include <sys/disp.h> 517c478bd9Sstevel@tonic-gate #include <sys/file.h> 527c478bd9Sstevel@tonic-gate #include <sys/taskq.h> 537c478bd9Sstevel@tonic-gate #include <sys/log.h> 547c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 557c478bd9Sstevel@tonic-gate #include <sys/uio.h> 567c478bd9Sstevel@tonic-gate #include <sys/stat.h> 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #include <sys/console.h> 597c478bd9Sstevel@tonic-gate #include <sys/consdev.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #include <sys/stream.h> 627c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 637c478bd9Sstevel@tonic-gate #include <sys/poll.h> 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #include <sys/debug.h> 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #include <sys/conf.h> 687c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 697c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate static int cnopen(dev_t *, int, int, struct cred *); 727c478bd9Sstevel@tonic-gate static int cnclose(dev_t, int, int, struct cred *); 737c478bd9Sstevel@tonic-gate static int cnread(dev_t, struct uio *, struct cred *); 747c478bd9Sstevel@tonic-gate static int cnwrite(dev_t, struct uio *, struct cred *); 757c478bd9Sstevel@tonic-gate static int cnioctl(dev_t, int, intptr_t, int, struct cred *, int *); 767c478bd9Sstevel@tonic-gate static int cnpoll(dev_t, short, int, short *, struct pollhead **); 777c478bd9Sstevel@tonic-gate static int cn_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 787c478bd9Sstevel@tonic-gate static int cn_attach(dev_info_t *, ddi_attach_cmd_t); 797c478bd9Sstevel@tonic-gate static int cn_detach(dev_info_t *, ddi_detach_cmd_t); 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate static dev_info_t *cn_dip; /* private copy of devinfo pointer */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static struct cb_ops cn_cb_ops = { 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate cnopen, /* open */ 867c478bd9Sstevel@tonic-gate cnclose, /* close */ 877c478bd9Sstevel@tonic-gate nodev, /* strategy */ 887c478bd9Sstevel@tonic-gate nodev, /* print */ 897c478bd9Sstevel@tonic-gate nodev, /* dump */ 907c478bd9Sstevel@tonic-gate cnread, /* read */ 917c478bd9Sstevel@tonic-gate cnwrite, /* write */ 927c478bd9Sstevel@tonic-gate cnioctl, /* ioctl */ 937c478bd9Sstevel@tonic-gate nodev, /* devmap */ 947c478bd9Sstevel@tonic-gate nodev, /* mmap */ 957c478bd9Sstevel@tonic-gate nodev, /* segmap */ 967c478bd9Sstevel@tonic-gate cnpoll, /* poll */ 977c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 987c478bd9Sstevel@tonic-gate 0, /* streamtab */ 997c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate }; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static struct dev_ops cn_ops = { 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 1067c478bd9Sstevel@tonic-gate 0, /* refcnt */ 1077c478bd9Sstevel@tonic-gate cn_info, /* info */ 1087c478bd9Sstevel@tonic-gate nulldev, /* identify */ 1097c478bd9Sstevel@tonic-gate nulldev, /* probe */ 1107c478bd9Sstevel@tonic-gate cn_attach, /* attach */ 1117c478bd9Sstevel@tonic-gate cn_detach, /* detach */ 1127c478bd9Sstevel@tonic-gate nodev, /* reset */ 1137c478bd9Sstevel@tonic-gate &cn_cb_ops, /* driver operations */ 11419397407SSherry Moore (struct bus_ops *)0, /* bus operations */ 11519397407SSherry Moore NULL, /* power */ 11619397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate }; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate /* 1217c478bd9Sstevel@tonic-gate * Global variables associated with the console device: 1227c478bd9Sstevel@tonic-gate * 1237c478bd9Sstevel@tonic-gate * XXX: There are too many of these! 124da6c28aaSamw * moved to space.c to become resident in the kernel so that cons 1257c478bd9Sstevel@tonic-gate * can be loadable. 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate extern dev_t rconsdev; /* "hardware" console */ 1297c478bd9Sstevel@tonic-gate extern vnode_t *rconsvp; /* pointer to vnode for that device */ 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * XXX: consulted in prsubr.c, for /proc entry point for obtaining ps info. 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate extern dev_t uconsdev; /* What the user thinks is the console device */ 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* 1377c478bd9Sstevel@tonic-gate * Private driver state: 1387c478bd9Sstevel@tonic-gate */ 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* 1417c478bd9Sstevel@tonic-gate * The underlying console device potentially can be opened through (at least) 1427c478bd9Sstevel@tonic-gate * two paths: through this driver and through the underlying device's driver. 1437c478bd9Sstevel@tonic-gate * To ensure that reference counts are meaningful and therefore that close 1447c478bd9Sstevel@tonic-gate * routines are called at the right time, it's important to make sure that 1457c478bd9Sstevel@tonic-gate * rconsvp's s_count field (i.e., the count on the underlying device) never 1467c478bd9Sstevel@tonic-gate * has a contribution of more than one through this driver, regardless of how 1477c478bd9Sstevel@tonic-gate * many times this driver's been opened. rconsopen keeps track of the 1487c478bd9Sstevel@tonic-gate * necessary information to ensure this property. 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate static uint_t rconsopen; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate #include <sys/types.h> 1547c478bd9Sstevel@tonic-gate #include <sys/conf.h> 1557c478bd9Sstevel@tonic-gate #include <sys/param.h> 1567c478bd9Sstevel@tonic-gate #include <sys/systm.h> 1577c478bd9Sstevel@tonic-gate #include <sys/errno.h> 1587c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate extern int nodev(), nulldev(); 1627c478bd9Sstevel@tonic-gate extern int dseekneg_flag; 1637c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1647c478bd9Sstevel@tonic-gate extern struct dev_ops cn_ops; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1717c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */ 17219397407SSherry Moore "Console redirection driver", 1737c478bd9Sstevel@tonic-gate &cn_ops, /* driver ops */ 1747c478bd9Sstevel@tonic-gate }; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1777c478bd9Sstevel@tonic-gate MODREV_1, 1787c478bd9Sstevel@tonic-gate &modldrv, 1797c478bd9Sstevel@tonic-gate NULL 1807c478bd9Sstevel@tonic-gate }; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate int 1837c478bd9Sstevel@tonic-gate _init(void) 1847c478bd9Sstevel@tonic-gate { 1857c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate int 1897c478bd9Sstevel@tonic-gate _fini(void) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate return (EBUSY); 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate int 1957c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * DDI glue routines 2027c478bd9Sstevel@tonic-gate */ 2037c478bd9Sstevel@tonic-gate static int 2047c478bd9Sstevel@tonic-gate cn_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 2057c478bd9Sstevel@tonic-gate { 2067c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH) 2077c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "syscon", S_IFCHR, 2107c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, NULL) == DDI_FAILURE) { 2117c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "systty", S_IFCHR, 2147c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, NULL) == DDI_FAILURE) { 2157c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2167c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "console", S_IFCHR, 2197c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, NULL) == DDI_FAILURE) { 2207c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2217c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2227c478bd9Sstevel@tonic-gate } 223*aecfc01dSrui zang - Sun Microsystems - Beijing China 2247c478bd9Sstevel@tonic-gate cn_dip = devi; 2257c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate static int 2297c478bd9Sstevel@tonic-gate cn_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) 2327c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2337c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2347c478bd9Sstevel@tonic-gate uconsdev = NODEV; 2357c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2397c478bd9Sstevel@tonic-gate static int 2407c478bd9Sstevel@tonic-gate cn_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2417c478bd9Sstevel@tonic-gate { 2427c478bd9Sstevel@tonic-gate int error = DDI_FAILURE; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate switch (infocmd) { 2457c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 2467c478bd9Sstevel@tonic-gate if (getminor((dev_t)arg) == 0 && cn_dip != NULL) { 2477c478bd9Sstevel@tonic-gate *result = (void *) cn_dip; 2487c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate break; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 2537c478bd9Sstevel@tonic-gate if (getminor((dev_t)arg) == 0) { 2547c478bd9Sstevel@tonic-gate *result = (void *)0; 2557c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate break; 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate default: 2607c478bd9Sstevel@tonic-gate break; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate return (error); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * XXX Caution: before allowing more than 256 minor devices on the 2687c478bd9Sstevel@tonic-gate * console, make sure you understand the 'compatibility' hack 2697c478bd9Sstevel@tonic-gate * in ufs_iget() that translates old dev_t's to new dev_t's. 2707c478bd9Sstevel@tonic-gate * See bugid 1098104 for the sordid details. 2717c478bd9Sstevel@tonic-gate */ 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2747c478bd9Sstevel@tonic-gate static int 2757c478bd9Sstevel@tonic-gate cnopen(dev_t *dev, int flag, int state, struct cred *cred) 2767c478bd9Sstevel@tonic-gate { 2777c478bd9Sstevel@tonic-gate int err; 2787c478bd9Sstevel@tonic-gate static int been_here; 2797c478bd9Sstevel@tonic-gate vnode_t *vp = rconsvp; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate ASSERT(cred != NULL); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate if (rconsvp == NULL) 2847c478bd9Sstevel@tonic-gate return (0); 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate /* 2887c478bd9Sstevel@tonic-gate * XXX: Clean up inactive PIDs from previous opens if any. 2897c478bd9Sstevel@tonic-gate * These would have been created as a result of an I_SETSIG 2907c478bd9Sstevel@tonic-gate * issued against console. This is a workaround, and 2917c478bd9Sstevel@tonic-gate * console driver must be correctly redesigned not to need 2927c478bd9Sstevel@tonic-gate * this hook. 2937c478bd9Sstevel@tonic-gate */ 2947c478bd9Sstevel@tonic-gate if (vp->v_stream) { 2957c478bd9Sstevel@tonic-gate str_cn_clean(vp); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate /* 2997c478bd9Sstevel@tonic-gate * XXX: Set hook to tell /proc about underlying console. (There's 3007c478bd9Sstevel@tonic-gate * gotta be a better way...) 3017c478bd9Sstevel@tonic-gate */ 3027c478bd9Sstevel@tonic-gate if (state != OTYP_CHR || getminor(*dev) != 0) 3037c478bd9Sstevel@tonic-gate return (ENXIO); 3047c478bd9Sstevel@tonic-gate if (been_here == 0) { 3057c478bd9Sstevel@tonic-gate uconsdev = *dev; 3067c478bd9Sstevel@tonic-gate been_here = 1; 3077c478bd9Sstevel@tonic-gate if (vn_open("/dev/console", UIO_SYSSPACE, FWRITE | FNOCTTY, 3087c478bd9Sstevel@tonic-gate 0, &console_vnode, 0, 0) == 0) 3097c478bd9Sstevel@tonic-gate console_taskq = taskq_create("console_taskq", 3107c478bd9Sstevel@tonic-gate 1, maxclsyspri - 1, LOG_LOWAT / LOG_MSGSIZE, 3117c478bd9Sstevel@tonic-gate LOG_HIWAT / LOG_MSGSIZE, TASKQ_PREPOPULATE); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 314da6c28aaSamw if ((err = VOP_OPEN(&vp, flag, cred, NULL)) != 0) 3157c478bd9Sstevel@tonic-gate return (err); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate /* 3187c478bd9Sstevel@tonic-gate * The underlying driver is not allowed to have cloned itself 3197c478bd9Sstevel@tonic-gate * for this open. 3207c478bd9Sstevel@tonic-gate */ 3217c478bd9Sstevel@tonic-gate if (vp != rconsvp) { 3227c478bd9Sstevel@tonic-gate /* 3237c478bd9Sstevel@tonic-gate * It might happen that someone set rconsvp to NULL 3247c478bd9Sstevel@tonic-gate * whilst we were in the middle of the open. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate if (rconsvp == NULL) { 327da6c28aaSamw (void) VOP_CLOSE(vp, flag, 1, (offset_t)0, cred, NULL); 3287c478bd9Sstevel@tonic-gate return (0); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate cmn_err(CE_PANIC, "cnopen: cloned open"); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate rconsopen++; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate return (0); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3397c478bd9Sstevel@tonic-gate static int 3407c478bd9Sstevel@tonic-gate cnclose(dev_t dev, int flag, int state, struct cred *cred) 3417c478bd9Sstevel@tonic-gate { 3427c478bd9Sstevel@tonic-gate int err = 0; 3437c478bd9Sstevel@tonic-gate vnode_t *vp; 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate /* 3467c478bd9Sstevel@tonic-gate * Since this is the _last_ close, it's our last chance to close the 3477c478bd9Sstevel@tonic-gate * underlying device. (Note that if someone else has the underlying 3487c478bd9Sstevel@tonic-gate * hardware console device open, we won't get here, since spec_close 3497c478bd9Sstevel@tonic-gate * will see s_count > 1.) 3507c478bd9Sstevel@tonic-gate */ 3517c478bd9Sstevel@tonic-gate if (state != OTYP_CHR) 3527c478bd9Sstevel@tonic-gate return (ENXIO); 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if (rconsvp == NULL) 3557c478bd9Sstevel@tonic-gate return (0); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate while ((rconsopen != 0) && ((vp = rconsvp) != NULL)) { 358da6c28aaSamw err = VOP_CLOSE(vp, flag, 1, (offset_t)0, cred, NULL); 3597c478bd9Sstevel@tonic-gate if (!err) { 3607c478bd9Sstevel@tonic-gate rconsopen--; 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate return (err); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3677c478bd9Sstevel@tonic-gate static int 3687c478bd9Sstevel@tonic-gate cnread(dev_t dev, struct uio *uio, struct cred *cred) 3697c478bd9Sstevel@tonic-gate { 3707c478bd9Sstevel@tonic-gate kcondvar_t sleep_forever; 3717c478bd9Sstevel@tonic-gate kmutex_t sleep_forever_mutex; 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate if (rconsvp == NULL) { 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * Go to sleep forever. This seems like the least 3767c478bd9Sstevel@tonic-gate * harmful thing to do if there's no console. 3777c478bd9Sstevel@tonic-gate * EOF might be better if we're ending up single-user 3787c478bd9Sstevel@tonic-gate * mode. 3797c478bd9Sstevel@tonic-gate */ 3807c478bd9Sstevel@tonic-gate cv_init(&sleep_forever, NULL, CV_DRIVER, NULL); 3817c478bd9Sstevel@tonic-gate mutex_init(&sleep_forever_mutex, NULL, MUTEX_DRIVER, NULL); 3827c478bd9Sstevel@tonic-gate mutex_enter(&sleep_forever_mutex); 3837c478bd9Sstevel@tonic-gate (void) cv_wait_sig(&sleep_forever, &sleep_forever_mutex); 3847c478bd9Sstevel@tonic-gate mutex_exit(&sleep_forever_mutex); 3857c478bd9Sstevel@tonic-gate return (EIO); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate if (rconsvp->v_stream != NULL) 3897c478bd9Sstevel@tonic-gate return (strread(rconsvp, uio, cred)); 3907c478bd9Sstevel@tonic-gate else 3917c478bd9Sstevel@tonic-gate return (cdev_read(rconsdev, uio, cred)); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3957c478bd9Sstevel@tonic-gate static int 3967c478bd9Sstevel@tonic-gate cnwrite(dev_t dev, struct uio *uio, struct cred *cred) 3977c478bd9Sstevel@tonic-gate { 3987c478bd9Sstevel@tonic-gate if (rconsvp == NULL) { 3997c478bd9Sstevel@tonic-gate uio->uio_resid = 0; 4007c478bd9Sstevel@tonic-gate return (0); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate if (rconsvp->v_stream != NULL) 4047c478bd9Sstevel@tonic-gate return (strwrite(rconsvp, uio, cred)); 4057c478bd9Sstevel@tonic-gate else 4067c478bd9Sstevel@tonic-gate return (cdev_write(rconsdev, uio, cred)); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /* ARGSUSED */ 4107c478bd9Sstevel@tonic-gate static int 411fea9cb91Slq150181 cnprivateioc(dev_t dev, int cmd, intptr_t arg, int flag, struct cred *cred, 412fea9cb91Slq150181 int *rvalp) 413fea9cb91Slq150181 { 414fea9cb91Slq150181 415fea9cb91Slq150181 /* currently we only support one ioctl */ 416fea9cb91Slq150181 if (cmd != CONS_GETTERM) 417fea9cb91Slq150181 return (EINVAL); 418fea9cb91Slq150181 419fea9cb91Slq150181 /* Confirm iwscn is immediate target of cn redirection */ 420fea9cb91Slq150181 if (rconsvp != wsconsvp) 421fea9cb91Slq150181 return (ENODEV); 422fea9cb91Slq150181 423fea9cb91Slq150181 /* 424fea9cb91Slq150181 * If the redirection client is not wc, it should return 425fea9cb91Slq150181 * error upon receiving the CONS_GETTERM ioctl. 426fea9cb91Slq150181 * 427fea9cb91Slq150181 * if it is wc, we know that the target supports the CONS_GETTERM 428fea9cb91Slq150181 * ioctl, which very conviently has the exact same data 429fea9cb91Slq150181 * format as this ioctl... so let's just pass it on. 430fea9cb91Slq150181 */ 431fea9cb91Slq150181 return (cdev_ioctl(rconsdev, CONS_GETTERM, arg, flag, cred, rvalp)); 432fea9cb91Slq150181 } 433fea9cb91Slq150181 434fea9cb91Slq150181 /* ARGSUSED */ 435fea9cb91Slq150181 static int 4367c478bd9Sstevel@tonic-gate cnioctl(dev_t dev, int cmd, intptr_t arg, int flag, struct cred *cred, 4377c478bd9Sstevel@tonic-gate int *rvalp) 4387c478bd9Sstevel@tonic-gate { 4397c478bd9Sstevel@tonic-gate if (rconsvp == NULL) 4407c478bd9Sstevel@tonic-gate return (0); 4417c478bd9Sstevel@tonic-gate 442fea9cb91Slq150181 if ((cmd & _CNIOC_MASK) == _CNIOC) 443fea9cb91Slq150181 return (cnprivateioc(dev, cmd, arg, flag, cred, rvalp)); 444fea9cb91Slq150181 else if (rconsvp->v_stream != NULL) 4457c478bd9Sstevel@tonic-gate return (strioctl(rconsvp, cmd, arg, flag, U_TO_K, cred, 4467c478bd9Sstevel@tonic-gate rvalp)); 4477c478bd9Sstevel@tonic-gate else 4487c478bd9Sstevel@tonic-gate return (cdev_ioctl(rconsdev, cmd, arg, flag, cred, rvalp)); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* ARGSUSED */ 4527c478bd9Sstevel@tonic-gate static int 4537c478bd9Sstevel@tonic-gate cnpoll(dev_t dev, short events, int anyyet, short *reventsp, 4547c478bd9Sstevel@tonic-gate struct pollhead **phpp) 4557c478bd9Sstevel@tonic-gate { 4567c478bd9Sstevel@tonic-gate if (rconsvp == NULL) 4577c478bd9Sstevel@tonic-gate return (nochpoll(dev, events, anyyet, reventsp, phpp)); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (rconsvp->v_stream != NULL) 4607c478bd9Sstevel@tonic-gate return (strpoll(rconsvp->v_stream, events, anyyet, reventsp, 4617c478bd9Sstevel@tonic-gate phpp)); 4627c478bd9Sstevel@tonic-gate else 4637c478bd9Sstevel@tonic-gate return (cdev_poll(rconsdev, events, anyyet, reventsp, phpp)); 4647c478bd9Sstevel@tonic-gate } 465