xref: /illumos-gate/usr/src/uts/common/sys/session.h (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 /*
40  * Session structure overview.
41  *
42  * Currently, the only structure in the kernel which has a pointer to a
43  * session structures is the proc_t via the p_sessp pointer.  To
44  * access a session proc_t->p_sessp pointer a caller must hold either
45  * pidlock or p_splock.  These locks only protect the p_sessp pointer
46  * itself and do not protect any of the contents of the session structure.
47  * To prevent the contents of a the session structure from changing the
48  * caller must grab s_lock.
49  *
50  * No callers should ever update the contents of the session structure
51  * directly.  Only the session management code should ever modify the
52  * contents of the session structure.  When the session code attempts
53  * to modify the contents of a session structure it must hold multiple
54  * locks.  The locking order for all the locks that may need to be
55  * acquired is:
56  * 	sd_lock -> pidlock -> p_splock -> s_lock
57  *
58  * If a caller requires access to a session structure for long
59  * periods of time or across operations that may block it should
60  * use the tty_hold() and sess_hold() interfaces.
61  *
62  * sess_hold() returns a pointer to a session structure associated
63  * with the proc_t that was passed in.  It also increments the reference
64  * count associated with that session structure to ensure that it
65  * can't be freed until after the caller is done with it and calls
66  * sess_rele().  This hold doesn't actually protect any of the
67  * contents of the session structure.
68  *
69  * tty_hold() returns a pointer to a session structure associated
70  * with the curproc.  It also "locks" the contents of the session
71  * structure.  This hold should be used when the caller will be
72  * doing operations on a controlling tty associated with the session.
73  * This operation doesn an implicit sess_hold() so that the session
74  * structure can't be free'd until after the caller is done with it
75  * and invokes tty_rele().
76  *
77  * NOTE: Neither of these functions (sess_hold() or tty_hold())
78  * prevent a process from changing its session.  Once these functions
79  * return a session pointer, that session pointer may no longer be
80  * associated with the current process.  If a caller wants to prevent
81  * a process from changing its session then it must hold pidlock or
82  * p_splock.
83  */
84 
85 typedef struct sess {
86 	struct pid *s_sidp;		/* session ID info, never changes */
87 
88 	kmutex_t s_lock;		/* protects everything below */
89 	uint_t s_ref; 			/* reference count */
90 	boolean_t s_sighuped;		/* ctty had sighup sent to it */
91 
92 	boolean_t s_exit;		/* sesion leader is exiting */
93 	kcondvar_t s_exit_cv;		/* Condvar for s_exit */
94 
95 	int s_cnt;			/* active users of this ctty */
96 	kcondvar_t s_cnt_cv;		/* Condvar for s_cnt */
97 
98 	/*
99 	 * The following fields can only be updated while s_lock is held
100 	 * and s_cnt is 0.  (ie, no one has a tty_hold() on this session.)
101 	 */
102 	dev_t s_dev;			/* tty's device number */
103 	struct vnode *s_vp;		/* tty's vnode */
104 	struct cred *s_cred;		/* allocation credentials */
105 } sess_t;
106 
107 #define	s_sid s_sidp->pid_id
108 
109 #if defined(_KERNEL)
110 
111 extern sess_t session0;
112 
113 /* forward referenced structure tags */
114 struct vnode;
115 struct proc;
116 struct stdata;
117 
118 extern void sess_hold(proc_t *p);
119 extern void sess_rele(sess_t *, boolean_t);
120 extern sess_t *tty_hold(void);
121 extern void tty_rele(sess_t *sp);
122 
123 
124 extern void sess_create(void);
125 extern int strctty(struct stdata *);
126 extern int freectty(boolean_t);
127 extern dev_t cttydev(struct proc *);
128 extern void ctty_clear_sighuped(void);
129 
130 #endif /* defined(_KERNEL) */
131 
132 #ifdef	__cplusplus
133 }
134 #endif
135 
136 #endif	/* _SYS_SESSION_H */
137