/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains global data and code shared between master and slave parts
* of the pseudo-terminal driver.
*
* Pseudo terminals (or pt's for short) are allocated dynamically.
* pt's are put in the global ptms_slots array indexed by minor numbers.
*
* The slots array is initially small (of the size NPTY_MIN). When more pt's are
* needed than the slot array size, the larger slot array is allocated and all
* opened pt's move to the new one.
*
* Resource allocation:
*
* pt_ttys structures are allocated via pt_ttys_alloc, which uses
* kmem_cache_alloc().
* Minor number space is allocated via vmem_alloc() interface.
* ptms_slots arrays are allocated via kmem_alloc().
*
* Minors are started from 1 instead of 0 because vmem_alloc returns 0 in case
* of failure. Also, in anticipation of removing clone device interface to
* pseudo-terminal subsystem, minor 0 should not be used. (Potential future
* development).
*
* After the table slot size reaches pt_maxdelta, we stop 2^N extension
* algorithm and start extending the slot table size by pt_maxdelta.
*
* /dev filesystem. We no longer call ddi_create_minor_node() on
* nodes based on the pt_ttys array.
*
* Synchronization:
*
* ptms_lock mutex which is implicitly initialized by declaring it global.
*
* Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
* pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
*
* PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
* which allow reader locks to be reacquired by the same thread (usual
* a thread to acquire a lock it already holds, even as a reader). The sole
* purpose of these macros is to guarantee that the peer queue will not
* disappear (due to closing peer) while it is used. It is safe to use
* PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
* they are not real locks but reference counts).
*
* PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
* be set to appropriate queues *after* qprocson() is called during open (to
* prevent peer from accessing the queue with incomplete plumbing) and set to
* NULL before qprocsoff() is called during close. Put and service procedures
* use PT_ENTER_READ/PT_EXIT_READ to prevent peer closes.
*
* protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
* holds.
*
* Lock Ordering:
*
* If both ptms_lock and per-pty lock should be held, ptms_lock should always
* be entered first, followed by per-pty lock.
*
* Global functions:
*
* void ptms_init(void);
*
* initialization needed for both pts and ptm. This initialization is done
* here and not in ptms_initspace because all these data structures are not
* needed if pseudo-terminals are not used in the system.
*
* struct pt_ttys *pt_ttys_alloc(void);
*
* Allocate new minor number and pseudo-terminal entry. May sleep.
* New minor number is recorded in pt_minor field of the entry returned.
* This routine also initializes pt_minor and pt_state fields of the new
* pseudo-terminal and puts a pointer to it into ptms_slots array.
*
* struct pt_ttys *ptms_minor2ptty(minor_t minor)
*
* Find pt_ttys structure by minor number.
* Returns NULL when minor is out of range.
*
* int ptms_minor_valid(minor_t minor, uid_t *ruid, gid_t *rgid)
*
* Check if minor refers to an allocated pty in the current zone.
* Returns
* 0 if not allocated or not for this zone.
* 1 if an allocated pty in the current zone.
* Also returns owner of pty.
*
* int ptms_minor_exists(minor_t minor)
* Check if minor refers to an allocated pty (in any zone)
* Returns
* 0 if not an allocated pty
* 1 if an allocated pty
*
* void ptms_set_owner(minor_t minor, uid_t ruid, gid_t rgid)
*
* Sets the owner associated with a pty.
*
* void ptms_close(struct pt_ttys *pt, uint_t flags_to_clear);
*
* set) free pt entry and corresponding slot.
*
* Tuneables and configuration:
*
* pt_cnt: minimum number of pseudo-terminals in the system. The system
* should provide at least this number of ptys (provided sufficient
* memory is available). It is different from the older semantics
* of pt_cnt meaning maximum number of ptys.
* Set to 0 by default.
*
* pt_max_pty: Maximum number of pseudo-terminals in the system. The system
* should not allocate more ptys than pt_max_pty (although, it may
* impose stricter maximum). Zero value means no user-defined
* maximum. This is intended to be used as "denial-of-service"
* protection.
* Set to 0 by default.
*
* Both pt_cnt and pt_max_pty may be modified during system lifetime
* with their semantics preserved.
*
* pt_init_cnt: Initial size of ptms_slots array. Set to NPTY_INITIAL.
*
* pt_ptyofmem: Approximate percentage of system memory that may be
* occupied by pty data structures. Initially set to NPTY_PERCENT.
* This variable is used once during initialization to estimate
* maximum number of ptys in the system. The actual maximum is
* determined as minimum of pt_max_pty and calculated value.
*
* pt_maxdelta: Maximum extension chunk of the slot table.
*/
#include <sys/sysmacros.h>
#include <sys/ddi_impldefs.h>
#ifdef DEBUG
#endif
/* Initial number of ptms slots */
/* Maximum increment of the slot table size */
/*
* Tuneable variables.
*/
/* Other global variables */
/*
* Slot array and its management variables
*/
static int ptms_constructor(void *, void *, int);
static void ptms_destructor(void *, void *);
/*
* pointer for ptms_slots array, one pt_ttys structure and one empty message
* preallocated for pts close.
*/
sizeof (struct pt_ttys *) + \
sizeof (dblk_t))
#ifdef DEBUG
int ptms_debug = 0;
#endif
/*
* Clear all bits of x except the highest bit
*/
/*
* Roundup the number to the nearest power of 2
*/
static uint_t
{
/*
* If x is a power of 2, return x, otherwise roundup.
*/
return (p == x ? p : (p * 2));
}
/*
* Allocate ptms_slots array and kmem cache for pt_ttys. This initialization is
* only called once during system lifetime. Called from ptm or pts _init
* routine.
*/
void
ptms_init(void)
{
if (ptms_slots == NULL) {
sizeof (struct pt_ttys), 0, ptms_constructor,
/* Allocate integer space for minor numbers */
/*
* Calculate available number of ptys - how many ptys can we
* allocate in pt_pctofmem % of available memory. The value is
* rounded up to the nearest power of 2.
*/
(100 * PTY_SIZE));
}
}
/*
* This routine attaches the pts dip.
*/
int
ptms_attach_slave(void)
{
return (-1);
return (0);
}
/*
* Called from /dev fs. Checks if dip is attached,
* and if it is, returns its major number.
*/
ptms_slave_attached(void)
{
if (pts_dip)
return (maj);
}
/*
* Allocate new minor number and pseudo-terminal entry. Returns the new entry or
* NULL if no memory or maximum number of entries reached.
*/
struct pt_ttys *
pt_ttys_alloc(void)
{
/*
* Always try to allocate new pty when pt_cnt minimum limit is not
* achieved. If it is achieved, the maximum is determined by either
* user-specified value (if it is non-zero) or our memory estimations -
* whatever is less.
*/
if (ptms_inuse >= pt_cnt) {
/*
* When system achieved required minimum of ptys, check for the
* denial of service limits.
*
* Since pt_max_pty may be zero, the formula below is used to
* avoid conditional expression. It will equal to pt_max_pty if
* it is not zero and ptms_ptymax otherwise.
*/
/* Do not try to allocate more than allowed */
return (NULL);
}
}
ptms_inuse++;
/*
* Allocate new minor number. If this fails, all slots are busy and
* we need to grow the hash.
*/
if (dminor == 0) {
/* Grow the cache and retry allocation */
}
if (dminor == 0) {
/* Not enough memory now */
ptms_inuse--;
return (NULL);
}
/* Not enough memory - this entry can't be used now. */
ptms_inuse--;
} else {
}
return (pt);
}
/*
* Get pt_ttys structure by minor number.
* Returns NULL when minor is out of range.
*/
struct pt_ttys *
{
return (pt);
}
/*
* permission on a pty
*/
void
{
return;
/*
* is no pty backing it - so we have nothing to do.
*/
if (dminor == 0)
return;
}
}
/*
* returns:
* 1 if the pty is allocated to the current zone.
* 0 otherwise
*
* If the pty is allocated to the current zone, it also returns the owner.
*/
int
{
int ret;
/*
* it also. Report the owner as root. It belongs to all zones.
*/
if (dminor == 0) {
*ruid = 0;
*rgid = 0;
return (1);
}
ret = 0;
ret = 1;
}
}
return (ret);
}
/*
* returns:
* 0 if the pty is not allocated
* 1 if the pty is allocated
*/
int
{
int ret;
return (ret);
}
/*
* Close the pt and clear flags_to_clear.
* If pt device is not opened by someone else, free it and clear its slot.
*/
void
{
/* No one owns the entry - free it */
ASSERT(ptms_inuse > 0);
ptms_inuse--;
/* Return minor number to the pool of minors */
/* Return pt to the cache */
}
}
/*
* Allocate another slot table twice as large as the original one (limited to
* global maximum). Migrate all pt to the new slot table and free the original
* one. Create more /devices entries for new devices.
*/
static minor_t
{
/* Allocate new ptms array */
return ((minor_t)0);
/* Increase clone index space */
return ((minor_t)0);
}
/* Migrate pt entries to a new location */
/* Allocate minor number and return it */
}
/*ARGSUSED*/
static int
{
return (0);
}
/*ARGSUSED*/
static void
{
}
#ifdef DEBUG
void
{
if (ptms_debug) {
if (ptms_debug & 2)
if (ptms_debug & 4)
else
}
}
void
{
if (ptms_debug) {
if (ptms_debug & 2)
if (ptms_debug & 4)
else
}
}
#endif