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 59acbbeafSnn35248 * Common Development and Distribution License (the "License"). 69acbbeafSnn35248 * 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 */ 21*19397407SSherry Moore 227c478bd9Sstevel@tonic-gate /* 23*19397407SSherry 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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* from S5R4 1.22 */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * Indirect driver for controlling tty. 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/errno.h> 387c478bd9Sstevel@tonic-gate #include <sys/conf.h> 397c478bd9Sstevel@tonic-gate #include <sys/proc.h> 407c478bd9Sstevel@tonic-gate #include <sys/tty.h> 417c478bd9Sstevel@tonic-gate #include <sys/stream.h> 427c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 437c478bd9Sstevel@tonic-gate #include <sys/cred.h> 447c478bd9Sstevel@tonic-gate #include <sys/uio.h> 457c478bd9Sstevel@tonic-gate #include <sys/session.h> 467c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 477c478bd9Sstevel@tonic-gate #include <sys/debug.h> 487c478bd9Sstevel@tonic-gate #include <sys/stat.h> 497c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 507c478bd9Sstevel@tonic-gate #include <sys/param.h> 517c478bd9Sstevel@tonic-gate #include <sys/systm.h> 527c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 537c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h> 547c478bd9Sstevel@tonic-gate #include <sys/file.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #define IS_STREAM(dev) (devopsp[getmajor(dev)]->devo_cb_ops->cb_str != NULL) 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate int syopen(dev_t *, int, int, cred_t *); 597c478bd9Sstevel@tonic-gate int syclose(dev_t, int, int, cred_t *); 607c478bd9Sstevel@tonic-gate int syread(dev_t, struct uio *, cred_t *); 617c478bd9Sstevel@tonic-gate int sywrite(dev_t, struct uio *, cred_t *); 627c478bd9Sstevel@tonic-gate int sypoll(dev_t, short, int, short *, struct pollhead **); 637c478bd9Sstevel@tonic-gate int syioctl(dev_t, int, intptr_t, int, cred_t *, int *); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate static int sy_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 667c478bd9Sstevel@tonic-gate static int sy_attach(dev_info_t *, ddi_attach_cmd_t); 677c478bd9Sstevel@tonic-gate static dev_info_t *sy_dip; /* private copy of devinfo pointer */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate struct cb_ops sy_cb_ops = { 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate syopen, /* open */ 727c478bd9Sstevel@tonic-gate syclose, /* close */ 737c478bd9Sstevel@tonic-gate nodev, /* strategy */ 747c478bd9Sstevel@tonic-gate nodev, /* print */ 757c478bd9Sstevel@tonic-gate nodev, /* dump */ 767c478bd9Sstevel@tonic-gate syread, /* read */ 777c478bd9Sstevel@tonic-gate sywrite, /* write */ 787c478bd9Sstevel@tonic-gate syioctl, /* ioctl */ 797c478bd9Sstevel@tonic-gate nodev, /* devmap */ 807c478bd9Sstevel@tonic-gate nodev, /* mmap */ 817c478bd9Sstevel@tonic-gate nodev, /* segmap */ 827c478bd9Sstevel@tonic-gate sypoll, /* poll */ 837c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 847c478bd9Sstevel@tonic-gate 0, /* streamtab */ 857c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate }; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate struct dev_ops sy_ops = { 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 927c478bd9Sstevel@tonic-gate 0, /* refcnt */ 937c478bd9Sstevel@tonic-gate sy_info, /* info */ 947c478bd9Sstevel@tonic-gate nulldev, /* identify */ 957c478bd9Sstevel@tonic-gate nulldev, /* probe */ 967c478bd9Sstevel@tonic-gate sy_attach, /* attach */ 977c478bd9Sstevel@tonic-gate nodev, /* detach */ 987c478bd9Sstevel@tonic-gate nodev, /* reset */ 997c478bd9Sstevel@tonic-gate &sy_cb_ops, /* driver operations */ 100*19397407SSherry Moore (struct bus_ops *)0, /* bus operations */ 101*19397407SSherry Moore NULL, /* power */ 102*19397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 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 */ 118*19397407SSherry Moore "Indirect driver for tty 'sy'", 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; 2009acbbeafSnn35248 sess_t *sp; 2017c478bd9Sstevel@tonic-gate int error; 2027c478bd9Sstevel@tonic-gate 2039acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 2049acbbeafSnn35248 return (EINTR); 2059acbbeafSnn35248 2069acbbeafSnn35248 if (sp->s_dev == NODEV) { 2079acbbeafSnn35248 tty_rele(sp); 2087c478bd9Sstevel@tonic-gate return (ENXIO); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2119acbbeafSnn35248 ttyd = sp->s_dev; 2129acbbeafSnn35248 ttyvp = sp->s_vp; 2139acbbeafSnn35248 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 */ 220da6c28aaSamw error = VOP_OPEN(&ttyvp, FNOCTTY | flag, cr, NULL); 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); 2429acbbeafSnn35248 ASSERT(csp->s_count > 1); 2437c478bd9Sstevel@tonic-gate csp->s_count--; 2447c478bd9Sstevel@tonic-gate mutex_exit(&csp->s_lock); 2457c478bd9Sstevel@tonic-gate } 2469acbbeafSnn35248 2479acbbeafSnn35248 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 { 2629acbbeafSnn35248 sess_t *sp; 2637c478bd9Sstevel@tonic-gate int error; 2647c478bd9Sstevel@tonic-gate 2659acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 2669acbbeafSnn35248 return (EINTR); 2677c478bd9Sstevel@tonic-gate 2689acbbeafSnn35248 if (sp->s_dev == NODEV) { 2699acbbeafSnn35248 tty_rele(sp); 2709acbbeafSnn35248 return (ENXIO); 2719acbbeafSnn35248 } 2729acbbeafSnn35248 2739acbbeafSnn35248 error = VOP_READ(sp->s_vp, uiop, 0, cr, NULL); 2749acbbeafSnn35248 2759acbbeafSnn35248 tty_rele(sp); 2769acbbeafSnn35248 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 { 2839acbbeafSnn35248 sess_t *sp; 2847c478bd9Sstevel@tonic-gate int error; 2857c478bd9Sstevel@tonic-gate 2869acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 2879acbbeafSnn35248 return (EINTR); 2889acbbeafSnn35248 2899acbbeafSnn35248 if (sp->s_dev == NODEV) { 2909acbbeafSnn35248 tty_rele(sp); 2917c478bd9Sstevel@tonic-gate return (ENXIO); 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate 2949acbbeafSnn35248 error = VOP_WRITE(sp->s_vp, uiop, 0, cr, NULL); 2959acbbeafSnn35248 2969acbbeafSnn35248 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 { 3069acbbeafSnn35248 sess_t *sp; 3077c478bd9Sstevel@tonic-gate int error; 3087c478bd9Sstevel@tonic-gate 3099acbbeafSnn35248 if (cmd == TIOCNOTTY) { 3109acbbeafSnn35248 /* 3119acbbeafSnn35248 * we can't allow this ioctl. the reason is that it 3129acbbeafSnn35248 * attempts to remove the ctty for a session. to do 3139acbbeafSnn35248 * this the ctty can't be in use but we grab a hold on 3149acbbeafSnn35248 * the current ctty (via tty_hold) to perform this ioctl. 3159acbbeafSnn35248 * if we were to allow this ioctl to pass through we 3169acbbeafSnn35248 * would deadlock with ourselves. 3179acbbeafSnn35248 */ 3189acbbeafSnn35248 return (EINVAL); 3197c478bd9Sstevel@tonic-gate } 3209acbbeafSnn35248 3219acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 3229acbbeafSnn35248 return (EINTR); 3239acbbeafSnn35248 3249acbbeafSnn35248 if (sp->s_dev == NODEV) { 3259acbbeafSnn35248 tty_rele(sp); 3269acbbeafSnn35248 return (ENXIO); 3279acbbeafSnn35248 } 3289acbbeafSnn35248 329da6c28aaSamw error = VOP_IOCTL(sp->s_vp, cmd, arg, mode, cr, rvalp, NULL); 3309acbbeafSnn35248 3319acbbeafSnn35248 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 { 3429acbbeafSnn35248 sess_t *sp; 3437c478bd9Sstevel@tonic-gate int error; 3447c478bd9Sstevel@tonic-gate 3459acbbeafSnn35248 if ((sp = tty_hold()) == NULL) 3469acbbeafSnn35248 return (EINTR); 3479acbbeafSnn35248 3489acbbeafSnn35248 if (sp->s_dev == NODEV) { 3499acbbeafSnn35248 tty_rele(sp); 3507c478bd9Sstevel@tonic-gate return (ENXIO); 3517c478bd9Sstevel@tonic-gate } 3529acbbeafSnn35248 353da6c28aaSamw error = VOP_POLL(sp->s_vp, events, anyyet, reventsp, phpp, NULL); 3549acbbeafSnn35248 3559acbbeafSnn35248 tty_rele(sp); 3567c478bd9Sstevel@tonic-gate return (error); 3577c478bd9Sstevel@tonic-gate } 358