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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4 1.8 */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate * This module establishes a unique connection on 35*7c478bd9Sstevel@tonic-gate * a STREAMS-based pipe. 36*7c478bd9Sstevel@tonic-gate */ 37*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/errno.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/signal.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/user.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/fstyp.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/stream.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 48*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 49*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 50*7c478bd9Sstevel@tonic-gate #include <sys/fs/fifonode.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate /* 55*7c478bd9Sstevel@tonic-gate * This is the loadable module wrapper. 56*7c478bd9Sstevel@tonic-gate */ 57*7c478bd9Sstevel@tonic-gate #include <sys/conf.h> 58*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate extern struct streamtab conninfo; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate static struct fmodsw fsw = { 63*7c478bd9Sstevel@tonic-gate "connld", 64*7c478bd9Sstevel@tonic-gate &conninfo, 65*7c478bd9Sstevel@tonic-gate D_NEW | D_MP 66*7c478bd9Sstevel@tonic-gate }; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = { 73*7c478bd9Sstevel@tonic-gate &mod_strmodops, "Streams-based pipes", &fsw 74*7c478bd9Sstevel@tonic-gate }; 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 77*7c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlstrmod, NULL 78*7c478bd9Sstevel@tonic-gate }; 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate int 81*7c478bd9Sstevel@tonic-gate _init() 82*7c478bd9Sstevel@tonic-gate { 83*7c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate int 87*7c478bd9Sstevel@tonic-gate _fini() 88*7c478bd9Sstevel@tonic-gate { 89*7c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate int 93*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate /* 99*7c478bd9Sstevel@tonic-gate * Define local and external routines. 100*7c478bd9Sstevel@tonic-gate */ 101*7c478bd9Sstevel@tonic-gate int connopen(queue_t *, dev_t *, int, int, cred_t *); 102*7c478bd9Sstevel@tonic-gate int connclose(queue_t *, int, cred_t *); 103*7c478bd9Sstevel@tonic-gate int connput(queue_t *, mblk_t *); 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate /* 106*7c478bd9Sstevel@tonic-gate * Define STREAMS header information. 107*7c478bd9Sstevel@tonic-gate */ 108*7c478bd9Sstevel@tonic-gate static struct module_info conn_info = { 109*7c478bd9Sstevel@tonic-gate 1003, 110*7c478bd9Sstevel@tonic-gate "connld", 111*7c478bd9Sstevel@tonic-gate 0, 112*7c478bd9Sstevel@tonic-gate INFPSZ, 113*7c478bd9Sstevel@tonic-gate STRHIGH, 114*7c478bd9Sstevel@tonic-gate STRLOW 115*7c478bd9Sstevel@tonic-gate }; 116*7c478bd9Sstevel@tonic-gate static struct qinit connrinit = { 117*7c478bd9Sstevel@tonic-gate connput, 118*7c478bd9Sstevel@tonic-gate NULL, 119*7c478bd9Sstevel@tonic-gate connopen, 120*7c478bd9Sstevel@tonic-gate connclose, 121*7c478bd9Sstevel@tonic-gate NULL, 122*7c478bd9Sstevel@tonic-gate &conn_info, 123*7c478bd9Sstevel@tonic-gate NULL 124*7c478bd9Sstevel@tonic-gate }; 125*7c478bd9Sstevel@tonic-gate static struct qinit connwinit = { 126*7c478bd9Sstevel@tonic-gate connput, 127*7c478bd9Sstevel@tonic-gate NULL, 128*7c478bd9Sstevel@tonic-gate NULL, 129*7c478bd9Sstevel@tonic-gate NULL, 130*7c478bd9Sstevel@tonic-gate NULL, 131*7c478bd9Sstevel@tonic-gate &conn_info, 132*7c478bd9Sstevel@tonic-gate NULL 133*7c478bd9Sstevel@tonic-gate }; 134*7c478bd9Sstevel@tonic-gate struct streamtab conninfo = { 135*7c478bd9Sstevel@tonic-gate &connrinit, 136*7c478bd9Sstevel@tonic-gate &connwinit 137*7c478bd9Sstevel@tonic-gate }; 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate /* 140*7c478bd9Sstevel@tonic-gate * For each invocation of connopen(), create a new pipe. One end of the pipe 141*7c478bd9Sstevel@tonic-gate * is sent to the process on the other end of this STREAM. The vnode for 142*7c478bd9Sstevel@tonic-gate * the other end is returned to the open() system call as the vnode for 143*7c478bd9Sstevel@tonic-gate * the opened object. 144*7c478bd9Sstevel@tonic-gate * 145*7c478bd9Sstevel@tonic-gate * On the first invocation of connopen(), a flag is set and the routine 146*7c478bd9Sstevel@tonic-gate * returns 0, since the first open corresponds to the pushing of the module. 147*7c478bd9Sstevel@tonic-gate */ 148*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 149*7c478bd9Sstevel@tonic-gate int 150*7c478bd9Sstevel@tonic-gate connopen(queue_t *rqp, dev_t *devp, int flag, int sflag, cred_t *crp) 151*7c478bd9Sstevel@tonic-gate { 152*7c478bd9Sstevel@tonic-gate int error = 0; 153*7c478bd9Sstevel@tonic-gate vnode_t *streamvp; 154*7c478bd9Sstevel@tonic-gate fifonode_t *streamfnp; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate if ((streamvp = strq2vp(rqp)) == NULL) { 157*7c478bd9Sstevel@tonic-gate return (EINVAL); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate /* 161*7c478bd9Sstevel@tonic-gate * CONNLD is only allowed to be pushed onto a "pipe" that has both 162*7c478bd9Sstevel@tonic-gate * of its ends open. 163*7c478bd9Sstevel@tonic-gate */ 164*7c478bd9Sstevel@tonic-gate if (streamvp->v_type != VFIFO) { 165*7c478bd9Sstevel@tonic-gate error = EINVAL; 166*7c478bd9Sstevel@tonic-gate goto out; 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate streamfnp = VTOF(streamvp); 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate if (!(streamfnp->fn_flag & ISPIPE) || 172*7c478bd9Sstevel@tonic-gate streamfnp->fn_dest->fn_open == 0) { 173*7c478bd9Sstevel@tonic-gate error = EPIPE; 174*7c478bd9Sstevel@tonic-gate goto out; 175*7c478bd9Sstevel@tonic-gate } 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate /* 178*7c478bd9Sstevel@tonic-gate * If this is the first time CONNLD was opened while on this stream, 179*7c478bd9Sstevel@tonic-gate * it is being pushed. Therefore, set a flag and return 0. 180*7c478bd9Sstevel@tonic-gate */ 181*7c478bd9Sstevel@tonic-gate if (rqp->q_ptr == 0) { 182*7c478bd9Sstevel@tonic-gate if (streamfnp->fn_flag & FIFOCONNLD) { 183*7c478bd9Sstevel@tonic-gate error = ENXIO; 184*7c478bd9Sstevel@tonic-gate goto out; 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate rqp->q_ptr = (caddr_t)1; 187*7c478bd9Sstevel@tonic-gate streamfnp->fn_flag |= FIFOCONNLD; 188*7c478bd9Sstevel@tonic-gate qprocson(rqp); 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate out: 191*7c478bd9Sstevel@tonic-gate VN_RELE(streamvp); 192*7c478bd9Sstevel@tonic-gate return (error); 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 196*7c478bd9Sstevel@tonic-gate int 197*7c478bd9Sstevel@tonic-gate connclose(queue_t *q, int cflag, cred_t *crp) 198*7c478bd9Sstevel@tonic-gate { 199*7c478bd9Sstevel@tonic-gate vnode_t *streamvp; 200*7c478bd9Sstevel@tonic-gate fifonode_t *streamfnp; 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate qprocsoff(q); 203*7c478bd9Sstevel@tonic-gate streamvp = strq2vp(q); 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate ASSERT(streamvp != NULL); 206*7c478bd9Sstevel@tonic-gate ASSERT(streamvp->v_type == VFIFO); 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate streamfnp = VTOF(streamvp); 209*7c478bd9Sstevel@tonic-gate streamfnp->fn_flag &= ~FIFOCONNLD; 210*7c478bd9Sstevel@tonic-gate VN_RELE(streamvp); 211*7c478bd9Sstevel@tonic-gate return (0); 212*7c478bd9Sstevel@tonic-gate } 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate /* 215*7c478bd9Sstevel@tonic-gate * Use same put procedure for write and read queues. 216*7c478bd9Sstevel@tonic-gate */ 217*7c478bd9Sstevel@tonic-gate int 218*7c478bd9Sstevel@tonic-gate connput(queue_t *q, mblk_t *bp) 219*7c478bd9Sstevel@tonic-gate { 220*7c478bd9Sstevel@tonic-gate putnext(q, bp); 221*7c478bd9Sstevel@tonic-gate return (0); 222*7c478bd9Sstevel@tonic-gate } 223