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. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #ifndef _SYS_SESSION_H 317c478bd9Sstevel@tonic-gate #define _SYS_SESSION_H 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #ifdef __cplusplus 367c478bd9Sstevel@tonic-gate extern "C" { 377c478bd9Sstevel@tonic-gate #endif 387c478bd9Sstevel@tonic-gate 39*9acbbeafSnn35248 /* 40*9acbbeafSnn35248 * Session structure overview. 41*9acbbeafSnn35248 * 42*9acbbeafSnn35248 * Currently, the only structure in the kernel which has a pointer to a 43*9acbbeafSnn35248 * session structures is the proc_t via the p_sessp pointer. To 44*9acbbeafSnn35248 * access a session proc_t->p_sessp pointer a caller must hold either 45*9acbbeafSnn35248 * pidlock or p_splock. These locks only protect the p_sessp pointer 46*9acbbeafSnn35248 * itself and do not protect any of the contents of the session structure. 47*9acbbeafSnn35248 * To prevent the contents of a the session structure from changing the 48*9acbbeafSnn35248 * caller must grab s_lock. 49*9acbbeafSnn35248 * 50*9acbbeafSnn35248 * No callers should ever update the contents of the session structure 51*9acbbeafSnn35248 * directly. Only the session management code should ever modify the 52*9acbbeafSnn35248 * contents of the session structure. When the session code attempts 53*9acbbeafSnn35248 * to modify the contents of a session structure it must hold multiple 54*9acbbeafSnn35248 * locks. The locking order for all the locks that may need to be 55*9acbbeafSnn35248 * acquired is: 56*9acbbeafSnn35248 * sd_lock -> pidlock -> p_splock -> s_lock 57*9acbbeafSnn35248 * 58*9acbbeafSnn35248 * If a caller requires access to a session structure for long 59*9acbbeafSnn35248 * periods of time or across operations that may block it should 60*9acbbeafSnn35248 * use the tty_hold() and sess_hold() interfaces. 61*9acbbeafSnn35248 * 62*9acbbeafSnn35248 * sess_hold() returns a pointer to a session structure associated 63*9acbbeafSnn35248 * with the proc_t that was passed in. It also increments the reference 64*9acbbeafSnn35248 * count associated with that session structure to ensure that it 65*9acbbeafSnn35248 * can't be freed until after the caller is done with it and calls 66*9acbbeafSnn35248 * sess_rele(). This hold doesn't actually protect any of the 67*9acbbeafSnn35248 * contents of the session structure. 68*9acbbeafSnn35248 * 69*9acbbeafSnn35248 * tty_hold() returns a pointer to a session structure associated 70*9acbbeafSnn35248 * with the curproc. It also "locks" the contents of the session 71*9acbbeafSnn35248 * structure. This hold should be used when the caller will be 72*9acbbeafSnn35248 * doing operations on a controlling tty associated with the session. 73*9acbbeafSnn35248 * This operation doesn an implicit sess_hold() so that the session 74*9acbbeafSnn35248 * structure can't be free'd until after the caller is done with it 75*9acbbeafSnn35248 * and invokes tty_rele(). 76*9acbbeafSnn35248 * 77*9acbbeafSnn35248 * NOTE: Neither of these functions (sess_hold() or tty_hold()) 78*9acbbeafSnn35248 * prevent a process from changing its session. Once these functions 79*9acbbeafSnn35248 * return a session pointer, that session pointer may no longer be 80*9acbbeafSnn35248 * associated with the current process. If a caller wants to prevent 81*9acbbeafSnn35248 * a process from changing its session then it must hold pidlock or 82*9acbbeafSnn35248 * p_splock. 83*9acbbeafSnn35248 */ 84*9acbbeafSnn35248 857c478bd9Sstevel@tonic-gate typedef struct sess { 86*9acbbeafSnn35248 struct pid *s_sidp; /* session ID info, never changes */ 87*9acbbeafSnn35248 88*9acbbeafSnn35248 kmutex_t s_lock; /* protects everything below */ 897c478bd9Sstevel@tonic-gate uint_t s_ref; /* reference count */ 90*9acbbeafSnn35248 boolean_t s_sighuped; /* ctty had sighup sent to it */ 91*9acbbeafSnn35248 92*9acbbeafSnn35248 boolean_t s_exit; /* sesion leader is exiting */ 93*9acbbeafSnn35248 kcondvar_t s_exit_cv; /* Condvar for s_exit */ 94*9acbbeafSnn35248 95*9acbbeafSnn35248 int s_cnt; /* active users of this ctty */ 96*9acbbeafSnn35248 kcondvar_t s_cnt_cv; /* Condvar for s_cnt */ 97*9acbbeafSnn35248 98*9acbbeafSnn35248 /* 99*9acbbeafSnn35248 * The following fields can only be updated while s_lock is held 100*9acbbeafSnn35248 * and s_cnt is 0. (ie, no one has a tty_hold() on this session.) 101*9acbbeafSnn35248 */ 1027c478bd9Sstevel@tonic-gate dev_t s_dev; /* tty's device number */ 1037c478bd9Sstevel@tonic-gate struct vnode *s_vp; /* tty's vnode */ 1047c478bd9Sstevel@tonic-gate struct cred *s_cred; /* allocation credentials */ 1057c478bd9Sstevel@tonic-gate } sess_t; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate #define s_sid s_sidp->pid_id 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate #if defined(_KERNEL) 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate extern sess_t session0; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* forward referenced structure tags */ 1147c478bd9Sstevel@tonic-gate struct vnode; 1157c478bd9Sstevel@tonic-gate struct proc; 116*9acbbeafSnn35248 struct stdata; 1177c478bd9Sstevel@tonic-gate 118*9acbbeafSnn35248 extern void sess_hold(proc_t *p); 119*9acbbeafSnn35248 extern void sess_rele(sess_t *, boolean_t); 120*9acbbeafSnn35248 extern sess_t *tty_hold(void); 121*9acbbeafSnn35248 extern void tty_rele(sess_t *sp); 122*9acbbeafSnn35248 123*9acbbeafSnn35248 1247c478bd9Sstevel@tonic-gate extern void sess_create(void); 125*9acbbeafSnn35248 extern int strctty(struct stdata *); 126*9acbbeafSnn35248 extern int freectty(boolean_t); 1277c478bd9Sstevel@tonic-gate extern dev_t cttydev(struct proc *); 128*9acbbeafSnn35248 extern void ctty_clear_sighuped(void); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #endif /* defined(_KERNEL) */ 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate #endif 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate #endif /* _SYS_SESSION_H */ 137