iwscons.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Redirecting driver; used to handle workstation console redirection.
*
* Redirects all I/O through a given device instance to the device designated
* as the current target, as given by the vnode associated with the first
* entry in the list of redirections for the given device instance. The
* implementation assumes that this vnode denotes a STREAMS device; this is
* perhaps a bug.
*
* Supports the SRIOCSREDIR ioctl for designating a new redirection target.
* The new target is added to the front of a list of potentially active
* designees. Should the device at the front of this list be closed, the new
* front entry assumes active duty. (Stated differently, redirection targets
* stack, except that it's possible for entries in the interior of the stack
* to go away.)
*
* Supports the SRIOCISREDIR ioctl for inquiring whether the descriptor given
* as argument is the current front of the redirection list associated with
* the descriptor on which the ioctl was issued.
*
* Every open instance of this driver corresponds to an instance of the
* underlying client driver. If the redirection stack would otherwise become
* empty, this device (designated by the wd_vp field of the wcd_data
* structure) is implicitly opened and added to the front of the list. Thus,
* there's always an active device for handling i/o through an open instance
* of this driver.
*
* XXX: Names -- many of the names in this driver and its companion STREAMS
* module still reflect its origins as the workstation console
* redirection driver. Ultimately, they should be changed to reflect the
* fact that this driver is potentially a general purpose redirection
* driver. In the meantime, the driver is still specialized to have a
* single client -- the workstation console driver -- and its file name
* remains iwscons.c to reflect that specialization.
*
* Proposed change: "iwscn" becomes either "dr" (for "streams redirecting
* driver") or "srm" (for "streams redirecting module"), as appropriate.
*
* XXX: Add mechanism for notifying a redirectee that it's no longer the
* current redirectee? (This in contrast to the current facility for
* letting it ask.)
*/
#include <sys/sysmacros.h>
#include <sys/strredir.h>
/*
* Private copy of devinfo pointer; iwscninfo uses it.
*/
static dev_info_t *iwscn_dip;
struct cb_ops iwscn_cb_ops = {
iwscnopen, /* open */
iwscnclose, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
iwscnread, /* read */
iwscnwrite, /* write */
iwscnioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
iwscnpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
0, /* streamtab */
};
DEVO_REV, /* devo_rev, */
0, /* refcnt */
iwscninfo, /* info */
nulldev, /* identify */
nulldev, /* probe */
iwscnattach, /* attach */
nodev, /* detach */
nodev, /* reset */
&iwscn_cb_ops, /* driver operations */
NULL /* bus operations */
};
/*
* Sometime the open would hang due to a pending read. The new lock
* iwscn_open_lock and the read lock are held in open to assure a single
*/
/*
* These next two fields, protected by iwscn_lock, pass the data to wcmopen()
* from the ioctl SRIOCSREDIR. wcmopen() uses the data only if the thread
* matches. This keeps other threads from interfering.
*/
/* push redirm */
/*
* Forward declarations of private routines.
*/
static void sridealloc(wcd_data_t *);
/*
* The head of the list of open instances.
*/
static wcd_data_t *wcddata;
/*
* Currently, the only client of this driver is the workstation console
* driver. Thus, we can get away with hard-wiring a reference to it here.
*
* To handle multiple clients, the driver must be revised as follows.
* 1) Add a registration routine that clients can call to announce
* themselves to this driver. The routine should take as arguments the
* major device number of the corresponding instantiation of the
* redirecting driver and a pointer to its dedvnops ops vector.
* 2) Maintain a list (or perhaps hash array) or registered clients,
* recording for each the srvnops ops vector and a pointer to the list
* of open instances for that client.
* 3) Modify the driver entry points to use their dev argument to look up
* the proper instantiation, get the list of open instances, and then use
* that as they currently use the open instance list.
* 4) To allow clients to unload themselves, we probably need an unregister
* routine. This routine would have to cope with active open instances.
*/
extern srvnops_t wscons_srvnops;
/*
* Module linkage information for the kernel.
*/
&mod_driverops, /* Type of module. This one is a pseudo driver */
"Workstation Redirection driver 'iwscn' %I%",
&iwscn_ops, /* driver ops */
};
static struct modlinkage modlinkage = {
&modldrv,
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
return (EBUSY);
}
int
{
}
/*
* DDI glue routines.
*/
/*ARGSUSED*/
static int
{
static char been_here;
if (!been_here) {
been_here = 1;
}
return (-1);
}
return (DDI_SUCCESS);
}
/* ARGSUSED */
static int
{
int error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
error = DDI_FAILURE;
} else {
error = DDI_SUCCESS;
}
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)0;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
/* ARGSUSED */
static int
int flag,
int state, /* should be OTYP_CHR */
{
wcd_data_t *wd;
int err = 0;
return (ENXIO);
/*
* First open for this instance; get a state structure for it.
*/
/*
* Call the client driver to obtain a held vnode for the
* underlying "real" device instance.
*
* XXX: There's wired in knowledge of the client driver here.
*/
if (err != 0) {
sridealloc(wd);
return (err);
}
}
/*
* Reinitalize the list if necessary.
*
* XXX: Is it possible for the list to empty completely while this
* instance is still open? If not, this if should be coalesced
* with the previous one.
*/
/*
* There's no corresponding redirecting module instance for
* the underlying device.
*/
}
/*
* XXX cleanup the sig list. Hook for console driver.
*/
}
return (err);
}
/* ARGSUSED */
static int
int flag,
int state, /* should be OTYP_CHR */
{
wcd_data_t *wd;
int err = 0;
return (ENXIO);
/*
* Remove all outstanding redirections for this instance.
*/
/*
* Since this is the _last_ close, it's our last chance to close the
* underlying device. (Note that if someone else has the underlying
* workstation console device open, we won't get here, since
* spec_close will see s_count > 1.)
*/
if (!err)
wd->wd_wsconsopen--;
}
if (!err)
/*
* We don't need the vnode that the client driver gave us any more.
*
* XXX: There's wired in knowledge of the client driver here.
*/
sridealloc(wd);
return (err);
}
static int
{
wcd_data_t *wd;
int error;
return (error);
}
static int
{
wcd_data_t *wd;
int error;
return (error);
}
static int
{
wcd_data_t *wd;
int err = 0;
file_t *f;
switch (cmd) {
case SRIOCSREDIR: {
return (EBUSY);
}
/*
* Find the vnode corresponding to the file descriptor
* argument and verify that it names a stream.
*/
break;
}
break;
}
/*
* allocate the private data for redirmod, and pass it through
* a global to wcmopen(). This is all protected by iwscn_lock.
*/
/*
* Push a new instance of the redirecting module onto the
* stream, so that its close routine can notify us when the
* overall stream is closed. (In turn, we'll then remove it
* from the redirection list.)
*/
iwscn_thread = NULL;
break;
}
/*
* Push it onto the redirection stack.
*/
/*
* Fill in the redirecting module instance's private data with
* information to let it get to our redirection list when its
* close routine is called. Cross-link it with the
* redirection list entry.
*/
break;
}
case SRIOCISREDIR:
break;
}
/*
* Return value is 1 if the argument descriptor is the current
* redirection target, and 0 otherwise.
*/
break;
case I_POP: {
/*
* XXX - This is a big kludge the handles a deadlock case
* when we are trying to pop off the redirection
* module. Since this should only happen on a close
* of the device, and since it hangs the system, just
* do not allow a pop of the redirection module to happen.
* Popping other modules is allowed.
*/
break;
}
break;
}
"module not allowed");
return (EINVAL);
}
}
/* Process ioctl normally */
break;
}
default:
break;
}
return (err);
}
static int
short events,
int anyyet,
short *reventsp,
{
wcd_data_t *wd;
int error;
return (error);
}
/*
* Auxiliary routines.
*/
/*
* Additional public interfaces.
*/
/*
* Reset the current workstation console designee to the device denoted by the
* wl_vp field of the first entry in the redirection list. Called from
* iwscnopen and from the SRIOCSREDIR case of iwscnioctl, in both cases after
* the target vp has been set to its new value.
*/
static int
{
int err = 0;
/*
* If we're reverting back to the workstation console, make sure it's
* open.
*/
/*
* The underlying driver is not allowed to have cloned itself
* for this open.
*/
panic("srreset: Illegal clone");
/*NOTREACHED*/
}
if (!err)
wd->wd_wsconsopen++;
}
return (err);
}
/*
* Remove vp from the redirection list rooted at *rwlp, should it be there.
* If zap is nonzero, deallocate the entry and remove dangling references to
* the it from the corresponding redirecting module instance's wcm_data
* structure.
*
* If the entry doesn't exist upon completion, return NULL; otherwise return a
* pointer to it.
*/
static wcrlist_t *
{
break;
return (NULL);
if (zap == 0)
return (wlp);
/*
* Make sure there are no dangling references leading to the entry
* from the corresponding redirecting module instance.
*/
}
return (NULL);
}
/*
* srpop - remove redirection because the target stream is being closed.
* Called from wcmclose().
*/
void
{
}
}
/*
* Routines for allocating, deallocating, and finding wcd_data structures.
*
* For a given instantiation of the driver, its open instance structures are
* linked together into a list, on the assumption that there will never be
* enough open instances to make search efficiency a serious concern.
*/
/*
* Look up the instance structure denoted by unit.
*/
static wcd_data_t *
{
continue;
return (wd);
}
/*
* Allocate a wcd_data structure for the instance denoted by unit, link it in
* place, and return a pointer to it. If it's already allocated, simply
* return a pointer to it.
*/
static wcd_data_t *
{
wcd_data_t **wdpp;
break;
/* Already allocated and in place. */
return (wdp);
}
}
/*
* wdpp now points to the proper insertion point for unit's
* per-instance structure.
*/
return (wdp);
}
/*
* Deallocate the wcd_data structure denoted by wd and unlink it from the
* list of open instances.
*/
static void
{
wcd_data_t **wdpp;
break;
/*
* Not there. This should probably be a panic.
*/
return;
}
}
/*
* Push vp onto the redirection list rooted at *wlpp. If it's already there,
* move it to the front position. Return a pointer to its list entry.
*
* N.B.: It is the caller's responsibility to initialize all fields in the
* entry other than the wl_next and wl_vp fields.
*/
static wcrlist_t *
{
/*
* The hold will prevent underlying device from closing
* while this vnode is still on the redirection list.
*/
}
return (nwlp);
}