xref: /titanic_52/usr/src/uts/common/sys/ptms.h (revision eb5a5c7888f20ad6fa4580e785308db395ef6dfc)
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
5facf4a8dSllai1  * Common Development and Distribution License (the "License").
6facf4a8dSllai1  * 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 /*
22*eb5a5c78SSurya 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	*/
257c478bd9Sstevel@tonic-gate /*	  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 
359acbbeafSnn35248 #ifdef _KERNEL
369acbbeafSnn35248 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Structures and definitions supporting the pseudo terminal
397c478bd9Sstevel@tonic-gate  * drivers. This structure is private and should not be used by any
407c478bd9Sstevel@tonic-gate  * applications.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate struct pt_ttys {
437c478bd9Sstevel@tonic-gate 	queue_t *ptm_rdq; 	/* master's read queue pointer */
447c478bd9Sstevel@tonic-gate 	queue_t *pts_rdq; 	/* slave's read queue pointer */
457c478bd9Sstevel@tonic-gate 	mblk_t	*pt_nullmsg;	/* 0-bytes message block for pts close */
467c478bd9Sstevel@tonic-gate 	pid_t	 pt_pid;	/* process id (for debugging) */
477c478bd9Sstevel@tonic-gate 	minor_t	 pt_minor;	/* Minor number of this pty */
487c478bd9Sstevel@tonic-gate 	int	 pt_refcnt;	/* reference count for ptm_rdq/pts_rdq uses */
497c478bd9Sstevel@tonic-gate 	ushort_t pt_state;	/* state of master/slave pair */
507c478bd9Sstevel@tonic-gate 	kcondvar_t pt_cv;	/* condition variable for exclusive access */
517c478bd9Sstevel@tonic-gate 	kmutex_t pt_lock;	/* Per-element lock */
527c478bd9Sstevel@tonic-gate 	zoneid_t pt_zoneid;	/* Zone membership for this pty */
53facf4a8dSllai1 	uid_t	 pt_ruid;	/* Real owner of pty */
54facf4a8dSllai1 	gid_t	 pt_rgid;	/* Real group owner of pty */
557c478bd9Sstevel@tonic-gate };
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * pt_state values
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate #define	PTLOCK		0x01	/* master/slave pair is locked */
617c478bd9Sstevel@tonic-gate #define	PTMOPEN 	0x02  	/* master side is open */
627c478bd9Sstevel@tonic-gate #define	PTSOPEN 	0x04	/* slave side is open */
637c478bd9Sstevel@tonic-gate #define	PTSTTY		0x08	/* slave side is tty */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Multi-threading primitives.
677c478bd9Sstevel@tonic-gate  * Values of pt_refcnt: -1 if a writer is accessing the struct
687c478bd9Sstevel@tonic-gate  *			0  if no one is reading or writing
697c478bd9Sstevel@tonic-gate  *			> 0 equals to the number of readers accessing the struct
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate #define	PT_ENTER_READ(p) {			\
727c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
737c478bd9Sstevel@tonic-gate 	while ((p)->pt_refcnt < 0)		\
747c478bd9Sstevel@tonic-gate 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
757c478bd9Sstevel@tonic-gate 	(p)->pt_refcnt++;			\
767c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	PT_ENTER_WRITE(p) {			\
807c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
817c478bd9Sstevel@tonic-gate 	while ((p)->pt_refcnt != 0)		\
827c478bd9Sstevel@tonic-gate 		cv_wait(&((p)->pt_cv), &(p)->pt_lock);	\
837c478bd9Sstevel@tonic-gate 	(p)->pt_refcnt = -1;			\
847c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	PT_EXIT_READ(p) {			\
887c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
897c478bd9Sstevel@tonic-gate 	ASSERT((p)->pt_refcnt > 0);		\
907c478bd9Sstevel@tonic-gate 	if ((--((p)->pt_refcnt)) == 0)		\
917c478bd9Sstevel@tonic-gate 		cv_broadcast(&(p)->pt_cv);	\
927c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	PT_EXIT_WRITE(p) {			\
967c478bd9Sstevel@tonic-gate 	mutex_enter(&(p)->pt_lock);		\
977c478bd9Sstevel@tonic-gate 	ASSERT((p)->pt_refcnt == -1);		\
987c478bd9Sstevel@tonic-gate 	(p)->pt_refcnt = 0;			\
997c478bd9Sstevel@tonic-gate 	cv_broadcast(&(p)->pt_cv);		\
1007c478bd9Sstevel@tonic-gate 	mutex_exit(&(p)->pt_lock);		\
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * ptms_lock and pt_cnt are defined in ptms_conf.c
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate extern kmutex_t		ptms_lock;
1077c478bd9Sstevel@tonic-gate extern dev_info_t 	*pts_dip;	/* private copy of devinfo ptr */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate extern void ptms_init(void);
1107c478bd9Sstevel@tonic-gate extern struct pt_ttys *pt_ttys_alloc(void);
1117c478bd9Sstevel@tonic-gate extern void ptms_close(struct pt_ttys *, uint_t);
1127c478bd9Sstevel@tonic-gate extern struct pt_ttys *ptms_minor2ptty(minor_t);
113facf4a8dSllai1 extern int ptms_attach_slave(void);
114facf4a8dSllai1 extern int ptms_minor_valid(minor_t ptmin, uid_t *uid, gid_t *gid);
115facf4a8dSllai1 extern int ptms_minor_exists(minor_t ptmin);
116facf4a8dSllai1 extern void ptms_set_owner(minor_t ptmin, uid_t uid, gid_t gid);
117facf4a8dSllai1 extern major_t ptms_slave_attached(void);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #ifdef DEBUG
1207c478bd9Sstevel@tonic-gate extern void ptms_log(char *, uint_t);
1217c478bd9Sstevel@tonic-gate extern void ptms_logp(char *, uintptr_t);
1227c478bd9Sstevel@tonic-gate #define	DDBG(a, b) ptms_log(a, b)
1237c478bd9Sstevel@tonic-gate #define	DDBGP(a, b) ptms_logp(a, b)
1247c478bd9Sstevel@tonic-gate #else
1257c478bd9Sstevel@tonic-gate #define	DDBG(a, b)
1267c478bd9Sstevel@tonic-gate #define	DDBGP(a, b)
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1307c478bd9Sstevel@tonic-gate 
1319acbbeafSnn35248 typedef struct pt_own {
1329acbbeafSnn35248 	uid_t	pto_ruid;
1339acbbeafSnn35248 	gid_t	pto_rgid;
1349acbbeafSnn35248 } pt_own_t;
1359acbbeafSnn35248 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * ioctl commands
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  *  ISPTM: Determines whether the file descriptor is that of an open master
1407c478bd9Sstevel@tonic-gate  *	   device. Return code of zero indicates that the file descriptor
1417c478bd9Sstevel@tonic-gate  *	   represents master device.
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * UNLKPT: Unlocks the master and slave devices.  It returns 0 on success. On
1447c478bd9Sstevel@tonic-gate  *	   failure, the errno is set to EINVAL indicating that the master
1457c478bd9Sstevel@tonic-gate  *	   device is not open.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  *  ZONEPT: Sets the zoneid of the pair of master and slave devices.  It
1487c478bd9Sstevel@tonic-gate  *	    returns 0 upon success.  Used to force a pty 'into' a zone upon
1497c478bd9Sstevel@tonic-gate  *	    zone entry.
150facf4a8dSllai1  *
151facf4a8dSllai1  * PT_OWNER: Sets uid and gid for slave device.  It returns 0 on success.
152facf4a8dSllai1  *
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate #define	ISPTM		(('P'<<8)|1)	/* query for master */
1557c478bd9Sstevel@tonic-gate #define	UNLKPT		(('P'<<8)|2)	/* unlock master/slave pair */
1567c478bd9Sstevel@tonic-gate #define	PTSSTTY		(('P'<<8)|3)	/* set tty flag */
1577c478bd9Sstevel@tonic-gate #define	ZONEPT		(('P'<<8)|4)	/* set zone of master/slave pair */
15849e92448Svikram #define	OWNERPT		(('P'<<8)|5)	/* set owner/group for slave device */
159facf4a8dSllai1 
1607c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate #endif
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #endif	/* _SYS_PTMS_H */
165