10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*560Smeem * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate 310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <sys/sysmacros.h> 350Sstevel@tonic-gate #include <sys/param.h> 360Sstevel@tonic-gate #include <sys/systm.h> 370Sstevel@tonic-gate #include <sys/file.h> 380Sstevel@tonic-gate #include <sys/vnode.h> 390Sstevel@tonic-gate #include <sys/errno.h> 400Sstevel@tonic-gate #include <sys/signal.h> 410Sstevel@tonic-gate #include <sys/cred.h> 420Sstevel@tonic-gate #include <sys/policy.h> 430Sstevel@tonic-gate #include <sys/conf.h> 440Sstevel@tonic-gate #include <sys/debug.h> 450Sstevel@tonic-gate #include <sys/proc.h> 460Sstevel@tonic-gate #include <sys/session.h> 470Sstevel@tonic-gate #include <sys/kmem.h> 480Sstevel@tonic-gate #include <sys/cmn_err.h> 490Sstevel@tonic-gate #include <sys/strsubr.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate sess_t session0 = { 520Sstevel@tonic-gate 1, /* s_ref */ 530Sstevel@tonic-gate NODEV, /* s_dev */ 540Sstevel@tonic-gate NULL, /* s_vp */ 550Sstevel@tonic-gate &pid0, /* s_sidp */ 560Sstevel@tonic-gate NULL /* s_cred */ 570Sstevel@tonic-gate }; 580Sstevel@tonic-gate 590Sstevel@tonic-gate void 600Sstevel@tonic-gate sess_rele(sess_t *sp) 610Sstevel@tonic-gate { 620Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 630Sstevel@tonic-gate 640Sstevel@tonic-gate ASSERT(sp->s_ref != 0); 650Sstevel@tonic-gate if (--sp->s_ref == 0) { 660Sstevel@tonic-gate if (sp == &session0) 670Sstevel@tonic-gate panic("sp == &session0"); 680Sstevel@tonic-gate PID_RELE(sp->s_sidp); 690Sstevel@tonic-gate mutex_destroy(&sp->s_lock); 700Sstevel@tonic-gate cv_destroy(&sp->s_wait_cv); 710Sstevel@tonic-gate kmem_free(sp, sizeof (sess_t)); 720Sstevel@tonic-gate } 730Sstevel@tonic-gate } 740Sstevel@tonic-gate 750Sstevel@tonic-gate void 760Sstevel@tonic-gate sess_create(void) 770Sstevel@tonic-gate { 780Sstevel@tonic-gate proc_t *pp; 790Sstevel@tonic-gate sess_t *sp; 800Sstevel@tonic-gate 810Sstevel@tonic-gate pp = ttoproc(curthread); 820Sstevel@tonic-gate 830Sstevel@tonic-gate sp = kmem_zalloc(sizeof (sess_t), KM_SLEEP); 840Sstevel@tonic-gate 850Sstevel@tonic-gate mutex_init(&sp->s_lock, NULL, MUTEX_DEFAULT, NULL); 860Sstevel@tonic-gate cv_init(&sp->s_wait_cv, NULL, CV_DEFAULT, NULL); 870Sstevel@tonic-gate 880Sstevel@tonic-gate mutex_enter(&pidlock); 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * We need to protect p_pgidp with p_lock because 920Sstevel@tonic-gate * /proc looks at it while holding only p_lock. 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate mutex_enter(&pp->p_lock); 950Sstevel@tonic-gate pgexit(pp); 960Sstevel@tonic-gate SESS_RELE(pp->p_sessp); 970Sstevel@tonic-gate 980Sstevel@tonic-gate sp->s_sidp = pp->p_pidp; 990Sstevel@tonic-gate sp->s_ref = 1; 1000Sstevel@tonic-gate sp->s_dev = NODEV; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate pp->p_sessp = sp; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate pgjoin(pp, pp->p_pidp); 1050Sstevel@tonic-gate mutex_exit(&pp->p_lock); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate PID_HOLD(sp->s_sidp); 1080Sstevel@tonic-gate mutex_exit(&pidlock); 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate void 1120Sstevel@tonic-gate freectty(sess_t *sp) 1130Sstevel@tonic-gate { 114*560Smeem vnode_t *vp = sp->s_vp; 115*560Smeem cred_t *cred = sp->s_cred; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate strfreectty(vp->v_stream); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate mutex_enter(&sp->s_lock); 1200Sstevel@tonic-gate while (sp->s_cnt > 0) { 1210Sstevel@tonic-gate cv_wait(&sp->s_wait_cv, &sp->s_lock); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate ASSERT(sp->s_cnt == 0); 1240Sstevel@tonic-gate ASSERT(vp->v_count >= 1); 1250Sstevel@tonic-gate sp->s_vp = NULL; 126*560Smeem sp->s_cred = NULL; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 129*560Smeem * It is possible for the VOP_CLOSE below to call stralloctty() 1300Sstevel@tonic-gate * and reallocate a new tty vnode. To prevent that the 1310Sstevel@tonic-gate * session is marked as closing here. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate sp->s_flag = SESS_CLOSE; 1350Sstevel@tonic-gate mutex_exit(&sp->s_lock); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * This will be the only thread with access to 1390Sstevel@tonic-gate * this vnode, from this point on. 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate (void) VOP_CLOSE(vp, 0, 1, (offset_t)0, cred); 1430Sstevel@tonic-gate VN_RELE(vp); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate crfree(cred); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * ++++++++++++++++++++++++ 1500Sstevel@tonic-gate * ++ SunOS4.1 Buyback ++ 1510Sstevel@tonic-gate * ++++++++++++++++++++++++ 1520Sstevel@tonic-gate * 1530Sstevel@tonic-gate * vhangup: Revoke access of the current tty by all processes 1540Sstevel@tonic-gate * Used by privileged users to give a "clean" terminal at login 1550Sstevel@tonic-gate */ 1560Sstevel@tonic-gate int 157*560Smeem vhangup(void) 1580Sstevel@tonic-gate { 1590Sstevel@tonic-gate if (secpolicy_sys_config(CRED(), B_FALSE) != 0) 1600Sstevel@tonic-gate return (set_errno(EPERM)); 1610Sstevel@tonic-gate /* 1620Sstevel@tonic-gate * This routine used to call freectty() under a condition that 1630Sstevel@tonic-gate * could never happen. So this code has never actually done 164*560Smeem * anything, and evidently nobody has ever noticed. 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate return (0); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate dev_t 1700Sstevel@tonic-gate cttydev(proc_t *pp) 1710Sstevel@tonic-gate { 1720Sstevel@tonic-gate sess_t *sp = pp->p_sessp; 1730Sstevel@tonic-gate if (sp->s_vp == NULL) 1740Sstevel@tonic-gate return (NODEV); 1750Sstevel@tonic-gate return (sp->s_dev); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate void 1790Sstevel@tonic-gate alloctty(proc_t *pp, vnode_t *vp) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate sess_t *sp = pp->p_sessp; 1820Sstevel@tonic-gate cred_t *crp; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate sp->s_vp = vp; 1850Sstevel@tonic-gate sp->s_dev = vp->v_rdev; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 1880Sstevel@tonic-gate crhold(crp = pp->p_cred); 1890Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 1900Sstevel@tonic-gate sp->s_cred = crp; 1910Sstevel@tonic-gate } 192