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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * This file contains global data and code shared between master and slave parts
30 * of the pseudo-terminal driver.
32 * Pseudo terminals (or pt's for short) are allocated dynamically.
33 * pt's are put in the global ptms_slots array indexed by minor numbers.
35 * The slots array is initially small (of the size NPTY_MIN). When more pt's are
36 * needed than the slot array size, the larger slot array is allocated and all
37 * opened pt's move to the new one.
39 * Resource allocation:
41 * pt_ttys structures are allocated via pt_ttys_alloc, which uses
43 * Minor number space is allocated via vmem_alloc() interface.
44 * ptms_slots arrays are allocated via kmem_alloc().
46 * Minors are started from 1 instead of 0 because vmem_alloc returns 0 in case
47 * of failure. Also, in anticipation of removing clone device interface to
48 * pseudo-terminal subsystem, minor 0 should not be used. (Potential future
51 * After the table slot size reaches pt_maxdelta, we stop 2^N extension
52 * algorithm and start extending the slot table size by pt_maxdelta.
54 * Device entries /dev/pts directory are created dynamically by the
55 * /dev filesystem. We no longer call ddi_create_minor_node() on
56 * behalf of the slave driver. The /dev filesystem creates /dev/pts
57 * nodes based on the pt_ttys array.
61 * All global data synchronization between ptm/pts is done via global
62 * ptms_lock mutex which is implicitly initialized by declaring it global.
64 * Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
65 * pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
67 * PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
68 * which allow reader locks to be reacquired by the same thread (usual
69 * reader/writer locks can't be used for that purpose since it is illegal for
70 * a thread to acquire a lock it already holds, even as a reader). The sole
71 * purpose of these macros is to guarantee that the peer queue will not
72 * disappear (due to closing peer) while it is used. It is safe to use
73 * PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
74 * they are not real locks but reference counts).
76 * PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
77 * open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
78 * be set to appropriate queues *after* qprocson() is called during open (to
79 * prevent peer from accessing the queue with incomplete plumbing) and set to
80 * NULL before qprocsoff() is called during close. Put and service procedures
81 * use PT_ENTER_READ/PT_EXIT_READ to prevent peer closes.
83 * The pt_nullmsg field is only used in open/close routines and is also
84 * protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
89 * If both ptms_lock and per-pty lock should be held, ptms_lock should always
90 * be entered first, followed by per-pty lock.
94 * void ptms_init(void);
96 * Called by pts/ptm _init entry points. It performes one-time
97 * initialization needed for both pts and ptm. This initialization is done
98 * here and not in ptms_initspace because all these data structures are not
99 * needed if pseudo-terminals are not used in the system.
101 * struct pt_ttys *pt_ttys_alloc(void);
103 * Allocate new minor number and pseudo-terminal entry. May sleep.
104 * New minor number is recorded in pt_minor field of the entry returned.
105 * This routine also initializes pt_minor and pt_state fields of the new
106 * pseudo-terminal and puts a pointer to it into ptms_slots array.
108 * struct pt_ttys *ptms_minor2ptty(minor_t minor)
110 * Find pt_ttys structure by minor number.
111 * Returns NULL when minor is out of range.
113 * int ptms_minor_valid(minor_t minor, uid_t *ruid, gid_t *rgid)
115 * Check if minor refers to an allocated pty in the current zone.
117 * 0 if not allocated or not for this zone.
118 * 1 if an allocated pty in the current zone.
119 * Also returns owner of pty.
121 * int ptms_minor_exists(minor_t minor)
122 * Check if minor refers to an allocated pty (in any zone)
124 * 0 if not an allocated pty
125 * 1 if an allocated pty
127 * void ptms_set_owner(minor_t minor, uid_t ruid, gid_t rgid)
129 * Sets the owner associated with a pty.
131 * void ptms_close(struct pt_ttys *pt, uint_t flags_to_clear);
133 * Clear flags_to_clear in pt and if no one owns it (PTMOPEN/PTSOPEN not
134 * set) free pt entry and corresponding slot.
136 * Tuneables and configuration:
138 * pt_cnt: minimum number of pseudo-terminals in the system. The system
139 * should provide at least this number of ptys (provided sufficient
140 * memory is available). It is different from the older semantics
141 * of pt_cnt meaning maximum number of ptys.
142 * Set to 0 by default.
144 * pt_max_pty: Maximum number of pseudo-terminals in the system. The system
145 * should not allocate more ptys than pt_max_pty (although, it may
146 * impose stricter maximum). Zero value means no user-defined
147 * maximum. This is intended to be used as "denial-of-service"
149 * Set to 0 by default.
151 * Both pt_cnt and pt_max_pty may be modified during system lifetime
152 * with their semantics preserved.
154 * pt_init_cnt: Initial size of ptms_slots array. Set to NPTY_INITIAL.
156 * pt_ptyofmem: Approximate percentage of system memory that may be
157 * occupied by pty data structures. Initially set to NPTY_PERCENT.
158 * This variable is used once during initialization to estimate
159 * maximum number of ptys in the system. The actual maximum is
160 * determined as minimum of pt_max_pty and calculated value.
162 * pt_maxdelta: Maximum extension chunk of the slot table.
167 #include <sys/types.h>
168 #include <sys/param.h>
169 #include <sys/termios.h>
170 #include <sys/stream.h>
171 #include <sys/stropts.h>
172 #include <sys/kmem.h>
173 #include <sys/ptms.h>
174 #include <sys/stat.h>
175 #include <sys/sunddi.h>
177 #include <sys/bitmap.h>
178 #include <sys/sysmacros.h>
179 #include <sys/ddi_impldefs.h>
180 #include <sys/zone.h>
182 #include <sys/strlog.h>
186 /* Initial number of ptms slots */
187 #define NPTY_INITIAL 16
189 #define NPTY_PERCENT 5
191 /* Maximum increment of the slot table size */
192 #define PTY_MAXDELTA 128
195 * Tuneable variables.
197 uint_t pt_cnt
= 0; /* Minimum number of ptys */
198 size_t pt_max_pty
= 0; /* Maximum number of ptys */
199 uint_t pt_init_cnt
= NPTY_INITIAL
; /* Initial number of ptms slots */
200 uint_t pt_pctofmem
= NPTY_PERCENT
; /* Percent of memory to use for ptys */
201 uint_t pt_maxdelta
= PTY_MAXDELTA
; /* Max increment for slot table size */
203 /* Other global variables */
205 kmutex_t ptms_lock
; /* Global data access lock */
208 * Slot array and its management variables
210 static struct pt_ttys
**ptms_slots
= NULL
; /* Slots for actual pt structures */
211 static size_t ptms_nslots
= 0; /* Size of slot array */
212 static size_t ptms_ptymax
= 0; /* Maximum number of ptys */
213 static size_t ptms_inuse
= 0; /* # of ptys currently allocated */
215 dev_info_t
*pts_dip
= NULL
; /* set if slave is attached */
217 static struct kmem_cache
*ptms_cache
= NULL
; /* pty cache */
219 static vmem_t
*ptms_minor_arena
= NULL
; /* Arena for device minors */
221 static uint_t
ptms_roundup(uint_t
);
222 static int ptms_constructor(void *, void *, int);
223 static void ptms_destructor(void *, void *);
224 static minor_t
ptms_grow(void);
227 * Total size occupied by one pty. Each pty master/slave pair consumes one
228 * pointer for ptms_slots array, one pt_ttys structure and one empty message
229 * preallocated for pts close.
232 #define PTY_SIZE (sizeof (struct pt_ttys) + \
233 sizeof (struct pt_ttys *) + \
242 * Clear all bits of x except the highest bit
244 #define truncate(x) ((x) <= 2 ? (x) : (1 << (highbit(x) - 1)))
247 * Roundup the number to the nearest power of 2
250 ptms_roundup(uint_t x
)
252 uint_t p
= truncate(x
); /* x with non-high bits stripped */
255 * If x is a power of 2, return x, otherwise roundup.
257 return (p
== x
? p
: (p
* 2));
261 * Allocate ptms_slots array and kmem cache for pt_ttys. This initialization is
262 * only called once during system lifetime. Called from ptm or pts _init
268 mutex_enter(&ptms_lock
);
270 if (ptms_slots
== NULL
) {
271 ptms_slots
= kmem_zalloc(pt_init_cnt
*
272 sizeof (struct pt_ttys
*), KM_SLEEP
);
274 ptms_cache
= kmem_cache_create("pty_map",
275 sizeof (struct pt_ttys
), 0, ptms_constructor
,
276 ptms_destructor
, NULL
, NULL
, NULL
, 0);
278 ptms_nslots
= pt_init_cnt
;
280 /* Allocate integer space for minor numbers */
281 ptms_minor_arena
= vmem_create("ptms_minor", (void *)1,
282 ptms_nslots
, 1, NULL
, NULL
, NULL
, 0,
283 VM_SLEEP
| VMC_IDENTIFIER
);
286 * Calculate available number of ptys - how many ptys can we
287 * allocate in pt_pctofmem % of available memory. The value is
288 * rounded up to the nearest power of 2.
290 ptms_ptymax
= ptms_roundup((pt_pctofmem
* kmem_maxavail()) /
293 mutex_exit(&ptms_lock
);
297 * This routine attaches the pts dip.
300 ptms_attach_slave(void)
302 if (pts_dip
== NULL
&& i_ddi_attach_pseudo_node("pts") == NULL
)
310 * Called from /dev fs. Checks if dip is attached,
311 * and if it is, returns its major number.
314 ptms_slave_attached(void)
316 major_t maj
= DDI_MAJOR_T_NONE
;
318 mutex_enter(&ptms_lock
);
320 maj
= ddi_driver_major(pts_dip
);
321 mutex_exit(&ptms_lock
);
327 * Allocate new minor number and pseudo-terminal entry. Returns the new entry or
328 * NULL if no memory or maximum number of entries reached.
334 struct pt_ttys
*pt
= NULL
;
336 mutex_enter(&ptms_lock
);
339 * Always try to allocate new pty when pt_cnt minimum limit is not
340 * achieved. If it is achieved, the maximum is determined by either
341 * user-specified value (if it is non-zero) or our memory estimations -
344 if (ptms_inuse
>= pt_cnt
) {
346 * When system achieved required minimum of ptys, check for the
347 * denial of service limits.
349 * Since pt_max_pty may be zero, the formula below is used to
350 * avoid conditional expression. It will equal to pt_max_pty if
351 * it is not zero and ptms_ptymax otherwise.
353 size_t user_max
= (pt_max_pty
== 0 ? ptms_ptymax
: pt_max_pty
);
355 /* Do not try to allocate more than allowed */
356 if (ptms_inuse
>= min(ptms_ptymax
, user_max
)) {
357 mutex_exit(&ptms_lock
);
364 * Allocate new minor number. If this fails, all slots are busy and
365 * we need to grow the hash.
367 dminor
= (minor_t
)(uintptr_t)
368 vmem_alloc(ptms_minor_arena
, 1, VM_NOSLEEP
);
371 /* Grow the cache and retry allocation */
372 dminor
= ptms_grow();
376 /* Not enough memory now */
378 mutex_exit(&ptms_lock
);
382 pt
= kmem_cache_alloc(ptms_cache
, KM_NOSLEEP
);
384 /* Not enough memory - this entry can't be used now. */
385 vmem_free(ptms_minor_arena
, (void *)(uintptr_t)dminor
, 1);
388 pt
->pt_minor
= dminor
;
389 pt
->pt_pid
= curproc
->p_pid
; /* For debugging */
390 pt
->pt_state
= (PTMOPEN
| PTLOCK
);
391 pt
->pt_zoneid
= getzoneid();
392 pt
->pt_ruid
= 0; /* we don't know uid/gid yet. Report as root */
394 ASSERT(ptms_slots
[dminor
- 1] == NULL
);
395 ptms_slots
[dminor
- 1] = pt
;
398 mutex_exit(&ptms_lock
);
403 * Get pt_ttys structure by minor number.
404 * Returns NULL when minor is out of range.
407 ptms_minor2ptty(minor_t dminor
)
409 struct pt_ttys
*pt
= NULL
;
411 ASSERT(mutex_owned(&ptms_lock
));
412 if ((dminor
>= 1) && (dminor
<= ptms_nslots
) && ptms_slots
!= NULL
)
413 pt
= ptms_slots
[dminor
- 1];
419 * Invoked in response to chown on /dev/pts nodes to change the
420 * permission on a pty
423 ptms_set_owner(minor_t dminor
, uid_t ruid
, gid_t rgid
)
430 if (ruid
< 0 || rgid
< 0)
434 * /dev/pts/0 is not used, but some applications may check it. There
435 * is no pty backing it - so we have nothing to do.
440 mutex_enter(&ptms_lock
);
441 pt
= ptms_minor2ptty(dminor
);
442 if (pt
!= NULL
&& pt
->pt_zoneid
== getzoneid()) {
446 mutex_exit(&ptms_lock
);
450 * Given a ptm/pts minor number
452 * 1 if the pty is allocated to the current zone.
455 * If the pty is allocated to the current zone, it also returns the owner.
458 ptms_minor_valid(minor_t dminor
, uid_t
*ruid
, gid_t
*rgid
)
470 * /dev/pts/0 is not used, but some applications may check it, so create
471 * it also. Report the owner as root. It belongs to all zones.
480 mutex_enter(&ptms_lock
);
481 pt
= ptms_minor2ptty(dminor
);
483 ASSERT(pt
->pt_ruid
>= 0);
484 ASSERT(pt
->pt_rgid
>= 0);
485 if (pt
->pt_zoneid
== getzoneid()) {
491 mutex_exit(&ptms_lock
);
497 * Given a ptm/pts minor number
499 * 0 if the pty is not allocated
500 * 1 if the pty is allocated
503 ptms_minor_exists(minor_t dminor
)
507 mutex_enter(&ptms_lock
);
508 ret
= ptms_minor2ptty(dminor
) ? 1 : 0;
509 mutex_exit(&ptms_lock
);
515 * Close the pt and clear flags_to_clear.
516 * If pt device is not opened by someone else, free it and clear its slot.
519 ptms_close(struct pt_ttys
*pt
, uint_t flags_to_clear
)
523 ASSERT(MUTEX_NOT_HELD(&ptms_lock
));
526 mutex_enter(&ptms_lock
);
528 mutex_enter(&pt
->pt_lock
);
529 pt
->pt_state
&= ~flags_to_clear
;
530 flags
= pt
->pt_state
;
531 mutex_exit(&pt
->pt_lock
);
533 if (! (flags
& (PTMOPEN
| PTSOPEN
))) {
534 /* No one owns the entry - free it */
536 ASSERT(pt
->ptm_rdq
== NULL
);
537 ASSERT(pt
->pts_rdq
== NULL
);
538 ASSERT(pt
->pt_nullmsg
== NULL
);
539 ASSERT(pt
->pt_refcnt
== 0);
540 ASSERT(pt
->pt_minor
<= ptms_nslots
);
541 ASSERT(ptms_slots
[pt
->pt_minor
- 1] == pt
);
542 ASSERT(ptms_inuse
> 0);
548 ptms_slots
[pt
->pt_minor
- 1] = NULL
;
549 /* Return minor number to the pool of minors */
550 vmem_free(ptms_minor_arena
, (void *)(uintptr_t)pt
->pt_minor
, 1);
551 /* Return pt to the cache */
552 kmem_cache_free(ptms_cache
, pt
);
554 mutex_exit(&ptms_lock
);
558 * Allocate another slot table twice as large as the original one (limited to
559 * global maximum). Migrate all pt to the new slot table and free the original
560 * one. Create more /devices entries for new devices.
565 minor_t old_size
= ptms_nslots
;
566 minor_t delta
= MIN(pt_maxdelta
, old_size
);
567 minor_t new_size
= old_size
+ delta
;
568 struct pt_ttys
**ptms_old
= ptms_slots
;
569 struct pt_ttys
**ptms_new
;
570 void *vaddr
; /* vmem_add return value */
572 ASSERT(MUTEX_HELD(&ptms_lock
));
574 DDBG("ptmopen(%d): need to grow\n", (int)ptms_inuse
);
576 /* Allocate new ptms array */
577 ptms_new
= kmem_zalloc(new_size
* sizeof (struct pt_ttys
*),
579 if (ptms_new
== NULL
)
582 /* Increase clone index space */
583 vaddr
= vmem_add(ptms_minor_arena
, (void *)(uintptr_t)(old_size
+ 1),
584 new_size
- old_size
, VM_NOSLEEP
);
587 kmem_free(ptms_new
, new_size
* sizeof (struct pt_ttys
*));
591 /* Migrate pt entries to a new location */
592 ptms_nslots
= new_size
;
593 bcopy(ptms_old
, ptms_new
, old_size
* sizeof (struct pt_ttys
*));
594 ptms_slots
= ptms_new
;
595 kmem_free(ptms_old
, old_size
* sizeof (struct pt_ttys
*));
597 /* Allocate minor number and return it */
598 return ((minor_t
)(uintptr_t)
599 vmem_alloc(ptms_minor_arena
, 1, VM_NOSLEEP
));
604 ptms_constructor(void *maddr
, void *arg
, int kmflags
)
606 struct pt_ttys
*pt
= maddr
;
610 pt
->pt_nullmsg
= NULL
;
615 pt
->pt_zoneid
= GLOBAL_ZONEID
;
617 cv_init(&pt
->pt_cv
, NULL
, CV_DEFAULT
, NULL
);
618 mutex_init(&pt
->pt_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
624 ptms_destructor(void *maddr
, void *arg
)
626 struct pt_ttys
*pt
= maddr
;
628 ASSERT(pt
->pt_refcnt
== 0);
629 ASSERT(pt
->pt_state
== 0);
630 ASSERT(pt
->ptm_rdq
== NULL
);
631 ASSERT(pt
->pts_rdq
== NULL
);
633 mutex_destroy(&pt
->pt_lock
);
634 cv_destroy(&pt
->pt_cv
);
639 ptms_log(char *str
, uint_t arg
)
643 cmn_err(CE_CONT
, str
, arg
);
645 (void) strlog(PTMOD_ID
, -1, 0, SL_TRACE
| SL_ERROR
,
648 (void) strlog(PTMOD_ID
, -1, 0, SL_TRACE
, str
, arg
);
653 ptms_logp(char *str
, uintptr_t arg
)
657 cmn_err(CE_CONT
, str
, arg
);
659 (void) strlog(PTMOD_ID
, -1, 0, SL_TRACE
| SL_ERROR
,
662 (void) strlog(PTMOD_ID
, -1, 0, SL_TRACE
, str
, arg
);