1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Redirection STREAMS module. 31*7c478bd9Sstevel@tonic-gate * 32*7c478bd9Sstevel@tonic-gate * This module is intended for use in conjunction with instantiations of the 33*7c478bd9Sstevel@tonic-gate * redirection driver. Its purpose in life is to detect when the stream that 34*7c478bd9Sstevel@tonic-gate * it's pushed on is closed, thereupon calling back into the redirection 35*7c478bd9Sstevel@tonic-gate * driver so that the driver can cancel redirection to the stream. 36*7c478bd9Sstevel@tonic-gate */ 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/errno.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate #include <sys/stream.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate #include <sys/strredir.h> 49*7c478bd9Sstevel@tonic-gate #include <sys/thread.h> 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate #include <sys/conf.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 53*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 54*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate /* 57*7c478bd9Sstevel@tonic-gate * Forward declarations for private routines. 58*7c478bd9Sstevel@tonic-gate */ 59*7c478bd9Sstevel@tonic-gate static int wcmopen(queue_t *, dev_t *, int, int, cred_t *); 60*7c478bd9Sstevel@tonic-gate static int wcmclose(queue_t *, int, cred_t *); 61*7c478bd9Sstevel@tonic-gate static int wcmput(queue_t *, mblk_t *); 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate static struct module_info wcminfo = { 64*7c478bd9Sstevel@tonic-gate _STRREDIR_MODID, 65*7c478bd9Sstevel@tonic-gate "redirmod", 66*7c478bd9Sstevel@tonic-gate 0, 67*7c478bd9Sstevel@tonic-gate INFPSZ, 68*7c478bd9Sstevel@tonic-gate 5120, 69*7c478bd9Sstevel@tonic-gate 1024 70*7c478bd9Sstevel@tonic-gate }; 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate static struct qinit wcmrinit = { 73*7c478bd9Sstevel@tonic-gate wcmput, /* put */ 74*7c478bd9Sstevel@tonic-gate NULL, /* service */ 75*7c478bd9Sstevel@tonic-gate wcmopen, /* open */ 76*7c478bd9Sstevel@tonic-gate wcmclose, /* close */ 77*7c478bd9Sstevel@tonic-gate NULL, /* qadmin */ 78*7c478bd9Sstevel@tonic-gate &wcminfo, 79*7c478bd9Sstevel@tonic-gate NULL /* mstat */ 80*7c478bd9Sstevel@tonic-gate }; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate static struct qinit wcmwinit = { 83*7c478bd9Sstevel@tonic-gate wcmput, /* put */ 84*7c478bd9Sstevel@tonic-gate NULL, /* service */ 85*7c478bd9Sstevel@tonic-gate wcmopen, /* open */ 86*7c478bd9Sstevel@tonic-gate wcmclose, /* close */ 87*7c478bd9Sstevel@tonic-gate NULL, /* qadmin */ 88*7c478bd9Sstevel@tonic-gate &wcminfo, 89*7c478bd9Sstevel@tonic-gate NULL /* mstat */ 90*7c478bd9Sstevel@tonic-gate }; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate static struct streamtab redirminfo = { 93*7c478bd9Sstevel@tonic-gate &wcmrinit, 94*7c478bd9Sstevel@tonic-gate &wcmwinit, 95*7c478bd9Sstevel@tonic-gate NULL, 96*7c478bd9Sstevel@tonic-gate NULL 97*7c478bd9Sstevel@tonic-gate }; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate static struct fmodsw fsw = { 100*7c478bd9Sstevel@tonic-gate "redirmod", 101*7c478bd9Sstevel@tonic-gate &redirminfo, 102*7c478bd9Sstevel@tonic-gate D_NEW | D_MP 103*7c478bd9Sstevel@tonic-gate }; 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = { 106*7c478bd9Sstevel@tonic-gate &mod_strmodops, 107*7c478bd9Sstevel@tonic-gate "redirection module", 108*7c478bd9Sstevel@tonic-gate &fsw 109*7c478bd9Sstevel@tonic-gate }; 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 112*7c478bd9Sstevel@tonic-gate MODREV_1, &modlstrmod, NULL 113*7c478bd9Sstevel@tonic-gate }; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate int 116*7c478bd9Sstevel@tonic-gate _init() 117*7c478bd9Sstevel@tonic-gate { 118*7c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate int 122*7c478bd9Sstevel@tonic-gate _fini() 123*7c478bd9Sstevel@tonic-gate { 124*7c478bd9Sstevel@tonic-gate return (EBUSY); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate int 128*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 129*7c478bd9Sstevel@tonic-gate { 130*7c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 134*7c478bd9Sstevel@tonic-gate static int 135*7c478bd9Sstevel@tonic-gate wcmopen(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *cred) 136*7c478bd9Sstevel@tonic-gate { 137*7c478bd9Sstevel@tonic-gate extern kthread_t *iwscn_thread; 138*7c478bd9Sstevel@tonic-gate extern wcm_data_t *iwscn_wcm_data; 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate if (sflag != MODOPEN) 141*7c478bd9Sstevel@tonic-gate return (EINVAL); 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * There's nothing to do if we're already open. 145*7c478bd9Sstevel@tonic-gate */ 146*7c478bd9Sstevel@tonic-gate if (q->q_ptr == NULL) { 147*7c478bd9Sstevel@tonic-gate /* 148*7c478bd9Sstevel@tonic-gate * Attach the per open instance state structure. 149*7c478bd9Sstevel@tonic-gate * Its fields were * initialized elsewhere (from the 150*7c478bd9Sstevel@tonic-gate * SRIOCSREDIR case of of the redirection driver's ioctl 151*7c478bd9Sstevel@tonic-gate * routine). 152*7c478bd9Sstevel@tonic-gate * To prevent other threads from getting this, check thread_id. 153*7c478bd9Sstevel@tonic-gate */ 154*7c478bd9Sstevel@tonic-gate if (curthread != iwscn_thread) 155*7c478bd9Sstevel@tonic-gate return (EINVAL); 156*7c478bd9Sstevel@tonic-gate q->q_ptr = WR(q)->q_ptr = iwscn_wcm_data; 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate qprocson(q); 159*7c478bd9Sstevel@tonic-gate return (0); 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 163*7c478bd9Sstevel@tonic-gate static int 164*7c478bd9Sstevel@tonic-gate wcmclose(queue_t *q, int flag, cred_t *cred) 165*7c478bd9Sstevel@tonic-gate { 166*7c478bd9Sstevel@tonic-gate wcm_data_t *mdp = (wcm_data_t *)q->q_ptr; 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate qprocsoff(q); 169*7c478bd9Sstevel@tonic-gate srpop(mdp, flag, cred); 170*7c478bd9Sstevel@tonic-gate WR(q)->q_ptr = q->q_ptr = NULL; 171*7c478bd9Sstevel@tonic-gate kmem_free(mdp, sizeof (*mdp)); 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate return (0); 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate /* 177*7c478bd9Sstevel@tonic-gate * This module's only purpose in life is to intercept closes on the stream 178*7c478bd9Sstevel@tonic-gate * it's pushed on. It passes all messages on unchanged, in both directions. 179*7c478bd9Sstevel@tonic-gate */ 180*7c478bd9Sstevel@tonic-gate static int 181*7c478bd9Sstevel@tonic-gate wcmput(queue_t *q, mblk_t *mp) 182*7c478bd9Sstevel@tonic-gate { 183*7c478bd9Sstevel@tonic-gate putnext(q, mp); 184*7c478bd9Sstevel@tonic-gate return (0); 185*7c478bd9Sstevel@tonic-gate } 186