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 /*
23ceeba6f9Srui zang - Sun Microsystems - Beijing China * Copyright (c) 1982, 2010, Oracle and/or its affiliates. All rights reserved.
248e935259SBryan Cantrill * Copyright 2015, Joyent, Inc. All rights reserved.
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>
578e935259SBryan Cantrill #include <sys/limits.h>
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #include <sys/console.h>
607c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate #include <sys/stream.h>
637c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
647c478bd9Sstevel@tonic-gate #include <sys/poll.h>
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #include <sys/debug.h>
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #include <sys/conf.h>
697c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
707c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
71ceeba6f9Srui zang - Sun Microsystems - Beijing China #include <sys/vt.h>
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate static int cnopen(dev_t *, int, int, struct cred *);
747c478bd9Sstevel@tonic-gate static int cnclose(dev_t, int, int, struct cred *);
757c478bd9Sstevel@tonic-gate static int cnread(dev_t, struct uio *, struct cred *);
767c478bd9Sstevel@tonic-gate static int cnwrite(dev_t, struct uio *, struct cred *);
777c478bd9Sstevel@tonic-gate static int cnioctl(dev_t, int, intptr_t, int, struct cred *, int *);
787c478bd9Sstevel@tonic-gate static int cnpoll(dev_t, short, int, short *, struct pollhead **);
797c478bd9Sstevel@tonic-gate static int cn_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
807c478bd9Sstevel@tonic-gate static int cn_attach(dev_info_t *, ddi_attach_cmd_t);
817c478bd9Sstevel@tonic-gate static int cn_detach(dev_info_t *, ddi_detach_cmd_t);
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate static dev_info_t *cn_dip; /* private copy of devinfo pointer */
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate static struct cb_ops cn_cb_ops = {
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate cnopen, /* open */
887c478bd9Sstevel@tonic-gate cnclose, /* close */
897c478bd9Sstevel@tonic-gate nodev, /* strategy */
907c478bd9Sstevel@tonic-gate nodev, /* print */
917c478bd9Sstevel@tonic-gate nodev, /* dump */
927c478bd9Sstevel@tonic-gate cnread, /* read */
937c478bd9Sstevel@tonic-gate cnwrite, /* write */
947c478bd9Sstevel@tonic-gate cnioctl, /* ioctl */
957c478bd9Sstevel@tonic-gate nodev, /* devmap */
967c478bd9Sstevel@tonic-gate nodev, /* mmap */
977c478bd9Sstevel@tonic-gate nodev, /* segmap */
987c478bd9Sstevel@tonic-gate cnpoll, /* poll */
997c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
1007c478bd9Sstevel@tonic-gate 0, /* streamtab */
1017c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate static struct dev_ops cn_ops = {
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
1087c478bd9Sstevel@tonic-gate 0, /* refcnt */
1097c478bd9Sstevel@tonic-gate cn_info, /* info */
1107c478bd9Sstevel@tonic-gate nulldev, /* identify */
1117c478bd9Sstevel@tonic-gate nulldev, /* probe */
1127c478bd9Sstevel@tonic-gate cn_attach, /* attach */
1137c478bd9Sstevel@tonic-gate cn_detach, /* detach */
1147c478bd9Sstevel@tonic-gate nodev, /* reset */
1157c478bd9Sstevel@tonic-gate &cn_cb_ops, /* driver operations */
11619397407SSherry Moore (struct bus_ops *)0, /* bus operations */
11719397407SSherry Moore NULL, /* power */
11819397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * Global variables associated with the console device:
1247c478bd9Sstevel@tonic-gate *
1257c478bd9Sstevel@tonic-gate * XXX: There are too many of these!
126da6c28aaSamw * moved to space.c to become resident in the kernel so that cons
1277c478bd9Sstevel@tonic-gate * can be loadable.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate extern dev_t rconsdev; /* "hardware" console */
1317c478bd9Sstevel@tonic-gate extern vnode_t *rconsvp; /* pointer to vnode for that device */
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate * XXX: consulted in prsubr.c, for /proc entry point for obtaining ps info.
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate extern dev_t uconsdev; /* What the user thinks is the console device */
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Private driver state:
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * The underlying console device potentially can be opened through (at least)
1447c478bd9Sstevel@tonic-gate * two paths: through this driver and through the underlying device's driver.
1457c478bd9Sstevel@tonic-gate * To ensure that reference counts are meaningful and therefore that close
1467c478bd9Sstevel@tonic-gate * routines are called at the right time, it's important to make sure that
1477c478bd9Sstevel@tonic-gate * rconsvp's s_count field (i.e., the count on the underlying device) never
1487c478bd9Sstevel@tonic-gate * has a contribution of more than one through this driver, regardless of how
1497c478bd9Sstevel@tonic-gate * many times this driver's been opened. rconsopen keeps track of the
1507c478bd9Sstevel@tonic-gate * necessary information to ensure this property.
1517c478bd9Sstevel@tonic-gate */
1527c478bd9Sstevel@tonic-gate static uint_t rconsopen;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate #include <sys/types.h>
1567c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1577c478bd9Sstevel@tonic-gate #include <sys/param.h>
1587c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1597c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1607c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate extern int nodev(), nulldev();
1647c478bd9Sstevel@tonic-gate extern int dseekneg_flag;
1657c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1667c478bd9Sstevel@tonic-gate extern struct dev_ops cn_ops;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1737c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */
17419397407SSherry Moore "Console redirection driver",
1757c478bd9Sstevel@tonic-gate &cn_ops, /* driver ops */
1767c478bd9Sstevel@tonic-gate };
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1797c478bd9Sstevel@tonic-gate MODREV_1,
1807c478bd9Sstevel@tonic-gate &modldrv,
1817c478bd9Sstevel@tonic-gate NULL
1827c478bd9Sstevel@tonic-gate };
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate int
_init(void)1857c478bd9Sstevel@tonic-gate _init(void)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate int
_fini(void)1917c478bd9Sstevel@tonic-gate _fini(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate return (EBUSY);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1977c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate * DDI glue routines
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate static int
cn_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2067c478bd9Sstevel@tonic-gate cn_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH)
2097c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "syscon", S_IFCHR,
212a64c6771SToomas Soome 0, DDI_PSEUDO, 0) == DDI_FAILURE) {
2137c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "systty", S_IFCHR,
216a64c6771SToomas Soome 0, DDI_PSEUDO, 0) == DDI_FAILURE) {
2177c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2187c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "console", S_IFCHR,
221a64c6771SToomas Soome 0, DDI_PSEUDO, 0) == DDI_FAILURE) {
2227c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2237c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2247c478bd9Sstevel@tonic-gate }
225aecfc01dSrui zang - Sun Microsystems - Beijing China
2267c478bd9Sstevel@tonic-gate cn_dip = devi;
2277c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate static int
cn_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2317c478bd9Sstevel@tonic-gate cn_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH)
2347c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2357c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2367c478bd9Sstevel@tonic-gate uconsdev = NODEV;
2377c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate static int
cn_info(dev_info_t * dip __unused,ddi_info_cmd_t infocmd,void * arg,void ** result)241*c542a624SJoshua M. Clulow cn_info(dev_info_t *dip __unused, ddi_info_cmd_t infocmd, void *arg,
242*c542a624SJoshua M. Clulow void **result)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate int error = DDI_FAILURE;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate switch (infocmd) {
2477c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
2487c478bd9Sstevel@tonic-gate if (getminor((dev_t)arg) == 0 && cn_dip != NULL) {
2497c478bd9Sstevel@tonic-gate *result = (void *) cn_dip;
2507c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate break;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
2557c478bd9Sstevel@tonic-gate if (getminor((dev_t)arg) == 0) {
2567c478bd9Sstevel@tonic-gate *result = (void *)0;
2577c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate break;
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate default:
2627c478bd9Sstevel@tonic-gate break;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate return (error);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate * XXX Caution: before allowing more than 256 minor devices on the
2707c478bd9Sstevel@tonic-gate * console, make sure you understand the 'compatibility' hack
2717c478bd9Sstevel@tonic-gate * in ufs_iget() that translates old dev_t's to new dev_t's.
2727c478bd9Sstevel@tonic-gate * See bugid 1098104 for the sordid details.
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate /* ARGSUSED */
2767c478bd9Sstevel@tonic-gate static int
cnopen(dev_t * dev,int flag,int state,struct cred * cred)2777c478bd9Sstevel@tonic-gate cnopen(dev_t *dev, int flag, int state, struct cred *cred)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate int err;
2807c478bd9Sstevel@tonic-gate static int been_here;
2817c478bd9Sstevel@tonic-gate vnode_t *vp = rconsvp;
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate ASSERT(cred != NULL);
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate if (rconsvp == NULL)
2867c478bd9Sstevel@tonic-gate return (0);
2877c478bd9Sstevel@tonic-gate
288361ed64aSJames Anderson /*
289361ed64aSJames Anderson * Enable virtual console I/O for console logging if needed.
290361ed64aSJames Anderson */
291361ed64aSJames Anderson if (vsconsvp != NULL && vsconsvp->v_stream == NULL) {
292361ed64aSJames Anderson if (VOP_OPEN(&vsconsvp, FREAD | FWRITE, cred, NULL) != 0) {
293361ed64aSJames Anderson cmn_err(CE_WARN, "cnopen: failed to open vsconsvp "
294361ed64aSJames Anderson "for virtual console logging");
295361ed64aSJames Anderson }
296361ed64aSJames Anderson }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate * XXX: Clean up inactive PIDs from previous opens if any.
3007c478bd9Sstevel@tonic-gate * These would have been created as a result of an I_SETSIG
3017c478bd9Sstevel@tonic-gate * issued against console. This is a workaround, and
3027c478bd9Sstevel@tonic-gate * console driver must be correctly redesigned not to need
3037c478bd9Sstevel@tonic-gate * this hook.
3047c478bd9Sstevel@tonic-gate */
3057c478bd9Sstevel@tonic-gate if (vp->v_stream) {
3067c478bd9Sstevel@tonic-gate str_cn_clean(vp);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate * XXX: Set hook to tell /proc about underlying console. (There's
3117c478bd9Sstevel@tonic-gate * gotta be a better way...)
3127c478bd9Sstevel@tonic-gate */
3137c478bd9Sstevel@tonic-gate if (state != OTYP_CHR || getminor(*dev) != 0)
3147c478bd9Sstevel@tonic-gate return (ENXIO);
3157c478bd9Sstevel@tonic-gate if (been_here == 0) {
3167c478bd9Sstevel@tonic-gate uconsdev = *dev;
3177c478bd9Sstevel@tonic-gate been_here = 1;
3187c478bd9Sstevel@tonic-gate if (vn_open("/dev/console", UIO_SYSSPACE, FWRITE | FNOCTTY,
3197c478bd9Sstevel@tonic-gate 0, &console_vnode, 0, 0) == 0)
3207c478bd9Sstevel@tonic-gate console_taskq = taskq_create("console_taskq",
3217c478bd9Sstevel@tonic-gate 1, maxclsyspri - 1, LOG_LOWAT / LOG_MSGSIZE,
3227c478bd9Sstevel@tonic-gate LOG_HIWAT / LOG_MSGSIZE, TASKQ_PREPOPULATE);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
325da6c28aaSamw if ((err = VOP_OPEN(&vp, flag, cred, NULL)) != 0)
3267c478bd9Sstevel@tonic-gate return (err);
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * The underlying driver is not allowed to have cloned itself
3307c478bd9Sstevel@tonic-gate * for this open.
3317c478bd9Sstevel@tonic-gate */
3327c478bd9Sstevel@tonic-gate if (vp != rconsvp) {
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate * It might happen that someone set rconsvp to NULL
3357c478bd9Sstevel@tonic-gate * whilst we were in the middle of the open.
3367c478bd9Sstevel@tonic-gate */
3377c478bd9Sstevel@tonic-gate if (rconsvp == NULL) {
338da6c28aaSamw (void) VOP_CLOSE(vp, flag, 1, (offset_t)0, cred, NULL);
3397c478bd9Sstevel@tonic-gate return (0);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate cmn_err(CE_PANIC, "cnopen: cloned open");
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate rconsopen++;
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate return (0);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate static int
cnclose(dev_t dev __unused,int flag,int state,struct cred * cred)350*c542a624SJoshua M. Clulow cnclose(dev_t dev __unused, int flag, int state, struct cred *cred)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate int err = 0;
3537c478bd9Sstevel@tonic-gate vnode_t *vp;
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate * Since this is the _last_ close, it's our last chance to close the
3577c478bd9Sstevel@tonic-gate * underlying device. (Note that if someone else has the underlying
3587c478bd9Sstevel@tonic-gate * hardware console device open, we won't get here, since spec_close
3597c478bd9Sstevel@tonic-gate * will see s_count > 1.)
3607c478bd9Sstevel@tonic-gate */
3617c478bd9Sstevel@tonic-gate if (state != OTYP_CHR)
3627c478bd9Sstevel@tonic-gate return (ENXIO);
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate if (rconsvp == NULL)
3657c478bd9Sstevel@tonic-gate return (0);
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate while ((rconsopen != 0) && ((vp = rconsvp) != NULL)) {
368da6c28aaSamw err = VOP_CLOSE(vp, flag, 1, (offset_t)0, cred, NULL);
3697c478bd9Sstevel@tonic-gate if (!err) {
3707c478bd9Sstevel@tonic-gate rconsopen--;
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate return (err);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate static int
cnread(dev_t dev __unused,struct uio * uio,struct cred * cred)377*c542a624SJoshua M. Clulow cnread(dev_t dev __unused, struct uio *uio, struct cred *cred)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate kcondvar_t sleep_forever;
3807c478bd9Sstevel@tonic-gate kmutex_t sleep_forever_mutex;
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate if (rconsvp == NULL) {
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate * Go to sleep forever. This seems like the least
3857c478bd9Sstevel@tonic-gate * harmful thing to do if there's no console.
3867c478bd9Sstevel@tonic-gate * EOF might be better if we're ending up single-user
3877c478bd9Sstevel@tonic-gate * mode.
3887c478bd9Sstevel@tonic-gate */
3897c478bd9Sstevel@tonic-gate cv_init(&sleep_forever, NULL, CV_DRIVER, NULL);
3907c478bd9Sstevel@tonic-gate mutex_init(&sleep_forever_mutex, NULL, MUTEX_DRIVER, NULL);
3917c478bd9Sstevel@tonic-gate mutex_enter(&sleep_forever_mutex);
3927c478bd9Sstevel@tonic-gate (void) cv_wait_sig(&sleep_forever, &sleep_forever_mutex);
3937c478bd9Sstevel@tonic-gate mutex_exit(&sleep_forever_mutex);
3947c478bd9Sstevel@tonic-gate return (EIO);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate if (rconsvp->v_stream != NULL)
3987c478bd9Sstevel@tonic-gate return (strread(rconsvp, uio, cred));
3997c478bd9Sstevel@tonic-gate else
4007c478bd9Sstevel@tonic-gate return (cdev_read(rconsdev, uio, cred));
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate static int
cnwrite(dev_t dev __unused,struct uio * uio,struct cred * cred)404*c542a624SJoshua M. Clulow cnwrite(dev_t dev __unused, struct uio *uio, struct cred *cred)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate if (rconsvp == NULL) {
4077c478bd9Sstevel@tonic-gate uio->uio_resid = 0;
4087c478bd9Sstevel@tonic-gate return (0);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate
411361ed64aSJames Anderson /*
412361ed64aSJames Anderson * Output to virtual console for logging if enabled.
413361ed64aSJames Anderson */
414361ed64aSJames Anderson if (vsconsvp != NULL && vsconsvp->v_stream != NULL) {
415361ed64aSJames Anderson struiod_t uiod;
4168e935259SBryan Cantrill struct iovec buf[IOV_MAX_STACK];
4178e935259SBryan Cantrill int iovlen = 0;
4188e935259SBryan Cantrill
4198e935259SBryan Cantrill if (uio->uio_iovcnt > IOV_MAX_STACK) {
4208e935259SBryan Cantrill iovlen = uio->uio_iovcnt * sizeof (iovec_t);
4218e935259SBryan Cantrill uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
4228e935259SBryan Cantrill } else {
4238e935259SBryan Cantrill uiod.d_iov = buf;
4248e935259SBryan Cantrill }
425361ed64aSJames Anderson
426361ed64aSJames Anderson /*
427361ed64aSJames Anderson * strwrite modifies uio so need to make copy.
428361ed64aSJames Anderson */
4298e935259SBryan Cantrill (void) uiodup(uio, &uiod.d_uio, uiod.d_iov, uio->uio_iovcnt);
430361ed64aSJames Anderson
431361ed64aSJames Anderson (void) strwrite(vsconsvp, &uiod.d_uio, cred);
4328e935259SBryan Cantrill if (iovlen != 0)
4338e935259SBryan Cantrill kmem_free(uiod.d_iov, iovlen);
434361ed64aSJames Anderson }
435361ed64aSJames Anderson
4367c478bd9Sstevel@tonic-gate if (rconsvp->v_stream != NULL)
4377c478bd9Sstevel@tonic-gate return (strwrite(rconsvp, uio, cred));
4387c478bd9Sstevel@tonic-gate else
4397c478bd9Sstevel@tonic-gate return (cdev_write(rconsdev, uio, cred));
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate static int
cnprivateioc(dev_t dev __unused,int cmd,intptr_t arg,int flag,struct cred * cred,int * rvalp)443*c542a624SJoshua M. Clulow cnprivateioc(dev_t dev __unused, int cmd, intptr_t arg, int flag,
444*c542a624SJoshua M. Clulow struct cred *cred, int *rvalp)
445fea9cb91Slq150181 {
446*c542a624SJoshua M. Clulow if (cmd == CONS_GETDEV) {
447*c542a624SJoshua M. Clulow /*
448*c542a624SJoshua M. Clulow * The user has requested the device number of the redirection
449*c542a624SJoshua M. Clulow * client.
450*c542a624SJoshua M. Clulow */
451*c542a624SJoshua M. Clulow STRUCT_DECL(cons_getdev, cnd);
452*c542a624SJoshua M. Clulow STRUCT_INIT(cnd, flag);
453fea9cb91Slq150181
454*c542a624SJoshua M. Clulow bzero(STRUCT_BUF(cnd), STRUCT_SIZE(cnd));
455*c542a624SJoshua M. Clulow
456*c542a624SJoshua M. Clulow if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
457*c542a624SJoshua M. Clulow dev32_t rconsdev32;
458*c542a624SJoshua M. Clulow
459*c542a624SJoshua M. Clulow if (cmpldev(&rconsdev32, rconsdev) != 1) {
460*c542a624SJoshua M. Clulow return (EOVERFLOW);
461*c542a624SJoshua M. Clulow }
462*c542a624SJoshua M. Clulow
463*c542a624SJoshua M. Clulow STRUCT_FSET(cnd, cnd_rconsdev, rconsdev32);
464*c542a624SJoshua M. Clulow } else {
465*c542a624SJoshua M. Clulow STRUCT_FSET(cnd, cnd_rconsdev, rconsdev);
466*c542a624SJoshua M. Clulow }
467*c542a624SJoshua M. Clulow
468*c542a624SJoshua M. Clulow if (ddi_copyout(STRUCT_BUF(cnd), (void *)arg,
469*c542a624SJoshua M. Clulow STRUCT_SIZE(cnd), flag) != 0) {
470*c542a624SJoshua M. Clulow return (EFAULT);
471*c542a624SJoshua M. Clulow }
472*c542a624SJoshua M. Clulow
473*c542a624SJoshua M. Clulow return (0);
474*c542a624SJoshua M. Clulow }
475*c542a624SJoshua M. Clulow
476fea9cb91Slq150181 if (cmd != CONS_GETTERM)
477fea9cb91Slq150181 return (EINVAL);
478fea9cb91Slq150181
479fea9cb91Slq150181 /* Confirm iwscn is immediate target of cn redirection */
480fea9cb91Slq150181 if (rconsvp != wsconsvp)
481fea9cb91Slq150181 return (ENODEV);
482fea9cb91Slq150181
483fea9cb91Slq150181 /*
484fea9cb91Slq150181 * If the redirection client is not wc, it should return
485fea9cb91Slq150181 * error upon receiving the CONS_GETTERM ioctl.
486fea9cb91Slq150181 *
487fea9cb91Slq150181 * if it is wc, we know that the target supports the CONS_GETTERM
488fea9cb91Slq150181 * ioctl, which very conviently has the exact same data
489fea9cb91Slq150181 * format as this ioctl... so let's just pass it on.
490fea9cb91Slq150181 */
491fea9cb91Slq150181 return (cdev_ioctl(rconsdev, CONS_GETTERM, arg, flag, cred, rvalp));
492fea9cb91Slq150181 }
493fea9cb91Slq150181
494fea9cb91Slq150181 /* ARGSUSED */
495fea9cb91Slq150181 static int
cnioctl(dev_t dev,int cmd,intptr_t arg,int flag,struct cred * cred,int * rvalp)4967c478bd9Sstevel@tonic-gate cnioctl(dev_t dev, int cmd, intptr_t arg, int flag, struct cred *cred,
4977c478bd9Sstevel@tonic-gate int *rvalp)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate if (rconsvp == NULL)
5007c478bd9Sstevel@tonic-gate return (0);
5017c478bd9Sstevel@tonic-gate
502ceeba6f9Srui zang - Sun Microsystems - Beijing China /*
503ceeba6f9Srui zang - Sun Microsystems - Beijing China * In wc, VT_SET_CONSUSER which comes from minor node 0
504ceeba6f9Srui zang - Sun Microsystems - Beijing China * has two sources -- either /dev/console or /dev/vt/0 .
505ceeba6f9Srui zang - Sun Microsystems - Beijing China * We need a way to differentiate them, so here we
506ceeba6f9Srui zang - Sun Microsystems - Beijing China * change VT_SET_CONSUSER to a private VT_RESET_CONSUSER
507ceeba6f9Srui zang - Sun Microsystems - Beijing China * ioctl.
508ceeba6f9Srui zang - Sun Microsystems - Beijing China */
509ceeba6f9Srui zang - Sun Microsystems - Beijing China if (cmd == VT_SET_CONSUSER)
510ceeba6f9Srui zang - Sun Microsystems - Beijing China cmd = VT_RESET_CONSUSER;
511ceeba6f9Srui zang - Sun Microsystems - Beijing China
512fea9cb91Slq150181 if ((cmd & _CNIOC_MASK) == _CNIOC)
513fea9cb91Slq150181 return (cnprivateioc(dev, cmd, arg, flag, cred, rvalp));
514ceeba6f9Srui zang - Sun Microsystems - Beijing China
515ceeba6f9Srui zang - Sun Microsystems - Beijing China if (rconsvp->v_stream != NULL)
516ceeba6f9Srui zang - Sun Microsystems - Beijing China return (strioctl(rconsvp, cmd, arg, flag, U_TO_K,
517ceeba6f9Srui zang - Sun Microsystems - Beijing China cred, rvalp));
518ceeba6f9Srui zang - Sun Microsystems - Beijing China
5197c478bd9Sstevel@tonic-gate return (cdev_ioctl(rconsdev, cmd, arg, flag, cred, rvalp));
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /* ARGSUSED */
5237c478bd9Sstevel@tonic-gate static int
cnpoll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)5247c478bd9Sstevel@tonic-gate cnpoll(dev_t dev, short events, int anyyet, short *reventsp,
5257c478bd9Sstevel@tonic-gate struct pollhead **phpp)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate if (rconsvp == NULL)
5287c478bd9Sstevel@tonic-gate return (nochpoll(dev, events, anyyet, reventsp, phpp));
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate if (rconsvp->v_stream != NULL)
5317c478bd9Sstevel@tonic-gate return (strpoll(rconsvp->v_stream, events, anyyet, reventsp,
5327c478bd9Sstevel@tonic-gate phpp));
5337c478bd9Sstevel@tonic-gate else
5347c478bd9Sstevel@tonic-gate return (cdev_poll(rconsdev, events, anyyet, reventsp, phpp));
5357c478bd9Sstevel@tonic-gate }
536