1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 /* 33 * Pseudo Terminal Master Driver. 34 * 35 * The pseudo-tty subsystem simulates a terminal connection, where the master 36 * side represents the terminal and the slave represents the user process's 37 * special device end point. The master device is set up as a cloned device 38 * where its major device number is the major for the clone device and its minor 39 * device number is the major for the ptm driver. There are no nodes in the file 40 * system for master devices. The master pseudo driver is opened using the 41 * open(2) system call with /dev/ptmx as the device parameter. The clone open 42 * finds the next available minor device for the ptm major device. 43 * 44 * A master device is available only if it and its corresponding slave device 45 * are not already open. When the master device is opened, the corresponding 46 * slave device is automatically locked out. Only one open is allowed on a 47 * master device. Multiple opens are allowed on the slave device. After both 48 * the master and slave have been opened, the user has two file descriptors 49 * which are the end points of a full duplex connection composed of two streams 50 * which are automatically connected at the master and slave drivers. The user 51 * may then push modules onto either side of the stream pair. 52 * 53 * The master and slave drivers pass all messages to their adjacent queues. 54 * Only the M_FLUSH needs some processing. Because the read queue of one side 55 * is connected to the write queue of the other, the FLUSHR flag is changed to 56 * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP 57 * message is sent to the slave device which will render the device 58 * unusable. The process on the slave side gets the EIO when attempting to write 59 * on that stream but it will be able to read any data remaining on the stream 60 * head read queue. When all the data has been read, read() returns 0 61 * indicating that the stream can no longer be used. On the last close of the 62 * slave device, a 0-length message is sent to the master device. When the 63 * application on the master side issues a read() or getmsg() and 0 is returned, 64 * the user of the master device decides whether to issue a close() that 65 * dismantles the pseudo-terminal subsystem. If the master device is not closed, 66 * the pseudo-tty subsystem will be available to another user to open the slave 67 * device. 68 * 69 * If O_NONBLOCK or O_NDELAY is set, read on the master side returns -1 with 70 * errno set to EAGAIN if no data is available, and write returns -1 with errno 71 * set to EAGAIN if there is internal flow control. 72 * 73 * IOCTLS: 74 * 75 * ISPTM: determines whether the file descriptor is that of an open master 76 * device. Return code of zero indicates that the file descriptor 77 * represents master device. 78 * 79 * UNLKPT: unlocks the master and slave devices. It returns 0 on success. On 80 * failure, the errno is set to EINVAL indicating that the master 81 * device is not open. 82 * 83 * ZONEPT: sets the zone membership of ths associated pts device. 84 * 85 * Synchronization: 86 * 87 * All global data synchronization between ptm/pts is done via global 88 * ptms_lock mutex which is initialized at system boot time from 89 * ptms_initspace (called from space.c). 90 * 91 * Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and 92 * pt_nullmsg) are protected by pt_ttys.pt_lock mutex. 93 * 94 * PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks 95 * which allow reader locks to be reacquired by the same thread (usual 96 * reader/writer locks can't be used for that purpose since it is illegal for 97 * a thread to acquire a lock it already holds, even as a reader). The sole 98 * purpose of these macros is to guarantee that the peer queue will not 99 * disappear (due to closing peer) while it is used. It is safe to use 100 * PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since 101 * they are not real locks but reference counts). 102 * 103 * PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave 104 * open/close paths to modify ptm_rdq and pts_rdq fields. These fields should 105 * be set to appropriate queues *after* qprocson() is called during open (to 106 * prevent peer from accessing the queue with incomplete plumbing) and set to 107 * NULL before qprocsoff() is called during close. 108 * 109 * The pt_nullmsg field is only used in open/close routines and it is also 110 * protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex 111 * holds. 112 * 113 * Lock Ordering: 114 * 115 * If both ptms_lock and per-pty lock should be held, ptms_lock should always 116 * be entered first, followed by per-pty lock. 117 * 118 * See ptms.h, pts.c and ptms_conf.c for more information. 119 */ 120 121 #include <sys/types.h> 122 #include <sys/param.h> 123 #include <sys/file.h> 124 #include <sys/sysmacros.h> 125 #include <sys/stream.h> 126 #include <sys/stropts.h> 127 #include <sys/proc.h> 128 #include <sys/errno.h> 129 #include <sys/debug.h> 130 #include <sys/cmn_err.h> 131 #include <sys/ptms.h> 132 #include <sys/stat.h> 133 #include <sys/strsun.h> 134 #include <sys/systm.h> 135 #include <sys/modctl.h> 136 #include <sys/conf.h> 137 #include <sys/ddi.h> 138 #include <sys/sunddi.h> 139 #include <sys/zone.h> 140 141 #ifdef DEBUG 142 int ptm_debug = 0; 143 #define DBG(a) if (ptm_debug) cmn_err(CE_NOTE, a) 144 #else 145 #define DBG(a) 146 #endif 147 148 static int ptmopen(queue_t *, dev_t *, int, int, cred_t *); 149 static int ptmclose(queue_t *, int, cred_t *); 150 static void ptmwput(queue_t *, mblk_t *); 151 static void ptmrsrv(queue_t *); 152 static void ptmwsrv(queue_t *); 153 154 /* 155 * Master Stream Pseudo Terminal Module: stream data structure definitions 156 */ 157 158 static struct module_info ptm_info = { 159 0xdead, 160 "ptm", 161 0, 162 512, 163 512, 164 128 165 }; 166 167 static struct qinit ptmrint = { 168 NULL, 169 (int (*)()) ptmrsrv, 170 ptmopen, 171 ptmclose, 172 NULL, 173 &ptm_info, 174 NULL 175 }; 176 177 static struct qinit ptmwint = { 178 (int (*)()) ptmwput, 179 (int (*)()) ptmwsrv, 180 NULL, 181 NULL, 182 NULL, 183 &ptm_info, 184 NULL 185 }; 186 187 static struct streamtab ptminfo = { 188 &ptmrint, 189 &ptmwint, 190 NULL, 191 NULL 192 }; 193 194 static int ptm_attach(dev_info_t *, ddi_attach_cmd_t); 195 static int ptm_detach(dev_info_t *, ddi_detach_cmd_t); 196 static int ptm_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 197 198 static dev_info_t *ptm_dip; /* private devinfo pointer */ 199 200 /* 201 * this will define (struct cb_ops cb_ptm_ops) and (struct dev_ops ptm_ops) 202 */ 203 DDI_DEFINE_STREAM_OPS(ptm_ops, nulldev, nulldev, ptm_attach, ptm_detach, 204 nodev, ptm_devinfo, D_MP, &ptminfo); 205 206 /* 207 * Module linkage information for the kernel. 208 */ 209 210 static struct modldrv modldrv = { 211 &mod_driverops, /* Type of module. This one is a pseudo driver */ 212 "Master streams driver 'ptm' %I%", 213 &ptm_ops, /* driver ops */ 214 }; 215 216 static struct modlinkage modlinkage = { 217 MODREV_1, 218 &modldrv, 219 NULL 220 }; 221 222 int 223 _init(void) 224 { 225 int rc; 226 227 if ((rc = mod_install(&modlinkage)) == 0) 228 ptms_init(); 229 return (rc); 230 } 231 232 int 233 _fini(void) 234 { 235 return (mod_remove(&modlinkage)); 236 } 237 238 int 239 _info(struct modinfo *modinfop) 240 { 241 return (mod_info(&modlinkage, modinfop)); 242 } 243 244 static int 245 ptm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 246 { 247 if (cmd != DDI_ATTACH) 248 return (DDI_FAILURE); 249 250 if (ddi_create_minor_node(devi, "ptmajor", S_IFCHR, 251 0, DDI_PSEUDO, NULL) == DDI_FAILURE) { 252 ddi_remove_minor_node(devi, NULL); 253 return (DDI_FAILURE); 254 } 255 if (ddi_create_minor_node(devi, "ptmx", S_IFCHR, 256 0, DDI_PSEUDO, CLONE_DEV) == DDI_FAILURE) { 257 ddi_remove_minor_node(devi, NULL); 258 return (DDI_FAILURE); 259 } 260 ptm_dip = devi; 261 262 return (DDI_SUCCESS); 263 } 264 265 static int 266 ptm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 267 { 268 if (cmd != DDI_DETACH) 269 return (DDI_FAILURE); 270 271 ddi_remove_minor_node(devi, NULL); 272 return (DDI_SUCCESS); 273 } 274 275 /*ARGSUSED*/ 276 static int 277 ptm_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 278 void **result) 279 { 280 int error; 281 282 switch (infocmd) { 283 case DDI_INFO_DEVT2DEVINFO: 284 if (ptm_dip == NULL) { 285 error = DDI_FAILURE; 286 } else { 287 *result = (void *)ptm_dip; 288 error = DDI_SUCCESS; 289 } 290 break; 291 case DDI_INFO_DEVT2INSTANCE: 292 *result = (void *)0; 293 error = DDI_SUCCESS; 294 break; 295 default: 296 error = DDI_FAILURE; 297 } 298 return (error); 299 } 300 301 302 /* ARGSUSED */ 303 /* 304 * Open a minor of the master device. Store the write queue pointer and set the 305 * pt_state field to (PTMOPEN | PTLOCK). 306 * This code will work properly with both clone opens and direct opens of the 307 * master device. 308 */ 309 static int 310 ptmopen( 311 queue_t *rqp, /* pointer to the read side queue */ 312 dev_t *devp, /* pointer to stream tail's dev */ 313 int oflag, /* the user open(2) supplied flags */ 314 int sflag, /* open state flag */ 315 cred_t *credp) /* credentials */ 316 { 317 extern dev_info_t *pts_dip; 318 319 struct pt_ttys *ptmp; 320 mblk_t *mop; /* ptr to a setopts message block */ 321 struct stroptions *sop; 322 minor_t dminor = getminor(*devp); 323 324 /* Allow reopen */ 325 if (rqp->q_ptr != NULL) 326 return (0); 327 328 if (sflag & MODOPEN) 329 return (ENXIO); 330 331 if (!(sflag & CLONEOPEN) && dminor != 0) { 332 /* 333 * This is a direct open to specific master device through an 334 * artificially created entry with specific minor in 335 * /dev/directory. Such behavior is not supported. 336 */ 337 return (ENXIO); 338 } 339 340 /* 341 * pts dependency: pt_ttys_alloc(), used below, really needs the pts 342 * driver (and pts_dip variable) to be initialized to successfully 343 * create device nodes. 344 */ 345 if (pts_dip == NULL) 346 (void) i_ddi_attach_pseudo_node("pts"); 347 348 mop = allocb(sizeof (struct stroptions), BPRI_MED); 349 if (mop == NULL) { 350 DDBG("ptmopen(): mop allocation failed\n", 0); 351 return (ENOMEM); 352 } 353 354 if ((ptmp = pt_ttys_alloc()) == NULL) { 355 DDBG("ptmopen(): pty allocation failed\n", 0); 356 freemsg(mop); 357 return (ENOMEM); 358 } 359 360 dminor = ptmp->pt_minor; 361 362 DDBGP("ptmopen(): allocated ptmp %p\n", (uintptr_t)ptmp); 363 DDBG("ptmopen(): allocated minor %d\n", dminor); 364 365 WR(rqp)->q_ptr = rqp->q_ptr = ptmp; 366 367 qprocson(rqp); 368 369 /* Allow slave to send messages to master */ 370 PT_ENTER_WRITE(ptmp); 371 ptmp->ptm_rdq = rqp; 372 PT_EXIT_WRITE(ptmp); 373 374 /* 375 * set up hi/lo water marks on stream head read queue 376 * and add controlling tty if not set 377 */ 378 mop->b_datap->db_type = M_SETOPTS; 379 mop->b_wptr += sizeof (struct stroptions); 380 sop = (struct stroptions *)mop->b_rptr; 381 if (oflag & FNOCTTY) 382 sop->so_flags = SO_HIWAT | SO_LOWAT; 383 else 384 sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 385 sop->so_hiwat = 512; 386 sop->so_lowat = 256; 387 putnext(rqp, mop); 388 389 /* 390 * The input, devp, is a major device number, the output is put 391 * into the same parm as a major,minor pair. 392 */ 393 *devp = makedevice(getmajor(*devp), dminor); 394 395 return (0); 396 } 397 398 399 /* 400 * Find the address to private data identifying the slave's write queue. 401 * Send a hang-up message up the slave's read queue to designate the 402 * master/slave pair is tearing down. Uattach the master and slave by 403 * nulling out the write queue fields in the private data structure. 404 * Finally, unlock the master/slave pair and mark the master as closed. 405 */ 406 /*ARGSUSED1*/ 407 static int 408 ptmclose(queue_t *rqp, int flag, cred_t *credp) 409 { 410 struct pt_ttys *ptmp; 411 queue_t *pts_rdq; 412 413 ASSERT(rqp->q_ptr); 414 415 ptmp = (struct pt_ttys *)rqp->q_ptr; 416 PT_ENTER_READ(ptmp); 417 if (ptmp->pts_rdq) { 418 pts_rdq = ptmp->pts_rdq; 419 if (pts_rdq->q_next) { 420 DBG(("send hangup message to slave\n")); 421 (void) putnextctl(pts_rdq, M_HANGUP); 422 } 423 } 424 PT_EXIT_READ(ptmp); 425 /* 426 * ptm_rdq should be cleared before call to qprocsoff() to prevent pts 427 * write procedure to attempt using ptm_rdq after qprocsoff. 428 */ 429 PT_ENTER_WRITE(ptmp); 430 ptmp->ptm_rdq = NULL; 431 freemsg(ptmp->pt_nullmsg); 432 ptmp->pt_nullmsg = NULL; 433 /* 434 * qenable slave side write queue so that it can flush 435 * its messages as master's read queue is going away 436 */ 437 if (ptmp->pts_rdq) 438 qenable(WR(ptmp->pts_rdq)); 439 PT_EXIT_WRITE(ptmp); 440 441 qprocsoff(rqp); 442 443 /* Finish the close */ 444 rqp->q_ptr = NULL; 445 WR(rqp)->q_ptr = NULL; 446 447 ptms_close(ptmp, PTMOPEN | PTLOCK); 448 449 return (0); 450 } 451 452 /* 453 * The wput procedure will only handle ioctl and flush messages. 454 */ 455 static void 456 ptmwput(queue_t *qp, mblk_t *mp) 457 { 458 struct pt_ttys *ptmp; 459 struct iocblk *iocp; 460 461 DBG(("entering ptmwput\n")); 462 ASSERT(qp->q_ptr); 463 464 ptmp = (struct pt_ttys *)qp->q_ptr; 465 PT_ENTER_READ(ptmp); 466 467 switch (mp->b_datap->db_type) { 468 /* 469 * if write queue request, flush master's write 470 * queue and send FLUSHR up slave side. If read 471 * queue request, convert to FLUSHW and putnext(). 472 */ 473 case M_FLUSH: 474 { 475 unsigned char flush_flg = 0; 476 477 DBG(("ptm got flush request\n")); 478 if (*mp->b_rptr & FLUSHW) { 479 DBG(("got FLUSHW, flush ptm write Q\n")); 480 if (*mp->b_rptr & FLUSHBAND) 481 /* 482 * if it is a FLUSHBAND, do flushband. 483 */ 484 flushband(qp, *(mp->b_rptr + 1), 485 FLUSHDATA); 486 else 487 flushq(qp, FLUSHDATA); 488 flush_flg = (*mp->b_rptr & ~FLUSHW) | FLUSHR; 489 } 490 if (*mp->b_rptr & FLUSHR) { 491 DBG(("got FLUSHR, set FLUSHW\n")); 492 flush_flg |= (*mp->b_rptr & ~FLUSHR) | FLUSHW; 493 } 494 if (flush_flg != 0 && ptmp->pts_rdq && 495 !(ptmp->pt_state & PTLOCK)) { 496 DBG(("putnext to pts\n")); 497 *mp->b_rptr = flush_flg; 498 putnext(ptmp->pts_rdq, mp); 499 } else 500 freemsg(mp); 501 break; 502 } 503 504 case M_IOCTL: 505 iocp = (struct iocblk *)mp->b_rptr; 506 switch (iocp->ioc_cmd) { 507 default: 508 if ((ptmp->pt_state & PTLOCK) || 509 (ptmp->pts_rdq == NULL)) { 510 DBG(("got M_IOCTL but no slave\n")); 511 miocnak(qp, mp, 0, EINVAL); 512 PT_EXIT_READ(ptmp); 513 return; 514 } 515 (void) putq(qp, mp); 516 break; 517 case UNLKPT: 518 mutex_enter(&ptmp->pt_lock); 519 ptmp->pt_state &= ~PTLOCK; 520 mutex_exit(&ptmp->pt_lock); 521 /*FALLTHROUGH*/ 522 case ISPTM: 523 DBG(("ack the UNLKPT/ISPTM\n")); 524 miocack(qp, mp, 0, 0); 525 break; 526 case ZONEPT: 527 { 528 zoneid_t z; 529 int error; 530 531 if ((error = drv_priv(iocp->ioc_cr)) != 0) { 532 miocnak(qp, mp, 0, error); 533 break; 534 } 535 if ((error = miocpullup(mp, sizeof (zoneid_t))) != 0) { 536 miocnak(qp, mp, 0, error); 537 break; 538 } 539 z = *((zoneid_t *)mp->b_cont->b_rptr); 540 if (z < MIN_ZONEID || z > MAX_ZONEID) { 541 miocnak(qp, mp, 0, EINVAL); 542 break; 543 } 544 545 mutex_enter(&ptmp->pt_lock); 546 ptmp->pt_zoneid = z; 547 mutex_exit(&ptmp->pt_lock); 548 miocack(qp, mp, 0, 0); 549 break; 550 } 551 } 552 break; 553 554 case M_READ: 555 /* Caused by ldterm - can not pass to slave */ 556 freemsg(mp); 557 break; 558 559 /* 560 * send other messages to slave 561 */ 562 default: 563 if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) { 564 DBG(("got msg. but no slave\n")); 565 mp = mexchange(NULL, mp, 2, M_ERROR, -1); 566 if (mp != NULL) { 567 mp->b_rptr[0] = NOERROR; 568 mp->b_rptr[1] = EINVAL; 569 qreply(qp, mp); 570 } 571 PT_EXIT_READ(ptmp); 572 return; 573 } 574 DBG(("put msg on master's write queue\n")); 575 (void) putq(qp, mp); 576 break; 577 } 578 DBG(("return from ptmwput()\n")); 579 PT_EXIT_READ(ptmp); 580 } 581 582 583 /* 584 * enable the write side of the slave. This triggers the 585 * slave to send any messages queued on its write side to 586 * the read side of this master. 587 */ 588 static void 589 ptmrsrv(queue_t *qp) 590 { 591 struct pt_ttys *ptmp; 592 593 DBG(("entering ptmrsrv\n")); 594 ASSERT(qp->q_ptr); 595 596 ptmp = (struct pt_ttys *)qp->q_ptr; 597 PT_ENTER_READ(ptmp); 598 if (ptmp->pts_rdq) { 599 qenable(WR(ptmp->pts_rdq)); 600 } 601 PT_EXIT_READ(ptmp); 602 DBG(("leaving ptmrsrv\n")); 603 } 604 605 606 /* 607 * If there are messages on this queue that can be sent to 608 * slave, send them via putnext(). Else, if queued messages 609 * cannot be sent, leave them on this queue. If priority 610 * messages on this queue, send them to slave no matter what. 611 */ 612 static void 613 ptmwsrv(queue_t *qp) 614 { 615 struct pt_ttys *ptmp; 616 mblk_t *mp; 617 618 DBG(("entering ptmwsrv\n")); 619 ASSERT(qp->q_ptr); 620 621 ptmp = (struct pt_ttys *)qp->q_ptr; 622 PT_ENTER_READ(ptmp); 623 if ((ptmp->pt_state & PTLOCK) || (ptmp->pts_rdq == NULL)) { 624 DBG(("in master write srv proc but no slave\n")); 625 /* 626 * Free messages on the write queue and send 627 * NAK for any M_IOCTL type messages to wakeup 628 * the user process waiting for ACK/NAK from 629 * the ioctl invocation 630 */ 631 while ((mp = getq(qp)) != NULL) { 632 if (mp->b_datap->db_type == M_IOCTL) 633 miocnak(qp, mp, 0, EINVAL); 634 else 635 freemsg(mp); 636 } 637 flushq(qp, FLUSHALL); 638 639 mp = mexchange(NULL, NULL, 2, M_ERROR, -1); 640 if (mp != NULL) { 641 mp->b_rptr[0] = NOERROR; 642 mp->b_rptr[1] = EINVAL; 643 qreply(qp, mp); 644 } 645 PT_EXIT_READ(ptmp); 646 return; 647 } 648 /* 649 * while there are messages on this write queue... 650 */ 651 while ((mp = getq(qp)) != NULL) { 652 /* 653 * if don't have control message and cannot put 654 * msg. on slave's read queue, put it back on 655 * this queue. 656 */ 657 if (mp->b_datap->db_type <= QPCTL && 658 !bcanputnext(ptmp->pts_rdq, mp->b_band)) { 659 DBG(("put msg. back on queue\n")); 660 (void) putbq(qp, mp); 661 break; 662 } 663 /* 664 * else send the message up slave's stream 665 */ 666 DBG(("send message to slave\n")); 667 putnext(ptmp->pts_rdq, mp); 668 } 669 DBG(("leaving ptmwsrv\n")); 670 PT_EXIT_READ(ptmp); 671 } 672