xref: /titanic_41/usr/src/uts/common/sys/ptms.h (revision 6dfee4834394825da35b977ca71cdc965bc7b6a4)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #ifndef _SYS_PTMS_H
31 #define	_SYS_PTMS_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 /*
40  * Structures and definitions supporting the pseudo terminal
41  * drivers. This structure is private and should not be used by any
42  * applications.
43  */
44 struct pt_ttys {
45 	queue_t *ptm_rdq; 	/* master's read queue pointer */
46 	queue_t *pts_rdq; 	/* slave's read queue pointer */
47 	mblk_t	*pt_nullmsg;	/* 0-bytes message block for pts close */
48 	pid_t	 pt_pid;	/* process id (for debugging) */
49 	minor_t	 pt_minor;	/* Minor number of this pty */
50 	int	 pt_refcnt;	/* reference count for ptm_rdq/pts_rdq uses */
51 	ushort_t pt_state;	/* state of master/slave pair */
52 	kcondvar_t pt_cv;	/* condition variable for exclusive access */
53 	kmutex_t pt_lock;	/* Per-element lock */
54 	zoneid_t pt_zoneid;	/* Zone membership for this pty */
55 };
56 
57 /*
58  * pt_state values
59  */
60 #define	PTLOCK		0x01	/* master/slave pair is locked */
61 #define	PTMOPEN 	0x02  	/* master side is open */
62 #define	PTSOPEN 	0x04	/* slave side is open */
63 #define	PTSTTY		0x08	/* slave side is tty */
64 
65 #ifdef _KERNEL
66 
67 /*
68  * Multi-threading primitives.
69  * Values of pt_refcnt: -1 if a writer is accessing the struct
70  *			0  if no one is reading or writing
71  *			> 0 equals to the number of readers accessing the struct
72  */
73 #define	PT_ENTER_READ(p) {			\
74 	mutex_enter(&(p)->pt_lock);		\
75 	while ((p)->pt_refcnt < 0)		\
76 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
77 	(p)->pt_refcnt++;			\
78 	mutex_exit(&(p)->pt_lock);		\
79 }
80 
81 #define	PT_ENTER_WRITE(p) {			\
82 	mutex_enter(&(p)->pt_lock);		\
83 	while ((p)->pt_refcnt != 0)		\
84 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
85 	(p)->pt_refcnt = -1;			\
86 	mutex_exit(&(p)->pt_lock);		\
87 }
88 
89 #define	PT_EXIT_READ(p) {			\
90 	mutex_enter(&(p)->pt_lock);		\
91 	ASSERT((p)->pt_refcnt > 0);		\
92 	if ((--((p)->pt_refcnt)) == 0)		\
93 		cv_broadcast(&(p)->pt_cv);	\
94 	mutex_exit(&(p)->pt_lock);		\
95 }
96 
97 #define	PT_EXIT_WRITE(p) {			\
98 	mutex_enter(&(p)->pt_lock);		\
99 	ASSERT((p)->pt_refcnt == -1);		\
100 	(p)->pt_refcnt = 0;			\
101 	cv_broadcast(&(p)->pt_cv);		\
102 	mutex_exit(&(p)->pt_lock);		\
103 }
104 
105 /*
106  * ptms_lock and pt_cnt are defined in ptms_conf.c
107  */
108 extern kmutex_t		ptms_lock;
109 extern dev_info_t 	*pts_dip;	/* private copy of devinfo ptr */
110 
111 extern void ptms_init(void);
112 extern struct pt_ttys *pt_ttys_alloc(void);
113 extern void ptms_close(struct pt_ttys *, uint_t);
114 extern struct pt_ttys *ptms_minor2ptty(minor_t);
115 extern int ptms_create_pts_nodes(dev_info_t *);
116 extern int ptms_destroy_pts_nodes(dev_info_t *);
117 
118 #ifdef DEBUG
119 extern void ptms_log(char *, uint_t);
120 extern void ptms_logp(char *, uintptr_t);
121 #define	DDBG(a, b) ptms_log(a, b)
122 #define	DDBGP(a, b) ptms_logp(a, b)
123 #else
124 #define	DDBG(a, b)
125 #define	DDBGP(a, b)
126 #endif
127 
128 #endif /* _KERNEL */
129 
130 /*
131  * ioctl commands
132  *
133  *   ISPTM: Determines whether the file descriptor is that of an open master
134  *	    device. Return code of zero indicates that the file descriptor
135  *	    represents master device.
136  *
137  *  UNLKPT: Unlocks the master and slave devices.  It returns 0 on success. On
138  *	    failure, the errno is set to EINVAL indicating that the master
139  *	    device is not open.
140  *
141  *  ZONEPT: Sets the zoneid of the pair of master and slave devices.  It
142  *	    returns 0 upon success.  Used to force a pty 'into' a zone upon
143  *	    zone entry.
144  */
145 #define	ISPTM	(('P'<<8)|1)	/* query for master */
146 #define	UNLKPT	(('P'<<8)|2)	/* unlock master/slave pair */
147 #define	PTSSTTY	(('P'<<8)|3)	/* set tty flag */
148 #define	ZONEPT	(('P'<<8)|4)	/* set zone of master/slave pair */
149 
150 #ifdef	__cplusplus
151 }
152 #endif
153 
154 #endif	/* _SYS_PTMS_H */
155