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 5*9acbbeafSnn35248 * Common Development and Distribution License (the "License"). 6*9acbbeafSnn35248 * 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 */ 217c478bd9Sstevel@tonic-gate /* 22*9acbbeafSnn35248 * Copyright 2006 Sun Microsystems, Inc. 237c478bd9Sstevel@tonic-gate * All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate /* from S5R4 1.22 */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * Indirect driver for controlling tty. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <sys/errno.h> 397c478bd9Sstevel@tonic-gate #include <sys/conf.h> 407c478bd9Sstevel@tonic-gate #include <sys/proc.h> 417c478bd9Sstevel@tonic-gate #include <sys/tty.h> 427c478bd9Sstevel@tonic-gate #include <sys/stream.h> 437c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 447c478bd9Sstevel@tonic-gate #include <sys/cred.h> 457c478bd9Sstevel@tonic-gate #include <sys/uio.h> 467c478bd9Sstevel@tonic-gate #include <sys/session.h> 477c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 487c478bd9Sstevel@tonic-gate #include <sys/debug.h> 497c478bd9Sstevel@tonic-gate #include <sys/stat.h> 507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 517c478bd9Sstevel@tonic-gate #include <sys/param.h> 527c478bd9Sstevel@tonic-gate #include <sys/systm.h> 537c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 547c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h> 557c478bd9Sstevel@tonic-gate #include <sys/file.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define IS_STREAM(dev) (devopsp[getmajor(dev)]->devo_cb_ops->cb_str != NULL) 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate int syopen(dev_t *, int, int, cred_t *); 607c478bd9Sstevel@tonic-gate int syclose(dev_t, int, int, cred_t *); 617c478bd9Sstevel@tonic-gate int syread(dev_t, struct uio *, cred_t *); 627c478bd9Sstevel@tonic-gate int sywrite(dev_t, struct uio *, cred_t *); 637c478bd9Sstevel@tonic-gate int sypoll(dev_t, short, int, short *, struct pollhead **); 647c478bd9Sstevel@tonic-gate int syioctl(dev_t, int, intptr_t, int, cred_t *, int *); 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static int sy_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 677c478bd9Sstevel@tonic-gate static int sy_attach(dev_info_t *, ddi_attach_cmd_t); 687c478bd9Sstevel@tonic-gate static dev_info_t *sy_dip; /* private copy of devinfo pointer */ 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate struct cb_ops sy_cb_ops = { 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate syopen, /* open */ 737c478bd9Sstevel@tonic-gate syclose, /* close */ 747c478bd9Sstevel@tonic-gate nodev, /* strategy */ 757c478bd9Sstevel@tonic-gate nodev, /* print */ 767c478bd9Sstevel@tonic-gate nodev, /* dump */ 777c478bd9Sstevel@tonic-gate syread, /* read */ 787c478bd9Sstevel@tonic-gate sywrite, /* write */ 797c478bd9Sstevel@tonic-gate syioctl, /* ioctl */ 807c478bd9Sstevel@tonic-gate nodev, /* devmap */ 817c478bd9Sstevel@tonic-gate nodev, /* mmap */ 827c478bd9Sstevel@tonic-gate nodev, /* segmap */ 837c478bd9Sstevel@tonic-gate sypoll, /* poll */ 847c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 857c478bd9Sstevel@tonic-gate 0, /* streamtab */ 867c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate }; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate struct dev_ops sy_ops = { 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 937c478bd9Sstevel@tonic-gate 0, /* refcnt */ 947c478bd9Sstevel@tonic-gate sy_info, /* info */ 957c478bd9Sstevel@tonic-gate nulldev, /* identify */ 967c478bd9Sstevel@tonic-gate nulldev, /* probe */ 977c478bd9Sstevel@tonic-gate sy_attach, /* attach */ 987c478bd9Sstevel@tonic-gate nodev, /* detach */ 997c478bd9Sstevel@tonic-gate nodev, /* reset */ 1007c478bd9Sstevel@tonic-gate &sy_cb_ops, /* driver operations */ 1017c478bd9Sstevel@tonic-gate (struct bus_ops *)0 /* bus operations */ 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate }; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate extern int nodev(void); 1077c478bd9Sstevel@tonic-gate extern int nulldev(void); 1087c478bd9Sstevel@tonic-gate extern int dseekneg_flag; 1097c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1107c478bd9Sstevel@tonic-gate extern struct dev_ops sy_ops; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1177c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */ 1187c478bd9Sstevel@tonic-gate "Indirect driver for tty 'sy' %I%", 1197c478bd9Sstevel@tonic-gate &sy_ops, /* driver ops */ 1207c478bd9Sstevel@tonic-gate }; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1237c478bd9Sstevel@tonic-gate MODREV_1, 1247c478bd9Sstevel@tonic-gate &modldrv, 1257c478bd9Sstevel@tonic-gate NULL 1267c478bd9Sstevel@tonic-gate }; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate int 1307c478bd9Sstevel@tonic-gate _init(void) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate int 1377c478bd9Sstevel@tonic-gate _fini(void) 1387c478bd9Sstevel@tonic-gate { 1397c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate int 1437c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1497c478bd9Sstevel@tonic-gate static int 1507c478bd9Sstevel@tonic-gate sy_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "tty", S_IFCHR, 1537c478bd9Sstevel@tonic-gate 0, DDI_PSEUDO, NULL) == DDI_FAILURE) { 1547c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 1557c478bd9Sstevel@tonic-gate return (-1); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate sy_dip = devi; 1587c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1627c478bd9Sstevel@tonic-gate static int 1637c478bd9Sstevel@tonic-gate sy_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1647c478bd9Sstevel@tonic-gate { 1657c478bd9Sstevel@tonic-gate dev_t dev = (dev_t)arg; 1667c478bd9Sstevel@tonic-gate int error; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate switch (infocmd) { 1697c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1707c478bd9Sstevel@tonic-gate if (sy_dip == NULL) { 1717c478bd9Sstevel@tonic-gate *result = (void *)NULL; 1727c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 1737c478bd9Sstevel@tonic-gate } else { 1747c478bd9Sstevel@tonic-gate *result = (void *) sy_dip; 1757c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate break; 1787c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1797c478bd9Sstevel@tonic-gate if (getminor(dev) != 0) { 1807c478bd9Sstevel@tonic-gate *result = (void *)-1; 1817c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 1827c478bd9Sstevel@tonic-gate } else { 1837c478bd9Sstevel@tonic-gate *result = (void *)0; 1847c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate break; 1877c478bd9Sstevel@tonic-gate default: 1887c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate return (error); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1957c478bd9Sstevel@tonic-gate int 1967c478bd9Sstevel@tonic-gate syopen(dev_t *devp, int flag, int otyp, struct cred *cr) 1977c478bd9Sstevel@tonic-gate { 1987c478bd9Sstevel@tonic-gate dev_t ttyd; 1997c478bd9Sstevel@tonic-gate vnode_t *ttyvp; 200*9acbbeafSnn35248 sess_t *sp; 2017c478bd9Sstevel@tonic-gate int error; 2027c478bd9Sstevel@tonic-gate 203*9acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 204*9acbbeafSnn35248 return (EINTR); 205*9acbbeafSnn35248 206*9acbbeafSnn35248 if (sp->s_dev == NODEV) { 207*9acbbeafSnn35248 tty_rele(sp); 2087c478bd9Sstevel@tonic-gate return (ENXIO); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 211*9acbbeafSnn35248 ttyd = sp->s_dev; 212*9acbbeafSnn35248 ttyvp = sp->s_vp; 213*9acbbeafSnn35248 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * Open the control terminal. The control terminal may be 2167c478bd9Sstevel@tonic-gate * opened multiple times and it is closed in freectty(). 2177c478bd9Sstevel@tonic-gate * The multi-open, single-clone means that no cloning 2187c478bd9Sstevel@tonic-gate * can happen via this open, hence the assertion. 2197c478bd9Sstevel@tonic-gate */ 2207c478bd9Sstevel@tonic-gate error = VOP_OPEN(&ttyvp, FNOCTTY | flag, cr); 2217c478bd9Sstevel@tonic-gate if (error == 0) { 2227c478bd9Sstevel@tonic-gate struct snode *csp; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * XXX: This driver binds a single minor number to the 2267c478bd9Sstevel@tonic-gate * current controlling tty of the process issueing the 2277c478bd9Sstevel@tonic-gate * open / close. If we implement a traditional close 2287c478bd9Sstevel@tonic-gate * for this driver then specfs will only invoke this driver 2297c478bd9Sstevel@tonic-gate * on the last close of our one minor number - which is not 2307c478bd9Sstevel@tonic-gate * what we want. Since we already get the open / close 2317c478bd9Sstevel@tonic-gate * semantic that we want from makectty and freectty, we reach 2327c478bd9Sstevel@tonic-gate * back into the common snode and decrease the open count so 2337c478bd9Sstevel@tonic-gate * that the specfs filtering of all but the last close 2347c478bd9Sstevel@tonic-gate * does not get in our way. To clean this up, a new cb_flag 2357c478bd9Sstevel@tonic-gate * that causes specfs to call the driver on each close 2367c478bd9Sstevel@tonic-gate * should be considered. 2377c478bd9Sstevel@tonic-gate */ 2387c478bd9Sstevel@tonic-gate ASSERT(ttyd == ttyvp->v_rdev); 2397c478bd9Sstevel@tonic-gate ASSERT(vn_matchops(ttyvp, spec_getvnodeops())); 2407c478bd9Sstevel@tonic-gate csp = VTOS(VTOS(ttyvp)->s_commonvp); 2417c478bd9Sstevel@tonic-gate mutex_enter(&csp->s_lock); 242*9acbbeafSnn35248 ASSERT(csp->s_count > 1); 2437c478bd9Sstevel@tonic-gate csp->s_count--; 2447c478bd9Sstevel@tonic-gate mutex_exit(&csp->s_lock); 2457c478bd9Sstevel@tonic-gate } 246*9acbbeafSnn35248 247*9acbbeafSnn35248 tty_rele(sp); 2487c478bd9Sstevel@tonic-gate return (error); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2527c478bd9Sstevel@tonic-gate int 2537c478bd9Sstevel@tonic-gate syclose(dev_t dev, int flag, int otyp, struct cred *cr) 2547c478bd9Sstevel@tonic-gate { 2557c478bd9Sstevel@tonic-gate return (0); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2597c478bd9Sstevel@tonic-gate int 2607c478bd9Sstevel@tonic-gate syread(dev_t dev, struct uio *uiop, struct cred *cr) 2617c478bd9Sstevel@tonic-gate { 262*9acbbeafSnn35248 sess_t *sp; 2637c478bd9Sstevel@tonic-gate int error; 2647c478bd9Sstevel@tonic-gate 265*9acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 266*9acbbeafSnn35248 return (EINTR); 2677c478bd9Sstevel@tonic-gate 268*9acbbeafSnn35248 if (sp->s_dev == NODEV) { 269*9acbbeafSnn35248 tty_rele(sp); 270*9acbbeafSnn35248 return (ENXIO); 271*9acbbeafSnn35248 } 272*9acbbeafSnn35248 273*9acbbeafSnn35248 error = VOP_READ(sp->s_vp, uiop, 0, cr, NULL); 274*9acbbeafSnn35248 275*9acbbeafSnn35248 tty_rele(sp); 276*9acbbeafSnn35248 return (error); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2807c478bd9Sstevel@tonic-gate int 2817c478bd9Sstevel@tonic-gate sywrite(dev_t dev, struct uio *uiop, struct cred *cr) 2827c478bd9Sstevel@tonic-gate { 283*9acbbeafSnn35248 sess_t *sp; 2847c478bd9Sstevel@tonic-gate int error; 2857c478bd9Sstevel@tonic-gate 286*9acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 287*9acbbeafSnn35248 return (EINTR); 288*9acbbeafSnn35248 289*9acbbeafSnn35248 if (sp->s_dev == NODEV) { 290*9acbbeafSnn35248 tty_rele(sp); 2917c478bd9Sstevel@tonic-gate return (ENXIO); 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate 294*9acbbeafSnn35248 error = VOP_WRITE(sp->s_vp, uiop, 0, cr, NULL); 295*9acbbeafSnn35248 296*9acbbeafSnn35248 tty_rele(sp); 2977c478bd9Sstevel@tonic-gate return (error); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3027c478bd9Sstevel@tonic-gate int 3037c478bd9Sstevel@tonic-gate syioctl(dev_t dev, int cmd, intptr_t arg, int mode, struct cred *cr, 3047c478bd9Sstevel@tonic-gate int *rvalp) 3057c478bd9Sstevel@tonic-gate { 306*9acbbeafSnn35248 sess_t *sp; 3077c478bd9Sstevel@tonic-gate int error; 3087c478bd9Sstevel@tonic-gate 309*9acbbeafSnn35248 if (cmd == TIOCNOTTY) { 310*9acbbeafSnn35248 /* 311*9acbbeafSnn35248 * we can't allow this ioctl. the reason is that it 312*9acbbeafSnn35248 * attempts to remove the ctty for a session. to do 313*9acbbeafSnn35248 * this the ctty can't be in use but we grab a hold on 314*9acbbeafSnn35248 * the current ctty (via tty_hold) to perform this ioctl. 315*9acbbeafSnn35248 * if we were to allow this ioctl to pass through we 316*9acbbeafSnn35248 * would deadlock with ourselves. 317*9acbbeafSnn35248 */ 318*9acbbeafSnn35248 return (EINVAL); 3197c478bd9Sstevel@tonic-gate } 320*9acbbeafSnn35248 321*9acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 322*9acbbeafSnn35248 return (EINTR); 323*9acbbeafSnn35248 324*9acbbeafSnn35248 if (sp->s_dev == NODEV) { 325*9acbbeafSnn35248 tty_rele(sp); 326*9acbbeafSnn35248 return (ENXIO); 327*9acbbeafSnn35248 } 328*9acbbeafSnn35248 329*9acbbeafSnn35248 error = VOP_IOCTL(sp->s_vp, cmd, arg, mode, cr, rvalp); 330*9acbbeafSnn35248 331*9acbbeafSnn35248 tty_rele(sp); 3327c478bd9Sstevel@tonic-gate return (error); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3387c478bd9Sstevel@tonic-gate int 3397c478bd9Sstevel@tonic-gate sypoll(dev_t dev, short events, int anyyet, short *reventsp, 3407c478bd9Sstevel@tonic-gate struct pollhead **phpp) 3417c478bd9Sstevel@tonic-gate { 342*9acbbeafSnn35248 sess_t *sp; 3437c478bd9Sstevel@tonic-gate int error; 3447c478bd9Sstevel@tonic-gate 345*9acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 346*9acbbeafSnn35248 return (EINTR); 347*9acbbeafSnn35248 348*9acbbeafSnn35248 if (sp->s_dev == NODEV) { 349*9acbbeafSnn35248 tty_rele(sp); 3507c478bd9Sstevel@tonic-gate return (ENXIO); 3517c478bd9Sstevel@tonic-gate } 352*9acbbeafSnn35248 353*9acbbeafSnn35248 error = VOP_POLL(sp->s_vp, events, anyyet, reventsp, phpp); 354*9acbbeafSnn35248 355*9acbbeafSnn35248 tty_rele(sp); 3567c478bd9Sstevel@tonic-gate return (error); 3577c478bd9Sstevel@tonic-gate } 358