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 59acbbeafSnn * Common Development and Distribution License (the "License"). 69acbbeafSnn * 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 /* 229acbbeafSnn * 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 */ 27*b4203d75SMarcel Telka /* 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 #ifdef __cplusplus 347c478bd9Sstevel@tonic-gate extern "C" { 357c478bd9Sstevel@tonic-gate #endif 367c478bd9Sstevel@tonic-gate 379acbbeafSnn /* 389acbbeafSnn * Session structure overview. 399acbbeafSnn * 409acbbeafSnn * Currently, the only structure in the kernel which has a pointer to a 419acbbeafSnn * session structures is the proc_t via the p_sessp pointer. To 429acbbeafSnn * access a session proc_t->p_sessp pointer a caller must hold either 439acbbeafSnn * pidlock or p_splock. These locks only protect the p_sessp pointer 449acbbeafSnn * itself and do not protect any of the contents of the session structure. 459acbbeafSnn * To prevent the contents of a the session structure from changing the 469acbbeafSnn * caller must grab s_lock. 479acbbeafSnn * 489acbbeafSnn * No callers should ever update the contents of the session structure 499acbbeafSnn * directly. Only the session management code should ever modify the 509acbbeafSnn * contents of the session structure. When the session code attempts 519acbbeafSnn * to modify the contents of a session structure it must hold multiple 529acbbeafSnn * locks. The locking order for all the locks that may need to be 539acbbeafSnn * acquired is: 549acbbeafSnn * sd_lock -> pidlock -> p_splock -> s_lock 559acbbeafSnn * 569acbbeafSnn * If a caller requires access to a session structure for long 579acbbeafSnn * periods of time or across operations that may block it should 589acbbeafSnn * use the tty_hold() and sess_hold() interfaces. 599acbbeafSnn * 609acbbeafSnn * sess_hold() returns a pointer to a session structure associated 619acbbeafSnn * with the proc_t that was passed in. It also increments the reference 629acbbeafSnn * count associated with that session structure to ensure that it 639acbbeafSnn * can't be freed until after the caller is done with it and calls 649acbbeafSnn * sess_rele(). This hold doesn't actually protect any of the 659acbbeafSnn * contents of the session structure. 669acbbeafSnn * 679acbbeafSnn * tty_hold() returns a pointer to a session structure associated 689acbbeafSnn * with the curproc. It also "locks" the contents of the session 699acbbeafSnn * structure. This hold should be used when the caller will be 709acbbeafSnn * doing operations on a controlling tty associated with the session. 719acbbeafSnn * This operation doesn an implicit sess_hold() so that the session 729acbbeafSnn * structure can't be free'd until after the caller is done with it 739acbbeafSnn * and invokes tty_rele(). 749acbbeafSnn * 759acbbeafSnn * NOTE: Neither of these functions (sess_hold() or tty_hold()) 769acbbeafSnn * prevent a process from changing its session. Once these functions 779acbbeafSnn * return a session pointer, that session pointer may no longer be 789acbbeafSnn * associated with the current process. If a caller wants to prevent 799acbbeafSnn * a process from changing its session then it must hold pidlock or 809acbbeafSnn * p_splock. 819acbbeafSnn */ 829acbbeafSnn 837c478bd9Sstevel@tonic-gate typedef struct sess { 849acbbeafSnn struct pid *s_sidp; /* session ID info, never changes */ 857c478bd9Sstevel@tonic-gate 869acbbeafSnn kmutex_t s_lock; /* protects everything below */ 879acbbeafSnn uint_t s_ref; /* reference count */ 889acbbeafSnn boolean_t s_sighuped; /* ctty had sighup sent to it */ 897c478bd9Sstevel@tonic-gate 909acbbeafSnn boolean_t s_exit; /* sesion leader is exiting */ 919acbbeafSnn kcondvar_t s_exit_cv; /* Condvar for s_exit */ 927c478bd9Sstevel@tonic-gate 939acbbeafSnn int s_cnt; /* active users of this ctty */ 949acbbeafSnn kcondvar_t s_cnt_cv; /* Condvar for s_cnt */ 957c478bd9Sstevel@tonic-gate 969acbbeafSnn /* 979acbbeafSnn * The following fields can only be updated while s_lock is held 989acbbeafSnn * and s_cnt is 0. (ie, no one has a tty_hold() on this session.) 999acbbeafSnn */ 1009acbbeafSnn dev_t s_dev; /* tty's device number */ 1019acbbeafSnn struct vnode *s_vp; /* tty's vnode */ 1029acbbeafSnn struct cred *s_cred; /* allocation credentials */ 1039acbbeafSnn } sess_t; 1047c478bd9Sstevel@tonic-gate 1059acbbeafSnn #define s_sid s_sidp->pid_id 1067c478bd9Sstevel@tonic-gate 1079acbbeafSnn #if defined(_KERNEL) 1087c478bd9Sstevel@tonic-gate 1099acbbeafSnn extern sess_t session0; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* forward referenced structure tags */ 1127c478bd9Sstevel@tonic-gate struct vnode; 1137c478bd9Sstevel@tonic-gate struct proc; 1149acbbeafSnn struct stdata; 1159acbbeafSnn 1169acbbeafSnn extern void sess_hold(proc_t *p); 1179acbbeafSnn extern void sess_rele(sess_t *, boolean_t); 1189acbbeafSnn extern sess_t *tty_hold(void); 1199acbbeafSnn extern void tty_rele(sess_t *sp); 1209acbbeafSnn 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate extern void sess_create(void); 1239acbbeafSnn extern int strctty(struct stdata *); 1249acbbeafSnn extern int freectty(boolean_t); 1257c478bd9Sstevel@tonic-gate extern dev_t cttydev(struct proc *); 1269acbbeafSnn extern void ctty_clear_sighuped(void); 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate #endif /* defined(_KERNEL) */ 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate #endif 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate #endif /* _SYS_SESSION_H */ 135