xref: /onnv-gate/usr/src/uts/common/io/connld.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4 1.8	*/
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * This module establishes a unique connection on
35*0Sstevel@tonic-gate  * a STREAMS-based pipe.
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/sysmacros.h>
39*0Sstevel@tonic-gate #include <sys/param.h>
40*0Sstevel@tonic-gate #include <sys/systm.h>
41*0Sstevel@tonic-gate #include <sys/errno.h>
42*0Sstevel@tonic-gate #include <sys/signal.h>
43*0Sstevel@tonic-gate #include <sys/user.h>
44*0Sstevel@tonic-gate #include <sys/fstyp.h>
45*0Sstevel@tonic-gate #include <sys/stropts.h>
46*0Sstevel@tonic-gate #include <sys/stream.h>
47*0Sstevel@tonic-gate #include <sys/strsubr.h>
48*0Sstevel@tonic-gate #include <sys/vnode.h>
49*0Sstevel@tonic-gate #include <sys/file.h>
50*0Sstevel@tonic-gate #include <sys/fs/fifonode.h>
51*0Sstevel@tonic-gate #include <sys/debug.h>
52*0Sstevel@tonic-gate #include <sys/ddi.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * This is the loadable module wrapper.
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate #include <sys/conf.h>
58*0Sstevel@tonic-gate #include <sys/modctl.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate extern struct streamtab conninfo;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static struct fmodsw fsw = {
63*0Sstevel@tonic-gate 	"connld",
64*0Sstevel@tonic-gate 	&conninfo,
65*0Sstevel@tonic-gate 	D_NEW | D_MP
66*0Sstevel@tonic-gate };
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * Module linkage information for the kernel.
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
73*0Sstevel@tonic-gate 	&mod_strmodops, "Streams-based pipes", &fsw
74*0Sstevel@tonic-gate };
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
77*0Sstevel@tonic-gate 	MODREV_1, (void *)&modlstrmod, NULL
78*0Sstevel@tonic-gate };
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate int
_init()81*0Sstevel@tonic-gate _init()
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate int
_fini()87*0Sstevel@tonic-gate _fini()
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate int
_info(struct modinfo * modinfop)93*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
94*0Sstevel@tonic-gate {
95*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate /*
99*0Sstevel@tonic-gate  * Define local and external routines.
100*0Sstevel@tonic-gate  */
101*0Sstevel@tonic-gate int connopen(queue_t *, dev_t *, int, int, cred_t *);
102*0Sstevel@tonic-gate int connclose(queue_t *, int, cred_t *);
103*0Sstevel@tonic-gate int connput(queue_t *, mblk_t *);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * Define STREAMS header information.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate static struct module_info conn_info = {
109*0Sstevel@tonic-gate 	1003,
110*0Sstevel@tonic-gate 	"connld",
111*0Sstevel@tonic-gate 	0,
112*0Sstevel@tonic-gate 	INFPSZ,
113*0Sstevel@tonic-gate 	STRHIGH,
114*0Sstevel@tonic-gate 	STRLOW
115*0Sstevel@tonic-gate };
116*0Sstevel@tonic-gate static struct qinit connrinit = {
117*0Sstevel@tonic-gate 	connput,
118*0Sstevel@tonic-gate 	NULL,
119*0Sstevel@tonic-gate 	connopen,
120*0Sstevel@tonic-gate 	connclose,
121*0Sstevel@tonic-gate 	NULL,
122*0Sstevel@tonic-gate 	&conn_info,
123*0Sstevel@tonic-gate 	NULL
124*0Sstevel@tonic-gate };
125*0Sstevel@tonic-gate static struct qinit connwinit = {
126*0Sstevel@tonic-gate 	connput,
127*0Sstevel@tonic-gate 	NULL,
128*0Sstevel@tonic-gate 	NULL,
129*0Sstevel@tonic-gate 	NULL,
130*0Sstevel@tonic-gate 	NULL,
131*0Sstevel@tonic-gate 	&conn_info,
132*0Sstevel@tonic-gate 	NULL
133*0Sstevel@tonic-gate };
134*0Sstevel@tonic-gate struct streamtab conninfo = {
135*0Sstevel@tonic-gate 	&connrinit,
136*0Sstevel@tonic-gate 	&connwinit
137*0Sstevel@tonic-gate };
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * For each invocation of connopen(), create a new pipe. One end of the pipe
141*0Sstevel@tonic-gate  * is sent to the process on the other end of this STREAM. The vnode for
142*0Sstevel@tonic-gate  * the other end is returned to the open() system call as the vnode for
143*0Sstevel@tonic-gate  * the opened object.
144*0Sstevel@tonic-gate  *
145*0Sstevel@tonic-gate  * On the first invocation of connopen(), a flag is set and the routine
146*0Sstevel@tonic-gate  * returns 0, since the first open corresponds to the pushing of the module.
147*0Sstevel@tonic-gate  */
148*0Sstevel@tonic-gate /*ARGSUSED*/
149*0Sstevel@tonic-gate int
connopen(queue_t * rqp,dev_t * devp,int flag,int sflag,cred_t * crp)150*0Sstevel@tonic-gate connopen(queue_t *rqp, dev_t *devp, int flag, int sflag, cred_t *crp)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate 	int error = 0;
153*0Sstevel@tonic-gate 	vnode_t *streamvp;
154*0Sstevel@tonic-gate 	fifonode_t *streamfnp;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if ((streamvp = strq2vp(rqp)) == NULL) {
157*0Sstevel@tonic-gate 		return (EINVAL);
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/*
161*0Sstevel@tonic-gate 	 * CONNLD is only allowed to be pushed onto a "pipe" that has both
162*0Sstevel@tonic-gate 	 * of its ends open.
163*0Sstevel@tonic-gate 	 */
164*0Sstevel@tonic-gate 	if (streamvp->v_type != VFIFO) {
165*0Sstevel@tonic-gate 		error = EINVAL;
166*0Sstevel@tonic-gate 		goto out;
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	streamfnp = VTOF(streamvp);
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (!(streamfnp->fn_flag & ISPIPE) ||
172*0Sstevel@tonic-gate 	    streamfnp->fn_dest->fn_open == 0) {
173*0Sstevel@tonic-gate 		error = EPIPE;
174*0Sstevel@tonic-gate 		goto out;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/*
178*0Sstevel@tonic-gate 	 * If this is the first time CONNLD was opened while on this stream,
179*0Sstevel@tonic-gate 	 * it is being pushed. Therefore, set a flag and return 0.
180*0Sstevel@tonic-gate 	 */
181*0Sstevel@tonic-gate 	if (rqp->q_ptr == 0) {
182*0Sstevel@tonic-gate 		if (streamfnp->fn_flag & FIFOCONNLD) {
183*0Sstevel@tonic-gate 			error = ENXIO;
184*0Sstevel@tonic-gate 			goto out;
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 		rqp->q_ptr = (caddr_t)1;
187*0Sstevel@tonic-gate 		streamfnp->fn_flag |= FIFOCONNLD;
188*0Sstevel@tonic-gate 		qprocson(rqp);
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate out:
191*0Sstevel@tonic-gate 	VN_RELE(streamvp);
192*0Sstevel@tonic-gate 	return (error);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate /*ARGSUSED*/
196*0Sstevel@tonic-gate int
connclose(queue_t * q,int cflag,cred_t * crp)197*0Sstevel@tonic-gate connclose(queue_t *q, int cflag, cred_t *crp)
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate 	vnode_t *streamvp;
200*0Sstevel@tonic-gate 	fifonode_t *streamfnp;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	qprocsoff(q);
203*0Sstevel@tonic-gate 	streamvp = strq2vp(q);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	ASSERT(streamvp != NULL);
206*0Sstevel@tonic-gate 	ASSERT(streamvp->v_type == VFIFO);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	streamfnp = VTOF(streamvp);
209*0Sstevel@tonic-gate 	streamfnp->fn_flag &= ~FIFOCONNLD;
210*0Sstevel@tonic-gate 	VN_RELE(streamvp);
211*0Sstevel@tonic-gate 	return (0);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate  * Use same put procedure for write and read queues.
216*0Sstevel@tonic-gate  */
217*0Sstevel@tonic-gate int
connput(queue_t * q,mblk_t * bp)218*0Sstevel@tonic-gate connput(queue_t *q, mblk_t *bp)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	putnext(q, bp);
221*0Sstevel@tonic-gate 	return (0);
222*0Sstevel@tonic-gate }
223