25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER START
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The contents of this file are subject to the terms of the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Common Development and Distribution License (the "License").
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You may not use this file except in compliance with the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * See the License for the specific language governing permissions
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and limitations under the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER END
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Use is subject to license terms.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#pragma ident "%Z%%M% %I% %E% SMI"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * This file contains global data and code shared between master and slave parts
ddc0e0b53c661f6e439e3b7072b3ef353eadb4afRichard Lowe * of the pseudo-terminal driver.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Pseudo terminals (or pt's for short) are allocated dynamically.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt's are put in the global ptms_slots array indexed by minor numbers.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The slots array is initially small (of the size NPTY_MIN). When more pt's are
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * needed than the slot array size, the larger slot array is allocated and all
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * opened pt's move to the new one.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Resource allocation:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt_ttys structures are allocated via pt_ttys_alloc, which uses
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * kmem_cache_alloc().
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Minor number space is allocated via vmem_alloc() interface.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ptms_slots arrays are allocated via kmem_alloc().
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Minors are started from 1 instead of 0 because vmem_alloc returns 0 in case
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * of failure. Also, in anticipation of removing clone device interface to
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pseudo-terminal subsystem, minor 0 should not be used. (Potential future
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * development).
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * After the table slot size reaches pt_maxdelta, we stop 2^N extension
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * algorithm and start extending the slot table size by pt_maxdelta.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Device entries /dev/pts directory are created dynamically by the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * /dev filesystem. We no longer call ddi_create_minor_node() on
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * behalf of the slave driver. The /dev filesystem creates /dev/pts
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * nodes based on the pt_ttys array.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Synchronization:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * All global data synchronization between ptm/pts is done via global
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ptms_lock mutex which is implicitly initialized by declaring it global.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * which allow reader locks to be reacquired by the same thread (usual
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * reader/writer locks can't be used for that purpose since it is illegal for
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * a thread to acquire a lock it already holds, even as a reader). The sole
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * purpose of these macros is to guarantee that the peer queue will not
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * disappear (due to closing peer) while it is used. It is safe to use
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * they are not real locks but reference counts).
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * be set to appropriate queues *after* qprocson() is called during open (to
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * prevent peer from accessing the queue with incomplete plumbing) and set to
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * NULL before qprocsoff() is called during close. Put and service procedures
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * use PT_ENTER_READ/PT_EXIT_READ to prevent peer closes.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The pt_nullmsg field is only used in open/close routines and is also
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Lock Ordering:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If both ptms_lock and per-pty lock should be held, ptms_lock should always
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * be entered first, followed by per-pty lock.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Global functions:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * void ptms_init(void);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Called by pts/ptm _init entry points. It performes one-time
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * initialization needed for both pts and ptm. This initialization is done
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * here and not in ptms_initspace because all these data structures are not
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * needed if pseudo-terminals are not used in the system.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * struct pt_ttys *pt_ttys_alloc(void);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Allocate new minor number and pseudo-terminal entry. May sleep.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * New minor number is recorded in pt_minor field of the entry returned.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * This routine also initializes pt_minor and pt_state fields of the new
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pseudo-terminal and puts a pointer to it into ptms_slots array.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * struct pt_ttys *ptms_minor2ptty(minor_t minor)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Find pt_ttys structure by minor number.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Returns NULL when minor is out of range.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * int ptms_minor_valid(minor_t minor, uid_t *ruid, gid_t *rgid)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Check if minor refers to an allocated pty in the current zone.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 0 if not allocated or not for this zone.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 if an allocated pty in the current zone.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Also returns owner of pty.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * int ptms_minor_exists(minor_t minor)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Check if minor refers to an allocated pty (in any zone)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 0 if not an allocated pty
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 if an allocated pty
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * void ptms_set_owner(minor_t minor, uid_t ruid, gid_t rgid)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Sets the owner associated with a pty.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * void ptms_close(struct pt_ttys *pt, uint_t flags_to_clear);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Clear flags_to_clear in pt and if no one owns it (PTMOPEN/PTSOPEN not
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * set) free pt entry and corresponding slot.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Tuneables and configuration:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt_cnt: minimum number of pseudo-terminals in the system. The system
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * should provide at least this number of ptys (provided sufficient
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * memory is available). It is different from the older semantics
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * of pt_cnt meaning maximum number of ptys.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Set to 0 by default.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt_max_pty: Maximum number of pseudo-terminals in the system. The system
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * should not allocate more ptys than pt_max_pty (although, it may
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * impose stricter maximum). Zero value means no user-defined
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * maximum. This is intended to be used as "denial-of-service"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Set to 0 by default.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Both pt_cnt and pt_max_pty may be modified during system lifetime
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * with their semantics preserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt_init_cnt: Initial size of ptms_slots array. Set to NPTY_INITIAL.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt_ptyofmem: Approximate percentage of system memory that may be
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * occupied by pty data structures. Initially set to NPTY_PERCENT.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * This variable is used once during initialization to estimate
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * maximum number of ptys in the system. The actual maximum is
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * determined as minimum of pt_max_pty and calculated value.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * pt_maxdelta: Maximum extension chunk of the slot table.
static int ptms_constructor(void *, void *, int);
static void ptms_destructor(void *, void *);
sizeof (struct pt_ttys *) + \
sizeof (dblk_t))
#ifdef DEBUG
static uint_t
if (pts_dip)
return (maj);
struct pt_ttys *
return (NULL);
ptms_inuse++;
if (dminor == 0) {
if (dminor == 0) {
ptms_inuse--;
return (NULL);
ptms_inuse--;
return (pt);
struct pt_ttys *
return (pt);
if (dminor == 0)
if (dminor == 0) {
*ruid = 0;
*rgid = 0;
ret = 0;
return (ret);
return (ret);
ptms_inuse--;
static minor_t
return ((minor_t)0);
return ((minor_t)0);
#ifdef DEBUG
if (ptms_debug) {
if (ptms_debug) {