xref: /illumos-gate/usr/src/uts/common/sys/ptms.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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26 /*	  All Rights Reserved  	*/
27 
28 
29 #ifndef _SYS_PTMS_H
30 #define	_SYS_PTMS_H
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 #ifdef _KERNEL
39 
40 /*
41  * Structures and definitions supporting the pseudo terminal
42  * drivers. This structure is private and should not be used by any
43  * applications.
44  */
45 struct pt_ttys {
46 	queue_t *ptm_rdq; 	/* master's read queue pointer */
47 	queue_t *pts_rdq; 	/* slave's read queue pointer */
48 	mblk_t	*pt_nullmsg;	/* 0-bytes message block for pts close */
49 	pid_t	 pt_pid;	/* process id (for debugging) */
50 	minor_t	 pt_minor;	/* Minor number of this pty */
51 	int	 pt_refcnt;	/* reference count for ptm_rdq/pts_rdq uses */
52 	ushort_t pt_state;	/* state of master/slave pair */
53 	kcondvar_t pt_cv;	/* condition variable for exclusive access */
54 	kmutex_t pt_lock;	/* Per-element lock */
55 	zoneid_t pt_zoneid;	/* Zone membership for this pty */
56 	uid_t	 pt_ruid;	/* Real owner of pty */
57 	gid_t	 pt_rgid;	/* Real group owner of pty */
58 };
59 
60 /*
61  * pt_state values
62  */
63 #define	PTLOCK		0x01	/* master/slave pair is locked */
64 #define	PTMOPEN 	0x02  	/* master side is open */
65 #define	PTSOPEN 	0x04	/* slave side is open */
66 #define	PTSTTY		0x08	/* slave side is tty */
67 
68 /*
69  * Multi-threading primitives.
70  * Values of pt_refcnt: -1 if a writer is accessing the struct
71  *			0  if no one is reading or writing
72  *			> 0 equals to the number of readers accessing the struct
73  */
74 #define	PT_ENTER_READ(p) {			\
75 	mutex_enter(&(p)->pt_lock);		\
76 	while ((p)->pt_refcnt < 0)		\
77 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
78 	(p)->pt_refcnt++;			\
79 	mutex_exit(&(p)->pt_lock);		\
80 }
81 
82 #define	PT_ENTER_WRITE(p) {			\
83 	mutex_enter(&(p)->pt_lock);		\
84 	while ((p)->pt_refcnt != 0)		\
85 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
86 	(p)->pt_refcnt = -1;			\
87 	mutex_exit(&(p)->pt_lock);		\
88 }
89 
90 #define	PT_EXIT_READ(p) {			\
91 	mutex_enter(&(p)->pt_lock);		\
92 	ASSERT((p)->pt_refcnt > 0);		\
93 	if ((--((p)->pt_refcnt)) == 0)		\
94 		cv_broadcast(&(p)->pt_cv);	\
95 	mutex_exit(&(p)->pt_lock);		\
96 }
97 
98 #define	PT_EXIT_WRITE(p) {			\
99 	mutex_enter(&(p)->pt_lock);		\
100 	ASSERT((p)->pt_refcnt == -1);		\
101 	(p)->pt_refcnt = 0;			\
102 	cv_broadcast(&(p)->pt_cv);		\
103 	mutex_exit(&(p)->pt_lock);		\
104 }
105 
106 /*
107  * ptms_lock and pt_cnt are defined in ptms_conf.c
108  */
109 extern kmutex_t		ptms_lock;
110 extern dev_info_t 	*pts_dip;	/* private copy of devinfo ptr */
111 
112 extern void ptms_init(void);
113 extern struct pt_ttys *pt_ttys_alloc(void);
114 extern void ptms_close(struct pt_ttys *, uint_t);
115 extern struct pt_ttys *ptms_minor2ptty(minor_t);
116 extern int ptms_attach_slave(void);
117 extern int ptms_minor_valid(minor_t ptmin, uid_t *uid, gid_t *gid);
118 extern int ptms_minor_exists(minor_t ptmin);
119 extern void ptms_set_owner(minor_t ptmin, uid_t uid, gid_t gid);
120 extern major_t ptms_slave_attached(void);
121 
122 #ifdef DEBUG
123 extern void ptms_log(char *, uint_t);
124 extern void ptms_logp(char *, uintptr_t);
125 #define	DDBG(a, b) ptms_log(a, b)
126 #define	DDBGP(a, b) ptms_logp(a, b)
127 #else
128 #define	DDBG(a, b)
129 #define	DDBGP(a, b)
130 #endif
131 
132 typedef struct __ptmptsopencb_arg *ptmptsopencb_arg_t;
133 typedef struct ptmptsopencb {
134 	boolean_t		(*ppocb_func)(ptmptsopencb_arg_t);
135 	ptmptsopencb_arg_t	ppocb_arg;
136 } ptmptsopencb_t;
137 
138 #endif /* _KERNEL */
139 
140 typedef struct pt_own {
141 	uid_t	pto_ruid;
142 	gid_t	pto_rgid;
143 } pt_own_t;
144 
145 /*
146  * ioctl commands
147  *
148  *  ISPTM: Determines whether the file descriptor is that of an open master
149  *	   device. Return code of zero indicates that the file descriptor
150  *	   represents master device.
151  *
152  * UNLKPT: Unlocks the master and slave devices.  It returns 0 on success. On
153  *	   failure, the errno is set to EINVAL indicating that the master
154  *	   device is not open.
155  *
156  *  ZONEPT: Sets the zoneid of the pair of master and slave devices.  It
157  *	    returns 0 upon success.  Used to force a pty 'into' a zone upon
158  *	    zone entry.
159  *
160  * PT_OWNER: Sets uid and gid for slave device.  It returns 0 on success.
161  *
162  */
163 #define	ISPTM		(('P'<<8)|1)	/* query for master */
164 #define	UNLKPT		(('P'<<8)|2)	/* unlock master/slave pair */
165 #define	PTSSTTY		(('P'<<8)|3)	/* set tty flag */
166 #define	ZONEPT		(('P'<<8)|4)	/* set zone of master/slave pair */
167 #define	OWNERPT		(('P'<<8)|5)	/* set owner/group for slave device */
168 
169 #ifdef _KERNEL
170 /*
171  * kernel ioctl commands
172  *
173  * PTMPTSOPENCB: Returns a callback function pointer and opaque argument.
174  *	      The return value of the callback function when it's invoked
175  *	      with the opaque argument passed to it will indicate if the
176  *	      pts slave device is currently open.
177  */
178 #define	PTMPTSOPENCB	(('P'<<8)|6)	/* check if the slave is open */
179 
180 #endif /* _KERNEL */
181 
182 #ifdef	__cplusplus
183 }
184 #endif
185 
186 #endif	/* _SYS_PTMS_H */
187