zcons.c revision 27e6fb2101ae37ba3de6dbb1567bf7558ffaccad
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Zone Console Driver.
*
* for system zones. Its implementation is straightforward. Each instance
* of the driver represents a global-zone/local-zone pair (this maps in a
* straightforward way to the commonly used terminal notion of "master side"
* and "slave side", and we use that terminology throughout).
*
* by zoneadmd in userland, using the devctl framework; thus the driver
* does not need to maintain any sort of "admin" node.
*
* The driver shuttles I/O from master side to slave side and back. In a break
* it will simply be discarded. This is so that if zoneadmd is not holding
* the master side console open (i.e. it has died somehow), processes in
* the zone do not experience any errors and I/O to the console does not
* hang.
*
* TODO: we may want to revisit the other direction; i.e. we may want
* zoneadmd to be able to detect whether no zone processes are holding the
* console open, an unusual situation.
*/
#include <sys/sysmacros.h>
/*
* The instance number is encoded in the dev_t in the minor number; the lowest
* bit of the minor number is used to track the master vs. slave side of the
* virtual console. The rest of the bits in the minor number are the instance.
*/
#define ZC_MASTER_MINOR 0
#define ZC_SLAVE_MINOR 1
int zcons_debug = 0;
/*
* Zone Console Pseudo Terminal Module: stream data structure definitions
*/
static struct module_info zc_info = {
31337, /* c0z we r hAx0rs */
"zcons",
0,
2048,
128
};
NULL,
(int (*)()) zc_rsrv,
NULL,
&zc_info,
};
(int (*)()) zc_wput,
(int (*)()) zc_wsrv,
NULL,
NULL,
NULL,
&zc_info,
};
static struct streamtab zc_tab_info = {
&zc_rinit,
&zc_winit,
NULL,
};
/*
* this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops)
*/
/*
* Module linkage information for the kernel.
*/
&mod_driverops, /* Type of module. This one is a pseudo driver */
"Zone console driver 'zcons' %I%",
&zc_ops /* driver ops */
};
static struct modlinkage modlinkage = {
&modldrv,
};
typedef struct zc_state {
int zc_state;
} zc_state_t;
#define ZC_STATE_MOPEN 0x01
#define ZC_STATE_SOPEN 0x02
static void *zc_soft_state;
int
_init(void)
{
int err;
sizeof (zc_state_t), 0)) != 0) {
return (err);
}
return (err);
}
int
_fini(void)
{
int err;
return (err);
}
return (0);
}
int
{
}
static int
{
int instance;
if (cmd != DDI_ATTACH)
return (DDI_FAILURE);
return (DDI_FAILURE);
return (DDI_FAILURE);
}
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
{
int instance;
if (cmd != DDI_DETACH)
return (DDI_FAILURE);
return (DDI_FAILURE);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
/*
* zc_getinfo()
* getinfo(9e) entrypoint.
*/
/*ARGSUSED*/
static int
{
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
return (DDI_FAILURE);
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
return (DDI_SUCCESS);
}
return (DDI_FAILURE);
}
/*
* Return the equivalent queue from the other side of the relationship.
* e.g.: given the slave's write queue, return the master's write queue.
*/
static queue_t *
{
return (zcs->zc_slave_rdq);
return (zcs->zc_master_rdq);
else
return (NULL);
}
/*
* For debugging and outputting messages. Returns the name of the side of
* the relationship associated with this queue.
*/
static const char *
{
return ("master");
}
return ("slave");
}
/*ARGSUSED*/
static int
int oflag, /* the user open(2) supplied flags */
int sflag, /* open state flag */
{
struct stroptions *sop;
/*
* Enforce exclusivity on the master side; the only consumer should
* be the zoneadmd for the zone.
*/
return (EBUSY);
DBG("zc_master_open(): mop allocation failed\n");
return (ENOMEM);
}
/*
* q_ptr stores driver private data; stash the soft state data on both
* read and write sides of the queue.
*/
/*
* Following qprocson(), the master side is fully plumbed into the
* will allow the slave to send messages to us (the master).
* This cannot occur before qprocson() because the master is not
* ready to process them until that point.
*/
/*
* controlling tty as needed.
*/
else
return (0);
}
/*ARGSUSED*/
static int
int oflag, /* the user open(2) supplied flags */
int sflag, /* open state flag */
{
struct stroptions *sop;
/*
* The slave side can be opened as many times as needed.
*/
return (0);
}
DBG("zc_slave_open(): mop allocation failed\n");
return (ENOMEM);
}
/*
* q_ptr stores driver private data; stash the soft state data on both
* read and write sides of the queue.
*/
/*
* Must follow qprocson(), since we aren't ready to process until then.
*/
/*
* controlling tty as needed.
*/
return (0);
}
/*
* open(9e) entrypoint; checks sflag, and rejects anything unordinary.
*/
static int
int oflag, /* the user open(2) supplied flags */
int sflag, /* open state flag */
{
int ret;
if (sflag != 0)
return (EINVAL);
return (ENXIO);
case ZC_MASTER_MINOR:
break;
case ZC_SLAVE_MINOR:
break;
default:
break;
}
return (ret);
}
/*
* close(9e) entrypoint.
*/
/*ARGSUSED1*/
static int
{
DBG("Closing master side");
/*
* qenable slave side write queue so that it can flush
* its messages as master's read queue is going away
*/
}
DBG("Closing slave side");
else
}
/*
* Qenable master side write queue so that it can flush its
* messages as slaves's read queue is going away.
*/
}
return (0);
}
static void
{
/*
* FLUSHW only. Change to FLUSHR and putnext other side,
* then we are done.
*/
return;
}
/*
* It is a FLUSHRW; we copy the mblk and send
* it to the other side, since we still need to use
* the mblk in FLUSHR processing, below.
*/
}
}
DBG("qreply(qp) turning FLUSHR around\n");
return;
}
}
/*
* wput(9E) is symmetric for master and slave sides, so this handles both
* without splitting the codepath.
*
* zc_wput() looks at the other side; if there is no process holding that
* side open, it frees the message. This prevents processes from hanging
* if no one is holding open the console. Otherwise, it putnext's high
* priority messages, putnext's normal messages if possible, and otherwise
* enqueues the messages; in the case that something is enqueued, wsrv(9E)
* will take care of eventually shuttling I/O to the other side.
*/
static void
{
switch (type) {
case M_FLUSH:
break;
case M_IOCTL:
break;
default:
break;
}
return;
}
switch (type) {
case M_READ: /* supposedly from ldterm? */
DBG("zc_wput: tossing M_READ\n");
break;
case M_FLUSH:
break;
default:
/*
* Put this to the other side.
*/
break;
}
return;
}
/*
* Only putnext if there isn't already something in the queue.
* otherwise things would wind up out of order.
*/
DBG("wput: putting message to other side\n");
} else {
DBG("wput: putting msg onto queue\n");
}
}
/*
* rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both
* without splitting up the codepath.
*
* Enable the write side of the partner. This triggers the partner to send
* messages queued on its write side to this queue's read side.
*/
static void
{
/*
* Care must be taken here, as either of the master or slave side
* qptr could be NULL.
*/
DBG("zc_rsrv: other side isn't listening\n");
return;
}
}
/*
* This routine is symmetric for master and slave, so it handles both without
* splitting up the codepath.
*
* If there are messages on this queue that can be sent to the other, send
* them via putnext(). Else, if queued messages cannot be sent, leave them
* on this queue.
*/
static void
{
/*
* Partner has no read queue, so take the data, and throw it away.
*/
DBG("zc_wsrv: other side isn't listening");
else
}
return;
}
/*
* while there are messages on this write queue...
*/
/*
* Due to the way zc_wput is implemented, we should never
* see a control message here.
*/
DBG("wsrv: send message to other side\n");
} else {
DBG("wsrv: putting msg back on queue\n");
break;
}
}
}