xref: /illumos-gate/usr/src/uts/common/sys/ptms.h (revision 1fa2a664)
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
5facf4a8dSllai  * Common Development and Distribution License (the "License").
6facf4a8dSllai  * 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 /*
22eb5a5c78SSurya Prakki  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
25b4203d75SMarcel Telka /*	  All Rights Reserved	*/
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef _SYS_PTMS_H
297c478bd9Sstevel@tonic-gate #define	_SYS_PTMS_H
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
327c478bd9Sstevel@tonic-gate extern "C" {
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate 
359acbbeafSnn #ifdef _KERNEL
369acbbeafSnn 
377c478bd9Sstevel@tonic-gate /*
38*1fa2a664SJoshua M. Clulow  * Structures and definitions supporting the pseudo-terminal drivers. This
39*1fa2a664SJoshua M. Clulow  * structure is private and should not be used by any applications.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate struct pt_ttys {
42*1fa2a664SJoshua M. Clulow 	queue_t *ptm_rdq;	/* manager's read queue pointer */
43*1fa2a664SJoshua M. Clulow 	queue_t *pts_rdq;	/* subsidiary's read queue pointer */
447c478bd9Sstevel@tonic-gate 	mblk_t	*pt_nullmsg;	/* 0-bytes message block for pts close */
457c478bd9Sstevel@tonic-gate 	pid_t	 pt_pid;	/* process id (for debugging) */
467c478bd9Sstevel@tonic-gate 	minor_t	 pt_minor;	/* Minor number of this pty */
477c478bd9Sstevel@tonic-gate 	int	 pt_refcnt;	/* reference count for ptm_rdq/pts_rdq uses */
48*1fa2a664SJoshua M. Clulow 	ushort_t pt_state;	/* state of manager/subsidiary pair */
497c478bd9Sstevel@tonic-gate 	kcondvar_t pt_cv;	/* condition variable for exclusive access */
507c478bd9Sstevel@tonic-gate 	kmutex_t pt_lock;	/* Per-element lock */
517c478bd9Sstevel@tonic-gate 	zoneid_t pt_zoneid;	/* Zone membership for this pty */
52facf4a8dSllai 	uid_t	 pt_ruid;	/* Real owner of pty */
53facf4a8dSllai 	gid_t	 pt_rgid;	/* Real group owner of pty */
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * pt_state values
587c478bd9Sstevel@tonic-gate  */
59*1fa2a664SJoshua M. Clulow #define	PTLOCK		0x01	/* manager/subsidiary pair is locked */
60*1fa2a664SJoshua M. Clulow #define	PTMOPEN		0x02	/* manager side is open */
61*1fa2a664SJoshua M. Clulow #define	PTSOPEN		0x04	/* subsidiary side is open */
62*1fa2a664SJoshua M. Clulow #define	PTSTTY		0x08	/* subsidiary side is tty */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Multi-threading primitives.
667c478bd9Sstevel@tonic-gate  * Values of pt_refcnt: -1 if a writer is accessing the struct
677c478bd9Sstevel@tonic-gate  *			0  if no one is reading or writing
687c478bd9Sstevel@tonic-gate  *			> 0 equals to the number of readers accessing the struct
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate #define	PT_ENTER_READ(p) {			\
717c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
727c478bd9Sstevel@tonic-gate 	while ((p)->pt_refcnt < 0)		\
737c478bd9Sstevel@tonic-gate 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
747c478bd9Sstevel@tonic-gate 	(p)->pt_refcnt++;			\
757c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	PT_ENTER_WRITE(p) {			\
797c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
807c478bd9Sstevel@tonic-gate 	while ((p)->pt_refcnt != 0)		\
817c478bd9Sstevel@tonic-gate 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
827c478bd9Sstevel@tonic-gate 	(p)->pt_refcnt = -1;			\
837c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	PT_EXIT_READ(p) {			\
877c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
887c478bd9Sstevel@tonic-gate 	ASSERT((p)->pt_refcnt > 0);		\
897c478bd9Sstevel@tonic-gate 	if ((--((p)->pt_refcnt)) == 0)		\
907c478bd9Sstevel@tonic-gate 		cv_broadcast(&(p)->pt_cv);	\
917c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	PT_EXIT_WRITE(p) {			\
957c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
967c478bd9Sstevel@tonic-gate 	ASSERT((p)->pt_refcnt == -1);		\
977c478bd9Sstevel@tonic-gate 	(p)->pt_refcnt = 0;			\
987c478bd9Sstevel@tonic-gate 	cv_broadcast(&(p)->pt_cv);		\
997c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * ptms_lock and pt_cnt are defined in ptms_conf.c
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate extern kmutex_t		ptms_lock;
106*1fa2a664SJoshua M. Clulow extern dev_info_t	*pts_dip;	/* private copy of devinfo ptr */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate extern void ptms_init(void);
1097c478bd9Sstevel@tonic-gate extern struct pt_ttys *pt_ttys_alloc(void);
1107c478bd9Sstevel@tonic-gate extern void ptms_close(struct pt_ttys *, uint_t);
1117c478bd9Sstevel@tonic-gate extern struct pt_ttys *ptms_minor2ptty(minor_t);
112*1fa2a664SJoshua M. Clulow extern int ptms_attach_subsidiary(void);
113facf4a8dSllai extern int ptms_minor_valid(minor_t ptmin, uid_t *uid, gid_t *gid);
114facf4a8dSllai extern int ptms_minor_exists(minor_t ptmin);
115facf4a8dSllai extern void ptms_set_owner(minor_t ptmin, uid_t uid, gid_t gid);
116*1fa2a664SJoshua M. Clulow extern major_t ptms_subsidiary_attached(void);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef DEBUG
1197c478bd9Sstevel@tonic-gate extern void ptms_log(char *, uint_t);
1207c478bd9Sstevel@tonic-gate extern void ptms_logp(char *, uintptr_t);
1217c478bd9Sstevel@tonic-gate #define	DDBG(a, b) ptms_log(a, b)
1227c478bd9Sstevel@tonic-gate #define	DDBGP(a, b) ptms_logp(a, b)
1237c478bd9Sstevel@tonic-gate #else
1247c478bd9Sstevel@tonic-gate #define	DDBG(a, b)
1257c478bd9Sstevel@tonic-gate #define	DDBGP(a, b)
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1297c478bd9Sstevel@tonic-gate 
1309acbbeafSnn typedef struct pt_own {
1319acbbeafSnn 	uid_t	pto_ruid;
1329acbbeafSnn 	gid_t	pto_rgid;
1339acbbeafSnn } pt_own_t;
1349acbbeafSnn 
1357c478bd9Sstevel@tonic-gate /*
136*1fa2a664SJoshua M. Clulow  * IOCTL COMMANDS
1377c478bd9Sstevel@tonic-gate  *
138*1fa2a664SJoshua M. Clulow  *	ISPTM
139*1fa2a664SJoshua M. Clulow  *		Determines whether the file descriptor is that of an open
140*1fa2a664SJoshua M. Clulow  *		manager device.  Return code of zero indicates that the file
141*1fa2a664SJoshua M. Clulow  *		descriptor represents a manager device.
1427c478bd9Sstevel@tonic-gate  *
143*1fa2a664SJoshua M. Clulow  *	UNLKPT
144*1fa2a664SJoshua M. Clulow  *		Unlocks the manager and subsidiary devices.  It returns 0 on
145*1fa2a664SJoshua M. Clulow  *		success.  On failure, the errno is set to EINVAL indicating
146*1fa2a664SJoshua M. Clulow  *		that the manager device is not open.
1477c478bd9Sstevel@tonic-gate  *
148*1fa2a664SJoshua M. Clulow  *	ZONEPT
149*1fa2a664SJoshua M. Clulow  *		Sets the zoneid of the pair of manager and subsidiary devices.
150*1fa2a664SJoshua M. Clulow  *		It returns 0 upon success.  Used to force a pty 'into' a zone
151*1fa2a664SJoshua M. Clulow  *		upon zone entry.
152facf4a8dSllai  *
153*1fa2a664SJoshua M. Clulow  *	PT_OWNER
154*1fa2a664SJoshua M. Clulow  *		Sets uid and gid for subsidiary device.  It returns 0 on
155*1fa2a664SJoshua M. Clulow  *		success.
1567c478bd9Sstevel@tonic-gate  */
157*1fa2a664SJoshua M. Clulow #define	ISPTM		(('P'<<8)|1)  /* query for manager */
158*1fa2a664SJoshua M. Clulow #define	UNLKPT		(('P'<<8)|2)  /* unlock manager/subsidiary pair */
159*1fa2a664SJoshua M. Clulow #define	PTSSTTY		(('P'<<8)|3)  /* set tty flag */
160*1fa2a664SJoshua M. Clulow #define	ZONEPT		(('P'<<8)|4)  /* set zone of manager/subsidiary pair */
161*1fa2a664SJoshua M. Clulow #define	OWNERPT		(('P'<<8)|5)  /* set owner/group for subsidiary */
162facf4a8dSllai 
1637c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate #endif	/* _SYS_PTMS_H */
168