xref: /illumos-gate/usr/src/uts/common/syscall/pipe.c (revision 7c478bd9)
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 2005 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"	/* from SVr4.0 1.11 */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/fs/fifonode.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate char _depends_on[] = "fs/fifofs";
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate longlong_t pipe();
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static struct sysent pipe_sysent = {
58*7c478bd9Sstevel@tonic-gate 	0,
59*7c478bd9Sstevel@tonic-gate 	SE_32RVAL1 | SE_32RVAL2 | SE_NOUNLOAD | SE_ARGC,
60*7c478bd9Sstevel@tonic-gate 	(int (*)())pipe
61*7c478bd9Sstevel@tonic-gate };
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
67*7c478bd9Sstevel@tonic-gate 	&mod_syscallops, "pipe(2) syscall", &pipe_sysent
68*7c478bd9Sstevel@tonic-gate };
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
71*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
72*7c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pipe(2) syscall", &pipe_sysent
73*7c478bd9Sstevel@tonic-gate };
74*7c478bd9Sstevel@tonic-gate #endif
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
77*7c478bd9Sstevel@tonic-gate 	MODREV_1,
78*7c478bd9Sstevel@tonic-gate 	&modlsys,
79*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
80*7c478bd9Sstevel@tonic-gate 	&modlsys32,
81*7c478bd9Sstevel@tonic-gate #endif
82*7c478bd9Sstevel@tonic-gate 	NULL
83*7c478bd9Sstevel@tonic-gate };
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate int
86*7c478bd9Sstevel@tonic-gate _init(void)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate int
92*7c478bd9Sstevel@tonic-gate _fini(void)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	return (EBUSY);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate int
98*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate  * pipe(2) system call.
105*7c478bd9Sstevel@tonic-gate  * Create a pipe by connecting two streams together. Associate
106*7c478bd9Sstevel@tonic-gate  * each end of the pipe with a vnode, a file descriptor and
107*7c478bd9Sstevel@tonic-gate  * one of the streams.
108*7c478bd9Sstevel@tonic-gate  */
109*7c478bd9Sstevel@tonic-gate longlong_t
110*7c478bd9Sstevel@tonic-gate pipe()
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	vnode_t *vp1, *vp2;
113*7c478bd9Sstevel@tonic-gate 	struct file *fp1, *fp2;
114*7c478bd9Sstevel@tonic-gate 	int error = 0;
115*7c478bd9Sstevel@tonic-gate 	int fd1, fd2;
116*7c478bd9Sstevel@tonic-gate 	rval_t	r;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	/*
119*7c478bd9Sstevel@tonic-gate 	 * Allocate and initialize two vnodes.
120*7c478bd9Sstevel@tonic-gate 	 */
121*7c478bd9Sstevel@tonic-gate 	makepipe(&vp1, &vp2);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	/*
124*7c478bd9Sstevel@tonic-gate 	 * Allocate and initialize two file table entries and two
125*7c478bd9Sstevel@tonic-gate 	 * file pointers. Each file pointer is open for read and
126*7c478bd9Sstevel@tonic-gate 	 * write.
127*7c478bd9Sstevel@tonic-gate 	 */
128*7c478bd9Sstevel@tonic-gate 	if (error = falloc(vp1, FWRITE|FREAD, &fp1, &fd1)) {
129*7c478bd9Sstevel@tonic-gate 		VN_RELE(vp1);
130*7c478bd9Sstevel@tonic-gate 		VN_RELE(vp2);
131*7c478bd9Sstevel@tonic-gate 		return ((longlong_t)set_errno(error));
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if (error = falloc(vp2, FWRITE|FREAD, &fp2, &fd2))
135*7c478bd9Sstevel@tonic-gate 		goto out2;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/*
138*7c478bd9Sstevel@tonic-gate 	 * Create two stream heads and attach to each vnode.
139*7c478bd9Sstevel@tonic-gate 	 */
140*7c478bd9Sstevel@tonic-gate 	if (error = fifo_stropen(&vp1, FWRITE|FREAD, fp1->f_cred, 0, 0))
141*7c478bd9Sstevel@tonic-gate 		goto out;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if (error = fifo_stropen(&vp2, FWRITE|FREAD, fp2->f_cred, 0, 0)) {
144*7c478bd9Sstevel@tonic-gate 		(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0,
145*7c478bd9Sstevel@tonic-gate 		    fp1->f_cred);
146*7c478bd9Sstevel@tonic-gate 		goto out;
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	strmate(vp1, vp2);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	VTOF(vp1)->fn_ino = VTOF(vp2)->fn_ino = fifogetid();
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/*
154*7c478bd9Sstevel@tonic-gate 	 * Now fill in the entries that falloc reserved
155*7c478bd9Sstevel@tonic-gate 	 */
156*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fp1->f_tlock);
157*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fp2->f_tlock);
158*7c478bd9Sstevel@tonic-gate 	setf(fd1, fp1);
159*7c478bd9Sstevel@tonic-gate 	setf(fd2, fp2);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/*
162*7c478bd9Sstevel@tonic-gate 	 * Return the file descriptors to the user. They now
163*7c478bd9Sstevel@tonic-gate 	 * point to two different vnodes which have different
164*7c478bd9Sstevel@tonic-gate 	 * stream heads.
165*7c478bd9Sstevel@tonic-gate 	 */
166*7c478bd9Sstevel@tonic-gate 	r.r_val1 = fd1;
167*7c478bd9Sstevel@tonic-gate 	r.r_val2 = fd2;
168*7c478bd9Sstevel@tonic-gate 	return (r.r_vals);
169*7c478bd9Sstevel@tonic-gate out:
170*7c478bd9Sstevel@tonic-gate 	unfalloc(fp2);
171*7c478bd9Sstevel@tonic-gate 	setf(fd2, NULL);
172*7c478bd9Sstevel@tonic-gate out2:
173*7c478bd9Sstevel@tonic-gate 	unfalloc(fp1);
174*7c478bd9Sstevel@tonic-gate 	setf(fd1, NULL);
175*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp1);
176*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp2);
177*7c478bd9Sstevel@tonic-gate 	return ((longlong_t)set_errno(error));
178*7c478bd9Sstevel@tonic-gate }
179