xref: /illumos-gate/usr/src/uts/common/sys/session.h (revision b4203d75)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved	*/
28 
29 
30 #ifndef _SYS_SESSION_H
31 #define	_SYS_SESSION_H
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 /*
38  * Session structure overview.
39  *
40  * Currently, the only structure in the kernel which has a pointer to a
41  * session structures is the proc_t via the p_sessp pointer.  To
42  * access a session proc_t->p_sessp pointer a caller must hold either
43  * pidlock or p_splock.  These locks only protect the p_sessp pointer
44  * itself and do not protect any of the contents of the session structure.
45  * To prevent the contents of a the session structure from changing the
46  * caller must grab s_lock.
47  *
48  * No callers should ever update the contents of the session structure
49  * directly.  Only the session management code should ever modify the
50  * contents of the session structure.  When the session code attempts
51  * to modify the contents of a session structure it must hold multiple
52  * locks.  The locking order for all the locks that may need to be
53  * acquired is:
54  * 	sd_lock -> pidlock -> p_splock -> s_lock
55  *
56  * If a caller requires access to a session structure for long
57  * periods of time or across operations that may block it should
58  * use the tty_hold() and sess_hold() interfaces.
59  *
60  * sess_hold() returns a pointer to a session structure associated
61  * with the proc_t that was passed in.  It also increments the reference
62  * count associated with that session structure to ensure that it
63  * can't be freed until after the caller is done with it and calls
64  * sess_rele().  This hold doesn't actually protect any of the
65  * contents of the session structure.
66  *
67  * tty_hold() returns a pointer to a session structure associated
68  * with the curproc.  It also "locks" the contents of the session
69  * structure.  This hold should be used when the caller will be
70  * doing operations on a controlling tty associated with the session.
71  * This operation doesn an implicit sess_hold() so that the session
72  * structure can't be free'd until after the caller is done with it
73  * and invokes tty_rele().
74  *
75  * NOTE: Neither of these functions (sess_hold() or tty_hold())
76  * prevent a process from changing its session.  Once these functions
77  * return a session pointer, that session pointer may no longer be
78  * associated with the current process.  If a caller wants to prevent
79  * a process from changing its session then it must hold pidlock or
80  * p_splock.
81  */
82 
83 typedef struct sess {
84 	struct pid *s_sidp;		/* session ID info, never changes */
85 
86 	kmutex_t s_lock;		/* protects everything below */
87 	uint_t s_ref; 			/* reference count */
88 	boolean_t s_sighuped;		/* ctty had sighup sent to it */
89 
90 	boolean_t s_exit;		/* sesion leader is exiting */
91 	kcondvar_t s_exit_cv;		/* Condvar for s_exit */
92 
93 	int s_cnt;			/* active users of this ctty */
94 	kcondvar_t s_cnt_cv;		/* Condvar for s_cnt */
95 
96 	/*
97 	 * The following fields can only be updated while s_lock is held
98 	 * and s_cnt is 0.  (ie, no one has a tty_hold() on this session.)
99 	 */
100 	dev_t s_dev;			/* tty's device number */
101 	struct vnode *s_vp;		/* tty's vnode */
102 	struct cred *s_cred;		/* allocation credentials */
103 } sess_t;
104 
105 #define	s_sid s_sidp->pid_id
106 
107 #if defined(_KERNEL)
108 
109 extern sess_t session0;
110 
111 /* forward referenced structure tags */
112 struct vnode;
113 struct proc;
114 struct stdata;
115 
116 extern void sess_hold(proc_t *p);
117 extern void sess_rele(sess_t *, boolean_t);
118 extern sess_t *tty_hold(void);
119 extern void tty_rele(sess_t *sp);
120 
121 
122 extern void sess_create(void);
123 extern int strctty(struct stdata *);
124 extern int freectty(boolean_t);
125 extern dev_t cttydev(struct proc *);
126 extern void ctty_clear_sighuped(void);
127 
128 #endif /* defined(_KERNEL) */
129 
130 #ifdef	__cplusplus
131 }
132 #endif
133 
134 #endif	/* _SYS_SESSION_H */
135