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